1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9// <array>
10
11// T *data();
12
13#include <array>
14#include <cassert>
15#include <cstddef> // for std::max_align_t
16#include <cstdint>
17
18#include "test_macros.h"
19
20struct NoDefault {
21 TEST_CONSTEXPR NoDefault(int) {}
22};
23
24#if TEST_STD_VER < 11
25struct natural_alignment {
26 long t1;
27 long long t2;
28 double t3;
29 long double t4;
30};
31#endif
32
33TEST_CONSTEXPR_CXX17 bool tests() {
34 {
35 typedef double T;
36 typedef std::array<T, 3> C;
37 C c = {1, 2, 3.5};
38 ASSERT_NOEXCEPT(c.data());
39 T* p = c.data();
40 assert(p[0] == 1);
41 assert(p[1] == 2);
42 assert(p[2] == 3.5);
43 }
44 {
45 typedef double T;
46 typedef std::array<T, 0> C;
47 C c = {};
48 ASSERT_NOEXCEPT(c.data());
49 T* p = c.data();
50 (void)p;
51 }
52 {
53 typedef double T;
54 typedef std::array<const T, 0> C;
55 C c = {._M_elems: {}};
56 ASSERT_NOEXCEPT(c.data());
57 const T* p = c.data();
58 (void)p;
59 static_assert((std::is_same<decltype(c.data()), const T*>::value), "");
60 }
61 {
62 typedef NoDefault T;
63 typedef std::array<T, 0> C;
64 C c = {};
65 ASSERT_NOEXCEPT(c.data());
66 T* p = c.data();
67 (void)p;
68 }
69 {
70 std::array<int, 5> c = {0, 1, 2, 3, 4};
71 assert(c.data() == &c[0]);
72 assert(*c.data() == c[0]);
73 }
74
75 return true;
76}
77
78int main(int, char**) {
79 tests();
80#if TEST_STD_VER >= 17
81 static_assert(tests(), "");
82#endif
83
84 // Test the alignment of data()
85 {
86#if TEST_STD_VER < 11
87 typedef natural_alignment T;
88#else
89 typedef std::max_align_t T;
90#endif
91 typedef std::array<T, 0> C;
92 const C c = {};
93 const T* p = c.data();
94 std::uintptr_t pint = reinterpret_cast<std::uintptr_t>(p);
95 assert(pint % TEST_ALIGNOF(T) == 0);
96 }
97 return 0;
98}
99

source code of libcxx/test/std/containers/sequences/array/array.data/data.pass.cpp