| 1 |
// { dg-do run { target c++17 } } |
| 2 |
|
| 3 |
// Copyright (C) 2015-2025 Free Software Foundation, Inc. |
| 4 |
// |
| 5 |
// This file is part of the GNU ISO C++ Library. This library is free |
| 6 |
// software; you can redistribute it and/or modify it under the |
| 7 |
// terms of the GNU General Public License as published by the |
| 8 |
// Free Software Foundation; either version 3, or (at your option) |
| 9 |
// any later version. |
| 10 |
|
| 11 |
// This library is distributed in the hope that it will be useful, |
| 12 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
// GNU General Public License for more details. |
| 15 |
|
| 16 |
// You should have received a copy of the GNU General Public License along |
| 17 |
// with this library; see the file COPYING3. If not see |
| 18 |
// <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
// 24.8, container access [iterator.container] |
| 21 |
|
| 22 |
#include <iterator> |
| 23 |
#include <vector> |
| 24 |
#include <testsuite_hooks.h> |
| 25 |
|
| 26 |
void |
| 27 |
test01() |
| 28 |
{ |
| 29 |
int i[42]; |
| 30 |
VERIFY(std::data(i) == i); |
| 31 |
VERIFY(std::size(i) == 42); |
| 32 |
VERIFY(!std::empty(i)); |
| 33 |
} |
| 34 |
|
| 35 |
void |
| 36 |
test02() |
| 37 |
{ |
| 38 |
static constexpr int i[42]{}; |
| 39 |
constexpr auto d = std::data(i); |
| 40 |
static_assert(d == i); |
| 41 |
constexpr auto s = std::size(i); |
| 42 |
static_assert(s == 42); |
| 43 |
constexpr auto e = std::empty(i); |
| 44 |
static_assert(!e); |
| 45 |
} |
| 46 |
|
| 47 |
void |
| 48 |
test03() |
| 49 |
{ |
| 50 |
std::initializer_list<int> il{1,2,3}; |
| 51 |
VERIFY(std::data(il) == il.begin()); |
| 52 |
VERIFY(std::size(il) == 3); |
| 53 |
VERIFY(!std::empty(il)); |
| 54 |
std::initializer_list<int> il2{}; |
| 55 |
VERIFY(std::size(il2) == 0); |
| 56 |
VERIFY(std::empty(il2)); |
| 57 |
static constexpr std::initializer_list<int> il3{1,2,3}; |
| 58 |
constexpr auto d = std::data(il3); |
| 59 |
static_assert(d == il3.begin()); |
| 60 |
constexpr auto s = std::size(il3); |
| 61 |
static_assert(s == 3); |
| 62 |
constexpr auto e = std::empty(il3); |
| 63 |
static_assert(!e); |
| 64 |
} |
| 65 |
|
| 66 |
void |
| 67 |
test04() |
| 68 |
{ |
| 69 |
std::vector<int> v{1,2,3}; |
| 70 |
VERIFY(std::data(v) == v.data()); |
| 71 |
VERIFY(std::size(v) == v.size()); |
| 72 |
VERIFY(!std::empty(v)); |
| 73 |
std::vector<int> v2{}; |
| 74 |
VERIFY(std::size(v2) == v2.size()); |
| 75 |
VERIFY(std::empty(v2)); |
| 76 |
} |
| 77 |
|
| 78 |
int |
| 79 |
main() |
| 80 |
{ |
| 81 |
test01(); |
| 82 |
test02(); |
| 83 |
test03(); |
| 84 |
test04(); |
| 85 |
} |