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