Skip to content

Commit

Permalink
Add tests for NonMovableNonCopyable element, error type (issue #58)
Browse files Browse the repository at this point in the history
  • Loading branch information
martinmoene committed Jun 22, 2023
1 parent c600db9 commit d2250ee
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 12 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,8 @@ bad_expected_access: Allows to observe its error
bad_expected_access: Allows to change its error
bad_expected_access: Provides non-empty what()
expected: Allows to default construct
expected: Allows to default construct from noncopyable, noncopyable value type
expected: Allows to default construct from noncopyable, noncopyable error type
expected: Allows to copy-construct from expected: value
expected: Allows to copy-construct from expected: error
expected: Allows to move-construct from expected: value
Expand Down
52 changes: 40 additions & 12 deletions test/expected.t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,35 @@ using namespace nonstd;

struct Implicit { int x; Implicit(int v) : x(v) {} };
struct Explicit { int x; explicit Explicit(int v) : x(v) {} };
struct MoveOnly {

struct MoveOnly
{
int x;
explicit MoveOnly(int x) :x{x} {}
MoveOnly(const MoveOnly&) = delete;
MoveOnly(MoveOnly&& other) noexcept :x{other.x} {}
MoveOnly& operator=(const MoveOnly&) = delete;
MoveOnly& operator=(MoveOnly&& other) noexcept {
if (&other == this) return *this;

MoveOnly( MoveOnly const & ) = delete;
MoveOnly( MoveOnly && other ) noexcept : x{ other.x } {}

MoveOnly& operator=( MoveOnly const & ) = delete;
MoveOnly& operator=( MoveOnly && other ) noexcept
{
if (&other == this)
return *this;
x = other.x;
return *this;
}
};

struct NonMovableNonCopyable
{
NonMovableNonCopyable() = default;

NonMovableNonCopyable( NonMovableNonCopyable const & ) = delete;
NonMovableNonCopyable( NonMovableNonCopyable && ) = delete;
NonMovableNonCopyable& operator=( NonMovableNonCopyable const & ) = delete;
NonMovableNonCopyable& operator=( NonMovableNonCopyable && ) = delete;
};

bool operator==( Implicit a, Implicit b ) { return a.x == b.x; }
bool operator==( Explicit a, Explicit b ) { return a.x == b.x; }

Expand Down Expand Up @@ -619,6 +635,20 @@ CASE( "expected: Allows to default construct" )
EXPECT( e.has_value() );
}

CASE( "expected: Allows to default construct from noncopyable, noncopyable value type" )
{
expected<NonMovableNonCopyable, NonMovableNonCopyable> e;

EXPECT( e.has_value() );
}

CASE( "expected: Allows to default construct from noncopyable, noncopyable error type" )
{
expected<NonMovableNonCopyable, NonMovableNonCopyable> e{ unexpect_t{} };

EXPECT( !e.has_value() );
}

CASE( "expected: Allows to copy-construct from expected: value" )
{
expected<int, char> a = 7;
Expand Down Expand Up @@ -1892,13 +1922,11 @@ struct NonMovableNonCopyable

CASE( "issue-58" )
{
using namespace issue_59;

static_assert( !std::is_copy_constructible<NonMovableNonCopyable>::value, "is not copy constructible" );
static_assert( !std::is_move_constructible<NonMovableNonCopyable>::value, "is not move constructible" );
static_assert( !std::is_copy_constructible<issue_59::NonMovableNonCopyable>::value, "is not copy constructible" );
static_assert( !std::is_move_constructible<issue_59::NonMovableNonCopyable>::value, "is not move constructible" );

nonstd::expected<NonMovableNonCopyable, NonMovableNonCopyable> expected;
nonstd::expected<NonMovableNonCopyable, NonMovableNonCopyable> unexpected( nonstd::unexpect_t{} );
nonstd::expected<issue_59::NonMovableNonCopyable, issue_59::NonMovableNonCopyable> expected;
nonstd::expected<issue_59::NonMovableNonCopyable, issue_59::NonMovableNonCopyable> unexpected( nonstd::unexpect_t{} );

EXPECT( expected.has_value() );
EXPECT( !unexpected.has_value() );
Expand Down

0 comments on commit d2250ee

Please sign in to comment.