From 19ee553e7acd1ee735ab25f612d401b7ed4d1138 Mon Sep 17 00:00:00 2001 From: Mitra Darja Darvish Date: Thu, 23 Apr 2020 11:40:54 +0200 Subject: [PATCH] [TEST] Make most tests typed tests --- include/seqan3/range/views/kmer_hash.hpp | 9 +- test/unit/range/views/view_kmer_hash_test.cpp | 94 +++++++++++-------- 2 files changed, 60 insertions(+), 43 deletions(-) diff --git a/include/seqan3/range/views/kmer_hash.hpp b/include/seqan3/range/views/kmer_hash.hpp index f31027c2b22..2d7f189e0e7 100644 --- a/include/seqan3/range/views/kmer_hash.hpp +++ b/include/seqan3/range/views/kmer_hash.hpp @@ -178,12 +178,13 @@ class kmer_hash_view : public std::ranges::view_interface /*!\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 { - 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}; } }; diff --git a/test/unit/range/views/view_kmer_hash_test.cpp b/test/unit/range/views/view_kmer_hash_test.cpp index bb038469340..f4b878845de 100644 --- a/test/unit/range/views/view_kmer_hash_test.cpp +++ b/test/unit/range/views/view_kmer_hash_test.cpp @@ -17,8 +17,6 @@ #include #include -#include - #include using seqan3::operator""_dna4; @@ -28,6 +26,8 @@ using result_t = std::vector; 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 class kmer_hash_properties_test: public ::testing::Test {}; @@ -43,34 +43,44 @@ using underlying_range_types = ::testing::Types, TYPED_TEST_SUITE(kmer_hash_properties_test, underlying_range_types, ); -class kmer_hash_test : public ::testing::Test -{ -protected: - std::vector text1{"AAAAA"_dna4}; - result_t result1{0,0,0}; // Same for gapped and ungapped - - std::vector text2{"AC"_dna4}; - result_t result2{}; // Same for gapped and ungapped +class kmer_hash_test : public ::testing::Test {}; - std::vector 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); + EXPECT_EQ(ungapped2, text2 | ungapped_view | seqan3::views::to); + EXPECT_EQ(ungapped3, text3 | ungapped_view | seqan3::views::to); + EXPECT_EQ(ungapped4, text4 | ungapped_view | seqan3::views::to); + EXPECT_EQ(ungapped4, text1 | prefix_until_first_thymine | ungapped_view | seqan3::views::to); +} -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); - EXPECT_EQ(gapped, text | gapped_view | seqan3::views::to); - - 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); - EXPECT_EQ(result_t{2}, text | stop_at_t | gapped_view | seqan3::views::to); + 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); + EXPECT_EQ(gapped2, text2 | gapped_view | seqan3::views::to); + EXPECT_EQ(gapped3, text3 | gapped_view | seqan3::views::to); + EXPECT_EQ(gapped4, text4 | gapped_view | seqan3::views::to); + EXPECT_EQ(gapped4, text1 | prefix_until_first_thymine| gapped_view | seqan3::views::to); } -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; @@ -85,26 +95,32 @@ TYPED_TEST(kmer_hash_properties_test, concepts) EXPECT_FALSE((std::ranges::output_range)); } -TEST_F(kmer_hash_test, ungapped) -{ - EXPECT_EQ(result1, text1 | ungapped_view | seqan3::views::to); - EXPECT_EQ(result2, text2 | ungapped_view | seqan3::views::to); - EXPECT_EQ(ungapped3, text3 | ungapped_view | seqan3::views::to); -} - -TEST_F(kmer_hash_test, gapped) +TYPED_TEST(kmer_hash_properties_test, gapped_concepts) { - EXPECT_EQ(result1, text1 | gapped_view | seqan3::views::to); - EXPECT_EQ(result2, text2 | gapped_view | seqan3::views::to); - EXPECT_EQ(gapped3, text3 | gapped_view | seqan3::views::to); + TypeParam text{'A'_dna4, 'C'_dna4, 'G'_dna4, 'T'_dna4}; // ACGT + auto v1 = text | gapped_view; + EXPECT_TRUE(std::ranges::input_range); + EXPECT_TRUE(std::ranges::forward_range); + EXPECT_EQ(std::ranges::bidirectional_range, std::ranges::bidirectional_range); + EXPECT_EQ(std::ranges::random_access_range, std::ranges::random_access_range); + EXPECT_TRUE(std::ranges::view); + EXPECT_EQ(std::ranges::sized_range, std::ranges::sized_range); + EXPECT_FALSE(std::ranges::common_range); + EXPECT_TRUE(seqan3::const_iterable_range); + EXPECT_FALSE((std::ranges::output_range)); } -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) // 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