based

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

find_if_test.cpp (6783B)


1 #include <array> 2 3 #include <catch2/catch_test_macros.hpp> 4 5 #include "based/algorithm.hpp" 6 7 struct predicate 8 { 9 int goal; 10 11 explicit predicate(int init) 12 : goal(init) 13 { 14 } 15 16 auto operator()(int n) const { return n == goal; } 17 }; 18 19 TEST_CASE("find_if(empty)", "[algorithm/find_if]") 20 { 21 const std::array<int, 0> arr = {}; 22 const auto* it = 23 based::find_if(std::begin(arr), std::end(arr), predicate {0}); 24 25 REQUIRE(it == std::end(arr)); 26 } 27 28 TEST_CASE("find_if(one)", "[algorithm/find_if]") 29 { 30 const std::array arr = {0}; 31 32 SECTION("found") 33 { 34 const auto* itr = 35 based::find_if(std::begin(arr), std::end(arr), predicate {0}); 36 const auto idx = std::distance(std::cbegin(arr), itr); 37 REQUIRE(idx == 0); 38 } 39 40 SECTION("not found") 41 { 42 const auto* itr = 43 based::find_if(std::begin(arr), std::end(arr), predicate {1}); 44 const auto idx = std::distance(std::cbegin(arr), itr); 45 REQUIRE(idx == 1); 46 } 47 } 48 49 TEST_CASE("find_if(two)", "[algorithm/find_if]") 50 { 51 const std::array arr = {0, 1}; 52 53 SECTION("found 1") 54 { 55 const auto* itr = 56 based::find_if(std::begin(arr), std::end(arr), predicate {0}); 57 const auto idx = std::distance(std::cbegin(arr), itr); 58 REQUIRE(idx == 0); 59 } 60 61 SECTION("found 2") 62 { 63 const auto* itr = 64 based::find_if(std::begin(arr), std::end(arr), predicate {1}); 65 const auto idx = std::distance(std::cbegin(arr), itr); 66 REQUIRE(idx == 1); 67 } 68 69 SECTION("not found") 70 { 71 const auto* itr = 72 based::find_if(std::begin(arr), std::end(arr), predicate {2}); 73 const auto idx = std::distance(std::cbegin(arr), itr); 74 REQUIRE(idx == 2); 75 } 76 } 77 78 TEST_CASE("find_if(multiple)", "[algorithm/find_if]") 79 { 80 const std::array arr = {0, 0, 0, 0}; 81 82 SECTION("found") 83 { 84 const auto* itr = 85 based::find_if(std::begin(arr), std::end(arr), predicate {0}); 86 const auto idx = std::distance(std::cbegin(arr), itr); 87 REQUIRE(idx == 0); 88 } 89 90 SECTION("not found") 91 { 92 const auto* itr = 93 based::find_if(std::begin(arr), std::end(arr), predicate {1}); 94 const auto idx = std::distance(std::cbegin(arr), itr); 95 REQUIRE(idx == 4); 96 } 97 } 98 99 TEST_CASE("find_if_not(empty)", "[algorithm/find_if_not]") 100 { 101 const std::array<int, 0> arr = {}; 102 const auto* it = 103 based::find_if_not(std::begin(arr), std::end(arr), predicate {0}); 104 105 REQUIRE(it == std::end(arr)); 106 } 107 108 TEST_CASE("find_if_not(one)", "[algorithm/find_if_not]") 109 { 110 const std::array arr = {0}; 111 112 SECTION("found") 113 { 114 const auto* itr = 115 based::find_if_not(std::begin(arr), std::end(arr), predicate {1}); 116 117 const auto idx = std::distance(std::cbegin(arr), itr); 118 REQUIRE(idx == 0); 119 } 120 121 SECTION("not found") 122 { 123 const auto* itr = 124 based::find_if_not(std::begin(arr), std::end(arr), predicate {0}); 125 const auto idx = std::distance(std::cbegin(arr), itr); 126 REQUIRE(idx == 1); 127 } 128 } 129 130 TEST_CASE("find_if_not(two)", "[algorithm/find_if_not]") 131 { 132 const std::array arr = {0, 1}; 133 134 SECTION("found 1") 135 { 136 const auto* itr = 137 based::find_if_not(std::begin(arr), std::end(arr), predicate {1}); 138 const auto idx = std::distance(std::cbegin(arr), itr); 139 REQUIRE(idx == 0); 140 } 141 142 SECTION("found 2") 143 { 144 const auto* itr = 145 based::find_if_not(std::begin(arr), std::end(arr), predicate {0}); 146 const auto idx = std::distance(std::cbegin(arr), itr); 147 REQUIRE(idx == 1); 148 } 149 } 150 151 TEST_CASE("find_if_not(multiple)", "[algorithm/find_if_not]") 152 { 153 const std::array arr = {0, 0, 0, 0}; 154 155 SECTION("found") 156 { 157 const auto* itr = 158 based::find_if_not(std::begin(arr), std::end(arr), predicate {1}); 159 const auto idx = std::distance(std::cbegin(arr), itr); 160 REQUIRE(idx == 0); 161 } 162 163 SECTION("not found") 164 { 165 const auto* itr = 166 based::find_if_not(std::begin(arr), std::end(arr), predicate {0}); 167 const auto idx = std::distance(std::cbegin(arr), itr); 168 REQUIRE(idx == 4); 169 } 170 } 171 172 TEST_CASE("all(empty)", "[algorithm/all]") 173 { 174 const std::array<int, 0> arr = {}; 175 176 REQUIRE(based::all(std::begin(arr), std::end(arr), predicate {0})); 177 } 178 179 TEST_CASE("all(homogeneous)", "[algorithm/all]") 180 { 181 const std::array arr = {1, 1, 1, 1, 1, 1}; 182 183 REQUIRE(based::all(std::begin(arr), std::end(arr), predicate {1})); 184 REQUIRE(!based::all(std::begin(arr), std::end(arr), predicate {2})); 185 } 186 187 TEST_CASE("all(non homogenous)", "[algorithm/all]") 188 { 189 const std::array arr = {1, 2, 1, 1, 1, 2}; 190 191 REQUIRE(!based::all(std::begin(arr), std::end(arr), predicate {1})); 192 REQUIRE(!based::all(std::begin(arr), std::end(arr), predicate {2})); 193 } 194 195 TEST_CASE("none(empty)", "[algorithm/none]") 196 { 197 const std::array<int, 0> arr = {}; 198 199 REQUIRE(based::none(std::begin(arr), std::end(arr), predicate {0})); 200 } 201 202 TEST_CASE("none(homogeneous)", "[algorithm/none]") 203 { 204 const std::array arr = {1, 1, 1, 1, 1, 1}; 205 206 REQUIRE(based::none(std::begin(arr), std::end(arr), predicate {2})); 207 REQUIRE(!based::none(std::begin(arr), std::end(arr), predicate {1})); 208 } 209 210 TEST_CASE("none(non homogeneous)", "[algorithm/none]") 211 { 212 const std::array arr = {1, 2, 1, 1, 1, 2}; 213 214 REQUIRE(based::none(std::begin(arr), std::end(arr), predicate {0})); 215 REQUIRE(!based::none(std::begin(arr), std::end(arr), predicate {2})); 216 REQUIRE(!based::none(std::begin(arr), std::end(arr), predicate {1})); 217 } 218 219 TEST_CASE("not_all(empty)", "[algorithm/not_all]") 220 { 221 const std::array<int, 0> arr = {}; 222 223 REQUIRE(based::not_all(std::begin(arr), std::end(arr), predicate {0})); 224 } 225 226 TEST_CASE("not_all(homogeneous)", "[algorithm/not_all]") 227 { 228 const std::array arr = {1, 1, 1, 1, 1, 1}; 229 230 REQUIRE(based::not_all(std::begin(arr), std::end(arr), predicate {2})); 231 REQUIRE(!based::not_all(std::begin(arr), std::end(arr), predicate {1})); 232 } 233 234 TEST_CASE("not_all(non homogeneous)", "[algorithm/not_all]") 235 { 236 const std::array arr = {1, 2, 1, 1, 1, 2}; 237 238 REQUIRE(based::not_all(std::begin(arr), std::end(arr), predicate {0})); 239 REQUIRE(based::not_all(std::begin(arr), std::end(arr), predicate {1})); 240 REQUIRE(based::not_all(std::begin(arr), std::end(arr), predicate {2})); 241 } 242 243 TEST_CASE("some(empty)", "[algorithm/some]") 244 { 245 const std::array<int, 0> arr = {}; 246 247 REQUIRE(!based::some(std::begin(arr), std::end(arr), predicate {0})); 248 } 249 250 TEST_CASE("some(homogeneous)", "[algorithm/some]") 251 { 252 const std::array arr = {1, 1, 1, 1, 1, 1}; 253 254 REQUIRE(based::some(std::begin(arr), std::end(arr), predicate {1})); 255 REQUIRE(!based::some(std::begin(arr), std::end(arr), predicate {2})); 256 } 257 258 TEST_CASE("some(non homogeneous)", "[algorithm/some]") 259 { 260 const std::array arr = {1, 2, 1, 1, 1, 2}; 261 262 REQUIRE(based::some(std::begin(arr), std::end(arr), predicate {1})); 263 REQUIRE(based::some(std::begin(arr), std::end(arr), predicate {2})); 264 REQUIRE(!based::some(std::begin(arr), std::end(arr), predicate {0})); 265 }