Skip to content

Commit 863b764

Browse files
authored
Merge pull request #459 from elbeno/matcher-equivalence
2 parents 62b3708 + a6a630f commit 863b764

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

include/match/ops.hpp

+16
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
#include <match/or.hpp>
99
#include <match/simplify.hpp>
1010

11+
#include <compare>
12+
#include <utility>
13+
1114
template <match::matcher L, match::matcher R>
1215
[[nodiscard]] constexpr auto operator and(L const &lhs, R const &rhs)
1316
-> decltype(match::simplify(std::declval<match::and_t<L, R>>())) {
@@ -25,3 +28,16 @@ template <match::matcher M>
2528
-> decltype(match::simplify(match::negate(std::declval<M>()))) {
2629
return match::simplify(match::negate(m));
2730
}
31+
32+
template <match::matcher L, match::matcher R>
33+
[[nodiscard]] constexpr auto operator<=>(L const &lhs, R const &rhs)
34+
-> std::partial_ordering {
35+
auto const l = match::simplify(lhs);
36+
auto const r = match::simplify(rhs);
37+
auto const x = match::implies(l, r);
38+
auto const y = match::implies(r, l);
39+
if (not x and not y) {
40+
return std::partial_ordering::unordered;
41+
}
42+
return x <=> y;
43+
}

test/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ add_tests(
3838
lookup/strategies
3939
match/and
4040
match/constant
41+
match/equivalence
4142
match/implies
4243
match/not
4344
match/or

test/match/equivalence.cpp

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include "test_matcher.hpp"
2+
3+
#include <match/ops.hpp>
4+
5+
#include <catch2/catch_test_macros.hpp>
6+
7+
#include <compare>
8+
#include <type_traits>
9+
10+
TEST_CASE("less than", "[match equivalence]") {
11+
using T = test_m<0>;
12+
using U = match::and_t<T, test_m<1>>;
13+
constexpr auto result = T{} <=> U{};
14+
static_assert(result == std::partial_ordering::less);
15+
static_assert(T{} < U{});
16+
}
17+
18+
TEST_CASE("greater than", "[match equivalence]") {
19+
using T = test_m<0>;
20+
using U = match::or_t<T, test_m<1>>;
21+
constexpr auto result = T{} <=> U{};
22+
static_assert(result == std::partial_ordering::greater);
23+
static_assert(T{} > U{});
24+
}
25+
26+
TEST_CASE("equivalent", "[match equivalence]") {
27+
using T = test_m<0>;
28+
constexpr auto result = T{} <=> T{};
29+
static_assert(result == std::partial_ordering::equivalent);
30+
}
31+
32+
TEST_CASE("unorderable", "[match equivalence]") {
33+
using T = test_m<0>;
34+
using U = test_m<1>;
35+
constexpr auto result = T{} <=> U{};
36+
static_assert(result == std::partial_ordering::unordered);
37+
}

0 commit comments

Comments
 (0)