1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
11 // template <class T, size_t N>
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.
17 // Ignore error about requesting a large alignment not being ABI compatible with older AIX systems.
19 # pragma clang diagnostic ignored "-Waix-compat"
24 #include <type_traits>
25 #include <__type_traits/datasizeof.h>
27 #include "test_macros.h"
29 template <class T
, std::size_t Size
>
37 using Array
= std::array
<T
, 0>;
38 LIBCPP_STATIC_ASSERT(sizeof(Array
) == sizeof(T
), "");
39 LIBCPP_STATIC_ASSERT(TEST_ALIGNOF(Array
) == TEST_ALIGNOF(T
), "");
40 LIBCPP_STATIC_ASSERT(sizeof(Array
) == sizeof(T
[1]), "");
41 LIBCPP_STATIC_ASSERT(sizeof(Array
) == sizeof(MyArray
<T
, 1>), "");
42 LIBCPP_STATIC_ASSERT(TEST_ALIGNOF(Array
) == TEST_ALIGNOF(MyArray
<T
, 1>), "");
43 static_assert(!std::is_empty
<Array
>::value
, "");
45 // Make sure empty arrays don't have padding bytes
46 LIBCPP_STATIC_ASSERT(std::__libcpp_datasizeof
<Array
>::value
== sizeof(Array
), "");
50 using Array
= std::array
<T
, 1>;
51 static_assert(sizeof(Array
) == sizeof(T
), "");
52 static_assert(TEST_ALIGNOF(Array
) == TEST_ALIGNOF(T
), "");
53 static_assert(sizeof(Array
) == sizeof(T
[1]), "");
54 static_assert(sizeof(Array
) == sizeof(MyArray
<T
, 1>), "");
55 static_assert(TEST_ALIGNOF(Array
) == TEST_ALIGNOF(MyArray
<T
, 1>), "");
56 static_assert(!std::is_empty
<Array
>::value
, "");
60 using Array
= std::array
<T
, 2>;
61 static_assert(sizeof(Array
) == sizeof(T
) * 2, "");
62 static_assert(TEST_ALIGNOF(Array
) == TEST_ALIGNOF(T
), "");
63 static_assert(sizeof(Array
) == sizeof(T
[2]), "");
64 static_assert(sizeof(Array
) == sizeof(MyArray
<T
, 2>), "");
65 static_assert(TEST_ALIGNOF(Array
) == TEST_ALIGNOF(MyArray
<T
, 2>), "");
66 static_assert(!std::is_empty
<Array
>::value
, "");
70 using Array
= std::array
<T
, 3>;
71 static_assert(sizeof(Array
) == sizeof(T
) * 3, "");
72 static_assert(TEST_ALIGNOF(Array
) == TEST_ALIGNOF(T
), "");
73 static_assert(sizeof(Array
) == sizeof(T
[3]), "");
74 static_assert(sizeof(Array
) == sizeof(MyArray
<T
, 3>), "");
75 static_assert(TEST_ALIGNOF(Array
) == TEST_ALIGNOF(MyArray
<T
, 3>), "");
76 static_assert(!std::is_empty
<Array
>::value
, "");
80 using Array
= std::array
<T
, 444>;
81 static_assert(sizeof(Array
) == sizeof(T
) * 444, "");
82 static_assert(TEST_ALIGNOF(Array
) == TEST_ALIGNOF(T
), "");
83 static_assert(sizeof(Array
) == sizeof(T
[444]), "");
84 static_assert(sizeof(Array
) == sizeof(MyArray
<T
, 444>), "");
85 static_assert(TEST_ALIGNOF(Array
) == TEST_ALIGNOF(MyArray
<T
, 444>), "");
86 static_assert(!std::is_empty
<Array
>::value
, "");
101 #if TEST_STD_VER >= 11
102 struct alignas(TEST_ALIGNOF(std::max_align_t
) * 2) Overaligned1
{};
104 struct alignas(TEST_ALIGNOF(std::max_align_t
) * 2) Overaligned2
{
108 struct alignas(TEST_ALIGNOF(std::max_align_t
)) Overaligned3
{
112 struct alignas(8) Overaligned4
{
116 struct alignas(8) Overaligned5
{};
124 test_type
<long long>();
127 test_type
<long double>();
128 test_type
<char[1]>();
129 test_type
<char[2]>();
130 test_type
<char[3]>();
132 test_type
<Aggregate
>();
133 test_type
<WithPadding
>();
135 #if TEST_STD_VER >= 11
136 test_type
<std::max_align_t
>();
137 test_type
<Overaligned1
>();
138 test_type
<Overaligned2
>();
139 test_type
<Overaligned3
>();
140 test_type
<Overaligned4
>();
141 test_type
<Overaligned5
>();