[NFC][Py Reformat] Reformat python files in libcxx/libcxxabi
[llvm-project.git] / libcxx / test / std / language.support / cmp / cmp.partialord / partialord.pass.cpp
blob7d0ddb33298401d4f78f42ecb9c73be887d53ba2
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 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14, c++17
11 // <compare>
13 // class partial_ordering
16 #include <compare>
17 #include <type_traits>
18 #include <cassert>
20 #include "test_macros.h"
22 const volatile void* volatile sink;
24 void 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);
31 void test_signatures() {
32 auto& Eq = std::partial_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::partial_ordering);
49 ASSERT_SAME_TYPE(decltype(0 <=> Eq), std::partial_ordering);
52 constexpr void test_equality() {
53 auto& PartialEq = std::partial_ordering::equivalent;
54 auto& WeakEq = std::weak_ordering::equivalent;
55 assert(PartialEq == WeakEq);
57 auto& StrongEq = std::strong_ordering::equal;
58 assert(PartialEq == StrongEq);
61 constexpr 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}
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);
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));
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));
96 std::partial_ordering res = (Eq <=> 0);
97 ((void)res);
98 res = (0 <=> Eq);
99 ((void)res);
101 enum ExpectRes {
102 ER_Greater,
103 ER_Less,
104 ER_Equiv,
105 ER_Unord
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}
116 for (auto TC : SpaceshipTestCases)
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;
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);
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);
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);
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);
176 test_equality();
178 return true;
181 int main(int, char**) {
182 test_static_members();
183 test_signatures();
184 test_equality();
185 static_assert(test_constexpr(), "constexpr test failed");
187 return 0;