blob: ba44fd96e5b5cc89b0a939ab4c9a4fdeab62caf1 [file] [log] [blame]
Brian Silvermanda861352019-02-02 16:42:28 -08001#include "catch.hpp"
2#include "optional.hpp"
3
4#include <tuple>
5#include <vector>
6
7struct takes_init_and_variadic {
8 std::vector<int> v;
9 std::tuple<int, int> t;
10 template <class... Args>
11 takes_init_and_variadic(std::initializer_list<int> l, Args &&... args)
12 : v(l), t(std::forward<Args>(args)...) {}
13};
14
15TEST_CASE("Make optional", "[make_optional]") {
16 auto o1 = tl::make_optional(42);
17 auto o2 = tl::optional<int>(42);
18
19 constexpr bool is_same = std::is_same<decltype(o1), tl::optional<int>>::value;
20 REQUIRE(is_same);
21 REQUIRE(o1 == o2);
22
23 auto o3 = tl::make_optional<std::tuple<int, int, int, int>>(0, 1, 2, 3);
24 REQUIRE(std::get<0>(*o3) == 0);
25 REQUIRE(std::get<1>(*o3) == 1);
26 REQUIRE(std::get<2>(*o3) == 2);
27 REQUIRE(std::get<3>(*o3) == 3);
28
29 auto o4 = tl::make_optional<std::vector<int>>({0, 1, 2, 3});
30 REQUIRE(o4.value()[0] == 0);
31 REQUIRE(o4.value()[1] == 1);
32 REQUIRE(o4.value()[2] == 2);
33 REQUIRE(o4.value()[3] == 3);
34
35 auto o5 = tl::make_optional<takes_init_and_variadic>({0, 1}, 2, 3);
36 REQUIRE(o5->v[0] == 0);
37 REQUIRE(o5->v[1] == 1);
38 REQUIRE(std::get<0>(o5->t) == 2);
39 REQUIRE(std::get<1>(o5->t) == 3);
40
41 auto i = 42;
42 auto o6 = tl::make_optional<int&>(i);
43 REQUIRE((std::is_same<decltype(o6), tl::optional<int&>>::value));
44 REQUIRE(o6);
45 REQUIRE(*o6 == 42);
46}