From d901f362ccba8635951dcc9257e7bb13aff03514 Mon Sep 17 00:00:00 2001 From: Sy Brand Date: Thu, 24 Nov 2022 11:50:20 +0000 Subject: [PATCH] Fix equality test for expected --- include/tl/expected.hpp | 14 ++++++++++++++ tests/relops.cpp | 13 ++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/include/tl/expected.hpp b/include/tl/expected.hpp index 31b130a..2c852e6 100644 --- a/include/tl/expected.hpp +++ b/include/tl/expected.hpp @@ -2276,6 +2276,20 @@ constexpr bool operator!=(const expected &lhs, ? true : (!lhs.has_value() ? lhs.error() != rhs.error() : *lhs != *rhs); } +template +constexpr bool operator==(const expected &lhs, + const expected &rhs) { + return (lhs.has_value() != rhs.has_value()) + ? false + : (!lhs.has_value() ? lhs.error() == rhs.error() : true); +} +template +constexpr bool operator!=(const expected &lhs, + const expected &rhs) { + return (lhs.has_value() != rhs.has_value()) + ? true + : (!lhs.has_value() ? lhs.error() == rhs.error() : false); +} template constexpr bool operator==(const expected &x, const U &v) { diff --git a/tests/relops.cpp b/tests/relops.cpp index a1f8a73..99dee5f 100644 --- a/tests/relops.cpp +++ b/tests/relops.cpp @@ -2,5 +2,16 @@ #include TEST_CASE("Relational operators", "[relops]") { - //TODO + tl::expected o1 = 42; + tl::expected o2{tl::unexpect, 0}; + const tl::expected o3 = 42; + + REQUIRE(o1 == o1); + REQUIRE(o1 != o2); + REQUIRE(o1 == o3); + REQUIRE(o3 == o3); + + tl::expected o6; + + REQUIRE(o6 == o6); }