Skip to content

Commit

Permalink
Fix issue #923 equality operator for class expected (#926)
Browse files Browse the repository at this point in the history
* Add equality operators for class expected

* Add missing unequal operator

---------

Co-authored-by: Oliver Marx <[email protected]>
  • Loading branch information
Chiraffollo and Oliver Marx authored Jul 18, 2024
1 parent 744d54c commit 5e351cf
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 3 deletions.
71 changes: 68 additions & 3 deletions include/etl/expected.h
Original file line number Diff line number Diff line change
Expand Up @@ -940,11 +940,76 @@ 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<TError2>& rhs)
{
return lhs.error() == rhs.error();
}

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)
{
return !(lhs == rhs);
}

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

template <typename TValue, typename TError, typename TError2>
ETL_CONSTEXPR14
bool operator !=(const etl::expected<TValue, TError>& lhs, const etl::unexpected<TError2>& rhs)
{
return !(lhs == 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 == rhs);
}

//*******************************************
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);
}
};
}

0 comments on commit 5e351cf

Please sign in to comment.