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