size_and_alignment.pass.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is dual licensed under the MIT and the University of Illinois Open
  6. // Source Licenses. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. // <array>
  10. // template <class T, size_t N >
  11. // struct array
  12. // Test the size and alignment matches that of an array of a given type.
  13. #include <array>
  14. #include <iterator>
  15. #include <type_traits>
  16. #include <cstddef>
  17. #include "test_macros.h"
  18. template <class T, size_t Size>
  19. struct MyArray {
  20. T elems[Size];
  21. };
  22. template <class T, size_t Size>
  23. void test() {
  24. typedef T CArrayT[Size == 0 ? 1 : Size];
  25. typedef std::array<T, Size> ArrayT;
  26. typedef MyArray<T, Size == 0 ? 1 : Size> MyArrayT;
  27. static_assert(sizeof(ArrayT) == sizeof(CArrayT), "");
  28. static_assert(sizeof(ArrayT) == sizeof(MyArrayT), "");
  29. static_assert(TEST_ALIGNOF(ArrayT) == TEST_ALIGNOF(MyArrayT), "");
  30. #if defined(_LIBCPP_VERSION)
  31. ArrayT a;
  32. ((void)a);
  33. static_assert(sizeof(ArrayT) == sizeof(a.__elems_), "");
  34. static_assert(TEST_ALIGNOF(ArrayT) == __alignof__(a.__elems_), "");
  35. #endif
  36. }
  37. template <class T>
  38. void test_type() {
  39. test<T, 1>();
  40. test<T, 42>();
  41. test<T, 0>();
  42. }
  43. struct TEST_ALIGNAS(TEST_ALIGNOF(std::max_align_t) * 2) TestType1 {
  44. };
  45. struct TEST_ALIGNAS(TEST_ALIGNOF(std::max_align_t) * 2) TestType2 {
  46. char data[1000];
  47. };
  48. int main() {
  49. test_type<char>();
  50. test_type<int>();
  51. test_type<double>();
  52. test_type<long double>();
  53. test_type<std::max_align_t>();
  54. test_type<TestType1>();
  55. test_type<TestType2>();
  56. }