Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#923 Add equality operators for class expected #925

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 40 additions & 3 deletions include/etl/expected.h
Original file line number Diff line number Diff line change
Expand Up @@ -940,11 +940,48 @@ namespace etl
//*******************************************
/// Equivalence operator.
//*******************************************
template <typename TError>
template <typename TValue, typename TError, typename TValue2, typename TError2>
ETL_CONSTEXPR14
bool operator ==(const etl::expected<TValue, TError>& lhs, const etl::expected<TValue2, TError2>& rhs)
{
if (lhs.has_value() != rhs.has_value())
{
return false;
}
if (lhs.has_value())
{
return lhs.value() == rhs.value();
}
return lhs.error() == rhs.error();
}

template <typename TValue, typename TError, typename TValue2>
ETL_CONSTEXPR14
bool operator ==(const etl::expected<TValue, TError>& lhs, const TValue2& rhs)
{
if (!lhs.has_value())
{
return false;
}
return lhs.value() == rhs;
}

template <typename TValue, typename TError, typename TError2>
ETL_CONSTEXPR14
bool operator ==(const etl::expected<TValue, TError>& lhs, const etl::unexpected<TError2>& rhs)
{
if (lhs.has_value())
{
return false;
}
return lhs.error() == rhs;
}

template <typename TError, typename TError2>
ETL_CONSTEXPR14
bool operator ==(const etl::unexpected<TError>& lhs, const etl::unexpected<TError>& rhs)
bool operator ==(const etl::unexpected<TError>& lhs, const etl::unexpected<TError2>& rhs)
{
return lhs.error_value == rhs.error_value;
return lhs.error() == rhs.error();
}

//*******************************************
Expand Down
49 changes: 49 additions & 0 deletions test/test_expected.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -629,5 +629,54 @@ namespace
CHECK_TRUE(thrown);
CHECK_TRUE(exception_what == thrown_what);
}

//*************************************************************************
TEST(test_expected_equal_operator)
{
etl::expected<int, int> test_exp = 1;
etl::expected<int, int> test_exp_equal = 1;
etl::expected<int, int> test_exp_unequal = 2;
int test_val_equal = 1;
int test_val_unequal = 2;
etl::expected<int, int> test_unexp = etl::unexpected<int>(1);
etl::expected<int, int> test_unexp_equal = etl::unexpected<int>(1);
etl::expected<int, int> test_unexp_unequal = etl::unexpected<int>(2);

CHECK_TRUE(test_exp == test_exp_equal);
CHECK_FALSE(test_exp != test_exp_equal);

CHECK_FALSE(test_exp == test_exp_unequal);
CHECK_TRUE(test_exp != test_exp_unequal);

CHECK_TRUE(test_exp == test_val_equal);
CHECK_FALSE(test_exp != test_val_equal);

CHECK_FALSE(test_exp == test_val_unequal);
CHECK_TRUE(test_exp != test_val_unequal);

CHECK_FALSE(test_exp == test_unexp);
CHECK_TRUE(test_exp != test_unexp);

CHECK_TRUE(test_unexp == test_unexp_equal);
CHECK_FALSE(test_unexp != test_unexp_equal);

CHECK_FALSE(test_unexp == test_unexp_unequal);
CHECK_TRUE(test_unexp != test_unexp_unequal);
}


//*************************************************************************
TEST(test_unexpected_equal_operator)
{
etl::unexpected<int> test_unexp = etl::unexpected<int>(1);
etl::unexpected<int> test_unexp_equal = etl::unexpected<int>(1);
etl::unexpected<int> test_unexp_unequal = etl::unexpected<int>(2);

CHECK_TRUE(test_unexp == test_unexp_equal);
CHECK_FALSE(test_unexp != test_unexp_equal);

CHECK_FALSE(test_unexp == test_unexp_unequal);
CHECK_TRUE(test_unexp != test_unexp_unequal);
}
};
}
Loading