aligned_allocator_test.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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/aligned_allocator.hpp>
  8. #include <boost/align/is_aligned.hpp>
  9. #include <boost/core/lightweight_test.hpp>
  10. #include <cstring>
  11. template<std::size_t Alignment>
  12. void test_allocate()
  13. {
  14. {
  15. boost::alignment::aligned_allocator<int, Alignment> a;
  16. int* p = a.allocate(1);
  17. BOOST_TEST(p != 0);
  18. BOOST_TEST(boost::alignment::is_aligned(p, Alignment));
  19. std::memset(p, 0, 1);
  20. a.deallocate(p, 1);
  21. }
  22. {
  23. boost::alignment::aligned_allocator<int, Alignment> a;
  24. int* p = a.allocate(0);
  25. a.deallocate(p, 0);
  26. }
  27. }
  28. template<std::size_t Alignment>
  29. void test_construct()
  30. {
  31. boost::alignment::aligned_allocator<int, Alignment> a;
  32. int* p = a.allocate(1);
  33. a.construct(p, 1);
  34. BOOST_TEST(*p == 1);
  35. a.destroy(p);
  36. a.deallocate(p, 1);
  37. }
  38. template<std::size_t Alignment>
  39. void test_constructor()
  40. {
  41. {
  42. boost::alignment::aligned_allocator<char, Alignment> a1;
  43. boost::alignment::aligned_allocator<int, Alignment> a2(a1);
  44. BOOST_TEST(a2 == a1);
  45. }
  46. {
  47. boost::alignment::aligned_allocator<char, Alignment> a1;
  48. boost::alignment::aligned_allocator<void, Alignment> a2(a1);
  49. BOOST_TEST(a2 == a1);
  50. }
  51. {
  52. boost::alignment::aligned_allocator<void, Alignment> a1;
  53. boost::alignment::aligned_allocator<char, Alignment> a2(a1);
  54. BOOST_TEST(a2 == a1);
  55. }
  56. }
  57. template<std::size_t Alignment>
  58. void test_rebind()
  59. {
  60. {
  61. boost::alignment::aligned_allocator<char, Alignment> a1;
  62. typename boost::alignment::aligned_allocator<char,
  63. Alignment>::template rebind<int>::other a2(a1);
  64. BOOST_TEST(a2 == a1);
  65. }
  66. {
  67. boost::alignment::aligned_allocator<char, Alignment> a1;
  68. typename boost::alignment::aligned_allocator<char,
  69. Alignment>::template rebind<void>::other a2(a1);
  70. BOOST_TEST(a2 == a1);
  71. }
  72. {
  73. boost::alignment::aligned_allocator<void, Alignment> a1;
  74. typename boost::alignment::aligned_allocator<void,
  75. Alignment>::template rebind<char>::other a2(a1);
  76. BOOST_TEST(a2 == a1);
  77. }
  78. }
  79. template<std::size_t Alignment>
  80. void test()
  81. {
  82. test_allocate<Alignment>();
  83. test_construct<Alignment>();
  84. test_constructor<Alignment>();
  85. test_rebind<Alignment>();
  86. }
  87. int main()
  88. {
  89. test<1>();
  90. test<2>();
  91. test<4>();
  92. test<8>();
  93. test<16>();
  94. test<32>();
  95. test<64>();
  96. test<128>();
  97. return boost::report_errors();
  98. }