]> 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-2020 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
24 void
25 test01()
26 {
27 struct R
28 {
29 int i = 0;
30 int j = 0;
31 int* data() { return &j; }
32 const R* data() const noexcept { return nullptr; }
33 };
34 R r;
35 const R& c = r;
36 VERIFY( std::ranges::cdata(r) == (R*)nullptr );
37 static_assert( noexcept(std::ranges::cdata(r)) );
38 VERIFY( std::ranges::cdata(c) == (R*)nullptr );
39 static_assert( noexcept(std::ranges::cdata(c)) );
40 }
41
42 void
43 test02()
44 {
45 int a[] = { 0, 1 };
46 VERIFY( std::ranges::cdata(a) == a + 0 );
47 }
48
49 struct R
50 {
51 long l = 0;
52
53 int* data() const { return nullptr; }
54 friend long* begin(R&& r); // this function is not defined
55 friend const long* begin(const R& r) { return &r.l; }
56 friend const short* begin(const R&&); // not defined
57 };
58
59 // This is a lie, ranges::begin(R&&) returns a dangling iterator.
60 template<> constexpr bool std::ranges::enable_safe_range<R> = true;
61
62 void
63 test03()
64 {
65 R r;
66 const R& c = r;
67 VERIFY( std::ranges::cdata(r) == std::ranges::data(c) );
68 VERIFY( std::ranges::cdata(std::move(r)) == std::ranges::begin(c) );
69 VERIFY( std::ranges::cdata(std::move(c)) == std::ranges::begin(c) );
70 }
71
72 int
73 main()
74 {
75 test01();
76 test02();
77 test03();
78 }