Debian Codegraph @

libstdc++-v3/testsuite/std/ranges/iota/size.cc

raw
1 // Copyright (C) 2020-2025 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 compile { target c++2a } }
19 // { dg-add-options strict_std }
20
21 #include <ranges>
22 #include <limits>
23
24 template<typename T, typename U>
25 constexpr bool
26 equal(T t, U u) requires std::same_as<T, U>
27 {
28 return t == u;
29 }
30
31 template<typename W, typename S = std::make_unsigned_t<W>>
32 void
33 test_integer_iota()
34 {
35 using std::numeric_limits;
36
37 using V = std::ranges::iota_view<W, W>;
38 static_assert( std::ranges::sized_range<V> );
39
40 constexpr V zero(0, 0);
41 static_assert( equal(zero.size(), (S)0) );
42
43 constexpr V min(numeric_limits<W>::min(),
44 numeric_limits<W>::min());
45 static_assert( equal(min.size(), (S)0) );
46
47 constexpr V max(numeric_limits<W>::max(),
48 numeric_limits<W>::max());
49 static_assert( equal(max.size(), (S)0) );
50
51 constexpr V minmax(numeric_limits<W>::min(),
52 numeric_limits<W>::max());
53 if constexpr (sizeof(W) < sizeof(S))
54 {
55 using S2 = std::make_unsigned_t<W>;
56 static_assert( equal(minmax.size(), (S)numeric_limits<S2>::max()) );
57 }
58 else
59 static_assert( equal(minmax.size(), numeric_limits<S>::max()) );
60
61 constexpr V pospos(20, 22);
62 static_assert( equal(pospos.size(), (S)2) );
63
64 if constexpr (std::numeric_limits<W>::is_signed)
65 {
66 constexpr V negneg(-20, -2);
67 static_assert( equal(negneg.size(), (S)18) );
68
69 constexpr V negpos(-20, 22);
70 static_assert( equal(negpos.size(), (S)42) );
71 }
72 }
73
74 void
75 test01()
76 {
77 test_integer_iota<signed char, unsigned int>();
78 test_integer_iota<signed short, unsigned int>();
79 test_integer_iota<signed int>();
80 test_integer_iota<signed long>();
81 test_integer_iota<signed long long>();
82 test_integer_iota<unsigned char, unsigned int>();
83 test_integer_iota<unsigned short, unsigned int>();
84 test_integer_iota<unsigned int>();
85 test_integer_iota<unsigned long>();
86 test_integer_iota<unsigned long long>();
87
88 #ifdef __SIZEOF_INT128__
89 // When the target supports __int128 it can be used in iota_view
90 // even in strict mode where !integral<__int128>.
91 // Specify the size type explicitly, because make_unsigned_t<__int128>
92 // is undefined when !integral<__int128>.
93 test_integer_iota<__int128, unsigned __int128>();
94 test_integer_iota<unsigned __int128, unsigned __int128>();
95 #endif
96 }
97
98 constexpr int arr[3] = { 1, 2, 3 };
99
100 void
101 test02()
102 {
103 constexpr auto v = std::views::iota(std::begin(arr), std::end(arr));
104 static_assert( equal(v.size(), std::make_unsigned_t<std::ptrdiff_t>(3)) );
105
106 constexpr auto vv = std::views::iota(v.begin(), v.end());
107 constexpr auto vvsz = vv.size();
108 static_assert( ! std::numeric_limits<decltype(vvsz)>::is_signed );
109 static_assert( vvsz == 3 );
110 }