Skip to content

Commit

Permalink
Tests for Unexpected copy and move ctors
Browse files Browse the repository at this point in the history
  • Loading branch information
vt4a2h committed Oct 16, 2024
1 parent 68fca6e commit 5b0fa31
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/include/fl/expected/expected.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,23 @@ struct Unexpected
: m_error(il, std::forward<Args>(args)...)
{}

constexpr Unexpected(const Unexpected&) = default;
constexpr Unexpected(Unexpected&&) = default;
constexpr Unexpected(const Unexpected& src) noexcept(std::is_nothrow_copy_constructible_v<Error_>)
requires (std::is_copy_constructible_v<Error_>)
: m_error(src.m_error)
{}
constexpr Unexpected(Unexpected &&src) noexcept(std::is_nothrow_move_assignable_v<Error_>)
requires (std::is_move_constructible_v<Error_>)
: m_error(std::move(src.m_error))
{}

template <class Self>
constexpr auto&& error(this Self&& self) noexcept
{
return std::forward<Self>(self).m_error;
}

constexpr auto operator<=>(const Unexpected&) const = default;

private:
Error_ m_error;
};
Expand Down
43 changes: 43 additions & 0 deletions test/test_expected_unexpected_create.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@

#include <string>

constexpr fl::Unexpected<int> moveCtorConstexpr(int data)
{
auto unexpected = fl::Unexpected(data);
auto unexpectedMoved{std::move(unexpected)};

return unexpectedMoved;
}

TEST_CASE("Create unexpected")
{
SECTION("From value")
Expand All @@ -32,6 +40,41 @@ TEST_CASE("Create unexpected")
STATIC_REQUIRE(unexpected.error() == data);
}

SECTION("Copy ctor")
{
const auto unexpected = fl::Unexpected("42");
const auto unexpectedCopy{unexpected};

REQUIRE(unexpected == unexpectedCopy);
}

SECTION("[Constexpr] Copy ctor")
{
static constexpr auto unexpected = fl::Unexpected("42");
static constexpr auto unexpectedCopy{unexpected};

STATIC_REQUIRE(unexpected == unexpectedCopy);
}

SECTION("Move ctor")
{
auto unexpected = fl::Unexpected("42");
auto unexpectedMoved{std::move(unexpected)};

REQUIRE(fl::Unexpected("42") == unexpectedMoved);
}

SECTION("[Constexpr] Move ctor")
{
static constexpr int data = 42;

static constexpr auto unexpected = moveCtorConstexpr(data);

static constexpr auto expectedResult = fl::Unexpected(data);

STATIC_REQUIRE(unexpected == expectedResult);
}

SECTION("From value in-place")
{
const auto unexpected = fl::Unexpected<std::string>(std::in_place_t{}, "42");
Expand Down

0 comments on commit 5b0fa31

Please sign in to comment.