comparison libstdc++-v3/testsuite/23_containers/deque/operators/cmp_c++20.cc @ 19:2b5abeee2509 default tip

update gcc11
author anatofuz
date Mon, 25 May 2020 07:50:57 +0900
parents
children
comparison
equal deleted inserted replaced
18:1830386684a0 19:2b5abeee2509
1 // Copyright (C) 2020 Free Software Foundation, Inc.
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" }
19 // { dg-do run { target c++2a } }
20
21 #include <deque>
22 #include <testsuite_hooks.h>
23
24 void
25 test01()
26 {
27 std::deque<int> c1{ 1, 2, 3 }, c2{ 1, 2, 3, 4 }, c3{ 1, 2, 4 };
28 VERIFY( c1 == c1 );
29 VERIFY( std::is_eq(c1 <=> c1) );
30 VERIFY( c1 < c2 );
31 VERIFY( std::is_lt(c1 <=> c2) );
32 VERIFY( c1 < c3 );
33 VERIFY( std::is_lt(c1 <=> c3) );
34 VERIFY( c2 < c3 );
35 VERIFY( std::is_lt(c2 <=> c3) );
36
37 static_assert( std::totally_ordered<std::deque<int>> );
38
39 static_assert( std::three_way_comparable<std::deque<int>,
40 std::strong_ordering> );
41 static_assert( ! std::three_way_comparable<std::deque<float>,
42 std::strong_ordering> );
43 static_assert( ! std::three_way_comparable<std::deque<float>,
44 std::weak_ordering> );
45 static_assert( std::three_way_comparable<std::deque<float>,
46 std::partial_ordering> );
47
48 struct E
49 {
50 bool operator==(E) { return true; }
51 };
52 static_assert( ! std::totally_ordered<std::deque<E>> );
53 static_assert( ! std::three_way_comparable<E> );
54 static_assert( ! std::three_way_comparable<std::deque<E>> );
55 }
56
57 void
58 test02()
59 {
60 struct W
61 {
62 int value = 0;
63
64 bool operator==(W rhs) const noexcept
65 { return (value | 1) == (rhs.value | 1); }
66
67 std::weak_ordering
68 operator<=>(W rhs) const noexcept
69 { return (value | 1) <=> (rhs.value | 1); }
70 };
71
72 static_assert( std::totally_ordered<std::deque<W>> );
73
74 std::deque<W> c1{ {1}, {2}, {3} }, c2{ {0}, {3}, {3} };
75 static_assert( std::same_as<decltype(c1 <=> c1), std::weak_ordering> );
76 VERIFY( c1 == c2 );
77 VERIFY( std::is_eq(c1 <=> c2) );
78 }
79
80 void
81 test03()
82 {
83 struct P
84 {
85 int value = 0;
86
87 bool operator==(P rhs) const noexcept
88 {
89 if (value < 0 || rhs.value < 0)
90 return false;
91 return value == rhs.value;
92 }
93
94 std::partial_ordering
95 operator<=>(P rhs) const noexcept
96 {
97 if (value < 0 || rhs.value < 0)
98 return std::partial_ordering::unordered;
99 return value <=> rhs.value;
100 }
101 };
102
103 static_assert( std::totally_ordered<std::deque<P>> );
104
105 std::deque<P> c{ {1}, {2}, {-3} };
106 static_assert( std::three_way_comparable<P> );
107 static_assert( std::same_as<decltype(c <=> c), std::partial_ordering> );
108 VERIFY( (c <=> c) == std::partial_ordering::unordered );
109 }
110
111 void
112 test04()
113 {
114 struct L
115 {
116 int value = 0;
117
118 bool operator<(L rhs) const noexcept { return value < rhs.value; }
119 };
120
121 static_assert( std::totally_ordered<std::deque<L>> );
122
123 std::deque<L> c{ {1}, {2}, {3} }, d{ {1}, {2}, {3}, {4} };
124 static_assert( std::same_as<decltype(c <=> c), std::weak_ordering> );
125 VERIFY( std::is_lt(c <=> d) );
126 }
127
128 void
129 test05()
130 {
131 // deque iterators are random access, so should support <=>
132
133 std::deque<int> c{ 1, 2, 3 };
134 VERIFY( c.begin() == c.cbegin() );
135 VERIFY( std::is_eq(c.begin() <=> c.cbegin()) );
136
137 VERIFY( c.begin() < c.end() );
138 VERIFY( std::is_lt(c.begin() <=> c.end()) );
139
140 VERIFY( c.begin() < c.cend() );
141 VERIFY( std::is_lt(c.begin() <=> c.cend()) );
142
143 VERIFY( c.crbegin() == c.rbegin() );
144 VERIFY( std::is_eq(c.crbegin() <=> c.rbegin()) );
145
146 VERIFY( c.rend() > c.rbegin() );
147 VERIFY( std::is_gt(c.rend() <=> c.rbegin()) );
148
149 static_assert( std::same_as<decltype(c.begin() <=> c.begin()),
150 std::strong_ordering> );
151 }
152
153 int
154 main()
155 {
156 test01();
157 test02();
158 test03();
159 test04();
160 test05();
161 }