based

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

partition_test.cpp (4123B)


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