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 // type_traits
10
11 // alignment_of
12
13 #include <type_traits>
14 #include <cstdint>
15
16 #include "test_macros.h"
17
18 template <class T, unsigned A>
test_alignment_of()19 void test_alignment_of()
20 {
21 const unsigned AlignofResult = TEST_ALIGNOF(T);
22 static_assert( AlignofResult == A, "Golden value does not match result of alignof keyword");
23 static_assert( std::alignment_of<T>::value == AlignofResult, "");
24 static_assert( std::alignment_of<T>::value == A, "");
25 static_assert( std::alignment_of<const T>::value == A, "");
26 static_assert( std::alignment_of<volatile T>::value == A, "");
27 static_assert( std::alignment_of<const volatile T>::value == A, "");
28 #if TEST_STD_VER > 14
29 static_assert( std::alignment_of_v<T> == A, "");
30 static_assert( std::alignment_of_v<const T> == A, "");
31 static_assert( std::alignment_of_v<volatile T> == A, "");
32 static_assert( std::alignment_of_v<const volatile T> == A, "");
33 #endif
34 }
35
36 class Class
37 {
38 public:
39 ~Class();
40 };
41
main(int,char **)42 int main(int, char**)
43 {
44 test_alignment_of<int&, 4>();
45 test_alignment_of<Class, 1>();
46 test_alignment_of<int*, sizeof(intptr_t)>();
47 test_alignment_of<const int*, sizeof(intptr_t)>();
48 test_alignment_of<char[3], 1>();
49 test_alignment_of<int, 4>();
50 // The test case below is a hack. It's hard to detect what golden value
51 // we should expect. In most cases it should be 8. But in i386 builds
52 // with Clang >= 8 or GCC >= 8 the value is '4'.
53 test_alignment_of<double, TEST_ALIGNOF(double)>();
54 #if (defined(__ppc__) && !defined(__ppc64__))
55 test_alignment_of<bool, 4>(); // 32-bit PPC has four byte bool
56 #else
57 test_alignment_of<bool, 1>();
58 #endif
59 test_alignment_of<unsigned, 4>();
60
61 return 0;
62 }
63