]>
Commit | Line | Data |
---|---|---|
7adcbafe | 1 | // Copyright (C) 2020-2022 Free Software Foundation, Inc. |
bd2420f8 JW |
2 | // |
3 | // This file is part of the GNU ISO C++ Library. This library is free | |
4 | // software; you can redistribute it and/or modify it under the | |
5 | // terms of the GNU General Public License as published by the | |
6 | // Free Software Foundation; either version 3, or (at your option) | |
7 | // any later version. | |
8 | ||
9 | // This library is distributed in the hope that it will be useful, | |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | // GNU General Public License for more details. | |
13 | ||
14 | // You should have received a copy of the GNU General Public License along | |
15 | // with this library; see the file COPYING3. If not see | |
16 | // <http://www.gnu.org/licenses/>. | |
17 | ||
18 | // { dg-options "-std=gnu++2a" } | |
1ae8edf5 | 19 | // { dg-do compile { target c++2a } } |
a04b73e1 | 20 | // { dg-xfail-if "not supported" { debug_mode } } |
bd2420f8 JW |
21 | |
22 | #include <vector> | |
23 | #include <testsuite_hooks.h> | |
24 | ||
1ae8edf5 | 25 | constexpr bool |
bd2420f8 JW |
26 | test01() |
27 | { | |
28 | std::vector<int> c1{ 1, 2, 3 }, c2{ 1, 2, 3, 4 }, c3{ 1, 2, 4 }; | |
29 | VERIFY( c1 == c1 ); | |
30 | VERIFY( std::is_eq(c1 <=> c1) ); | |
31 | VERIFY( c1 < c2 ); | |
32 | VERIFY( std::is_lt(c1 <=> c2) ); | |
33 | VERIFY( c1 < c3 ); | |
34 | VERIFY( std::is_lt(c1 <=> c3) ); | |
35 | VERIFY( c2 < c3 ); | |
36 | VERIFY( std::is_lt(c2 <=> c3) ); | |
37 | ||
38 | static_assert( std::totally_ordered<std::vector<int>> ); | |
39 | ||
40 | static_assert( std::three_way_comparable<std::vector<int>, | |
41 | std::strong_ordering> ); | |
42 | static_assert( ! std::three_way_comparable<std::vector<float>, | |
43 | std::strong_ordering> ); | |
44 | static_assert( ! std::three_way_comparable<std::vector<float>, | |
45 | std::weak_ordering> ); | |
46 | static_assert( std::three_way_comparable<std::vector<float>, | |
47 | std::partial_ordering> ); | |
48 | ||
49 | struct E | |
50 | { | |
1ae8edf5 | 51 | constexpr bool operator==(E) const { return true; } |
bd2420f8 JW |
52 | }; |
53 | static_assert( ! std::totally_ordered<std::vector<E>> ); | |
54 | static_assert( ! std::three_way_comparable<E> ); | |
55 | static_assert( ! std::three_way_comparable<std::vector<E>> ); | |
1ae8edf5 JW |
56 | |
57 | return true; | |
bd2420f8 JW |
58 | } |
59 | ||
1ae8edf5 | 60 | constexpr bool |
bd2420f8 JW |
61 | test02() |
62 | { | |
63 | struct W | |
64 | { | |
65 | int value = 0; | |
66 | ||
1ae8edf5 | 67 | constexpr bool operator==(W rhs) const noexcept |
bd2420f8 JW |
68 | { return (value | 1) == (rhs.value | 1); } |
69 | ||
1ae8edf5 | 70 | constexpr std::weak_ordering |
bd2420f8 JW |
71 | operator<=>(W rhs) const noexcept |
72 | { return (value | 1) <=> (rhs.value | 1); } | |
73 | }; | |
74 | ||
75 | static_assert( std::totally_ordered<std::vector<W>> ); | |
76 | ||
77 | std::vector<W> c1{ {1}, {2}, {3} }, c2{ {0}, {3}, {3} }; | |
78 | static_assert( std::same_as<decltype(c1 <=> c1), std::weak_ordering> ); | |
79 | VERIFY( c1 == c2 ); | |
80 | VERIFY( std::is_eq(c1 <=> c2) ); | |
1ae8edf5 JW |
81 | |
82 | return true; | |
bd2420f8 JW |
83 | } |
84 | ||
1ae8edf5 | 85 | constexpr bool |
bd2420f8 JW |
86 | test03() |
87 | { | |
88 | struct P | |
89 | { | |
90 | int value = 0; | |
91 | ||
1ae8edf5 | 92 | constexpr bool operator==(P rhs) const noexcept |
bd2420f8 JW |
93 | { |
94 | if (value < 0 || rhs.value < 0) | |
95 | return false; | |
96 | return value == rhs.value; | |
97 | } | |
98 | ||
1ae8edf5 | 99 | constexpr std::partial_ordering |
bd2420f8 JW |
100 | operator<=>(P rhs) const noexcept |
101 | { | |
102 | if (value < 0 || rhs.value < 0) | |
103 | return std::partial_ordering::unordered; | |
104 | return value <=> rhs.value; | |
105 | } | |
106 | }; | |
107 | ||
108 | static_assert( std::totally_ordered<std::vector<P>> ); | |
109 | ||
110 | std::vector<P> c{ {1}, {2}, {-3} }; | |
111 | static_assert( std::three_way_comparable<P> ); | |
112 | static_assert( std::same_as<decltype(c <=> c), std::partial_ordering> ); | |
113 | VERIFY( (c <=> c) == std::partial_ordering::unordered ); | |
1ae8edf5 JW |
114 | |
115 | return true; | |
bd2420f8 JW |
116 | } |
117 | ||
1ae8edf5 | 118 | constexpr bool |
bd2420f8 JW |
119 | test04() |
120 | { | |
121 | struct L | |
122 | { | |
123 | int value = 0; | |
124 | ||
1ae8edf5 | 125 | constexpr bool operator<(L rhs) const noexcept { return value < rhs.value; } |
bd2420f8 JW |
126 | }; |
127 | ||
128 | static_assert( std::totally_ordered<std::vector<L>> ); | |
129 | ||
130 | std::vector<L> c{ {1}, {2}, {3} }, d{ {1}, {2}, {3}, {4} }; | |
131 | static_assert( std::same_as<decltype(c <=> c), std::weak_ordering> ); | |
132 | VERIFY( std::is_lt(c <=> d) ); | |
1ae8edf5 JW |
133 | |
134 | return true; | |
bd2420f8 JW |
135 | } |
136 | ||
1ae8edf5 | 137 | constexpr bool |
bd2420f8 JW |
138 | test05() |
139 | { | |
140 | // vector iterators are random access, so should support <=> | |
141 | ||
142 | std::vector<int> c{ 1, 2, 3 }; | |
143 | VERIFY( c.begin() == c.cbegin() ); | |
144 | VERIFY( std::is_eq(c.begin() <=> c.cbegin()) ); | |
145 | ||
146 | VERIFY( c.begin() < c.end() ); | |
147 | VERIFY( std::is_lt(c.begin() <=> c.end()) ); | |
148 | ||
149 | VERIFY( c.begin() < c.cend() ); | |
150 | VERIFY( std::is_lt(c.begin() <=> c.cend()) ); | |
151 | ||
152 | VERIFY( c.crbegin() == c.rbegin() ); | |
153 | VERIFY( std::is_eq(c.crbegin() <=> c.rbegin()) ); | |
154 | ||
155 | VERIFY( c.rend() > c.rbegin() ); | |
156 | VERIFY( std::is_gt(c.rend() <=> c.rbegin()) ); | |
157 | ||
158 | static_assert( std::same_as<decltype(c.begin() <=> c.begin()), | |
159 | std::strong_ordering> ); | |
bd2420f8 | 160 | |
1ae8edf5 | 161 | return true; |
bd2420f8 | 162 | } |
1ae8edf5 JW |
163 | |
164 | static_assert( test01() ); | |
165 | static_assert( test02() ); | |
166 | static_assert( test03() ); | |
167 | static_assert( test04() ); | |
168 | static_assert( test05() ); |