Debian Codegraph @

libstdc++-v3/testsuite/std/ranges/access/ssize.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 run { target c++20 } }
19
20 #include <ranges>
21 #include <testsuite_hooks.h>
22 #include <testsuite_iterators.h>
23
24 static_assert(__gnu_test::is_customization_point_object(std::ranges::ssize));
25
26 using std::ptrdiff_t;
27
28 void
29 test01()
30 {
31 constexpr int a[10] = { };
32 static_assert( std::same_as<decltype(std::ranges::ssize(a)), ptrdiff_t> );
33 static_assert( std::ranges::ssize(a) == 10 );
34 static_assert( noexcept(std::ranges::ssize(a)) );
35
36 int a2[2];
37 static_assert( std::same_as<decltype(std::ranges::ssize(a2)), ptrdiff_t> );
38 VERIFY( std::ranges::ssize(a2) == 2);
39 static_assert( noexcept(std::ranges::ssize(a2)) );
40 }
41
42 void
43 test02()
44 {
45 int a[3] = { };
46 __gnu_test::test_sized_range<int, __gnu_test::input_iterator_wrapper> ri(a);
47 VERIFY( std::ranges::ssize(ri) == 3 );
48 static_assert( noexcept(std::ranges::ssize(ri)) );
49 }
50
51 void
52 test04()
53 {
54 int a[] = { 0, 1 };
55 __gnu_test::test_range<int, __gnu_test::random_access_iterator_wrapper> r(a);
56 VERIFY( std::ranges::ssize(r) == std::ranges::end(r) - std::ranges::begin(r) );
57 }
58
59 struct R5
60 {
61 int size() const noexcept { return 0; }
62 R5* begin() { return this; }
63 R5* end() { return this + 1; }
64 };
65
66 template<>
67 constexpr bool std::ranges::disable_sized_range<R5> = true;
68
69 void
70 test05()
71 {
72 R5 r;
73 VERIFY( std::ranges::ssize(r) == 1 );
74 }
75
76 void
77 test06()
78 {
79 auto i = std::views::iota(1ull, 5u);
80 auto s = std::ranges::size(i);
81 auto ss = std::ranges::ssize(i);
82 // std::ranges::range_difference_t<decltype(i)> is larger than long long,
83 // but LWG 3403 says ranges::ssize(i) returns the signed version of the
84 // type that ranges::size(i) returns, not the range's difference_type.
85 static_assert( std::same_as<decltype(ss), std::make_signed_t<decltype(s)>> );
86 VERIFY( s == 4 );
87 }
88
89 void
90 test07()
91 {
92 #ifdef __SIZEOF_INT128__
93 struct R
94 {
95 unsigned __int128 size() const { return 4; }
96 };
97 R r;
98 static_assert( std::same_as<decltype(std::ranges::ssize(r)), __int128> );
99 VERIFY( std::ranges::ssize(r) == 4 );
100 #endif
101 }
102
103 int
104 main()
105 {
106 test01();
107 test02();
108 test04();
109 test05();
110 test06();
111 test07();
112 }