From 494b34fe40a2775294855d1a5674abe0db005ed4 Mon Sep 17 00:00:00 2001 From: Oliver Marx Date: Wed, 17 Jul 2024 12:44:45 +0200 Subject: [PATCH] Add equality operators for class expected --- include/etl/expected.h | 43 +++++++++++++++++++++++++++++++++--- test/test_expected.cpp | 49 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 3 deletions(-) diff --git a/include/etl/expected.h b/include/etl/expected.h index 0c3dd983e..b967cc5a1 100644 --- a/include/etl/expected.h +++ b/include/etl/expected.h @@ -940,11 +940,48 @@ 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) +bool operator ==(const etl::unexpected& lhs, const etl::unexpected& rhs) { - return lhs.error_value == rhs.error_value; + return lhs.error() == rhs.error(); } //******************************************* 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); + } }; }