]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/std/ranges/iota/max_size_type.cc
1 // Copyright (C) 2020-2022 Free Software Foundation, Inc.
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)
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.
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/>.
18 // { dg-options "-std=gnu++2a" }
19 // { dg-do run { target c++2a } }
20 // { dg-timeout-factor 4 }
24 #include <testsuite_hooks.h>
26 using max_size_t
= std::ranges::__detail::__max_size_type
;
27 using max_diff_t
= std::ranges::__detail::__max_diff_type
;
28 using rep_t
= max_size_t::__rep
;
30 static_assert(sizeof(max_size_t
) == sizeof(max_diff_t
));
32 static_assert(std::regular
<max_size_t
>);
33 static_assert(std::totally_ordered
<max_size_t
>);
35 static_assert(std::regular
<max_diff_t
>);
36 static_assert(std::totally_ordered
<max_diff_t
>);
38 // We can't use numeric_limits<rep_t>::max() here because __int128 is an
39 // integral type only in GNU mode.
40 constexpr max_size_t mu
= max_size_t(~rep_t(0));
41 constexpr max_size_t ou
= 1;
42 constexpr max_diff_t ns
= -1;
47 static_assert(max_size_t(7) % 3 == 1);
48 static_assert(max_size_t(7) % 4 == 3);
50 static_assert(-max_diff_t(1) == max_diff_t(-1));
51 static_assert(max_diff_t(3) % 2 == 1);
52 static_assert(max_diff_t(-3) / 2 == -1);
53 static_assert(max_diff_t(-3) % 2 == -1);
54 static_assert(max_diff_t(3) % -2 == 1);
55 static_assert(max_diff_t(-3) << 1 == -6);
56 static_assert(max_diff_t(-3) >> 1 == -2);
57 static_assert(max_diff_t(3) >> 1 == 1);
58 static_assert(max_diff_t(3) >> 2 == 0);
60 static_assert(max_diff_t(-5) / 3 == -1);
61 static_assert(max_diff_t(5) / -3 == -1);
62 static_assert(max_diff_t(-5) / -3 == 1);
63 static_assert(max_diff_t(5) / 3 == 1);
65 static_assert(max_diff_t(-6) / 3 == -2);
66 static_assert(max_diff_t(6) / -3 == -2);
67 static_assert(max_diff_t(-6) / -3 == 2);
68 static_assert(max_diff_t(6) / 3 == 2);
70 static_assert(~max_size_t(-3) == 2);
71 static_assert(~max_diff_t(-3) == 2);
73 static_assert(max_diff_t(1) < max_diff_t(3));
74 static_assert(max_diff_t(-1) < max_diff_t(3));
75 static_assert(max_diff_t(1) > max_diff_t(-3));
76 static_assert(max_diff_t(-1) > max_diff_t(-3));
78 static_assert(max_diff_t(mu
)/-1 == -max_diff_t(mu
));
79 static_assert(-max_diff_t(mu
)/1 == -max_diff_t(mu
));
80 static_assert(max_diff_t(mu
)>>1 == max_diff_t(mu
)/2);
81 static_assert(-max_diff_t(mu
+1) == max_diff_t(mu
+1));
82 static_assert(-(mu
+1) == mu
+1);
83 static_assert((mu
+1)<<1 == 0);
84 static_assert(max_diff_t(mu
+1)<<1 == 0);
85 static_assert(max_diff_t(mu
+1)>>1 < 0);
87 static_assert(int(max_diff_t(mu
+1)) == 0);
88 static_assert(rep_t(max_diff_t(mu
+1)) == 0);
89 static_assert(int(max_diff_t(mu
)) == -1);
90 static_assert(rep_t(max_diff_t(mu
)) == rep_t(-1));
92 static_assert(2*mu
+1 > 2*mu
);
93 static_assert(~(2*mu
+1) == 0);
94 static_assert(mu
/mu
== 1);
95 static_assert(2*mu
> mu
);
96 static_assert(2*mu
-mu
== mu
);
97 static_assert((2*mu
)/mu
== 2);
98 static_assert((2*mu
+1)/mu
== 2);
99 static_assert((2*mu
-1)/(mu
-1) == 2);
100 static_assert((2*mu
-1)/mu
== 1);
101 static_assert((2*mu
+-1)/mu
== 1);
102 static_assert(2*mu
-1 < 2*mu
);
103 static_assert(2*mu
-1 <= 2*mu
);
104 static_assert(2*mu
+1 > 2*mu
);
105 static_assert(2*mu
+1 >= 2*mu
);
106 static_assert((2*mu
)/1 == 2*mu
);
107 static_assert(mu
/mu
-1 == 0);
108 static_assert(mu
*0 == 0);
109 static_assert((2*mu
-1)*0 == 0);
110 static_assert((2*mu
-1)>>1 == mu
-1);
111 static_assert(mu
+-1+1 == mu
);
112 static_assert(mu
+1+-1 == mu
);
114 static_assert((2*mu
)/2 == mu
);
115 static_assert((2*mu
)>>1 == mu
);
116 static_assert((mu
<<1)>>1 == mu
);
117 static_assert(1/mu
== 0);
118 static_assert(mu
/1 == mu
);
119 static_assert(((mu
+1)|mu
) == -1);
120 static_assert((mu
+1)+(mu
+1) < mu
+1);
122 static_assert(max_size_t(ns
) == -1);
123 static_assert(-max_diff_t(ou
) == -1);
124 static_assert(-max_diff_t(-ou
) == 1);
125 static_assert(max_size_t(-max_diff_t(-ou
)) == 1);
126 static_assert(ns
*ns
== max_diff_t(ou
));
127 static_assert(max_size_t(ns
)*max_size_t(ns
) == ou
);
128 static_assert(-max_diff_t(0) == max_diff_t(0));
129 static_assert(-ou
-ou
== -2*ou
);
131 static_assert(int(ns
) == -1);
132 static_assert(rep_t(ns
) == rep_t(-1));
134 static_assert(max_size_t() == 0);
135 static_assert(max_diff_t() == 0);
137 auto f
= [] (auto a
) { a
/= a
; return a
; };
138 static_assert(f(max_size_t(5)) == 1);
139 static_assert(f(max_size_t(-5)) == 1);
140 static_assert(f(max_diff_t(5)) == 1);
142 auto g
= [] (auto a
) { a
>>= a
; return a
; };
143 static_assert(g(max_size_t(5)) == 0);
144 static_assert(g(max_diff_t(5)) == 0);
146 auto h
= [] (auto a
) { a
<<= a
; return a
; };
147 static_assert(h(max_size_t(3)) == 24);
148 static_assert(h(max_diff_t(3)) == 24);
150 auto w
= [] (auto a
) {
152 VERIFY( a
++ == b
&& a
== b
+1 );
153 VERIFY( a
-- == b
+1 && a
== b
);
155 VERIFY( ++(++a
) == b
+2 );
156 VERIFY( --(--a
) == b
);
159 static_assert(w(max_size_t(10)));
160 static_assert(w(-max_diff_t(10)));
162 #if __cpp_lib_three_way_comparison
163 static_assert(max_size_t
{1} <=> max_size_t
{9} == std::strong_ordering::less
);
164 static_assert(max_size_t
{3} <=> max_size_t
{2} == std::strong_ordering::greater
);
165 static_assert(max_size_t
{5} <=> max_size_t
{5} == std::strong_ordering::equal
);
166 static_assert(~max_size_t
{1} <=> ~max_size_t
{9} == std::strong_ordering::greater
);
167 static_assert(~max_size_t
{3} <=> ~max_size_t
{2} == std::strong_ordering::less
);
168 static_assert(~max_size_t
{5} <=> ~max_size_t
{5} == std::strong_ordering::equal
);
169 static_assert(~max_size_t
{5} <=> max_size_t
{9} == std::strong_ordering::greater
);
170 static_assert(~max_size_t
{9} <=> max_size_t
{5} == std::strong_ordering::greater
);
171 static_assert(max_size_t
{5} <=> ~max_size_t
{9} == std::strong_ordering::less
);
172 static_assert(max_size_t
{9} <=> ~max_size_t
{5} == std::strong_ordering::less
);
174 static_assert(max_diff_t
{1} <=> max_diff_t
{9} == std::strong_ordering::less
);
175 static_assert(max_diff_t
{3} <=> max_diff_t
{2} == std::strong_ordering::greater
);
176 static_assert(max_diff_t
{5} <=> max_diff_t
{5} == std::strong_ordering::equal
);
177 static_assert(max_diff_t
{-1} <=> max_diff_t
{-9} == std::strong_ordering::greater
);
178 static_assert(max_diff_t
{-3} <=> max_diff_t
{-2} == std::strong_ordering::less
);
179 static_assert(max_diff_t
{-5} <=> max_diff_t
{-5} == std::strong_ordering::equal
);
180 static_assert(max_diff_t
{-5} <=> max_diff_t
{9} == std::strong_ordering::less
);
181 static_assert(max_diff_t
{-9} <=> max_diff_t
{5} == std::strong_ordering::less
);
182 static_assert(max_diff_t
{5} <=> max_diff_t
{-9} == std::strong_ordering::greater
);
183 static_assert(max_diff_t
{9} <=> max_diff_t
{-5} == std::strong_ordering::greater
);
187 template<bool signed_p
, bool shorten_p
>
191 using hw_type
= std::conditional_t
<signed_p
, signed rep_t
, rep_t
>;
192 using max_type
= std::conditional_t
<signed_p
, max_diff_t
, max_size_t
>;
193 using shorten_type
= std::conditional_t
<shorten_p
, hw_type
, max_type
>;
194 const int hw_type_bit_size
= sizeof(hw_type
) * __CHAR_BIT__
;
195 const int limit
= 1000;
196 const int log2_limit
= 10;
197 static_assert((1 << log2_limit
) >= limit
);
198 const int min
= (signed_p
? -limit
: 0);
199 const int max
= limit
;
200 for (hw_type i
= min
; i
<= max
; i
++)
203 if (signed_p
|| shorten_p
)
205 ok
&= (~i
== shorten_type(~max_type(i
)));
206 ok
&= (-i
== shorten_type(-max_type(i
)));
208 for (hw_type j
= min
; j
<= max
; j
++)
210 ok
&= (i
*j
== shorten_type(max_type(i
)*j
));
211 ok
&= (i
+j
== shorten_type(max_type(i
)+j
));
214 ok
&= (i
/j
== shorten_type(max_type(i
)/j
));
215 ok
&= (i
%j
== shorten_type(max_type(i
)%j
));
217 if (signed_p
|| shorten_p
)
218 ok
&= (i
-j
== shorten_type(max_type(i
)-j
));
219 ok
&= ((i
&j
) == shorten_type(max_type(i
)&j
));
220 ok
&= ((i
|j
) == shorten_type(max_type(i
)|j
));
221 ok
&= ((i
^j
) == shorten_type(max_type(i
)^j
));
222 if (j
>= 0 && j
< hw_type(hw_type_bit_size
)
223 && (shorten_p
|| j
< hw_type(hw_type_bit_size
) - log2_limit
))
225 ok
&= ((i
>>j
) == shorten_type(max_type(i
)>>j
));
226 ok
&= ((i
<<j
) == shorten_type(max_type(i
)<<j
));
228 ok
&= (i
>j
) == (max_type(i
) > j
);
229 ok
&= (i
<j
) == (max_type(i
) < j
);
230 ok
&= (i
>=j
) == (max_type(i
) >= j
);
231 ok
&= (i
<=j
) == (max_type(i
) <= j
);
232 ok
&= (i
==j
) == (max_type(i
) == j
);
233 ok
&= (i
!=j
) == (max_type(i
) != j
);
237 "Inconsistency found: %d %d %lld %lld\n",
238 signed_p
, shorten_p
, (long long)i
, (long long)j
) ;
245 template<bool signed_p
, bool toggle_base_p
>
249 using hw_type
= std::conditional_t
<signed_p
, signed rep_t
, rep_t
>;
250 using max_type
= std::conditional_t
<signed_p
, max_diff_t
, max_size_t
>;
251 using base_type
= std::conditional_t
<toggle_base_p
, hw_type
, max_type
>;
252 constexpr int hw_type_bit_size
= sizeof(hw_type
) * __CHAR_BIT__
;
253 constexpr int limit
= 1000;
254 constexpr int log2_limit
= 10;
255 static_assert((1 << log2_limit
) >= limit
);
256 const int min
= (signed_p
? -limit
: 0);
257 const int max
= limit
;
258 for (hw_type i
= min
; i
<= max
; i
++)
262 for (hw_type j
= min
; j
<= max
; j
++)
265 ok
&= (k
== (max_type(i
)*j
));
267 ok
&= (k
== (max_type(i
)+j
));
271 ok
&= (k
== (max_type(i
)/j
));
273 ok
&= (k
== (max_type(i
)%j
));
278 ok
&= (k
== (max_type(i
)-j
));
281 ok
&= (k
== (max_type(i
)&j
));
283 ok
&= (k
== (max_type(i
)|j
));
285 ok
&= (k
== (max_type(i
)^j
));
286 if (j
>= 0 && j
< hw_type(hw_type_bit_size
)
287 && (!toggle_base_p
|| j
< hw_type(hw_type_bit_size
) - log2_limit
))
290 ok
&= (k
== (max_type(i
)>>j
));
292 ok
&= (k
== (max_type(i
)<<j
));
297 "Inconsistency found: %d %d %lld %lld\n",
298 signed_p
, toggle_base_p
, (long long)i
, (long long)j
) ;
308 constexpr int limit
= 1000;
309 for (int i
= -limit
; i
<= limit
; i
++)
311 VERIFY( -max_size_t(-i
) == i
);
312 for (int j
= i
; j
<= limit
; j
++)
314 VERIFY( max_size_t(-i
) * max_size_t(-j
) == i
*j
);
315 VERIFY( max_size_t(-j
) * max_size_t(-i
) == j
*i
);
316 VERIFY( rep_t(((mu
+1)+i
)*((mu
+1)+j
)) == rep_t(i
*j
) );
317 VERIFY( rep_t(((mu
+1)+j
)*((mu
+1)+i
)) == rep_t(j
*i
) );
320 auto r
= (mu
+i
)-((mu
+i
)/j
)*j
;
321 VERIFY( r
>= 0 && r
< j
);
322 VERIFY( r
== (mu
+i
)%j
);
331 #if __SIZEOF_INT128__
333 x
= static_cast<__int128
>(0);
334 x
= static_cast<unsigned __int128
>(0);
337 y
= static_cast<__int128
>(0);;
338 y
= static_cast<unsigned __int128
>(0);
342 using std::numeric_limits
;
344 static_assert(numeric_limits
<max_size_t
>::is_specialized
);
345 static_assert(!numeric_limits
<max_size_t
>::is_signed
);
346 static_assert(numeric_limits
<max_size_t
>::is_integer
);
347 static_assert(numeric_limits
<max_size_t
>::is_exact
);
348 // We can't unconditionally use numeric_limits here because __int128 is an
349 // integral type only in GNU mode.
350 #if __SIZEOF_INT128__
351 static_assert(numeric_limits
<max_size_t
>::digits
== 129);
352 static_assert(numeric_limits
<max_size_t
>::digits10
== 38);
353 static_assert(numeric_limits
<max_size_t
>::max()
354 == 2*max_size_t(~rep_t(0)) + 1);
356 static_assert(numeric_limits
<max_size_t
>::digits
357 == numeric_limits
<rep_t
>::digits
+ 1);
358 static_assert(numeric_limits
<max_size_t
>::digits10
359 == numeric_limits
<rep_t
>::digits10
);
360 static_assert(numeric_limits
<max_size_t
>::max()
361 == 2*max_size_t(numeric_limits
<rep_t
>::max())+1);
363 static_assert(numeric_limits
<max_size_t
>::min() == 0);
364 static_assert(numeric_limits
<max_size_t
>::max()
366 static_assert((numeric_limits
<max_size_t
>::max()
367 >> (numeric_limits
<max_size_t
>::digits
-1)) == 1);
368 static_assert(numeric_limits
<max_size_t
>::lowest()
369 == numeric_limits
<max_size_t
>::min());
371 static_assert(numeric_limits
<max_diff_t
>::is_specialized
);
372 static_assert(numeric_limits
<max_diff_t
>::is_signed
);
373 static_assert(numeric_limits
<max_diff_t
>::is_integer
);
374 static_assert(numeric_limits
<max_diff_t
>::is_exact
);
375 static_assert(numeric_limits
<max_diff_t
>::digits
376 == numeric_limits
<max_size_t
>::digits
- 1);
377 static_assert(numeric_limits
<max_diff_t
>::digits10
378 == numeric_limits
<max_size_t
>::digits10
);
379 // We can't unconditionally use numeric_limits here because __int128 is an
380 // integral type only in GNU mode.
381 #if __SIZEOF_INT128__
382 static_assert(numeric_limits
<max_diff_t
>::min() == -max_diff_t(~rep_t(0))-1);
383 static_assert(numeric_limits
<max_diff_t
>::max() == ~rep_t(0));
385 static_assert(numeric_limits
<max_diff_t
>::min()
386 == -max_diff_t(numeric_limits
<rep_t
>::max())-1);
387 static_assert(numeric_limits
<max_diff_t
>::max()
388 == numeric_limits
<rep_t
>::max());
390 static_assert(numeric_limits
<max_diff_t
>::lowest()
391 == numeric_limits
<max_diff_t
>::min());
392 static_assert(max_diff_t(max_size_t(1)
393 << (numeric_limits
<max_size_t
>::digits
-1))
394 == numeric_limits
<max_diff_t
>::min());
401 test02
<false,false>();
402 test02
<false,true>();
403 test02
<true,false>();
406 test03
<false,false>();
407 test03
<false,true>();
408 test03
<true,false>();