]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/std/ranges/access/cdata.cc
Partial implementation of C++20 of <ranges> header
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / std / ranges / access / cdata.cc
1 // Copyright (C) 2019 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) { return &r.l; }
55 friend const long* begin(const R&& r) { return &r.l + 1; }
56 };
57
58 void
59 test03()
60 {
61 R r;
62 const R& c = r;
63 VERIFY( std::ranges::cdata(std::move(r)) == std::ranges::data(std::move(c)) );
64 VERIFY( std::ranges::cdata(std::move(c)) == std::ranges::data(std::move(c)) );
65 }
66
67 int
68 main()
69 {
70 test01();
71 test02();
72 test03();
73 }