aligned_storage_empy_test.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // (C) Copyright Thorsten Ottosen 2009.
  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. namespace
  16. {
  17. template< unsigned N, unsigned Alignment >
  18. struct alignment_implementation1
  19. {
  20. boost::detail::aligned_storage::aligned_storage_imp<N,Alignment> type;
  21. const void* address() const { return type.address(); }
  22. };
  23. template< unsigned N, unsigned Alignment >
  24. struct alignment_implementation2 :
  25. #ifndef __BORLANDC__
  26. private
  27. #else
  28. public
  29. #endif
  30. boost::detail::aligned_storage::aligned_storage_imp<N,Alignment>
  31. {
  32. typedef boost::detail::aligned_storage::aligned_storage_imp<N,Alignment> type;
  33. const void* address() const { return static_cast<const type*>(this)->address(); }
  34. };
  35. template< unsigned N, class T >
  36. std::ptrdiff_t get_address1()
  37. {
  38. static alignment_implementation1<N*sizeof(T), tt::alignment_of<T>::value> imp1;
  39. return static_cast<const char*>(imp1.address()) - reinterpret_cast<const char*>(&imp1);
  40. }
  41. template< unsigned N, class T >
  42. std::ptrdiff_t get_address2()
  43. {
  44. static alignment_implementation2<N*sizeof(T), tt::alignment_of<T>::value> imp2;
  45. return static_cast<const char*>(imp2.address()) - reinterpret_cast<const char*>(&imp2);
  46. }
  47. template< class T >
  48. void do_check()
  49. {
  50. std::ptrdiff_t addr1 = get_address1<0,T>();
  51. std::ptrdiff_t addr2 = get_address2<0,T>();
  52. //
  53. // @remark: only the empty case differs
  54. //
  55. BOOST_CHECK( addr1 != addr2 );
  56. addr1 = get_address1<1,T>();
  57. addr2 = get_address2<1,T>();
  58. BOOST_CHECK( addr1 == addr2 );
  59. addr1 = get_address1<2,T>();
  60. addr2 = get_address2<2,T>();
  61. BOOST_CHECK( addr1 == addr2 );
  62. addr1 = get_address1<3,T>();
  63. addr2 = get_address2<3,T>();
  64. BOOST_CHECK( addr1 == addr2 );
  65. addr1 = get_address1<4,T>();
  66. addr2 = get_address2<4,T>();
  67. BOOST_CHECK( addr1 == addr2 );
  68. addr1 = get_address1<17,T>();
  69. addr2 = get_address2<17,T>();
  70. BOOST_CHECK( addr1 == addr2 );
  71. addr1 = get_address1<32,T>();
  72. addr2 = get_address2<32,T>();
  73. BOOST_CHECK( addr1 == addr2 );
  74. }
  75. }
  76. TT_TEST_BEGIN(type_with_empty_alignment_buffer)
  77. do_check<char>();
  78. do_check<short>();
  79. do_check<int>();
  80. do_check<long>();
  81. do_check<float>();
  82. do_check<double>();
  83. do_check<long double>();
  84. #ifdef BOOST_HAS_MS_INT64
  85. do_check<__int64>();
  86. #endif
  87. #ifdef BOOST_HAS_LONG_LONG
  88. do_check<boost::long_long_type>();
  89. #endif
  90. do_check<int(*)(int)>();
  91. do_check<int*>();
  92. do_check<VB>();
  93. do_check<VD>();
  94. do_check<enum_UDT>();
  95. do_check<mf2>();
  96. do_check<POD_UDT>();
  97. do_check<empty_UDT>();
  98. do_check<union_UDT>();
  99. do_check<boost::detail::max_align>();
  100. TT_TEST_END