Skip to content

Commit

Permalink
[TEST] Make most tests typed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MitraDarja committed Apr 28, 2020
1 parent 142f05b commit 19ee553
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 43 deletions.
9 changes: 5 additions & 4 deletions include/seqan3/range/views/kmer_hash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,13 @@ class kmer_hash_view : public std::ranges::view_interface<kmer_hash_view<urng_t>
/*!\brief Returns the size of the range, if the underlying range is a std::ranges::sized_range.
* \returns Size of range.
*/
uint64_t size()
auto size() const
requires std::ranges::sized_range<urng_t>
{
if (std::ranges::size(urange) >= shape_.size())
return std::ranges::size(urange) - shape_.size() + 1;
return 0;
auto range_size{std::ranges::size(urange)};
if (range_size >= shape_.size())
return range_size - shape_.size() + 1;
return decltype(range_size){0};
}
};

Expand Down
94 changes: 55 additions & 39 deletions test/unit/range/views/view_kmer_hash_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
#include <seqan3/range/views/take_until.hpp>
#include <seqan3/range/views/to.hpp>

#include <seqan3/core/debug_stream.hpp>

#include <gtest/gtest.h>

using seqan3::operator""_dna4;
Expand All @@ -28,6 +26,8 @@ using result_t = std::vector<size_t>;

static constexpr auto ungapped_view = seqan3::views::kmer_hash(seqan3::ungapped{3});
static constexpr auto gapped_view = seqan3::views::kmer_hash(0b101_shape);
static constexpr auto prefix_until_first_thymine = seqan3::views::take_until([] (seqan3::dna4 x)
{ return x == 'T'_dna4; });

template <typename T>
class kmer_hash_properties_test: public ::testing::Test {};
Expand All @@ -43,34 +43,44 @@ using underlying_range_types = ::testing::Types<std::vector<seqan3::dna4>,

TYPED_TEST_SUITE(kmer_hash_properties_test, underlying_range_types, );

class kmer_hash_test : public ::testing::Test
{
protected:
std::vector<seqan3::dna4> text1{"AAAAA"_dna4};
result_t result1{0,0,0}; // Same for gapped and ungapped

std::vector<seqan3::dna4> text2{"AC"_dna4};
result_t result2{}; // Same for gapped and ungapped
class kmer_hash_test : public ::testing::Test {};

std::vector<seqan3::dna4> text3{"ACG"_dna4};
result_t ungapped3{6};
result_t gapped3{2};
};
TYPED_TEST(kmer_hash_properties_test, ungapped_combined_with_container)
{
TypeParam text1{'A'_dna4, 'C'_dna4, 'G'_dna4, 'T'_dna4, 'A'_dna4, 'G'_dna4, 'C'_dna4}; // ACGTAGC
result_t ungapped1{6, 27, 44, 50, 9};
TypeParam text2{'A'_dna4, 'A'_dna4, 'A'_dna4, 'A'_dna4, 'A'_dna4}; // AAAAA
result_t ungapped2{0,0,0};
TypeParam text3{'A'_dna4, 'C'_dna4}; // AC
result_t ungapped3{};
TypeParam text4{'A'_dna4, 'C'_dna4, 'G'_dna4}; // AC
result_t ungapped4{6};

EXPECT_EQ(ungapped1, text1 | ungapped_view | seqan3::views::to<result_t>);
EXPECT_EQ(ungapped2, text2 | ungapped_view | seqan3::views::to<result_t>);
EXPECT_EQ(ungapped3, text3 | ungapped_view | seqan3::views::to<result_t>);
EXPECT_EQ(ungapped4, text4 | ungapped_view | seqan3::views::to<result_t>);
EXPECT_EQ(ungapped4, text1 | prefix_until_first_thymine | ungapped_view | seqan3::views::to<result_t>);
}

TYPED_TEST(kmer_hash_properties_test, combinability)
TYPED_TEST(kmer_hash_properties_test, gapped_combined_with_container)
{
TypeParam text{'A'_dna4, 'C'_dna4, 'G'_dna4, 'T'_dna4, 'A'_dna4, 'G'_dna4, 'C'_dna4}; // ACGTAGC
result_t ungapped{6,27,44,50,9};
result_t gapped{2,7,8,14,1};
EXPECT_EQ(ungapped, text | ungapped_view | seqan3::views::to<result_t>);
EXPECT_EQ(gapped, text | gapped_view | seqan3::views::to<result_t>);

auto stop_at_t = seqan3::views::take_until([] (seqan3::dna4 x) { return x == 'T'_dna4; });
EXPECT_EQ(result_t{6}, text | stop_at_t | ungapped_view | seqan3::views::to<result_t>);
EXPECT_EQ(result_t{2}, text | stop_at_t | gapped_view | seqan3::views::to<result_t>);
TypeParam text1{'A'_dna4, 'C'_dna4, 'G'_dna4, 'T'_dna4, 'A'_dna4, 'G'_dna4, 'C'_dna4}; // ACGTAGC
result_t gapped1{2, 7, 8, 14, 1};
TypeParam text2{'A'_dna4, 'A'_dna4, 'A'_dna4, 'A'_dna4, 'A'_dna4}; // AAAAA
result_t gapped2{0,0,0};
TypeParam text3{'A'_dna4, 'C'_dna4}; // AC
result_t gapped3{};
TypeParam text4{'A'_dna4, 'C'_dna4, 'G'_dna4}; // AC
result_t gapped4{2};
EXPECT_EQ(gapped1, text1 | gapped_view | seqan3::views::to<result_t>);
EXPECT_EQ(gapped2, text2 | gapped_view | seqan3::views::to<result_t>);
EXPECT_EQ(gapped3, text3 | gapped_view | seqan3::views::to<result_t>);
EXPECT_EQ(gapped4, text4 | gapped_view | seqan3::views::to<result_t>);
EXPECT_EQ(gapped4, text1 | prefix_until_first_thymine| gapped_view | seqan3::views::to<result_t>);
}

