Debian Codegraph @

libstdc++-v3/testsuite/std/ranges/access/data.cc

raw
1 // Copyright (C) 2019-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::data));
25
26 template<typename T>
27 concept has_data
28 = requires (T&& t) { std::ranges::data(std::forward<T>(t)); };
29
30 void
31 test01()
32 {
33 struct R
34 {
35 int i = 0;
36 int j = 0;
37 int* data() { return &j; }
38 const R* data() const noexcept { return nullptr; }
39 };
40 static_assert( has_data<R&> );
41 static_assert( has_data<const R&> );
42 R r;
43 const R& c = r;
44 VERIFY( std::ranges::data(r) == &r.j );
45 static_assert( !noexcept(std::ranges::data(r)) );
46 VERIFY( std::ranges::data(c) == (R*)nullptr );
47 static_assert( noexcept(std::ranges::data(c)) );
48
49 // not lvalues and not borrowed ranges
50 static_assert( !has_data<R> );
51 static_assert( !has_data<const R> );
52 }
53
54
55 void
56 test02()
57 {
58 int a[] = { 0, 1 };
59 VERIFY( std::ranges::data(a) == a + 0 );
60
61 __gnu_test::test_range<int, __gnu_test::contiguous_iterator_wrapper> r(a);
62 VERIFY( std::ranges::data(r) == std::to_address(std::ranges::begin(r)) );
63
64 static_assert( has_data<int(&)[2]> );
65 static_assert( has_data<decltype(r)&> );
66 static_assert( !has_data<int(&&)[2]> );
67 static_assert( !has_data<decltype(r)&&> );
68 }
69
70 struct R3
71 {
72 static inline int i;
73 static inline long l;
74
75 int* data() & { return &i; }
76 friend long* begin(const R3& r) { return &l; }
77 friend const short* begin(const R3&&); // not defined
78 };
79
80 template<> constexpr bool std::ranges::enable_borrowed_range<R3> = true;
81
82 void
83 test03()
84 {
85 static_assert( has_data<R3&> );
86 static_assert( has_data<R3> ); // borrowed range
87 static_assert( has_data<const R3&> );
88 static_assert( has_data<const R3> ); // borrowed range
89
90 R3 r;
91 const R3& c = r;
92 // PR libstdc++/100824
93 // ranges::data should treat the subexpression as an lvalue
94 VERIFY( std::ranges::data(std::move(r)) == &R3::i );
95 VERIFY( std::ranges::data(std::move(c)) == &R3::l );
96
97 // PR libstdc++/100824 comment 3
98 // Check for member data() should use decay-copy
99 struct A { int*&& data(); };
100 static_assert( has_data<A&> );
101 }
102
103 int
104 main()
105 {
106 test01();
107 test02();
108 test03();
109 }