aligned_storage_test.cpp 4.1 KB

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