summaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite/24_iterators/container_access.cc
blob: 965f458b6fa6d13b2ddf2c11c22246069474bf6d (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
// { dg-do run { target c++17 } }
// { dg-options "-std=gnu++17" }

// Copyright (C) 2015-2018 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/>.

// 24.8, container access [iterator.container]

#include <iterator>
#include <vector>
#include <testsuite_hooks.h>

void
test01()
{
  int i[42];
  VERIFY(std::data(i) == i);
  VERIFY(std::size(i) == 42);
  VERIFY(!std::empty(i));
}

void
test02()
{
  static constexpr int i[42]{};
  constexpr auto d = std::data(i);
  static_assert(d == i);
  constexpr auto s = std::size(i);
  static_assert(s == 42);
  constexpr auto e = std::empty(i);
  static_assert(!e);
}

void
test03()
{
  std::initializer_list<int> il{1,2,3};
  VERIFY(std::data(il) == il.begin());
  VERIFY(std::size(il) == 3);
  VERIFY(!std::empty(il));
  std::initializer_list<int> il2{};
  VERIFY(std::size(il2) == 0);
  VERIFY(std::empty(il2));
  static constexpr std::initializer_list<int> il3{1,2,3};
  constexpr auto d = std::data(il3);
  static_assert(d == il3.begin());
  constexpr auto s = std::size(il3);
  static_assert(s == 3);
  constexpr auto e = std::empty(il3);
  static_assert(!e);
}

void
test04()
{
  std::vector<int> v{1,2,3};
  VERIFY(std::data(v) == v.data());
  VERIFY(std::size(v) == v.size());
  VERIFY(!std::empty(v));
  std::vector<int> v2{};
  VERIFY(std::size(v2) == v2.size());
  VERIFY(std::empty(v2));
}

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