basedOpinionated utility library |
git clone git://git.dimitrijedobrota.com/based.git |
Log | Files | Refs | README | HACKING | CONTRIBUTING | CODE_OF_CONDUCT | BUILDING |
count_n_test.cpp (3564B)
1 #include <array> 2 3 #include <catch2/catch_test_macros.hpp> 4 5 #include "based/algorithm.hpp" 6 #include "based/type_traits.hpp" 7 8 TEST_CASE("count_n return type", "[algorithm/count_n]") 9 { 10 const std::array<int, 0> arr = {}; 11 12 REQUIRE(based::SameAs< 13 based::iter_dist_t<decltype(arr)::iterator>, 14 decltype(based::count_n(std::begin(arr), std::size(arr), 0).second)>); 15 16 REQUIRE(based::SameAs< 17 std::uint8_t, 18 decltype(based::count_n( 19 std::begin(arr), std::size(arr), 0, std::uint8_t {0}) 20 .second)>); 21 } 22 23 TEST_CASE("count_n(empty)", "[algorithm/count_n]") 24 { 25 const std::array<int, 0> arr = {}; 26 27 const auto [itr, count] = based::count_n(std::begin(arr), std::size(arr), 0); 28 29 REQUIRE(count == 0); 30 REQUIRE(itr == std::end(arr)); 31 } 32 33 TEST_CASE("count_n(homogeneous)", "[algorithm/count_n]") 34 { 35 const std::array arr = {1, 1, 1, 1, 1, 1}; 36 37 const auto [itr0, count0] = 38 based::count_n(std::begin(arr), std::size(arr), 0); 39 40 const auto [itr1, count1] = 41 based::count_n(std::begin(arr), std::size(arr), 1); 42 43 REQUIRE(count0 == 0); 44 REQUIRE(itr0 == std::end(arr)); 45 46 REQUIRE(count1 == 6); 47 REQUIRE(itr1 == std::end(arr)); 48 } 49 50 TEST_CASE("count_n(non homogeneous)", "[algorithm/count_n]") 51 { 52 const std::array arr = {1, 2, 1, 1, 1, 2}; 53 54 const auto [itr0, count0] = 55 based::count_n(std::begin(arr), std::size(arr), 0); 56 57 const auto [itr1, count1] = 58 based::count_n(std::begin(arr), std::size(arr), 1); 59 60 const auto [itr2, count2] = 61 based::count_n(std::begin(arr), std::size(arr), 2); 62 63 REQUIRE(count0 == 0); 64 REQUIRE(itr0 == std::end(arr)); 65 66 REQUIRE(count1 == 4); 67 REQUIRE(itr1 == std::end(arr)); 68 69 REQUIRE(count2 == 2); 70 REQUIRE(itr2 == std::end(arr)); 71 } 72 73 TEST_CASE("count_not_n return type", "[algorithm/count_not_n]") 74 { 75 const std::array<int, 0> arr = {}; 76 77 REQUIRE(based::SameAs<based::iter_dist_t<decltype(arr)::iterator>, 78 decltype(based::count_not_n( 79 std::begin(arr), std::size(arr), 0) 80 .second)>); 81 82 REQUIRE(based::SameAs< 83 std::uint8_t, 84 decltype(based::count_not_n( 85 std::begin(arr), std::size(arr), 0, std::uint8_t {0}) 86 .second)>); 87 } 88 89 TEST_CASE("count_not_n(empty)", "[algorithm/count_not_n]") 90 { 91 const std::array<int, 0> arr = {}; 92 93 const auto [itr, count] = 94 based::count_not_n(std::begin(arr), std::size(arr), 0); 95 96 REQUIRE(count == 0); 97 REQUIRE(itr == std::end(arr)); 98 } 99 100 TEST_CASE("count_not_n(homogeneous)", "[algorithm/count_not_n]") 101 { 102 const std::array arr = {1, 1, 1, 1, 1, 1}; 103 104 const auto [itr0, count0] = 105 based::count_not_n(std::begin(arr), std::size(arr), 0); 106 107 const auto [itr1, count1] = 108 based::count_not_n(std::begin(arr), std::size(arr), 1); 109 110 REQUIRE(count0 == 6); 111 REQUIRE(itr0 == std::end(arr)); 112 113 REQUIRE(count1 == 0); 114 REQUIRE(itr1 == std::end(arr)); 115 } 116 117 TEST_CASE("count_not_n(non homogeneous)", "[algorithm/count_not_n]") 118 { 119 const std::array arr = {1, 2, 1, 1, 1, 2}; 120 121 const auto [itr0, count0] = 122 based::count_not_n(std::begin(arr), std::size(arr), 0); 123 124 const auto [itr1, count1] = 125 based::count_not_n(std::begin(arr), std::size(arr), 1); 126 127 const auto [itr2, count2] = 128 based::count_not_n(std::begin(arr), std::size(arr), 2); 129 130 REQUIRE(count0 == 6); 131 REQUIRE(itr0 == std::end(arr)); 132 133 REQUIRE(count1 == 2); 134 REQUIRE(itr1 == std::end(arr)); 135 136 REQUIRE(count2 == 4); 137 REQUIRE(itr2 == std::end(arr)); 138 }