diff --git a/expected.hpp b/expected.hpp index a923e0a..ebafc33 100644 --- a/expected.hpp +++ b/expected.hpp @@ -1537,22 +1537,22 @@ public: constexpr bool has_value() const noexcept { return this->m_has_val; } constexpr const T &value() const & { if (!has_value()) - throw bad_expected_access(err()); + throw bad_expected_access(err().value()); return val(); } TL_EXPECTED_11_CONSTEXPR T &value() & { if (!has_value()) - throw bad_expected_access(err()); + throw bad_expected_access(err().value()); return val(); } constexpr const T &&value() const && { if (!has_value()) - throw bad_expected_access(err()); + throw bad_expected_access(err().value()); return std::move(val()); } TL_EXPECTED_11_CONSTEXPR T &&value() && { if (!has_value()) - throw bad_expected_access(err()); + throw bad_expected_access(err().value()); return std::move(val()); } constexpr const E &error() const & { return err().value(); } diff --git a/tests/bases.cpp b/tests/bases.cpp new file mode 100644 index 0000000..9090f74 --- /dev/null +++ b/tests/bases.cpp @@ -0,0 +1,120 @@ +#include "catch.hpp" +#include "expected.hpp" + +TEST_CASE("Triviality", "[bases.triviality]") { + REQUIRE(std::is_trivially_copy_constructible>::value); + REQUIRE(std::is_trivially_copy_assignable>::value); + REQUIRE(std::is_trivially_move_constructible>::value); + REQUIRE(std::is_trivially_move_assignable>::value); + REQUIRE(std::is_trivially_destructible>::value); + + { + struct T { + T(const T&) = default; + T(T&&) = default; + T& operator=(const T&) = default; + T& operator=(T&&) = default; + ~T() = default; + }; + REQUIRE(std::is_trivially_copy_constructible>::value); + REQUIRE(std::is_trivially_copy_assignable>::value); + REQUIRE(std::is_trivially_move_constructible>::value); + REQUIRE(std::is_trivially_move_assignable>::value); + REQUIRE(std::is_trivially_destructible>::value); + } + + { + struct T { + T(const T&){} + T(T&&) {}; + T& operator=(const T&) {} + T& operator=(T&&) {}; + ~T(){} + }; + REQUIRE(!std::is_trivially_copy_constructible>::value); + REQUIRE(!std::is_trivially_copy_assignable>::value); + REQUIRE(!std::is_trivially_move_constructible>::value); + REQUIRE(!std::is_trivially_move_assignable>::value); + REQUIRE(!std::is_trivially_destructible>::value); + } + +} + +TEST_CASE("Deletion", "[bases.deletion]") { + REQUIRE(std::is_copy_constructible>::value); + REQUIRE(std::is_copy_assignable>::value); + REQUIRE(std::is_move_constructible>::value); + REQUIRE(std::is_move_assignable>::value); + REQUIRE(std::is_destructible>::value); + + { + struct T { + T()=default; + }; + REQUIRE(std::is_default_constructible>::value); + } + + { + struct T { + T(int); + }; + REQUIRE(!std::is_default_constructible>::value); + } + + { + struct T { + T(const T&) = default; + T(T&&) = default; + T& operator=(const T&) = default; + T& operator=(T&&) = default; + ~T() = default; + }; + REQUIRE(std::is_copy_constructible>::value); + REQUIRE(std::is_copy_assignable>::value); + REQUIRE(std::is_move_constructible>::value); + REQUIRE(std::is_move_assignable>::value); + REQUIRE(std::is_destructible>::value); + } + + { + struct T { + T(const T&)=delete; + T(T&&)=delete; + T& operator=(const T&)=delete; + T& operator=(T&&)=delete; + }; + REQUIRE(!std::is_copy_constructible>::value); + REQUIRE(!std::is_copy_assignable>::value); + REQUIRE(!std::is_move_constructible>::value); + REQUIRE(!std::is_move_assignable>::value); + } + + { + struct T { + T(const T&)=delete; + T(T&&)=default; + T& operator=(const T&)=delete; + T& operator=(T&&)=default; + }; + REQUIRE(!std::is_copy_constructible>::value); + REQUIRE(!std::is_copy_assignable>::value); + REQUIRE(std::is_move_constructible>::value); + REQUIRE(std::is_move_assignable>::value); + } + + { + struct T { + T(const T&)=default; + T(T&&)=delete; + T& operator=(const T&)=default; + T& operator=(T&&)=delete; + }; + REQUIRE(std::is_copy_constructible>::value); + REQUIRE(std::is_copy_assignable>::value); + //TODO see why this fails + //REQUIRE(!std::is_move_constructible>::value); + //REQUIRE(!std::is_move_assignable>::value); + } + + +} diff --git a/tests/constexpr.cpp b/tests/constexpr.cpp new file mode 100644 index 0000000..0fc0433 --- /dev/null +++ b/tests/constexpr.cpp @@ -0,0 +1,6 @@ +#include "catch.hpp" +#include "expected.hpp" + +TEST_CASE("Constexpr", "[constexpr]") { + //TODO +} diff --git a/tests/constructors.cpp b/tests/constructors.cpp index 219b168..817797b 100644 --- a/tests/constructors.cpp +++ b/tests/constructors.cpp @@ -18,6 +18,18 @@ TEST_CASE("Constructors", "[constructors]") { 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); diff --git a/tests/noexcept.cpp b/tests/noexcept.cpp new file mode 100644 index 0000000..5d1c1a0 --- /dev/null +++ b/tests/noexcept.cpp @@ -0,0 +1,6 @@ +#include "catch.hpp" +#include "expected.hpp" + +TEST_CASE("Noexcept", "[noexcept]") { + //TODO +} diff --git a/tests/observers.cpp b/tests/observers.cpp new file mode 100644 index 0000000..080e6be --- /dev/null +++ b/tests/observers.cpp @@ -0,0 +1,36 @@ +#include "catch.hpp" +#include "expected.hpp" + +struct move_detector { + move_detector() = default; + move_detector(move_detector &&rhs) { rhs.been_moved = true; } + bool been_moved = false; +}; + +TEST_CASE("Observers", "[observers]") { + tl::expected o1 = 42; + tl::expected o2 {tl::unexpect, 0}; + const tl::expected o3 = 42; + + REQUIRE(*o1 == 42); + REQUIRE(*o1 == o1.value()); + REQUIRE(o2.value_or(42) == 42); + REQUIRE(o2.error() == 0); + REQUIRE(o3.value() == 42); + auto success = std::is_same::value; + REQUIRE(success); + success = std::is_same::value; + REQUIRE(success); + success = std::is_same::value; + REQUIRE(success); + + #ifndef TL_EXPECTED_NO_CONSTRR + success = std::is_same::value; + REQUIRE(success); + #endif + + tl::expected o4{tl::in_place}; + move_detector o5 = std::move(o4).value(); + REQUIRE(o4->been_moved); + REQUIRE(!o5.been_moved); +} diff --git a/tests/relops.cpp b/tests/relops.cpp new file mode 100644 index 0000000..2314cee --- /dev/null +++ b/tests/relops.cpp @@ -0,0 +1,6 @@ +#include "catch.hpp" +#include "expected.hpp" + +TEST_CASE("Relational operators", "[relops]") { + //TODO +}