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>
26 #ifdef _LIBCPP_VERSION
27 # include <__type_traits/datasizeof.h>
30 #include "test_macros.h"
32 template <class T
, std::size_t Size
>
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
, "");
48 // Make sure empty arrays don't have padding bytes
49 LIBCPP_STATIC_ASSERT(std::__datasizeof_v
<Array
> == sizeof(Array
), "");
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
, "");
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
, "");
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
, "");
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
, "");
104 #if TEST_STD_VER >= 11
105 struct alignas(TEST_ALIGNOF(std::max_align_t
) * 2) Overaligned1
{};
107 struct alignas(TEST_ALIGNOF(std::max_align_t
) * 2) Overaligned2
{
111 struct alignas(TEST_ALIGNOF(std::max_align_t
)) Overaligned3
{
115 struct alignas(8) Overaligned4
{
119 struct alignas(8) Overaligned5
{};
127 test_type
<long long>();
130 test_type
<long double>();
131 test_type
<char[1]>();
132 test_type
<char[2]>();
133 test_type
<char[3]>();
135 test_type
<Aggregate
>();
136 test_type
<WithPadding
>();
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
>();