aligned_storage_a2_test.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #ifdef _MSC_VER
  2. #pragma pack(2)
  3. #endif
  4. // (C) Copyright John Maddock 2000.
  5. // Use, modification and distribution are subject to the
  6. // Boost Software License, Version 1.0. (See accompanying file
  7. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  8. #ifdef TEST_STD
  9. # include <type_traits>
  10. # include <boost/type_traits/type_with_alignment.hpp> // max_align and long_long_type
  11. #else
  12. # include <boost/type_traits/alignment_of.hpp>
  13. # include <boost/type_traits/aligned_storage.hpp>
  14. # include <boost/type_traits/is_pod.hpp>
  15. #endif
  16. #include "test.hpp"
  17. #include "check_integral_constant.hpp"
  18. template <class T>
  19. union must_be_pod
  20. {
  21. int i;
  22. T t;
  23. };
  24. template <class T>
  25. inline void no_unused_warning(const volatile T&)
  26. {
  27. }
  28. template <class T>
  29. void do_check(const T&)
  30. {
  31. typedef typename tt::aligned_storage<T::value,T::value>::type t1;
  32. #if defined(BOOST_GCC) && (BOOST_GCC < 40800)
  33. // In order to test this with warnings-as-errors with GCC, we need
  34. // a slightly different initializer here to suppress spurious
  35. // GCC warnings (ie to work around a GCC buglet).
  36. t1 as1 = {{{ 0, }}};
  37. #else
  38. t1 as1 = { 0, };
  39. #endif
  40. must_be_pod<t1> pod1;
  41. no_unused_warning(as1);
  42. no_unused_warning(pod1);
  43. BOOST_TEST_MESSAGE(typeid(t1).name());
  44. BOOST_CHECK(::tt::alignment_of<t1>::value == T::value);
  45. BOOST_CHECK(sizeof(t1) == T::value);
  46. #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
  47. BOOST_CHECK(::tt::is_pod<t1>::value == true);
  48. #endif
  49. typedef typename tt::aligned_storage<T::value*2,T::value>::type t2;
  50. #if defined(BOOST_GCC) && (BOOST_GCC < 40800)
  51. t2 as2 = {{{ 0, }}};
  52. #else
  53. t2 as2 = { 0, };
  54. #endif
  55. must_be_pod<t2> pod2;
  56. no_unused_warning(as2);
  57. no_unused_warning(pod2);
  58. BOOST_TEST_MESSAGE(typeid(t2).name());
  59. BOOST_CHECK(::tt::alignment_of<t2>::value == T::value);
  60. BOOST_CHECK(sizeof(t2) == T::value*2);
  61. #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
  62. BOOST_CHECK(::tt::is_pod<t2>::value == true);
  63. #endif
  64. #ifndef TEST_STD
  65. // Non-Tr1 behaviour:
  66. typedef typename tt::aligned_storage<T::value,(std::size_t)(-1L)>::type t3;
  67. #if defined(BOOST_GCC) && (BOOST_GCC < 40800)
  68. t3 as3 = {{{ 0, }}};
  69. #else
  70. t3 as3 = { 0, };
  71. #endif
  72. must_be_pod<t3> pod3;
  73. no_unused_warning(as3);
  74. no_unused_warning(pod3);
  75. BOOST_TEST_MESSAGE(typeid(t3).name());
  76. BOOST_CHECK(::tt::alignment_of<t3>::value == ::tt::alignment_of< ::boost::detail::max_align>::value);
  77. BOOST_CHECK((sizeof(t3) % T::value) == 0);
  78. #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
  79. BOOST_CHECK(::tt::is_pod<t3>::value == true);
  80. #endif
  81. #endif
  82. }
  83. TT_TEST_BEGIN(type_with_alignment)
  84. do_check(tt::integral_constant<std::size_t,::tt::alignment_of<char>::value>());
  85. do_check(tt::integral_constant<std::size_t,::tt::alignment_of<short>::value>());
  86. do_check(tt::integral_constant<std::size_t,::tt::alignment_of<int>::value>());
  87. do_check(tt::integral_constant<std::size_t,::tt::alignment_of<long>::value>());
  88. do_check(tt::integral_constant<std::size_t,::tt::alignment_of<float>::value>());
  89. do_check(tt::integral_constant<std::size_t,::tt::alignment_of<double>::value>());
  90. do_check(tt::integral_constant<std::size_t,::tt::alignment_of<long double>::value>());
  91. #ifdef BOOST_HAS_LONG_LONG
  92. do_check(tt::integral_constant<std::size_t,::tt::alignment_of< ::boost::long_long_type>::value>());
  93. #endif
  94. #ifdef BOOST_HAS_MS_INT64
  95. do_check(tt::integral_constant<std::size_t,::tt::alignment_of<__int64>::value>());
  96. #endif
  97. do_check(tt::integral_constant<std::size_t,::tt::alignment_of<int[4]>::value>());
  98. do_check(tt::integral_constant<std::size_t,::tt::alignment_of<int(*)(int)>::value>());
  99. do_check(tt::integral_constant<std::size_t,::tt::alignment_of<int*>::value>());
  100. do_check(tt::integral_constant<std::size_t,::tt::alignment_of<VB>::value>());
  101. do_check(tt::integral_constant<std::size_t,::tt::alignment_of<VD>::value>());
  102. do_check(tt::integral_constant<std::size_t,::tt::alignment_of<enum_UDT>::value>());
  103. do_check(tt::integral_constant<std::size_t,::tt::alignment_of<mf2>::value>());
  104. do_check(tt::integral_constant<std::size_t,::tt::alignment_of<POD_UDT>::value>());
  105. do_check(tt::integral_constant<std::size_t,::tt::alignment_of<empty_UDT>::value>());
  106. do_check(tt::integral_constant<std::size_t,::tt::alignment_of<union_UDT>::value>());
  107. do_check(tt::integral_constant<std::size_t,::tt::alignment_of<boost::detail::max_align>::value>());
  108. TT_TEST_END