From 7fda8b9f65d1c85e6fd93f61dc23ade1acd6b5df Mon Sep 17 00:00:00 2001 From: Denis Blank Date: Thu, 30 Nov 2017 04:17:13 +0100 Subject: [PATCH] Fix some build errors in expected and add typed tests --- include/continuable/detail/awaiting.hpp | 5 +- include/continuable/detail/expected.hpp | 16 +++- test/unit-test/test-continuable-expected.cpp | 85 +++++++++++++++++++- 3 files changed, 99 insertions(+), 7 deletions(-) diff --git a/include/continuable/detail/awaiting.hpp b/include/continuable/detail/awaiting.hpp index 2bf78e8..2104944 100644 --- a/include/continuable/detail/awaiting.hpp +++ b/include/continuable/detail/awaiting.hpp @@ -37,8 +37,9 @@ #include #include -#include +// #include #include +#include namespace cti { namespace detail { @@ -54,7 +55,7 @@ class awaitable { Continuable continuable_; /// A cache which is used to pass the result of the continuation /// to the - util::expected cache_; + // util::expected cache_; public: /// Since continuables are evaluated lazily we are not diff --git a/include/continuable/detail/expected.hpp b/include/continuable/detail/expected.hpp index ac0245f..86f7dc6 100644 --- a/include/continuable/detail/expected.hpp +++ b/include/continuable/detail/expected.hpp @@ -63,6 +63,10 @@ struct expected_copy_base { Base& me = *static_cast(this); Base const& other = *static_cast(&right); +#ifndef _NDEBUG + me.set(slot_t::empty); +#endif + other.visit([&](auto&& value) { // ... me.init(std::move(value)); @@ -104,6 +108,10 @@ struct expected_move_base { Base& me = *static_cast(this); Base& other = *static_cast(&right); +#ifndef _NDEBUG + me.set(slot_t::empty); +#endif + other.visit([&](auto&& value) { // ... me.init(std::move(value)); @@ -181,8 +189,12 @@ public: explicit constexpr operator bool() const noexcept { return is_value(); } - T& operator*() const noexcept { - assert(!is_value()); + T& operator*() noexcept { + assert(is_value()); + return cast(); + } + T const& operator*() const noexcept { + assert(is_value()); return cast(); } diff --git a/test/unit-test/test-continuable-expected.cpp b/test/unit-test/test-continuable-expected.cpp index 1eabc2e..d84df07 100644 --- a/test/unit-test/test-continuable-expected.cpp +++ b/test/unit-test/test-continuable-expected.cpp @@ -21,21 +21,100 @@ SOFTWARE. **/ +#include + +#include +#include + #include "test-continuable.hpp" using cti::detail::types::error_type; using cti::detail::util::expected; -TEST(expected_tests, can_carry_errors) { +static int const CANARY = 373671; + +template +struct expected_all_tests : testing::Test { + template + auto supply(V&& value) const { + return std::forward(value); + } + template + auto get(V& value) const { + return value; + } +}; +template +struct expected_all_tests>> : testing::Test { + template + auto supply(V&& value) const { + return std::make_unique(std::forward(value)); + } + template + auto get(std::unique_ptr& value) const { + return *value; + } +}; + +using copyable_type = expected; +using unique_type = expected>; + +using expected_test_types = testing::Types; + +TYPED_TEST_CASE(expected_all_tests, expected_test_types); + +TYPED_TEST(expected_all_tests, can_carry_errors) { { - expected e(1); + TypeParam e(this->supply(CANARY)); EXPECT_TRUE(bool(e)); + EXPECT_EQ(this->get(*e), CANARY); EXPECT_TRUE(e.is_value()); EXPECT_FALSE(e.is_error()); } { - expected e(error_type{}); + TypeParam e(error_type{}); + EXPECT_FALSE(bool(e)); + EXPECT_FALSE(e.is_value()); + EXPECT_TRUE(e.is_error()); + } +} + +TYPED_TEST(expected_all_tests, is_empty_constructible) { + TypeParam e; + (void)e; +} + +TYPED_TEST(expected_all_tests, is_move_constructible) { + { + TypeParam e(TypeParam(this->supply(CANARY))); + EXPECT_TRUE(bool(e)); + EXPECT_EQ(this->get(*e), CANARY); + EXPECT_TRUE(e.is_value()); + EXPECT_FALSE(e.is_error()); + } + + { + TypeParam e(TypeParam(error_type{})); + EXPECT_FALSE(bool(e)); + EXPECT_FALSE(e.is_value()); + EXPECT_TRUE(e.is_error()); + } +} + +TEST(expected_copyable_tests, is_copy_constructible) { + { + copyable_type const e_old(CANARY); + copyable_type e(e_old); + EXPECT_TRUE(bool(e)); + EXPECT_EQ(*e, CANARY); + EXPECT_TRUE(e.is_value()); + EXPECT_FALSE(e.is_error()); + } + + { + copyable_type const e_old(error_type{}); + copyable_type e(e_old); EXPECT_FALSE(bool(e)); EXPECT_FALSE(e.is_value()); EXPECT_TRUE(e.is_error());