xref: /llvm-project/libcxx/test/std/containers/sequences/array/array.data/data_const.pass.cpp (revision 9fb3669429a8bc59622c26ab6f5cf6926ee97e7d)
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 // const T* data() const;
12 
13 #include <array>
14 #include <cassert>
15 #include <cstddef>       // for std::max_align_t
16 
17 #include "test_macros.h"
18 
19 struct NoDefault {
20     TEST_CONSTEXPR NoDefault(int) { }
21 };
22 
23 #if TEST_STD_VER < 11
24 struct natural_alignment {
25     long t1;
26     long long t2;
27     double t3;
28     long double t4;
29 };
30 #endif
31 
32 TEST_CONSTEXPR_CXX17 bool tests()
33 {
34     {
35         typedef double T;
36         typedef std::array<T, 3> C;
37         const C c = {1, 2, 3.5};
38         const T* p = c.data();
39         assert(p[0] == 1);
40         assert(p[1] == 2);
41         assert(p[2] == 3.5);
42     }
43     {
44         typedef double T;
45         typedef std::array<T, 0> C;
46         const C c = {};
47         const T* p = c.data();
48         (void)p;
49     }
50     {
51         typedef NoDefault T;
52         typedef std::array<T, 0> C;
53         const C c = {};
54         const T* p = c.data();
55         (void)p;
56     }
57     {
58         std::array<int, 5> const c = {0, 1, 2, 3, 4};
59         assert(c.data() == &c[0]);
60         assert(*c.data() == c[0]);
61     }
62 
63     return true;
64 }
65 
66 int main(int, char**)
67 {
68     tests();
69 #if TEST_STD_VER >= 17
70     static_assert(tests(), "");
71 #endif
72 
73     // Test the alignment of data()
74     {
75 #if TEST_STD_VER < 11
76         typedef natural_alignment T;
77 #else
78         typedef std::max_align_t T;
79 #endif
80         typedef std::array<T, 0> C;
81         const C c = {};
82         const T* p = c.data();
83         std::uintptr_t pint = reinterpret_cast<std::uintptr_t>(p);
84         assert(pint % TEST_ALIGNOF(T) == 0);
85     }
86 
87     return 0;
88 }
89