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