aligned_allocator_adaptor_test.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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_adaptor.hpp>
  8. #include <boost/align/is_aligned.hpp>
  9. #include <boost/core/lightweight_test.hpp>
  10. #include <new>
  11. #include <cstring>
  12. template<class T>
  13. class A {
  14. public:
  15. typedef T value_type;
  16. typedef T* pointer;
  17. typedef std::size_t size_type;
  18. typedef std::ptrdiff_t difference_type;
  19. template<class U>
  20. struct rebind {
  21. typedef A<U> other;
  22. };
  23. A(int state)
  24. : state_(state) { }
  25. template<class U>
  26. A(const A<U>& other)
  27. : state_(other.state()) { }
  28. T* allocate(std::size_t size, const void* = 0) {
  29. return static_cast<T*>(::operator new(sizeof(T) * size));
  30. }
  31. void deallocate(T* ptr, std::size_t) {
  32. ::operator delete(ptr);
  33. }
  34. void construct(T* ptr, const T& value) {
  35. ::new(static_cast<void*>(ptr)) T(value);
  36. }
  37. void destroy(T* ptr) {
  38. ptr->~T();
  39. }
  40. int state() const {
  41. return state_;
  42. }
  43. private:
  44. int state_;
  45. };
  46. template<class T, class U>
  47. inline bool
  48. operator==(const A<T>& a, const A<U>& b)
  49. {
  50. return a.state() == b.state();
  51. }
  52. template<class T, class U>
  53. inline bool
  54. operator!=(const A<T>& a, const A<U>& b)
  55. {
  56. return !(a == b);
  57. }
  58. template<std::size_t Alignment>
  59. void test_allocate()
  60. {
  61. {
  62. boost::alignment::aligned_allocator_adaptor<A<int>, Alignment> a(5);
  63. int* p = a.allocate(1);
  64. BOOST_TEST(p != 0);
  65. BOOST_TEST(boost::alignment::is_aligned(p, Alignment));
  66. std::memset(p, 0, 1);
  67. a.deallocate(p, 1);
  68. }
  69. {
  70. boost::alignment::aligned_allocator_adaptor<A<int>, Alignment> a(5);
  71. int* p = a.allocate(1);
  72. int* q = a.allocate(1, p);
  73. BOOST_TEST(q != 0);
  74. BOOST_TEST(boost::alignment::is_aligned(q, Alignment));
  75. std::memset(q, 0, 1);
  76. a.deallocate(q, 1);
  77. a.deallocate(p, 1);
  78. }
  79. {
  80. boost::alignment::aligned_allocator_adaptor<A<int>, Alignment> a(5);
  81. int* p = a.allocate(0);
  82. a.deallocate(p, 0);
  83. }
  84. }
  85. template<std::size_t Alignment>
  86. void test_construct()
  87. {
  88. boost::alignment::aligned_allocator_adaptor<A<int>, Alignment> a(5);
  89. int* p = a.allocate(1);
  90. a.construct(p, 1);
  91. BOOST_TEST(*p == 1);
  92. a.destroy(p);
  93. a.deallocate(p, 1);
  94. }
  95. template<std::size_t Alignment>
  96. void test_constructor()
  97. {
  98. {
  99. boost::alignment::aligned_allocator_adaptor<A<char>, Alignment> a(5);
  100. boost::alignment::aligned_allocator_adaptor<A<int>, Alignment> b(a);
  101. BOOST_TEST(b == a);
  102. }
  103. {
  104. A<int> a(5);
  105. boost::alignment::aligned_allocator_adaptor<A<int>, Alignment> b(a);
  106. BOOST_TEST(b.base() == a);
  107. }
  108. }
  109. template<std::size_t Alignment>
  110. void test_rebind()
  111. {
  112. boost::alignment::aligned_allocator_adaptor<A<int>, Alignment> a(5);
  113. typename boost::alignment::aligned_allocator_adaptor<A<int>,
  114. Alignment>::template rebind<int>::other b(a);
  115. BOOST_TEST(b == a);
  116. }
  117. template<std::size_t Alignment>
  118. void test()
  119. {
  120. test_allocate<Alignment>();
  121. test_construct<Alignment>();
  122. test_constructor<Alignment>();
  123. test_rebind<Alignment>();
  124. }
  125. int main()
  126. {
  127. test<1>();
  128. test<2>();
  129. test<4>();
  130. test<8>();
  131. test<16>();
  132. test<32>();
  133. test<64>();
  134. test<128>();
  135. return boost::report_errors();
  136. }