based

Opinionated utility library
git clone git://git.dimitrijedobrota.com/based.git
Log | Files | Refs | README | LICENSE | HACKING | CONTRIBUTING | CODE_OF_CONDUCT | BUILDING

count_if_n_test.cpp (4295B)


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 struct predicate 8 { 9 int goal; 10 11 explicit predicate(int init) 12 : goal(init) 13 { 14 } 15 16 auto operator()(int n) const { return n == goal; } 17 }; 18 19 TEST_CASE("count_if_n return type", "[algorithm/count_if_n]") 20 { 21 const std::array<int, 0> arr = {}; 22 23 SECTION("auto counter") 24 { 25 using res_t = decltype(based::count_if_n( 26 std::begin(arr), std::size(arr), predicate {0} 27 ) 28 .second); 29 REQUIRE(based::SameAs<based::iter_dist_t<decltype(arr)::iterator>, res_t>); 30 } 31 32 SECTION("explicit counter") 33 { 34 using res_t = decltype(based::count_if_n( 35 std::begin(arr), 36 std::size(arr), 37 predicate {0}, 38 std::uint8_t {0} 39 ) 40 .second); 41 REQUIRE(based::SameAs<std::uint8_t, res_t>); 42 } 43 } 44 45 TEST_CASE("count_if_n(empty)", "[algorithm/count_if_n]") 46 { 47 const std::array<int, 0> arr = {}; 48 49 const auto [itr, count] = 50 based::count_if_n(std::begin(arr), std::size(arr), predicate {0}); 51 52 REQUIRE(count == 0); 53 REQUIRE(itr == std::end(arr)); 54 } 55 56 TEST_CASE("count_if_n(homogeneous)", "[algorithm/count_if_n]") 57 { 58 const std::array arr = {1, 1, 1, 1, 1, 1}; 59 60 const auto [itr0, count0] = 61 based::count_if_n(std::begin(arr), std::size(arr), predicate {0}); 62 63 const auto [itr1, count1] = 64 based::count_if_n(std::begin(arr), std::size(arr), predicate {1}); 65 66 REQUIRE(count0 == 0); 67 REQUIRE(itr0 == std::end(arr)); 68 69 REQUIRE(count1 == 6); 70 REQUIRE(itr1 == std::end(arr)); 71 } 72 73 TEST_CASE("count_if_n(non homogeneous)", "[algorithm/count_if_n]") 74 { 75 const std::array arr = {1, 2, 1, 1, 1, 2}; 76 77 const auto [itr0, count0] = 78 based::count_if_n(std::begin(arr), std::size(arr), predicate {0}); 79 80 const auto [itr1, count1] = 81 based::count_if_n(std::begin(arr), std::size(arr), predicate {1}); 82 83 const auto [itr2, count2] = 84 based::count_if_n(std::begin(arr), std::size(arr), predicate {2}); 85 86 REQUIRE(count0 == 0); 87 REQUIRE(itr0 == std::end(arr)); 88 89 REQUIRE(count1 == 4); 90 REQUIRE(itr1 == std::end(arr)); 91 92 REQUIRE(count2 == 2); 93 REQUIRE(itr2 == std::end(arr)); 94 } 95 96 TEST_CASE("count_if_not_n return type", "[algorithm/count_if_not_n]") 97 { 98 const std::array<int, 0> arr = {}; 99 100 SECTION("auto counter") 101 { 102 using res_t = decltype(based::count_if_not_n( 103 std::begin(arr), std::size(arr), predicate {0} 104 )); 105 REQUIRE(based::SameAs< 106 based::iter_dist_t<decltype(arr)::iterator>, 107 res_t::second_type>); 108 } 109 110 SECTION("explicit counter") 111 { 112 using res_t = decltype(based::count_if_not_n( 113 std::begin(arr), std::size(arr), predicate {0}, std::uint8_t {0} 114 )); 115 REQUIRE(based::SameAs<std::uint8_t, res_t::second_type>); 116 } 117 } 118 119 TEST_CASE("count_if_not_n(empty)", "[algorithm/count_if_not_n]") 120 { 121 const std::array<int, 0> arr = {}; 122 123 const auto [itr, count] = 124 based::count_if_not_n(std::begin(arr), std::size(arr), predicate {0}); 125 126 REQUIRE(count == 0); 127 REQUIRE(itr == std::end(arr)); 128 } 129 130 TEST_CASE("count_if_not_n(homogeneous)", "[algorithm/count_if_not_n]") 131 { 132 const std::array arr = {1, 1, 1, 1, 1, 1}; 133 134 const auto [itr0, count0] = 135 based::count_if_not_n(std::begin(arr), std::size(arr), predicate {0}); 136 137 const auto [itr1, count1] = 138 based::count_if_not_n(std::begin(arr), std::size(arr), predicate {1}); 139 140 REQUIRE(count0 == 6); 141 REQUIRE(itr0 == std::end(arr)); 142 143 REQUIRE(count1 == 0); 144 REQUIRE(itr1 == std::end(arr)); 145 } 146 147 TEST_CASE("count_if_not_n(non homogeneous)", "[algorithm/count_if_not_n]") 148 { 149 const std::array arr = {1, 2, 1, 1, 1, 2}; 150 151 const auto [itr0, count0] = 152 based::count_if_not_n(std::begin(arr), std::size(arr), predicate {0}); 153 154 const auto [itr1, count1] = 155 based::count_if_not_n(std::begin(arr), std::size(arr), predicate {1}); 156 157 const auto [itr2, count2] = 158 based::count_if_not_n(std::begin(arr), std::size(arr), predicate {2}); 159 160 REQUIRE(count0 == 6); 161 REQUIRE(itr0 == std::end(arr)); 162 163 REQUIRE(count1 == 2); 164 REQUIRE(itr1 == std::end(arr)); 165 166 REQUIRE(count2 == 4); 167 REQUIRE(itr2 == std::end(arr)); 168 }