TYPED_TEST(kmer_hash_properties_test, concepts)
TYPED_TEST(kmer_hash_properties_test, ungapped_concepts)
{
TypeParam text{'A'_dna4, 'C'_dna4, 'G'_dna4, 'T'_dna4}; // ACGT
auto v1 = text | ungapped_view;
Expand All @@ -85,26 +95,32 @@ TYPED_TEST(kmer_hash_properties_test, concepts)
EXPECT_FALSE((std::ranges::output_range<decltype(v1), size_t>));
}

TEST_F(kmer_hash_test, ungapped)
{
EXPECT_EQ(result1, text1 | ungapped_view | seqan3::views::to<result_t>);
EXPECT_EQ(result2, text2 | ungapped_view | seqan3::views::to<result_t>);
EXPECT_EQ(ungapped3, text3 | ungapped_view | seqan3::views::to<result_t>);
}

TEST_F(kmer_hash_test, gapped)
TYPED_TEST(kmer_hash_properties_test, gapped_concepts)
{
EXPECT_EQ(result1, text1 | gapped_view | seqan3::views::to<result_t>);
EXPECT_EQ(result2, text2 | gapped_view | seqan3::views::to<result_t>);
EXPECT_EQ(gapped3, text3 | gapped_view | seqan3::views::to<result_t>);
TypeParam text{'A'_dna4, 'C'_dna4, 'G'_dna4, 'T'_dna4}; // ACGT
auto v1 = text | gapped_view;
EXPECT_TRUE(std::ranges::input_range<decltype(v1)>);
EXPECT_TRUE(std::ranges::forward_range<decltype(v1)>);
EXPECT_EQ(std::ranges::bidirectional_range<decltype(text)>, std::ranges::bidirectional_range<decltype(v1)>);
EXPECT_EQ(std::ranges::random_access_range<decltype(text)>, std::ranges::random_access_range<decltype(v1)>);
EXPECT_TRUE(std::ranges::view<decltype(v1)>);
EXPECT_EQ(std::ranges::sized_range<decltype(text)>, std::ranges::sized_range<decltype(v1)>);
EXPECT_FALSE(std::ranges::common_range<decltype(v1)>);
EXPECT_TRUE(seqan3::const_iterable_range<decltype(v1)>);
EXPECT_FALSE((std::ranges::output_range<decltype(v1), size_t>));
}

TEST_F(kmer_hash_test, invalid_sizes)
TYPED_TEST(kmer_hash_properties_test, invalid_sizes)
{
TypeParam text1{'A'_dna4, 'A'_dna4, 'A'_dna4, 'A'_dna4, 'A'_dna4};
EXPECT_NO_THROW(text1 | seqan3::views::kmer_hash(seqan3::ungapped{32}));
EXPECT_THROW(text1 | seqan3::views::kmer_hash(seqan3::ungapped{33}), std::invalid_argument);
EXPECT_NO_THROW(text1 | std::views::reverse | seqan3::views::kmer_hash(seqan3::ungapped{32}));
EXPECT_THROW(text1 | std::views::reverse | seqan3::views::kmer_hash(seqan3::ungapped{33}), std::invalid_argument);
if constexpr (std::ranges::bidirectional_range<TypeParam>) // excludes forward_list
{
EXPECT_NO_THROW(text1 | std::views::reverse | seqan3::views::kmer_hash(seqan3::ungapped{32}));
EXPECT_THROW(text1 | std::views::reverse | seqan3::views::kmer_hash(seqan3::ungapped{33}),
std::invalid_argument);
}

EXPECT_NO_THROW(text1 | seqan3::views::kmer_hash(0xFFFFFFFE001_shape)); // size=44, count=32
EXPECT_THROW(text1 | seqan3::views::kmer_hash(0xFFFFFFFFE009_shape), std::invalid_argument); // size=44, count=33
Expand Down

0 comments on commit 19ee553

Please sign in to comment.