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 {
36 typedef double T;
37 typedef std::array<T, 3> C;
38 C c = {1, 2, 3.5};
39 ASSERT_NOEXCEPT(c.data());
40 T* p = c.data();
41 assert(p[0] == 1);
42 assert(p[1] == 2);
43 assert(p[2] == 3.5);
44 }
45 {
46 typedef double T;
47 typedef std::array<T, 0> C;
48 C c = {};
49 ASSERT_NOEXCEPT(c.data());
50 T* p = c.data();
51 (void)p;
52 }
53 {
54 typedef double T;
55 typedef std::array<const T, 0> C;
56 C c = {._M_elems: {}};
57 ASSERT_NOEXCEPT(c.data());
58 const T* p = c.data();
59 (void)p;
60 static_assert((std::is_same<decltype(c.data()), const T*>::value), "");
61 }
62 {
63 typedef NoDefault T;
64 typedef std::array<T, 0> C;
65 C c = {};
66 ASSERT_NOEXCEPT(c.data());
67 T* p = c.data();
68 (void)p;
69 }
70 {
71 std::array<int, 5> c = {0, 1, 2, 3, 4};
72 assert(c.data() == &c[0]);
73 assert(*c.data() == c[0]);
74 }
75
76 return true;
77}
78
79int main(int, char**)
80{
81 tests();
82#if TEST_STD_VER >= 17
83 static_assert(tests(), "");
84#endif
85
86 // Test the alignment of data()
87 {
88#if TEST_STD_VER < 11
89 typedef natural_alignment T;
90#else
91 typedef std::max_align_t T;
92#endif
93 typedef std::array<T, 0> C;
94 const C c = {};
95 const T* p = c.data();
96 std::uintptr_t pint = reinterpret_cast<std::uintptr_t>(p);
97 assert(pint % TEST_ALIGNOF(T) == 0);
98 }
99 return 0;
100}
101

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