1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9// UNSUPPORTED: c++03, c++11, c++14, c++17
10
11// <compare>
12
13// class partial_ordering
14
15
16#include <compare>
17#include <type_traits>
18#include <cassert>
19
20#include "test_macros.h"
21
22const volatile void* volatile sink;
23
24void test_static_members() {
25 DoNotOptimize(&std::partial_ordering::less);
26 DoNotOptimize(&std::partial_ordering::equivalent);
27 DoNotOptimize(&std::partial_ordering::greater);
28 DoNotOptimize(&std::partial_ordering::unordered);
29}
30
31void test_signatures() {
32 auto& Eq = std::partial_ordering::equivalent;
33
34 ASSERT_NOEXCEPT(Eq == 0);
35 ASSERT_NOEXCEPT(0 == Eq);
36 ASSERT_NOEXCEPT(Eq != 0);
37 ASSERT_NOEXCEPT(0 != Eq);
38 ASSERT_NOEXCEPT(0 < Eq);
39 ASSERT_NOEXCEPT(Eq < 0);
40 ASSERT_NOEXCEPT(0 <= Eq);
41 ASSERT_NOEXCEPT(Eq <= 0);
42 ASSERT_NOEXCEPT(0 > Eq);
43 ASSERT_NOEXCEPT(Eq > 0);
44 ASSERT_NOEXCEPT(0 >= Eq);
45 ASSERT_NOEXCEPT(Eq >= 0);
46 ASSERT_NOEXCEPT(0 <=> Eq);
47 ASSERT_NOEXCEPT(Eq <=> 0);
48 ASSERT_SAME_TYPE(decltype(Eq <=> 0), std::partial_ordering);
49 ASSERT_SAME_TYPE(decltype(0 <=> Eq), std::partial_ordering);
50}
51
52constexpr void test_equality() {
53 auto& PartialEq = std::partial_ordering::equivalent;
54 auto& WeakEq = std::weak_ordering::equivalent;
55 assert(PartialEq == WeakEq);
56
57 auto& StrongEq = std::strong_ordering::equal;
58 assert(PartialEq == StrongEq);
59}
60
61constexpr bool test_constexpr() {
62 auto& Eq = std::partial_ordering::equivalent;
63 auto& Less = std::partial_ordering::less;
64 auto& Greater = std::partial_ordering::greater;
65 auto& Unord = std::partial_ordering::unordered;
66 struct {
67 std::partial_ordering Value;
68 bool ExpectEq;
69 bool ExpectNeq;
70 bool ExpectLess;
71 bool ExpectGreater;
72 } TestCases[] = {
73 {Eq, true, false, false, false},
74 {Less, false, true, true, false},
75 {Greater, false, true, false, true},
76 {Unord, false, true, false, false}
77 };
78 for (auto TC : TestCases) {
79 auto V = TC.Value;
80 assert((V == 0) == TC.ExpectEq);
81 assert((0 == V) == TC.ExpectEq);
82 assert((V != 0) == TC.ExpectNeq);
83 assert((0 != V) == TC.ExpectNeq);
84
85 assert((V < 0) == TC.ExpectLess);
86 assert((V > 0) == TC.ExpectGreater);
87 assert((V <= 0) == (TC.ExpectLess || TC.ExpectEq));
88 assert((V >= 0) == (TC.ExpectGreater || TC.ExpectEq));
89
90 assert((0 < V) == TC.ExpectGreater);
91 assert((0 > V) == TC.ExpectLess);
92 assert((0 <= V) == (TC.ExpectGreater || TC.ExpectEq));
93 assert((0 >= V) == (TC.ExpectLess || TC.ExpectEq));
94 }
95 {
96 std::partial_ordering res = (Eq <=> 0);
97 ((void)res);
98 res = (0 <=> Eq);
99 ((void)res);
100 }
101 enum ExpectRes {
102 ER_Greater,
103 ER_Less,
104 ER_Equiv,
105 ER_Unord
106 };
107 struct {
108 std::partial_ordering Value;
109 ExpectRes Expect;
110 } SpaceshipTestCases[] = {
111 {std::partial_ordering::equivalent, ER_Equiv},
112 {std::partial_ordering::less, ER_Less},
113 {std::partial_ordering::greater, ER_Greater},
114 {std::partial_ordering::unordered, ER_Unord}
115 };
116 for (auto TC : SpaceshipTestCases)
117 {
118 std::partial_ordering Res = (TC.Value <=> 0);
119 switch (TC.Expect) {
120 case ER_Equiv:
121 assert(Res == 0);
122 assert(0 == Res);
123 break;
124 case ER_Less:
125 assert(Res < 0);
126 break;
127 case ER_Greater:
128 assert(Res > 0);
129 break;
130 case ER_Unord:
131 assert(Res != 0);
132 assert(0 != Res);
133 assert((Res < 0) == false);
134 assert((Res > 0) == false);
135 assert((Res == 0) == false);
136 break;
137 }
138 }
139 {
140 static_assert(std::partial_ordering::less == std::partial_ordering::less);
141 static_assert(std::partial_ordering::less !=
142 std::partial_ordering::equivalent);
143 static_assert(std::partial_ordering::less !=
144 std::partial_ordering::greater);
145 static_assert(std::partial_ordering::less !=
146 std::partial_ordering::unordered);
147
148 static_assert(std::partial_ordering::equivalent !=
149 std::partial_ordering::less);
150 static_assert(std::partial_ordering::equivalent ==
151 std::partial_ordering::equivalent);
152 static_assert(std::partial_ordering::equivalent !=
153 std::partial_ordering::greater);
154 static_assert(std::partial_ordering::equivalent !=
155 std::partial_ordering::unordered);
156
157 static_assert(std::partial_ordering::greater !=
158 std::partial_ordering::less);
159 static_assert(std::partial_ordering::greater !=
160 std::partial_ordering::equivalent);
161 static_assert(std::partial_ordering::greater ==
162 std::partial_ordering::greater);
163 static_assert(std::partial_ordering::greater !=
164 std::partial_ordering::unordered);
165
166 static_assert(std::partial_ordering::unordered !=
167 std::partial_ordering::less);
168 static_assert(std::partial_ordering::unordered !=
169 std::partial_ordering::equivalent);
170 static_assert(std::partial_ordering::unordered !=
171 std::partial_ordering::greater);
172 static_assert(std::partial_ordering::unordered ==
173 std::partial_ordering::unordered);
174 }
175
176 test_equality();
177
178 return true;
179}
180
181int main(int, char**) {
182 test_static_members();
183 test_signatures();
184 test_equality();
185 static_assert(test_constexpr(), "constexpr test failed");
186
187 return 0;
188}
189

source code of libcxx/test/std/language.support/cmp/cmp.partialord/partialord.pass.cpp