based

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

count_test.cpp (2906B)


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 return type", "[algorithm/count]") 8 { 9 const std::array<int, 0> arr = {}; 10 11 SECTION("auto counter") 12 { 13 using res_t = decltype(based::count(std::begin(arr), std::end(arr), 0)); 14 REQUIRE(based::SameAs<based::iter_dist_t<decltype(arr)::iterator>, res_t>); 15 } 16 17 SECTION("explicit counter") 18 { 19 using res_t = decltype(based::count( 20 std::begin(arr), std::end(arr), 0, std::uint8_t {0} 21 )); 22 REQUIRE(based::SameAs<std::uint8_t, res_t>); 23 } 24 } 25 26 TEST_CASE("count(empty)", "[algorithm/count]") 27 { 28 const std::array<int, 0> arr = {}; 29 30 const auto count = based::count(std::begin(arr), std::end(arr), 0); 31 32 REQUIRE(count == 0); 33 } 34 35 TEST_CASE("count(homogeneous)", "[algorithm/count]") 36 { 37 const std::array arr = {1, 1, 1, 1, 1, 1}; 38 39 const auto count0 = based::count(std::begin(arr), std::end(arr), 0); 40 const auto count1 = based::count(std::begin(arr), std::end(arr), 1); 41 REQUIRE(count0 == 0); 42 REQUIRE(count1 == 6); 43 } 44 45 TEST_CASE("count(non homogeneous)", "[algorithm/count]") 46 { 47 const std::array arr = {1, 2, 1, 1, 1, 2}; 48 49 const auto count0 = based::count(std::begin(arr), std::end(arr), 0); 50 const auto count1 = based::count(std::begin(arr), std::end(arr), 1); 51 const auto count2 = based::count(std::begin(arr), std::end(arr), 2); 52 53 REQUIRE(count0 == 0); 54 REQUIRE(count1 == 4); 55 REQUIRE(count2 == 2); 56 } 57 58 TEST_CASE("count_not return type", "[algorithm/count_not]") 59 { 60 const std::array<int, 0> arr = {}; 61 62 SECTION("auto counter") 63 { 64 using res_t = decltype(based::count_not(std::begin(arr), std::end(arr), 0)); 65 REQUIRE(based::SameAs<based::iter_dist_t<decltype(arr)::iterator>, res_t>); 66 } 67 68 SECTION("explicit counter") 69 { 70 using res_t = decltype(based::count_not( 71 std::begin(arr), std::end(arr), 0, std::uint8_t {0} 72 )); 73 REQUIRE(based::SameAs<std::uint8_t, res_t>); 74 } 75 } 76 77 TEST_CASE("count_not(empty)", "[algorithm/count_not]") 78 { 79 const std::array<int, 0> arr = {}; 80 81 const auto count_not = based::count_not(std::begin(arr), std::end(arr), 0); 82 83 REQUIRE(count_not == 0); 84 } 85 86 TEST_CASE("count_not(homogeneous)", "[algorithm/count_not]") 87 { 88 const std::array arr = {1, 1, 1, 1, 1, 1}; 89 90 const auto count0 = based::count_not(std::begin(arr), std::end(arr), 0); 91 const auto count1 = based::count_not(std::begin(arr), std::end(arr), 1); 92 93 REQUIRE(count0 == 6); 94 REQUIRE(count1 == 0); 95 } 96 97 TEST_CASE("count_not(non homogeneous)", "[algorithm/count_not]") 98 { 99 const std::array arr = {1, 2, 1, 1, 1, 2}; 100 101 const auto count0 = based::count_not(std::begin(arr), std::end(arr), 0); 102 const auto count1 = based::count_not(std::begin(arr), std::end(arr), 1); 103 const auto count2 = based::count_not(std::begin(arr), std::end(arr), 2); 104 105 REQUIRE(count0 == 6); 106 REQUIRE(count1 == 2); 107 REQUIRE(count2 == 4); 108 }