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