based

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

partition_test.cpp (4111B)


0 #include <array> 1 2 #include <catch2/catch_test_macros.hpp> 3 4 #include "based/algorithm.hpp" 5 6 struct equal 7 { 8 auto operator()(int a) const { return a == goal; } 9 10 int goal; 11 }; 12 13 TEST_CASE("partitioned(empty)", "[algorithm/partitioned]") 14 { 15 std::array<int, 0> arr = {}; 16 17 REQUIRE(based::partitioned(std::begin(arr), std::end(arr), equal {4})); 18 } 19 20 TEST_CASE("partitioned(one equal)", "[algorithm/partitioned]") 21 { 22 std::array arr = {4}; 23 24 REQUIRE(based::partitioned(std::begin(arr), std::end(arr), equal {4})); 25 } 26 27 TEST_CASE("partitioned(one nonequal)", "[algorithm/partitioned]") 28 { 29 std::array arr = {1}; 30 31 REQUIRE(based::partitioned(std::begin(arr), std::end(arr), equal {4})); 32 } 33 34 TEST_CASE("partitioned(partitioned)", "[algorithm/partitioned]") 35 { 36 std::array arr = {0, 1, 2, 3, 4, 4, 4, 4}; 37 38 REQUIRE(based::partitioned(std::begin(arr), std::end(arr), equal {4})); 39 } 40 41 TEST_CASE("partitioned(nonpartitioned equal)", "[algorithm/partitioned]") 42 { 43 std::array arr = {4, 0, 1, 2, 3, 4, 4, 4}; 44 REQUIRE(!based::partitioned(std::begin(arr), std::end(arr), equal {4})); 45 } 46 47 TEST_CASE("partitioned(nonpartitioned nonequal)", "[algorithm/partitioned]") 48 { 49 std::array arr = {4, 0, 1, 2, 3, 4, 4, 4, 0}; 50 51 REQUIRE(!based::partitioned(std::begin(arr), std::end(arr), equal {4})); 52 } 53 54 TEST_CASE("partition_point(empty)", "[algorithm/partition_point]") 55 { 56 std::array<int, 0> arr = {}; 57 58 const auto* itr = 59 based::partition_point(std::begin(arr), std::end(arr), equal {4}); 60 REQUIRE(itr == std::end(arr)); 61 } 62 63 TEST_CASE("partition_point(one equal)", "[algorithm/partition_point]") 64 { 65 std::array arr = {4}; 66 67 const auto* itr = 68 based::partition_point(std::begin(arr), std::end(arr), equal {4}); 69 REQUIRE(itr == std::begin(arr)); 70 } 71 72 TEST_CASE("partition_point(one nonequal)", "[algorithm/partition_point]") 73 { 74 std::array arr = {1}; 75 76 const auto* itr = 77 based::partition_point(std::begin(arr), std::end(arr), equal {4}); 78 REQUIRE(itr == std::end(arr)); 79 } 80 81 TEST_CASE("partition_point(sequence)", "[algorithm/partition_point]") 82 { 83 std::array arr = {0, 1, 2, 3, 4, 4, 4, 4}; 84 85 const auto* itr = 86 based::partition_point(std::begin(arr), std::end(arr), equal {4}); 87 REQUIRE(itr == std::next(std::begin(arr), 4)); 88 } 89 90 TEST_CASE("lower_bound(empty)", "[algorithm/lower_bound]") 91 { 92 std::array<int, 0> arr = {}; 93 94 const auto* itr = 95 based::lower_bound(std::begin(arr), std::end(arr), 4, std::less<int> {}); 96 REQUIRE(itr == std::end(arr)); 97 } 98 99 TEST_CASE("lower_bound(one equal)", "[algorithm/lower_bound]") 100 { 101 std::array arr = {4}; 102 103 const auto* itr = 104 based::lower_bound(std::begin(arr), std::end(arr), 4, std::less<int> {}); 105 REQUIRE(itr == std::begin(arr)); 106 } 107 108 TEST_CASE("lower_bound(one nonequal)", "[algorithm/lower_bound]") 109 { 110 std::array arr = {1}; 111 112 const auto* itr = 113 based::lower_bound(std::begin(arr), std::end(arr), 4, std::less<int> {}); 114 REQUIRE(itr == std::end(arr)); 115 } 116 117 TEST_CASE("lower_bound(sequence)", "[algorithm/lower_bound]") 118 { 119 std::array arr = {0, 1, 2, 3, 3, 4}; 120 121 const auto* itr = 122 based::lower_bound(std::begin(arr), std::end(arr), 3, std::less<int> {}); 123 REQUIRE(itr == std::next(std::begin(arr), 3)); 124 } 125 126 TEST_CASE("upper_bound(empty)", "[algorithm/lower_bound]") 127 { 128 std::array<int, 0> arr = {}; 129 130 const auto* itr = 131 based::upper_bound(std::begin(arr), std::end(arr), 4, std::less<int> {}); 132 REQUIRE(itr == std::end(arr)); 133 } 134 135 TEST_CASE("upper_bound(one equal)", "[algorithm/lower_bound]") 136 { 137 std::array arr = {4}; 138 139 const auto* itr = 140 based::upper_bound(std::begin(arr), std::end(arr), 4, std::less<int> {}); 141 REQUIRE(itr == std::end(arr)); 142 } 143 144 TEST_CASE("upper_bound(one nonequal)", "[algorithm/lower_bound]") 145 { 146 std::array arr = {1}; 147 148 const auto* itr = 149 based::upper_bound(std::begin(arr), std::end(arr), 4, std::less<int> {}); 150 REQUIRE(itr == std::end(arr)); 151 } 152 153 TEST_CASE("upper_bound(sequence)", "[algorithm/lower_bound]") 154 { 155 std::array arr = {0, 1, 2, 3, 3, 4}; 156 157 const auto* itr = 158 based::upper_bound(std::begin(arr), std::end(arr), 3, std::less<int> {}); 159 REQUIRE(itr == std::next(std::begin(arr), 5)); 160 }