size_and_alignment.pass.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. // <array>
  9. // template <class T, size_t N >
  10. // struct array
  11. // Test the size and alignment matches that of an array of a given type.
  12. #include <array>
  13. #include <iterator>
  14. #include <type_traits>
  15. #include <cstddef>
  16. #include "test_macros.h"
  17. template <class T, size_t Size>
  18. struct MyArray {
  19. T elems[Size];
  20. };
  21. template <class T, size_t Size>
  22. void test() {
  23. typedef T CArrayT[Size == 0 ? 1 : Size];
  24. typedef std::array<T, Size> ArrayT;
  25. typedef MyArray<T, Size == 0 ? 1 : Size> MyArrayT;
  26. static_assert(sizeof(ArrayT) == sizeof(CArrayT), "");
  27. static_assert(sizeof(ArrayT) == sizeof(MyArrayT), "");
  28. static_assert(TEST_ALIGNOF(ArrayT) == TEST_ALIGNOF(MyArrayT), "");
  29. #if defined(_LIBCPP_VERSION)
  30. ArrayT a;
  31. ((void)a);
  32. static_assert(sizeof(ArrayT) == sizeof(a.__elems_), "");
  33. static_assert(TEST_ALIGNOF(ArrayT) == __alignof__(a.__elems_), "");
  34. #endif
  35. }
  36. template <class T>
  37. void test_type() {
  38. test<T, 1>();
  39. test<T, 42>();
  40. test<T, 0>();
  41. }
  42. struct TEST_ALIGNAS(TEST_ALIGNOF(std::max_align_t) * 2) TestType1 {
  43. };
  44. struct TEST_ALIGNAS(TEST_ALIGNOF(std::max_align_t) * 2) TestType2 {
  45. char data[1000];
  46. };
  47. //static_assert(sizeof(void*) == 4, "");
  48. int main(int, char**) {
  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. return 0;
  57. }