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