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_test.cpp (3438B)


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 return type", "[algorithm/count_if]") 20 { 21 const std::array<int, 0> arr = {}; 22 23 SECTION("auto counter") 24 { 25 using res_t = 26 decltype(based::count_if(std::begin(arr), std::end(arr), predicate {0}) 27 ); 28 REQUIRE(based::SameAs<based::iter_dist_t<decltype(arr)::iterator>, res_t>); 29 } 30 31 SECTION("explicit counter") 32 { 33 using res_t = decltype(based::count_if( 34 std::begin(arr), std::end(arr), predicate {0}, std::uint8_t {0} 35 )); 36 REQUIRE(based::SameAs<std::uint8_t, res_t>); 37 } 38 } 39 40 TEST_CASE("count_if(empty)", "[algorithm/count_if]") 41 { 42 const std::array<int, 0> arr = {}; 43 44 const auto count = 45 based::count_if(std::begin(arr), std::end(arr), predicate {0}); 46 47 REQUIRE(count == 0); 48 } 49 50 TEST_CASE("count_if(homogeneous)", "[algorithm/count_if]") 51 { 52 const std::array arr = {1, 1, 1, 1, 1, 1}; 53 54 const auto count0 = 55 based::count_if(std::begin(arr), std::end(arr), predicate {0}); 56 const auto count1 = 57 based::count_if(std::begin(arr), std::end(arr), predicate {1}); 58 59 REQUIRE(count0 == 0); 60 REQUIRE(count1 == 6); 61 } 62 63 TEST_CASE("count_if(non homogeneous)", "[algorithm/count_if]") 64 { 65 const std::array arr = {1, 2, 1, 1, 1, 2}; 66 67 const auto count0 = 68 based::count_if(std::begin(arr), std::end(arr), predicate {0}); 69 const auto count1 = 70 based::count_if(std::begin(arr), std::end(arr), predicate {1}); 71 const auto count2 = 72 based::count_if(std::begin(arr), std::end(arr), predicate {2}); 73 74 REQUIRE(count0 == 0); 75 REQUIRE(count1 == 4); 76 REQUIRE(count2 == 2); 77 } 78 79 TEST_CASE("count_if_not return type", "[algorithm/count_if_not]") 80 { 81 const std::array<int, 0> arr = {}; 82 83 SECTION("auto counter") 84 { 85 using res_t = decltype(based::count_if_not( 86 std::begin(arr), std::end(arr), predicate {0} 87 )); 88 REQUIRE(based::SameAs<based::iter_dist_t<decltype(arr)::iterator>, res_t>); 89 } 90 91 SECTION("explicit counter") 92 { 93 using res_t = decltype(based::count_if_not( 94 std::begin(arr), std::end(arr), predicate {0}, std::uint8_t {0} 95 )); 96 REQUIRE(based::SameAs<std::uint8_t, res_t>); 97 } 98 } 99 100 TEST_CASE("count_if_not(empty)", "[algorithm/count_if_not]") 101 { 102 const std::array<int, 0> arr = {}; 103 104 const auto count = 105 based::count_if_not(std::begin(arr), std::end(arr), predicate {0}); 106 107 REQUIRE(count == 0); 108 } 109 110 TEST_CASE("count_if_not(homogeneous)", "[algorithm/count_if_not]") 111 { 112 const std::array arr = {1, 1, 1, 1, 1, 1}; 113 114 const auto count0 = 115 based::count_if_not(std::begin(arr), std::end(arr), predicate {0}); 116 const auto count1 = 117 based::count_if_not(std::begin(arr), std::end(arr), predicate {1}); 118 119 REQUIRE(count0 == 6); 120 REQUIRE(count1 == 0); 121 } 122 123 TEST_CASE("count_if_not(non homogeneous)", "[algorithm/count_if_not]") 124 { 125 const std::array arr = {1, 2, 1, 1, 1, 2}; 126 127 const auto count0 = 128 based::count_if_not(std::begin(arr), std::end(arr), predicate {0}); 129 const auto count1 = 130 based::count_if_not(std::begin(arr), std::end(arr), predicate {1}); 131 const auto count2 = 132 based::count_if_not(std::begin(arr), std::end(arr), predicate {2}); 133 134 REQUIRE(count0 == 6); 135 REQUIRE(count1 == 2); 136 REQUIRE(count2 == 4); 137 }