based

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

count_test.cpp (2811B)


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