Fix some build errors in expected and add typed tests

This commit is contained in:
Denis Blank 2017-11-30 04:17:13 +01:00
parent bf89a98d60
commit 7fda8b9f65
3 changed files with 99 additions and 7 deletions

View File

@ -37,8 +37,9 @@
#include <experimental/coroutine>
#include <continuable/continuable-api.hpp>
#include <continuable/detail/expected.hpp>
// #include <continuable/detail/expected.hpp>
#include <continuable/detail/features.hpp>
#include <continuable/detail/types.hpp>
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<int /*TODO*/> cache_;
// util::expected<int /*TODO*/> cache_;
public:
/// Since continuables are evaluated lazily we are not

View File

@ -63,6 +63,10 @@ struct expected_copy_base {
Base& me = *static_cast<Base*>(this);
Base const& other = *static_cast<Base const*>(&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<Base*>(this);
Base& other = *static_cast<Base*>(&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>();
}
T const& operator*() const noexcept {
assert(is_value());
return cast<T>();
}

View File

@ -21,21 +21,100 @@
SOFTWARE.
**/
#include <memory>
#include <continuable/detail/expected.hpp>
#include <continuable/detail/types.hpp>
#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 <typename T>
struct expected_all_tests : testing::Test {
template <typename V>
auto supply(V&& value) const {
return std::forward<V>(value);
}
template <typename V>
auto get(V& value) const {
return value;
}
};
template <typename T>
struct expected_all_tests<expected<std::unique_ptr<T>>> : testing::Test {
template <typename V>
auto supply(V&& value) const {
return std::make_unique<T>(std::forward<V>(value));
}
template <typename V>
auto get(std::unique_ptr<V>& value) const {
return *value;
}
};
using copyable_type = expected<int>;
using unique_type = expected<std::unique_ptr<int>>;
using expected_test_types = testing::Types<unique_type, copyable_type>;
TYPED_TEST_CASE(expected_all_tests, expected_test_types);
TYPED_TEST(expected_all_tests, can_carry_errors) {
{
expected<int> 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<int> 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());