From 5e351cfd796eefecc8801442f1f8ead9b148c612 Mon Sep 17 00:00:00 2001 From: Chiraffollo <5937599+Chiraffollo@users.noreply.github.com> Date: Thu, 18 Jul 2024 10:03:03 +0200 Subject: [PATCH] Fix issue #923 equality operator for class expected (#926) * Add equality operators for class expected * Add missing unequal operator --------- Co-authored-by: Oliver Marx --- include/etl/expected.h | 71 ++++++++++++++++++++++++++++++++++++++++-- test/test_expected.cpp | 49 +++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+), 3 deletions(-) diff --git a/include/etl/expected.h b/include/etl/expected.h index 0c3dd983e..708a8f861 100644 --- a/include/etl/expected.h +++ b/include/etl/expected.h @@ -940,11 +940,76 @@ namespace etl //******************************************* /// Equivalence operator. //******************************************* -template +template +ETL_CONSTEXPR14 +bool operator ==(const etl::expected& lhs, const etl::expected& 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 +ETL_CONSTEXPR14 +bool operator ==(const etl::expected& lhs, const TValue2& rhs) +{ + if (!lhs.has_value()) + { + return false; + } + return lhs.value() == rhs; +} + +template +ETL_CONSTEXPR14 +bool operator ==(const etl::expected& lhs, const etl::unexpected& rhs) +{ + if (lhs.has_value()) + { + return false; + } + return lhs.error() == rhs; +} + +template +ETL_CONSTEXPR14 +bool operator ==(const etl::unexpected& lhs, const etl::unexpected& rhs) +{ + return lhs.error() == rhs.error(); +} + +template +ETL_CONSTEXPR14 +bool operator !=(const etl::expected& lhs, const etl::expected& rhs) +{ + return !(lhs == rhs); +} + +template +ETL_CONSTEXPR14 +bool operator !=(const etl::expected& lhs, const TValue2& rhs) +{ + return !(lhs == rhs); +} + +template +ETL_CONSTEXPR14 +bool operator !=(const etl::expected& lhs, const etl::unexpected& rhs) +{ + return !(lhs == rhs); +} + +template ETL_CONSTEXPR14 -bool operator ==(const etl::unexpected& lhs, const etl::unexpected& rhs) +bool operator !=(const etl::unexpected& lhs, const etl::unexpected& rhs) { - return lhs.error_value == rhs.error_value; + return !(lhs == rhs); } //******************************************* diff --git a/test/test_expected.cpp b/test/test_expected.cpp index a199707a0..ce6b78482 100644 --- a/test/test_expected.cpp +++ b/test/test_expected.cpp @@ -629,5 +629,54 @@ namespace CHECK_TRUE(thrown); CHECK_TRUE(exception_what == thrown_what); } + + //************************************************************************* + TEST(test_expected_equal_operator) + { + etl::expected test_exp = 1; + etl::expected test_exp_equal = 1; + etl::expected test_exp_unequal = 2; + int test_val_equal = 1; + int test_val_unequal = 2; + etl::expected test_unexp = etl::unexpected(1); + etl::expected test_unexp_equal = etl::unexpected(1); + etl::expected test_unexp_unequal = etl::unexpected(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 test_unexp = etl::unexpected(1); + etl::unexpected test_unexp_equal = etl::unexpected(1); + etl::unexpected test_unexp_unequal = etl::unexpected(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); + } }; }