]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/std/ranges/access/cdata.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / std / ranges / access / cdata.cc
CommitLineData
7adcbafe 1// Copyright (C) 2019-2022 Free Software Foundation, Inc.
6d0dff49
JW
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
328b52d6 21#include <ranges>
6d0dff49 22#include <testsuite_hooks.h>
b9e35ee6
JW
23#include <testsuite_iterators.h>
24
25static_assert(__gnu_test::is_customization_point_object(std::ranges::cdata));
6d0dff49 26
ee9548b3
JW
27template<typename T>
28 concept has_cdata
29 = requires (T&& t) { std::ranges::cdata(std::forward<T>(t)); };
30
6d0dff49
JW
31void
32test01()
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 };
ee9548b3
JW
41 static_assert( has_cdata<R&> );
42 static_assert( has_cdata<const R&> );
6d0dff49
JW
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)) );
ee9548b3
JW
49
50 // not lvalues and not borrowed ranges
51 static_assert( !has_cdata<R> );
52 static_assert( !has_cdata<const R> );
6d0dff49
JW
53}
54
55void
56test02()
57{
58 int a[] = { 0, 1 };
59 VERIFY( std::ranges::cdata(a) == a + 0 );
ee9548b3
JW
60
61 static_assert( has_cdata<int(&)[2]> );
62 static_assert( !has_cdata<int(&&)[2]> );
6d0dff49
JW
63}
64
ee9548b3 65struct R3
6d0dff49 66{
ee9548b3
JW
67 static inline int i = 0;
68 static inline long l = 0;
6d0dff49 69
ee9548b3
JW
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
6d0dff49
JW
74};
75
ee9548b3 76template<> constexpr bool std::ranges::enable_borrowed_range<R3> = true;
b5b2e387 77
6d0dff49
JW
78void
79test03()
80{
ee9548b3
JW
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;
b5b2e387 88 VERIFY( std::ranges::cdata(r) == std::ranges::data(c) );
ee9548b3 89 VERIFY( std::ranges::cdata(std::move(r)) == std::ranges::data(c) );
b5b2e387 90 VERIFY( std::ranges::cdata(std::move(c)) == std::ranges::begin(c) );
6d0dff49
JW
91}
92
93int
94main()
95{
96 test01();
97 test02();
98 test03();
99}