alignment_of_test.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. Copyright 2014 Glen Joseph Fernandes
  3. (glenjofe@gmail.com)
  4. Distributed under the Boost Software License, Version 1.0.
  5. (http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. #include <boost/align/alignment_of.hpp>
  8. #include <boost/core/lightweight_test.hpp>
  9. #include <boost/config.hpp>
  10. #include <cstddef>
  11. template<class T>
  12. struct remove_reference {
  13. typedef T type;
  14. };
  15. template<class T>
  16. struct remove_reference<T&> {
  17. typedef T type;
  18. };
  19. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  20. template<class T>
  21. struct remove_reference<T&&> {
  22. typedef T type;
  23. };
  24. #endif
  25. template<class T>
  26. struct remove_all_extents {
  27. typedef T type;
  28. };
  29. template<class T>
  30. struct remove_all_extents<T[]> {
  31. typedef typename remove_all_extents<T>::type type;
  32. };
  33. template<class T, std::size_t N>
  34. struct remove_all_extents<T[N]> {
  35. typedef typename remove_all_extents<T>::type type;
  36. };
  37. template<class T>
  38. struct remove_cv {
  39. typedef T type;
  40. };
  41. template<class T>
  42. struct remove_cv<const T> {
  43. typedef T type;
  44. };
  45. template<class T>
  46. struct remove_cv<volatile T> {
  47. typedef T type;
  48. };
  49. template<class T>
  50. struct remove_cv<const volatile T> {
  51. typedef T type;
  52. };
  53. template<class T>
  54. struct offset_value {
  55. char value;
  56. typename remove_cv<typename remove_all_extents<typename
  57. remove_reference<T>::type>::type>::type object;
  58. };
  59. template<class T>
  60. void test_type()
  61. {
  62. enum {
  63. N = boost::alignment::alignment_of<T>::value
  64. };
  65. BOOST_TEST(offsetof(offset_value<T>, object) == N);
  66. }
  67. template<class T>
  68. void test_reference()
  69. {
  70. test_type<T>();
  71. test_type<T&>();
  72. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  73. test_type<T&&>();
  74. #endif
  75. }
  76. template<class T>
  77. void test_array()
  78. {
  79. test_reference<T>();
  80. test_reference<T[2]>();
  81. test_type<T[]>();
  82. }
  83. template<class T>
  84. void test_cv()
  85. {
  86. test_array<T>();
  87. test_array<const T>();
  88. test_array<volatile T>();
  89. test_array<const volatile T>();
  90. }
  91. template<class T>
  92. struct Struct {
  93. T t;
  94. };
  95. template<class T>
  96. union Union {
  97. T t;
  98. };
  99. template<class T>
  100. void test()
  101. {
  102. test_cv<T>();
  103. test_cv<Struct<T> >();
  104. test_cv<Union<T> >();
  105. }
  106. void test_integral()
  107. {
  108. test<bool>();
  109. test<char>();
  110. test<signed char>();
  111. test<unsigned char>();
  112. test<wchar_t>();
  113. #if !defined(BOOST_NO_CXX11_CHAR16_T)
  114. test<char16_t>();
  115. #endif
  116. #if !defined(BOOST_NO_CXX11_CHAR32_T)
  117. test<char32_t>();
  118. #endif
  119. test<short>();
  120. test<unsigned short>();
  121. test<int>();
  122. test<unsigned int>();
  123. test<long>();
  124. test<unsigned long>();
  125. #if !defined(BOOST_NO_LONG_LONG)
  126. test<long long>();
  127. test<unsigned long long>();
  128. #endif
  129. }
  130. void test_floating_point()
  131. {
  132. test<float>();
  133. test<double>();
  134. test<long double>();
  135. }
  136. void test_nullptr_t()
  137. {
  138. #if !defined(BOOST_NO_CXX11_NULLPTR) && \
  139. !defined(BOOST_NO_CXX11_DECLTYPE)
  140. test<decltype(nullptr)>();
  141. #endif
  142. }
  143. class X;
  144. void test_pointer()
  145. {
  146. test<void*>();
  147. test<char*>();
  148. test<int*>();
  149. test<X*>();
  150. test<void(*)()>();
  151. }
  152. void test_member_pointer()
  153. {
  154. test<int X::*>();
  155. test<int(X::*)()>();
  156. }
  157. enum E {
  158. V = 1
  159. };
  160. void test_enum()
  161. {
  162. test<E>();
  163. }
  164. struct S { };
  165. class C { };
  166. union U { };
  167. void test_class()
  168. {
  169. test<S>();
  170. test<C>();
  171. test<U>();
  172. }
  173. int main()
  174. {
  175. test_integral();
  176. test_floating_point();
  177. test_nullptr_t();
  178. test_pointer();
  179. test_member_pointer();
  180. test_enum();
  181. test_class();
  182. return boost::report_errors();
  183. }