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// template <class T, size_t N>
12// struct array
13
14// Make sure std::array<T, N> has the correct object size and alignment.
15// This test is mostly meant to catch subtle ABI-breaking regressions.
16
17// Ignore error about requesting a large alignment not being ABI compatible with older AIX systems.
18#if defined(_AIX)
19# pragma clang diagnostic ignored "-Waix-compat"
20#endif
21
22#include <array>
23#include <cstddef>
24#include <type_traits>
25
26#ifdef _LIBCPP_VERSION
27# include <__type_traits/datasizeof.h>
28#endif
29
30#include "test_macros.h"
31
32template <class T, std::size_t Size>
33struct MyArray {
34 T elems[Size];
35};
36
37template <class T>
38void test_type() {
39 {
40 using Array = std::array<T, 0>;
41 LIBCPP_STATIC_ASSERT(sizeof(Array) == sizeof(T), "");
42 LIBCPP_STATIC_ASSERT(TEST_ALIGNOF(Array) == TEST_ALIGNOF(T), "");
43 LIBCPP_STATIC_ASSERT(sizeof(Array) == sizeof(T[1]), "");
44 LIBCPP_STATIC_ASSERT(sizeof(Array) == sizeof(MyArray<T, 1>), "");
45 LIBCPP_STATIC_ASSERT(TEST_ALIGNOF(Array) == TEST_ALIGNOF(MyArray<T, 1>), "");
46 static_assert(!std::is_empty<Array>::value, "");
47
48 // Make sure empty arrays don't have padding bytes
49 LIBCPP_STATIC_ASSERT(std::__libcpp_datasizeof<Array>::value == sizeof(Array), "");
50 }
51
52 {
53 using Array = std::array<T, 1>;
54 static_assert(sizeof(Array) == sizeof(T), "");
55 static_assert(TEST_ALIGNOF(Array) == TEST_ALIGNOF(T), "");
56 static_assert(sizeof(Array) == sizeof(T[1]), "");
57 static_assert(sizeof(Array) == sizeof(MyArray<T, 1>), "");
58 static_assert(TEST_ALIGNOF(Array) == TEST_ALIGNOF(MyArray<T, 1>), "");
59 static_assert(!std::is_empty<Array>::value, "");
60 }
61
62 {
63 using Array = std::array<T, 2>;
64 static_assert(sizeof(Array) == sizeof(T) * 2, "");
65 static_assert(TEST_ALIGNOF(Array) == TEST_ALIGNOF(T), "");
66 static_assert(sizeof(Array) == sizeof(T[2]), "");
67 static_assert(sizeof(Array) == sizeof(MyArray<T, 2>), "");
68 static_assert(TEST_ALIGNOF(Array) == TEST_ALIGNOF(MyArray<T, 2>), "");
69 static_assert(!std::is_empty<Array>::value, "");
70 }
71
72 {
73 using Array = std::array<T, 3>;
74 static_assert(sizeof(Array) == sizeof(T) * 3, "");
75 static_assert(TEST_ALIGNOF(Array) == TEST_ALIGNOF(T), "");
76 static_assert(sizeof(Array) == sizeof(T[3]), "");
77 static_assert(sizeof(Array) == sizeof(MyArray<T, 3>), "");
78 static_assert(TEST_ALIGNOF(Array) == TEST_ALIGNOF(MyArray<T, 3>), "");
79 static_assert(!std::is_empty<Array>::value, "");
80 }
81
82 {
83 using Array = std::array<T, 444>;
84 static_assert(sizeof(Array) == sizeof(T) * 444, "");
85 static_assert(TEST_ALIGNOF(Array) == TEST_ALIGNOF(T), "");
86 static_assert(sizeof(Array) == sizeof(T[444]), "");
87 static_assert(sizeof(Array) == sizeof(MyArray<T, 444>), "");
88 static_assert(TEST_ALIGNOF(Array) == TEST_ALIGNOF(MyArray<T, 444>), "");
89 static_assert(!std::is_empty<Array>::value, "");
90 }
91}
92
93struct Empty {};
94
95struct Aggregate {
96 int i;
97};
98
99struct WithPadding {
100 long double ld;
101 char c;
102};
103
104#if TEST_STD_VER >= 11
105struct alignas(TEST_ALIGNOF(std::max_align_t) * 2) Overaligned1 {};
106
107struct alignas(TEST_ALIGNOF(std::max_align_t) * 2) Overaligned2 {
108 char data[1000];
109};
110
111struct alignas(TEST_ALIGNOF(std::max_align_t)) Overaligned3 {
112 char data[1000];
113};
114
115struct alignas(8) Overaligned4 {
116 char c;
117};
118
119struct alignas(8) Overaligned5 {};
120#endif
121
122void test() {
123 test_type<char>();
124 test_type<short>();
125 test_type<int>();
126 test_type<long>();
127 test_type<long long>();
128 test_type<float>();
129 test_type<double>();
130 test_type<long double>();
131 test_type<char[1]>();
132 test_type<char[2]>();
133 test_type<char[3]>();
134 test_type<Empty>();
135 test_type<Aggregate>();
136 test_type<WithPadding>();
137
138#if TEST_STD_VER >= 11
139 test_type<std::max_align_t>();
140 test_type<Overaligned1>();
141 test_type<Overaligned2>();
142 test_type<Overaligned3>();
143 test_type<Overaligned4>();
144 test_type<Overaligned5>();
145#endif
146}
147

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