1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
|
#include <catch2/catch.hpp>
#include <tl/optional.hpp>
#include <string>
constexpr int get_int(int) { return 42; }
TL_OPTIONAL_11_CONSTEXPR tl::optional<int> get_opt_int(int) { return 42; }
// What is Clang Format up to?!
TEST_CASE("Monadic operations", "[monadic]") {
SECTION("map") { // lhs is empty
tl::optional<int> o1;
auto o1r = o1.map([](int i) { return i + 2; });
STATIC_REQUIRE((std::is_same<decltype(o1r), tl::optional<int>>::value));
REQUIRE(!o1r);
// lhs has value
tl::optional<int> o2 = 40;
auto o2r = o2.map([](int i) { return i + 2; });
STATIC_REQUIRE((std::is_same<decltype(o2r), tl::optional<int>>::value));
REQUIRE(o2r.value() == 42);
struct rval_call_map {
double operator()(int) && { return 42.0; };
};
// ensure that function object is forwarded
tl::optional<int> o3 = 42;
auto o3r = o3.map(rval_call_map{});
STATIC_REQUIRE((std::is_same<decltype(o3r), tl::optional<double>>::value));
REQUIRE(o3r.value() == 42);
// ensure that lhs is forwarded
tl::optional<int> o4 = 40;
auto o4r = std::move(o4).map([](int &&i) { return i + 2; });
STATIC_REQUIRE((std::is_same<decltype(o4r), tl::optional<int>>::value));
REQUIRE(o4r.value() == 42);
// ensure that lhs is const-propagated
const tl::optional<int> o5 = 40;
auto o5r = o5.map([](const int &i) { return i + 2; });
STATIC_REQUIRE((std::is_same<decltype(o5r), tl::optional<int>>::value));
REQUIRE(o5r.value() == 42);
// test void return
tl::optional<int> o7 = 40;
auto f7 = [](const int &) { return; };
auto o7r = o7.map(f7);
STATIC_REQUIRE(
(std::is_same<decltype(o7r), tl::optional<tl::monostate>>::value));
REQUIRE(o7r.has_value());
// test each overload in turn
tl::optional<int> o8 = 42;
auto o8r = o8.map([](int) { return 42; });
REQUIRE(*o8r == 42);
tl::optional<int> o9 = 42;
auto o9r = o9.map([](int) { return; });
REQUIRE(o9r);
tl::optional<int> o12 = 42;
auto o12r = std::move(o12).map([](int) { return 42; });
REQUIRE(*o12r == 42);
tl::optional<int> o13 = 42;
auto o13r = std::move(o13).map([](int) { return; });
REQUIRE(o13r);
const tl::optional<int> o16 = 42;
auto o16r = o16.map([](int) { return 42; });
REQUIRE(*o16r == 42);
const tl::optional<int> o17 = 42;
auto o17r = o17.map([](int) { return; });
REQUIRE(o17r);
const tl::optional<int> o20 = 42;
auto o20r = std::move(o20).map([](int) { return 42; });
REQUIRE(*o20r == 42);
const tl::optional<int> o21 = 42;
auto o21r = std::move(o21).map([](int) { return; });
REQUIRE(o21r);
tl::optional<int> o24 = tl::nullopt;
auto o24r = o24.map([](int) { return 42; });
REQUIRE(!o24r);
tl::optional<int> o25 = tl::nullopt;
auto o25r = o25.map([](int) { return; });
REQUIRE(!o25r);
tl::optional<int> o28 = tl::nullopt;
auto o28r = std::move(o28).map([](int) { return 42; });
REQUIRE(!o28r);
tl::optional<int> o29 = tl::nullopt;
auto o29r = std::move(o29).map([](int) { return; });
REQUIRE(!o29r);
const tl::optional<int> o32 = tl::nullopt;
auto o32r = o32.map([](int) { return 42; });
REQUIRE(!o32r);
const tl::optional<int> o33 = tl::nullopt;
auto o33r = o33.map([](int) { return; });
REQUIRE(!o33r);
const tl::optional<int> o36 = tl::nullopt;
auto o36r = std::move(o36).map([](int) { return 42; });
REQUIRE(!o36r);
const tl::optional<int> o37 = tl::nullopt;
auto o37r = std::move(o37).map([](int) { return; });
REQUIRE(!o37r);
// callable which returns a reference
tl::optional<int> o38 = 42;
auto o38r = o38.map([](int &i) -> const int & { return i; });
REQUIRE(o38r);
REQUIRE(*o38r == 42);
int i = 42;
tl::optional<int&> o39 = i;
o39.map([](int& x){x = 12;});
REQUIRE(i == 12);
}
SECTION("map constexpr") {
#if !defined(_MSC_VER) && defined(TL_OPTIONAL_CXX14)
// test each overload in turn
constexpr tl::optional<int> o16 = 42;
constexpr auto o16r = o16.map(get_int);
STATIC_REQUIRE(*o16r == 42);
constexpr tl::optional<int> o20 = 42;
constexpr auto o20r = std::move(o20).map(get_int);
STATIC_REQUIRE(*o20r == 42);
constexpr tl::optional<int> o32 = tl::nullopt;
constexpr auto o32r = o32.map(get_int);
STATIC_REQUIRE(!o32r);
constexpr tl::optional<int> o36 = tl::nullopt;
constexpr auto o36r = std::move(o36).map(get_int);
STATIC_REQUIRE(!o36r);
#endif
}
SECTION("transform") { // lhs is empty
tl::optional<int> o1;
auto o1r = o1.transform([](int i) { return i + 2; });
STATIC_REQUIRE((std::is_same<decltype(o1r), tl::optional<int>>::value));
REQUIRE(!o1r);
// lhs has value
tl::optional<int> o2 = 40;
auto o2r = o2.transform([](int i) { return i + 2; });
STATIC_REQUIRE((std::is_same<decltype(o2r), tl::optional<int>>::value));
REQUIRE(o2r.value() == 42);
struct rval_call_transform {
double operator()(int) && { return 42.0; };
};
// ensure that function object is forwarded
tl::optional<int> o3 = 42;
auto o3r = o3.transform(rval_call_transform{});
STATIC_REQUIRE((std::is_same<decltype(o3r), tl::optional<double>>::value));
REQUIRE(o3r.value() == 42);
// ensure that lhs is forwarded
tl::optional<int> o4 = 40;
auto o4r = std::move(o4).transform([](int&& i) { return i + 2; });
STATIC_REQUIRE((std::is_same<decltype(o4r), tl::optional<int>>::value));
REQUIRE(o4r.value() == 42);
// ensure that lhs is const-propagated
const tl::optional<int> o5 = 40;
auto o5r = o5.transform([](const int& i) { return i + 2; });
STATIC_REQUIRE((std::is_same<decltype(o5r), tl::optional<int>>::value));
REQUIRE(o5r.value() == 42);
// test void return
tl::optional<int> o7 = 40;
auto f7 = [](const int&) { return; };
auto o7r = o7.transform(f7);
STATIC_REQUIRE(
(std::is_same<decltype(o7r), tl::optional<tl::monostate>>::value));
REQUIRE(o7r.has_value());
// test each overload in turn
tl::optional<int> o8 = 42;
auto o8r = o8.transform([](int) { return 42; });
REQUIRE(*o8r == 42);
tl::optional<int> o9 = 42;
auto o9r = o9.transform([](int) { return; });
REQUIRE(o9r);
tl::optional<int> o12 = 42;
auto o12r = std::move(o12).transform([](int) { return 42; });
REQUIRE(*o12r == 42);
tl::optional<int> o13 = 42;
auto o13r = std::move(o13).transform([](int) { return; });
REQUIRE(o13r);
const tl::optional<int> o16 = 42;
auto o16r = o16.transform([](int) { return 42; });
REQUIRE(*o16r == 42);
const tl::optional<int> o17 = 42;
auto o17r = o17.transform([](int) { return; });
REQUIRE(o17r);
const tl::optional<int> o20 = 42;
auto o20r = std::move(o20).transform([](int) { return 42; });
REQUIRE(*o20r == 42);
const tl::optional<int> o21 = 42;
auto o21r = std::move(o21).transform([](int) { return; });
REQUIRE(o21r);
tl::optional<int> o24 = tl::nullopt;
auto o24r = o24.transform([](int) { return 42; });
REQUIRE(!o24r);
tl::optional<int> o25 = tl::nullopt;
auto o25r = o25.transform([](int) { return; });
REQUIRE(!o25r);
tl::optional<int> o28 = tl::nullopt;
auto o28r = std::move(o28).transform([](int) { return 42; });
REQUIRE(!o28r);
tl::optional<int> o29 = tl::nullopt;
auto o29r = std::move(o29).transform([](int) { return; });
REQUIRE(!o29r);
const tl::optional<int> o32 = tl::nullopt;
auto o32r = o32.transform([](int) { return 42; });
REQUIRE(!o32r);
const tl::optional<int> o33 = tl::nullopt;
auto o33r = o33.transform([](int) { return; });
REQUIRE(!o33r);
const tl::optional<int> o36 = tl::nullopt;
auto o36r = std::move(o36).transform([](int) { return 42; });
REQUIRE(!o36r);
const tl::optional<int> o37 = tl::nullopt;
auto o37r = std::move(o37).transform([](int) { return; });
REQUIRE(!o37r);
// callable which returns a reference
tl::optional<int> o38 = 42;
auto o38r = o38.transform([](int& i) -> const int& { return i; });
REQUIRE(o38r);
REQUIRE(*o38r == 42);
int i = 42;
tl::optional<int&> o39 = i;
o39.transform([](int& x) {x = 12; });
REQUIRE(i == 12);
}
SECTION("transform constexpr") {
#if !defined(_MSC_VER) && defined(TL_OPTIONAL_CXX14)
// test each overload in turn
constexpr tl::optional<int> o16 = 42;
constexpr auto o16r = o16.transform(get_int);
STATIC_REQUIRE(*o16r == 42);
constexpr tl::optional<int> o20 = 42;
constexpr auto o20r = std::move(o20).transform(get_int);
STATIC_REQUIRE(*o20r == 42);
constexpr tl::optional<int> o32 = tl::nullopt;
constexpr auto o32r = o32.transform(get_int);
STATIC_REQUIRE(!o32r);
constexpr tl::optional<int> o36 = tl::nullopt;
constexpr auto o36r = std::move(o36).transform(get_int);
STATIC_REQUIRE(!o36r);
#endif
}
SECTION("and_then") {
// lhs is empty
tl::optional<int> o1;
auto o1r = o1.and_then([](int) { return tl::optional<float>{42}; });
STATIC_REQUIRE((std::is_same<decltype(o1r), tl::optional<float>>::value));
REQUIRE(!o1r);
// lhs has value
tl::optional<int> o2 = 12;
auto o2r = o2.and_then([](int) { return tl::optional<float>{42}; });
STATIC_REQUIRE((std::is_same<decltype(o2r), tl::optional<float>>::value));
REQUIRE(o2r.value() == 42.f);
// lhs is empty, rhs returns empty
tl::optional<int> o3;
auto o3r = o3.and_then([](int) { return tl::optional<float>{}; });
STATIC_REQUIRE((std::is_same<decltype(o3r), tl::optional<float>>::value));
REQUIRE(!o3r);
// rhs returns empty
tl::optional<int> o4 = 12;
auto o4r = o4.and_then([](int) { return tl::optional<float>{}; });
STATIC_REQUIRE((std::is_same<decltype(o4r), tl::optional<float>>::value));
REQUIRE(!o4r);
struct rval_call_and_then {
tl::optional<double> operator()(int) && {
return tl::optional<double>(42.0);
};
};
// ensure that function object is forwarded
tl::optional<int> o5 = 42;
auto o5r = o5.and_then(rval_call_and_then{});
STATIC_REQUIRE((std::is_same<decltype(o5r), tl::optional<double>>::value));
REQUIRE(o5r.value() == 42);
// ensure that lhs is forwarded
tl::optional<int> o6 = 42;
auto o6r =
std::move(o6).and_then([](int &&i) { return tl::optional<double>(i); });
STATIC_REQUIRE((std::is_same<decltype(o6r), tl::optional<double>>::value));
REQUIRE(o6r.value() == 42);
// ensure that function object is const-propagated
const tl::optional<int> o7 = 42;
auto o7r =
o7.and_then([](const int &i) { return tl::optional<double>(i); });
STATIC_REQUIRE((std::is_same<decltype(o7r), tl::optional<double>>::value));
REQUIRE(o7r.value() == 42);
// test each overload in turn
tl::optional<int> o8 = 42;
auto o8r = o8.and_then([](int) { return tl::make_optional(42); });
REQUIRE(*o8r == 42);
tl::optional<int> o9 = 42;
auto o9r =
std::move(o9).and_then([](int) { return tl::make_optional(42); });
REQUIRE(*o9r == 42);
const tl::optional<int> o10 = 42;
auto o10r = o10.and_then([](int) { return tl::make_optional(42); });
REQUIRE(*o10r == 42);
const tl::optional<int> o11 = 42;
auto o11r =
std::move(o11).and_then([](int) { return tl::make_optional(42); });
REQUIRE(*o11r == 42);
tl::optional<int> o16 = tl::nullopt;
auto o16r = o16.and_then([](int) { return tl::make_optional(42); });
REQUIRE(!o16r);
tl::optional<int> o17 = tl::nullopt;
auto o17r =
std::move(o17).and_then([](int) { return tl::make_optional(42); });
REQUIRE(!o17r);
const tl::optional<int> o18 = tl::nullopt;
auto o18r = o18.and_then([](int) { return tl::make_optional(42); });
REQUIRE(!o18r);
const tl::optional<int> o19 = tl::nullopt;
auto o19r = std::move(o19).and_then([](int) { return tl::make_optional(42); });
REQUIRE(!o19r);
int i = 3;
tl::optional<int&> o20{i};
std::move(o20).and_then([](int& r){return tl::optional<int&>{++r};});
REQUIRE(o20);
REQUIRE(i == 4);
}
SECTION("constexpr and_then") {
#if !defined(_MSC_VER) && defined(TL_OPTIONAL_CXX14)
constexpr tl::optional<int> o10 = 42;
constexpr auto o10r = o10.and_then(get_opt_int);
REQUIRE(*o10r == 42);
constexpr tl::optional<int> o11 = 42;
constexpr auto o11r = std::move(o11).and_then(get_opt_int);
REQUIRE(*o11r == 42);
constexpr tl::optional<int> o18 = tl::nullopt;
constexpr auto o18r = o18.and_then(get_opt_int);
REQUIRE(!o18r);
constexpr tl::optional<int> o19 = tl::nullopt;
constexpr auto o19r = std::move(o19).and_then(get_opt_int);
REQUIRE(!o19r);
#endif
}
SECTION("or else") {
tl::optional<int> o1 = 42;
REQUIRE(*(o1.or_else([] { return tl::make_optional(13); })) == 42);
tl::optional<int> o2;
REQUIRE(*(o2.or_else([] { return tl::make_optional(13); })) == 13);
}
SECTION("disjunction") {
tl::optional<int> o1 = 42;
tl::optional<int> o2 = 12;
tl::optional<int> o3;
REQUIRE(*o1.disjunction(o2) == 42);
REQUIRE(*o1.disjunction(o3) == 42);
REQUIRE(*o2.disjunction(o1) == 12);
REQUIRE(*o2.disjunction(o3) == 12);
REQUIRE(*o3.disjunction(o1) == 42);
REQUIRE(*o3.disjunction(o2) == 12);
}
SECTION("conjunction") {
tl::optional<int> o1 = 42;
REQUIRE(*o1.conjunction(42.0) == 42.0);
REQUIRE(*o1.conjunction(std::string{"hello"}) == std::string{"hello"});
tl::optional<int> o2;
REQUIRE(!o2.conjunction(42.0));
REQUIRE(!o2.conjunction(std::string{"hello"}));
}
SECTION("map_or") {
tl::optional<int> o1 = 21;
REQUIRE((o1.map_or([](int x) { return x * 2; }, 13)) == 42);
tl::optional<int> o2;
REQUIRE((o2.map_or([](int x) { return x * 2; }, 13)) == 13);
}
SECTION("map_or_else") {
tl::optional<int> o1 = 21;
REQUIRE((o1.map_or_else([](int x) { return x * 2; }, [] { return 13; })) ==
42);
tl::optional<int> o2;
REQUIRE((o2.map_or_else([](int x) { return x * 2; }, [] { return 13; })) ==
13);
}
SECTION("take") {
tl::optional<int> o1 = 42;
REQUIRE(*o1.take() == 42);
REQUIRE(!o1);
tl::optional<int> o2;
REQUIRE(!o2.take());
REQUIRE(!o2);
}
struct foo {
void non_const() {}
};
#if defined(TL_OPTIONAL_CXX14) && !defined(TL_OPTIONAL_GCC49) && \
!defined(TL_OPTIONAL_GCC54) && !defined(TL_OPTIONAL_GCC55)
SECTION("Issue #1") {
tl::optional<foo> f = foo{};
auto l = [](auto &&x) { x.non_const(); };
f.map(l);
}
#endif
struct overloaded {
tl::optional<int> operator()(foo &) { return 0; }
tl::optional<std::string> operator()(const foo &) { return ""; }
};
SECTION("Issue #2") {
tl::optional<foo> f = foo{};
auto x = f.and_then(overloaded{});
}
};
|