From e4130ea27cc3a13780e95bd0654675336fa564ec Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Sat, 13 Jul 2024 15:29:54 +0200 Subject: domain/participant: add three way comparison operator --- domain/include/turns/domain/participant.hpp | 6 ++++++ domain/src/participant.cpp | 10 ++++++++++ domain/tests/participant.cpp | 30 +++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/domain/include/turns/domain/participant.hpp b/domain/include/turns/domain/participant.hpp index 760ba31..f6ceff7 100644 --- a/domain/include/turns/domain/participant.hpp +++ b/domain/include/turns/domain/participant.hpp @@ -1,6 +1,8 @@ #ifndef TURNS_DOMAIN_PARTICIPANT_HPP #define TURNS_DOMAIN_PARTICIPANT_HPP +#include + #include #include #include @@ -12,6 +14,8 @@ namespace turns::domain { auto static create(Glib::ustring name, float order_value) -> Glib::RefPtr; + auto operator<=>(participant const & other) const noexcept -> std::partial_ordering; + auto name() const noexcept -> Glib::ustring const &; auto name(Glib::ustring value) -> participant &; @@ -25,6 +29,8 @@ namespace turns::domain float m_order_value; }; + auto operator<=>(Glib::RefPtr const & lhs, Glib::RefPtr const & rhs) -> std::partial_ordering; + } // namespace turns::domain #endif \ No newline at end of file diff --git a/domain/src/participant.cpp b/domain/src/participant.cpp index 8302024..d7e6b30 100644 --- a/domain/src/participant.cpp +++ b/domain/src/participant.cpp @@ -16,6 +16,11 @@ namespace turns::domain { } + auto participant::operator<=>(participant const & other) const noexcept -> std::partial_ordering + { + return order() <=> other.order(); + } + auto participant::name() const noexcept -> Glib::ustring const & { return m_name; @@ -38,4 +43,9 @@ namespace turns::domain return *this; } + auto operator<=>(Glib::RefPtr const & lhs, Glib::RefPtr const & rhs) -> std::partial_ordering + { + return *lhs <=> *rhs; + } + } // namespace turns::domain \ No newline at end of file diff --git a/domain/tests/participant.cpp b/domain/tests/participant.cpp index b7429aa..ee8af74 100644 --- a/domain/tests/participant.cpp +++ b/domain/tests/participant.cpp @@ -1,8 +1,11 @@ #include "turns/domain/participant.hpp" #include + #include +#include + namespace turns::domain::tests { @@ -38,6 +41,33 @@ namespace turns::domain::tests instance->order(8); REQUIRE(instance->order() == 8); } + + SECTION("can be compared with another participant") + { + auto equivalent_instance = participant::create("Equivalent", constructed_order); + auto lesser_instance = participant::create("Lesser", constructed_order - 1); + auto greater_instance = participant::create("Greater", constructed_order + 1); + + SECTION("yielding std::partial_ordering::equivalent for itself") + { + REQUIRE((instance <=> equivalent_instance) == std::partial_ordering::equivalent); + } + + SECTION("yielding std::partial_ordering::equivalent for an equivalent participant") + { + REQUIRE((instance <=> equivalent_instance) == std::partial_ordering::equivalent); + } + + SECTION("yielding std::partial_ordering::greater for a lesser participant") + { + REQUIRE((instance <=> lesser_instance) == std::partial_ordering::greater); + } + + SECTION("yielding std::partial_ordering::less for a greater participant") + { + REQUIRE((instance <=> greater_instance) == std::partial_ordering::less); + } + } } } // namespace turns::domain::tests \ No newline at end of file -- cgit v1.2.3