summaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite/std/ranges/access/cdata.cc
blob: 7e3bb55a9502b5128f3dc0e634468d0322d32378 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright (C) 2019-2022 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3.  If not see
// <http://www.gnu.org/licenses/>.

// { dg-options "-std=gnu++2a" }
// { dg-do run { target c++2a } }

#include <ranges>
#include <testsuite_hooks.h>
#include <testsuite_iterators.h>

static_assert(__gnu_test::is_customization_point_object(std::ranges::cdata));

template<typename T>
  concept has_cdata
    = requires (T&& t) { std::ranges::cdata(std::forward<T>(t)); };

void
test01()
{
  struct R
  {
    int i = 0;
    int j = 0;
    int* data() { return &j; }
    const R* data() const noexcept { return nullptr; }
  };
  static_assert( has_cdata<R&> );
  static_assert( has_cdata<const R&> );
  R r;
  const R& c = r;
  VERIFY( std::ranges::cdata(r) == (R*)nullptr );
  static_assert( noexcept(std::ranges::cdata(r)) );
  VERIFY( std::ranges::cdata(c) == (R*)nullptr );
  static_assert( noexcept(std::ranges::cdata(c)) );

  // not lvalues and not borrowed ranges
  static_assert( !has_cdata<R> );
  static_assert( !has_cdata<const R> );
}

void
test02()
{
  int a[] = { 0, 1 };
  VERIFY( std::ranges::cdata(a) == a + 0 );

  static_assert( has_cdata<int(&)[2]> );
  static_assert( !has_cdata<int(&&)[2]> );
}

struct R3
{
  static inline int i = 0;
  static inline long l = 0;

  int* data() &; // this function is not defined
  friend long* begin(R3&& r); // not defined
  friend const long* begin(const R3& r) { return &r.l; }
  friend const short* begin(const R3&&); // not defined
};

template<> constexpr bool std::ranges::enable_borrowed_range<R3> = true;

void
test03()
{
  static_assert( has_cdata<R3&> );
  static_assert( has_cdata<R3> );  // borrowed range
  static_assert( has_cdata<const R3&> );
  static_assert( has_cdata<const R3> );  // borrowed range

  R3 r;
  const R3& c = r;
  VERIFY( std::ranges::cdata(r) == std::ranges::data(c) );
  VERIFY( std::ranges::cdata(std::move(r)) == std::ranges::data(c) );
  VERIFY( std::ranges::cdata(std::move(c)) == std::ranges::begin(c) );
}

int
main()
{
  test01();
  test02();
  test03();
}