]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/20_util/function_objects/comparisons_pointer.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 20_util / function_objects / comparisons_pointer.cc
1 // Copyright (C) 2018-2024 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-do run }
19
20 #include <functional>
21 #include <sstream>
22 #include <testsuite_hooks.h>
23
24 int b[8];
25 int a[8];
26
27 void
28 test01()
29 {
30 int* p = a + 8;
31 std::greater<int*> gt;
32
33 std::stringstream ss;
34 ss << gt(p, b) << ' ' << gt(b, p) << ' ' << (!gt(p, b) && !gt(b, p));
35 int sum = 0, n = 0;
36 while (ss >> n)
37 sum += n;
38 VERIFY( sum == 1 );
39
40 #if __cplusplus >= 201402L
41 static_assert( gt(a+1, a), "constexpr greater<int*>" );
42 static_assert( !gt(a, a+1), "constexpr greater<int*>" );
43
44 ss.str("");
45 ss.clear();
46 sum = 0;
47 int* p2 = a + 8;
48 std::greater<> gt2;
49 ss << gt2(p2, b) << ' ' << gt2(b, p2) << ' ' << (!gt2(p2, b) && !gt2(b, p2));
50 while (ss >> n)
51 sum += n;
52 VERIFY( sum == 1 );
53
54 static_assert( gt2(a+1, a), "constexpr greater<>" );
55 static_assert( !gt2(a, a+1), "constexpr greater<>" );
56 #endif
57 }
58
59 void
60 test02()
61 {
62 int* p = a + 8;
63 std::less<int*> lt;
64
65 std::stringstream ss;
66 ss << lt(p, b) << ' ' << lt(b, p) << ' ' << (!lt(p, b) && !lt(b, p));
67 int sum = 0, n = 0;
68 while (ss >> n)
69 sum += n;
70 VERIFY( sum == 1 );
71
72 #if __cplusplus >= 201402L
73 static_assert( lt(a, a+1), "constexpr less<int*>" );
74 static_assert( !lt(a+1, a), "constexpr less<int*>" );
75
76 ss.str("");
77 ss.clear();
78 sum = 0;
79 int* p2 = a + 8;
80 std::less<> lt2;
81 ss << lt2(p2, b) << ' ' << lt2(b, p2) << ' ' << (!lt2(p2, b) && !lt2(b, p2));
82 while (ss >> n)
83 sum += n;
84 VERIFY( sum == 1 );
85
86 static_assert( lt2(a, a+1), "constexpr less<>" );
87 static_assert( !lt2(a+1, a), "constexpr less<>" );
88 #endif
89 }
90
91 void
92 test03()
93 {
94 int* p = a + 8;
95 std::greater_equal<int*> ge;
96
97 std::stringstream ss;
98 ss << !ge(p, b) << ' ' << !ge(b, p) << ' ' << (ge(p, b) && ge(b, p));
99 int sum = 0, n = 0;
100 while (ss >> n)
101 sum += n;
102 VERIFY( sum == 1 );
103
104 #if __cplusplus >= 201402L
105 static_assert( !ge(a, a+1), "constexpr greater_equal<int*>" );
106 static_assert( ge(a, a), "constexpr greater_equal<int*>" );
107 static_assert( ge(a+1, a), "constexpr greater_equal<int*>" );
108
109 ss.str("");
110 ss.clear();
111 sum = 0;
112 int* p2 = a + 8;
113 std::greater_equal<> ge2;
114 ss << !ge2(p2, b) << ' ' << !ge2(b, p2) << ' ' << (ge2(p2, b) && ge2(b, p2));
115 while (ss >> n)
116 sum += n;
117 VERIFY( sum == 1 );
118
119 static_assert( !ge2(a, a+1), "constexpr greater_equal<>" );
120 static_assert( ge2(a, a), "constexpr greater_equal<>" );
121 static_assert( ge2(a+1, a), "constexpr greater_equal<>" );
122 #endif
123 }
124
125 void
126 test04()
127 {
128 int* p = a + 8;
129 std::less_equal<int*> le;
130
131 std::stringstream ss;
132 ss << !le(p, b) << ' ' << !le(b, p) << ' ' << (le(p, b) && le(b, p));
133 int sum = 0, n = 0;
134 while (ss >> n)
135 sum += n;
136 VERIFY( sum == 1 );
137
138 #if __cplusplus >= 201402L
139 static_assert( !le(a+1, a), "constexpr less_equal<int*>" );
140 static_assert( le(a, a), "constexpr less_equal<int*>" );
141 static_assert( le(a, a+1), "constexpr less_equal<int*>" );
142
143 ss.str("");
144 ss.clear();
145 sum = 0;
146 int* p2 = a + 8;
147 std::less_equal<> le2;
148 ss << !le2(p2, b) << ' ' << !le2(b, p2) << ' ' << (le2(p2, b) && le2(b, p2));
149 while (ss >> n)
150 sum += n;
151 VERIFY( sum == 1 );
152
153 static_assert( !le2(a+1, a), "constexpr less_equal<>" );
154 static_assert( le2(a, a), "constexpr less_equal<>" );
155 static_assert( le2(a, a+1), "constexpr less_equal<>" );
156 #endif
157 }
158
159 struct X {
160 operator const X*() const { return this; }
161 };
162
163 X x;
164 X y[4];
165
166 void
167 test05()
168 {
169 std::less<const X*> lt;
170 X* p = y + 4;
171 std::stringstream ss;
172 ss << lt(x, p) << ' ' << lt(p, x) << ' ' << (!lt(p, x) && !lt(x, p));
173 int sum = 0;
174 int n = 0;
175 while (ss >> n)
176 sum += n;
177 VERIFY( sum == 1 );
178
179 #if __cplusplus >= 201402L
180 static_assert( lt(y, y+1), "constexpr less<const X*>" );
181 static_assert( !lt(y+1, y), "constexpr less<const X*>" );
182
183 ss.str("");
184 ss.clear();
185 sum = 0;
186 X* p2 = y + 4;
187 std::less<> lt2;
188 ss << lt2(x, p2) << ' ' << lt2(p2, x) << ' ' << (!lt2(x, p2) && !lt2(p2, x));
189 while (ss >> n)
190 sum += n;
191 VERIFY( sum == 1 );
192
193 static_assert( lt2(y, y+1), "constexpr less<>" );
194 static_assert( !lt2(y+1, y), "constexpr less<>" );
195 #endif
196 }
197
198 struct Overloaded {
199 bool operator>(int) { return true; }
200 bool operator<(int) { return false; }
201 bool operator>=(int) { return true; }
202 bool operator<=(int) { return false; }
203 };
204 bool operator>(Overloaded, Overloaded) { return false; }
205 bool operator<(Overloaded, Overloaded) { return false; }
206 bool operator>=(Overloaded, Overloaded) { return true; }
207 bool operator<=(Overloaded, Overloaded) { return true; }
208
209 void
210 test06()
211 {
212 #if __cplusplus >= 201402L
213 std::greater<void> gt;
214 std::less<void> lt;
215 std::greater_equal<void> ge;
216 std::less_equal<void> le;
217
218 Overloaded o;
219 VERIFY( !gt(o, o) );
220 VERIFY( !lt(o, o) );
221 VERIFY( ge(o, o) );
222 VERIFY( le(o, o) );
223
224 VERIFY( gt(o, 1) );
225 VERIFY( !lt(o, 1) );
226 VERIFY( ge(o, 1) );
227 VERIFY( !le(o, 1) );
228 #endif
229 }
230
231 int
232 main()
233 {
234 test01();
235 test02();
236 test03();
237 test04();
238 test05();
239 test06();
240 }