is_aligned_test.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. Copyright 2014-2015 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/align/is_aligned.hpp>
  9. #include <boost/core/lightweight_test.hpp>
  10. #include <boost/config.hpp>
  11. template<std::size_t N>
  12. struct A { };
  13. template<std::size_t N>
  14. void test(char* p, A<N>)
  15. {
  16. BOOST_TEST(boost::alignment::is_aligned(p, N));
  17. BOOST_TEST(!boost::alignment::is_aligned(p + 1, N));
  18. }
  19. void test(char* p, A<1>)
  20. {
  21. BOOST_TEST(boost::alignment::is_aligned(p, 1));
  22. }
  23. template<class T>
  24. void test()
  25. {
  26. T o;
  27. test(reinterpret_cast<char*>(&o),
  28. A<boost::alignment::alignment_of<T>::value>());
  29. }
  30. class X;
  31. int main()
  32. {
  33. test<bool>();
  34. test<char>();
  35. test<wchar_t>();
  36. #if !defined(BOOST_NO_CXX11_CHAR16_T)
  37. test<char16_t>();
  38. #endif
  39. #if !defined(BOOST_NO_CXX11_CHAR32_T)
  40. test<char32_t>();
  41. #endif
  42. test<short>();
  43. test<int>();
  44. test<long>();
  45. #if !defined(BOOST_NO_LONG_LONG) && !defined(_MSC_VER)
  46. test<long long>();
  47. #endif
  48. test<float>();
  49. #if !defined(BOOST_MSVC)
  50. test<double>();
  51. test<long double>();
  52. #endif
  53. test<void*>();
  54. test<char*>();
  55. test<int*>();
  56. test<X*>();
  57. test<void(*)()>();
  58. #if !defined(_MSC_VER) || !defined(__clang__)
  59. #if !defined(BOOST_MSVC)
  60. test<int X::*>();
  61. test<int(X::*)()>();
  62. #endif
  63. #endif
  64. return boost::report_errors();
  65. }