#include "catch.hpp" #include "expected.hpp" #include #include #include struct takes_init_and_variadic { std::vector v; std::tuple t; template takes_init_and_variadic(std::initializer_list l, Args &&... args) : v(l), t(std::forward(args)...) {} }; TEST_CASE("Constructors", "[constructors]") { { tl::expected e; REQUIRE(e); REQUIRE(e == 0); } { tl::expected e = tl::make_unexpected(0); REQUIRE(!e); REQUIRE(e.error() == 0); } { tl::expected e (tl::unexpect, 0); REQUIRE(!e); REQUIRE(e.error() == 0); } { tl::expected e (tl::in_place, 42); REQUIRE(e); REQUIRE(e == 42); } { tl::expected,int> e (tl::in_place, {0,1}); REQUIRE(e); REQUIRE((*e)[0] == 0); REQUIRE((*e)[1] == 1); } { tl::expected,int> e (tl::in_place, 0, 1); REQUIRE(e); REQUIRE(std::get<0>(*e) == 0); REQUIRE(std::get<1>(*e) == 1); } { tl::expected e (tl::in_place, {0,1}, 2, 3); REQUIRE(e); REQUIRE(e->v[0] == 0); REQUIRE(e->v[1] == 1); REQUIRE(std::get<0>(e->t) == 2); REQUIRE(std::get<1>(e->t) == 3); } { tl::expected e; REQUIRE(std::is_default_constructible::value); REQUIRE(std::is_copy_constructible::value); REQUIRE(std::is_move_constructible::value); REQUIRE(std::is_copy_assignable::value); REQUIRE(std::is_move_assignable::value); REQUIRE(IS_TRIVIALLY_COPY_CONSTRUCTIBLE(decltype(e))::value); REQUIRE(IS_TRIVIALLY_COPY_ASSIGNABLE(decltype(e))::value); # if !defined(TL_EXPECTED_GCC49) REQUIRE(std::is_trivially_move_constructible::value); REQUIRE(std::is_trivially_move_assignable::value); # endif } { tl::expected e; REQUIRE(std::is_default_constructible::value); REQUIRE(std::is_copy_constructible::value); REQUIRE(std::is_move_constructible::value); REQUIRE(std::is_copy_assignable::value); REQUIRE(std::is_move_assignable::value); REQUIRE(!IS_TRIVIALLY_COPY_CONSTRUCTIBLE(decltype(e))::value); REQUIRE(!IS_TRIVIALLY_COPY_ASSIGNABLE(decltype(e))::value); # if !defined(TL_EXPECTED_GCC49) REQUIRE(!std::is_trivially_move_constructible::value); REQUIRE(!std::is_trivially_move_assignable::value); # endif } { tl::expected e; REQUIRE(std::is_default_constructible::value); REQUIRE(std::is_copy_constructible::value); REQUIRE(std::is_move_constructible::value); REQUIRE(std::is_copy_assignable::value); REQUIRE(std::is_move_assignable::value); REQUIRE(!IS_TRIVIALLY_COPY_CONSTRUCTIBLE(decltype(e))::value); REQUIRE(!IS_TRIVIALLY_COPY_ASSIGNABLE(decltype(e))::value); # if !defined(TL_EXPECTED_GCC49) REQUIRE(!std::is_trivially_move_constructible::value); REQUIRE(!std::is_trivially_move_assignable::value); # endif } { tl::expected e; REQUIRE(std::is_default_constructible::value); REQUIRE(std::is_copy_constructible::value); REQUIRE(std::is_move_constructible::value); REQUIRE(std::is_copy_assignable::value); REQUIRE(std::is_move_assignable::value); REQUIRE(!IS_TRIVIALLY_COPY_CONSTRUCTIBLE(decltype(e))::value); REQUIRE(!IS_TRIVIALLY_COPY_ASSIGNABLE(decltype(e))::value); # if !defined(TL_EXPECTED_GCC49) REQUIRE(!std::is_trivially_move_constructible::value); REQUIRE(!std::is_trivially_move_assignable::value); # endif } { tl::expected e; REQUIRE(e); } { tl::expected e (tl::unexpect, 42); REQUIRE(!e); REQUIRE(e.error() == 42); } }