]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/std/ranges/access/cdata.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / std / ranges / access / cdata.cc
1 // Copyright (C) 2019-2023 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-options "-std=gnu++2a" }
19 // { dg-do run { target c++2a } }
20
21 #include <ranges>
22 #include <testsuite_hooks.h>
23 #include <testsuite_iterators.h>
24
25 static_assert(__gnu_test::is_customization_point_object(std::ranges::cdata));
26
27 template<typename T>
28 concept has_cdata
29 = requires (T&& t) { std::ranges::cdata(std::forward<T>(t)); };
30
31 void
32 test01()
33 {
34 struct R
35 {
36 int i = 0;
37 int j = 0;
38 int* data() { return &j; }
39 const R* data() const noexcept { return nullptr; }
40 };
41 static_assert( has_cdata<R&> );
42 static_assert( has_cdata<const R&> );
43 R r;
44 const R& c = r;
45 VERIFY( std::ranges::cdata(r) == (R*)nullptr );
46 static_assert( noexcept(std::ranges::cdata(r)) );
47 VERIFY( std::ranges::cdata(c) == (R*)nullptr );
48 static_assert( noexcept(std::ranges::cdata(c)) );
49
50 // not lvalues and not borrowed ranges
51 static_assert( !has_cdata<R> );
52 static_assert( !has_cdata<const R> );
53 }
54
55 void
56 test02()
57 {
58 int a[] = { 0, 1 };
59 VERIFY( std::ranges::cdata(a) == a + 0 );
60
61 static_assert( has_cdata<int(&)[2]> );
62 static_assert( !has_cdata<int(&&)[2]> );
63 }
64
65 struct R3
66 {
67 static inline int i = 0;
68 static inline long l = 0;
69
70 int* data() &; // this function is not defined
71 friend long* begin(R3&& r); // not defined
72 friend const long* begin(const R3& r) { return &r.l; }
73 friend const short* begin(const R3&&); // not defined
74 };
75
76 template<> constexpr bool std::ranges::enable_borrowed_range<R3> = true;
77
78 void
79 test03()
80 {
81 static_assert( has_cdata<R3&> );
82 static_assert( has_cdata<R3> ); // borrowed range
83 static_assert( has_cdata<const R3&> );
84 static_assert( has_cdata<const R3> ); // borrowed range
85
86 R3 r;
87 const R3& c = r;
88 VERIFY( std::ranges::cdata(r) == std::ranges::data(c) );
89 VERIFY( std::ranges::cdata(std::move(r)) == std::ranges::data(c) );
90 VERIFY( std::ranges::cdata(std::move(c)) == std::ranges::begin(c) );
91 }
92
93 int
94 main()
95 {
96 test01();
97 test02();
98 test03();
99 }