1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14, c++17
13 // class strong_ordering
17 #include <type_traits>
20 #include "test_macros.h"
22 const volatile void* volatile sink
;
24 void test_static_members() {
25 DoNotOptimize(&std::strong_ordering::less
);
26 DoNotOptimize(&std::strong_ordering::equal
);
27 DoNotOptimize(&std::strong_ordering::equivalent
);
28 DoNotOptimize(&std::strong_ordering::greater
);
31 void test_signatures() {
32 auto& Eq
= std::strong_ordering::equivalent
;
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::strong_ordering
);
49 ASSERT_SAME_TYPE(decltype(0 <=> Eq
), std::strong_ordering
);
52 constexpr void test_equality() {
53 auto& StrongEq
= std::strong_ordering::equal
;
54 auto& PartialEq
= std::partial_ordering::equivalent
;
55 assert(StrongEq
== PartialEq
);
57 auto& WeakEq
= std::weak_ordering::equivalent
;
58 assert(StrongEq
== WeakEq
);
61 constexpr bool test_conversion() {
62 static_assert(std::is_convertible
<const std::strong_ordering
&,
63 std::partial_ordering
>::value
, "");
65 auto V
= std::strong_ordering::equivalent
;
66 std::partial_ordering WV
= V
;
70 auto V
= std::strong_ordering::less
;
71 std::partial_ordering WV
= V
;
75 auto V
= std::strong_ordering::greater
;
76 std::partial_ordering WV
= V
;
80 static_assert(std::is_convertible
<const std::strong_ordering
&,
81 std::weak_ordering
>::value
, "");
83 auto V
= std::strong_ordering::equivalent
;
84 std::weak_ordering WV
= V
;
88 auto V
= std::strong_ordering::less
;
89 std::weak_ordering WV
= V
;
93 auto V
= std::strong_ordering::greater
;
94 std::weak_ordering WV
= V
;
100 constexpr bool test_constexpr() {
101 auto& Eq
= std::strong_ordering::equal
;
102 auto& Equiv
= std::strong_ordering::equivalent
;
103 auto& Less
= std::strong_ordering::less
;
104 auto& Greater
= std::strong_ordering::greater
;
106 std::strong_ordering Value
;
112 {Eq
, true, false, false, false},
113 {Equiv
, true, false, false, false},
114 {Less
, false, true, true, false},
115 {Greater
, false, true, false, true},
117 for (auto TC
: TestCases
) {
119 assert((V
== 0) == TC
.ExpectEq
);
120 assert((0 == V
) == TC
.ExpectEq
);
121 assert((V
!= 0) == TC
.ExpectNeq
);
122 assert((0 != V
) == TC
.ExpectNeq
);
124 assert((V
< 0) == TC
.ExpectLess
);
125 assert((V
> 0) == TC
.ExpectGreater
);
126 assert((V
<= 0) == (TC
.ExpectLess
|| TC
.ExpectEq
));
127 assert((V
>= 0) == (TC
.ExpectGreater
|| TC
.ExpectEq
));
129 assert((0 < V
) == TC
.ExpectGreater
);
130 assert((0 > V
) == TC
.ExpectLess
);
131 assert((0 <= V
) == (TC
.ExpectGreater
|| TC
.ExpectEq
));
132 assert((0 >= V
) == (TC
.ExpectLess
|| TC
.ExpectEq
));
135 std::strong_ordering res
= (Eq
<=> 0);
146 std::strong_ordering Value
;
148 } SpaceshipTestCases
[] = {
149 {std::strong_ordering::equivalent
, ER_Equiv
},
150 {std::strong_ordering::less
, ER_Less
},
151 {std::strong_ordering::greater
, ER_Greater
},
153 for (auto TC
: SpaceshipTestCases
)
155 std::strong_ordering Res
= (TC
.Value
<=> 0);
170 static_assert(std::strong_ordering::less
== std::strong_ordering::less
);
171 static_assert(std::strong_ordering::less
!= std::strong_ordering::equal
);
172 static_assert(std::strong_ordering::less
!= std::strong_ordering::greater
);
174 static_assert(std::strong_ordering::equal
!= std::strong_ordering::less
);
175 static_assert(std::strong_ordering::equal
== std::strong_ordering::equal
);
176 static_assert(std::strong_ordering::equal
!= std::strong_ordering::greater
);
178 static_assert(std::strong_ordering::greater
!= std::strong_ordering::less
);
179 static_assert(std::strong_ordering::greater
!= std::strong_ordering::equal
);
180 static_assert(std::strong_ordering::greater
==
181 std::strong_ordering::greater
);
189 int main(int, char**) {
190 test_static_members();
193 static_assert(test_conversion(), "conversion test failed");
194 static_assert(test_constexpr(), "constexpr test failed");