polymorphic_allocator_test.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2015-2015. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/container for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. #include <boost/container/pmr/polymorphic_allocator.hpp>
  11. #include <boost/container/pmr/global_resource.hpp>
  12. #include <boost/core/lightweight_test.hpp>
  13. #include "derived_from_memory_resource.hpp"
  14. #include "propagation_test_allocator.hpp"
  15. using namespace boost::container::pmr;
  16. using namespace boost::container;
  17. void test_default_constructor()
  18. {
  19. polymorphic_allocator<int> a;
  20. BOOST_TEST(a.resource() == get_default_resource());
  21. }
  22. void test_resource_constructor()
  23. {
  24. polymorphic_allocator<int> a(0);
  25. BOOST_TEST(a.resource() == get_default_resource());
  26. derived_from_memory_resource d;
  27. polymorphic_allocator<int> b(&d);
  28. BOOST_TEST(&d == b.resource());
  29. }
  30. void test_copy_constructor()
  31. {
  32. derived_from_memory_resource d;
  33. polymorphic_allocator<int> b(&d);
  34. polymorphic_allocator<int> c(b);
  35. BOOST_TEST(b.resource() == c.resource());
  36. }
  37. void test_copy_assignment()
  38. {
  39. derived_from_memory_resource d;
  40. polymorphic_allocator<int> b(&d);
  41. polymorphic_allocator<int> c;
  42. BOOST_TEST(c.resource() == get_default_resource());
  43. c = b;
  44. BOOST_TEST(c.resource() == b.resource());
  45. }
  46. void test_allocate()
  47. {
  48. int dummy;
  49. derived_from_memory_resource d;
  50. polymorphic_allocator<int> p(&d);
  51. d.reset();
  52. d.do_allocate_return = &dummy;
  53. p.allocate(2);
  54. BOOST_TEST(d.do_allocate_called == true);
  55. BOOST_TEST(d.do_allocate_return == &dummy);
  56. //It shall allocate 2*sizeof(int), alignment_of<int>
  57. BOOST_TEST(d.do_allocate_bytes == 2*sizeof(int));
  58. BOOST_TEST(d.do_allocate_alignment == dtl::alignment_of<int>::value);
  59. }
  60. void test_deallocate()
  61. {
  62. int dummy;
  63. derived_from_memory_resource d;
  64. polymorphic_allocator<int> p(&d);
  65. d.reset();
  66. p.deallocate(&dummy, 3);
  67. BOOST_TEST(d.do_deallocate_called == true);
  68. //It shall deallocate 2*sizeof(int), alignment_of<int>
  69. BOOST_TEST(d.do_deallocate_p == &dummy);
  70. BOOST_TEST(d.do_deallocate_bytes == 3*sizeof(int));
  71. BOOST_TEST(d.do_deallocate_alignment == dtl::alignment_of<int>::value);
  72. }
  73. void test_construct()
  74. {
  75. //0 arg
  76. {
  77. typedef allocator_argument_tester<NotUsesAllocator, 0> value_type;
  78. value_type value;
  79. value.~value_type();
  80. polymorphic_allocator<int> pa;
  81. pa.construct(&value);
  82. BOOST_TEST(value.construction_type == NotUsesAllocator);
  83. BOOST_TEST(value.value == 0);
  84. value.~value_type();
  85. }
  86. {
  87. typedef allocator_argument_tester<ErasedTypePrefix, 0> value_type;
  88. value_type value;
  89. value.~value_type();
  90. polymorphic_allocator<int> pa;
  91. pa.construct(&value);
  92. BOOST_TEST(value.construction_type == ConstructiblePrefix);
  93. BOOST_TEST(value.value == 0);
  94. value.~value_type();
  95. }
  96. {
  97. typedef allocator_argument_tester<ErasedTypeSuffix, 0> value_type;
  98. value_type value;
  99. value.~value_type();
  100. polymorphic_allocator<int> pa;
  101. pa.construct(&value);
  102. BOOST_TEST(value.construction_type == ConstructibleSuffix);
  103. BOOST_TEST(value.value == 0);
  104. value.~value_type();
  105. }
  106. //1 arg
  107. {
  108. typedef allocator_argument_tester<NotUsesAllocator, 0> value_type;
  109. value_type value;
  110. value.~value_type();
  111. polymorphic_allocator<int> pa;
  112. pa.construct(&value, 2);
  113. BOOST_TEST(value.construction_type == NotUsesAllocator);
  114. BOOST_TEST(value.value == 2);
  115. value.~value_type();
  116. }
  117. {
  118. typedef allocator_argument_tester<ErasedTypePrefix, 0> value_type;
  119. value_type value;
  120. value.~value_type();
  121. polymorphic_allocator<int> pa;
  122. pa.construct(&value, 3);
  123. BOOST_TEST(value.construction_type == ConstructiblePrefix);
  124. BOOST_TEST(value.value == 3);
  125. value.~value_type();
  126. }
  127. {
  128. typedef allocator_argument_tester<ErasedTypeSuffix, 0> value_type;
  129. value_type value;
  130. value.~value_type();
  131. polymorphic_allocator<int> pa;
  132. pa.construct(&value, 4);
  133. BOOST_TEST(value.construction_type == ConstructibleSuffix);
  134. BOOST_TEST(value.value == 4);
  135. value.~value_type();
  136. }
  137. }
  138. struct char_holder
  139. {
  140. char m_char;
  141. ~char_holder()
  142. { destructor_called = true; }
  143. static bool destructor_called;
  144. };
  145. bool char_holder::destructor_called = false;
  146. void test_destroy()
  147. {
  148. char_holder ch;
  149. polymorphic_allocator<int> p;
  150. BOOST_TEST(char_holder::destructor_called == false);
  151. p.destroy(&ch);
  152. BOOST_TEST(char_holder::destructor_called == true);
  153. }
  154. void test_select_on_container_copy_construction()
  155. {
  156. //select_on_container_copy_construction shall return
  157. //a default constructed polymorphic_allocator
  158. //which uses the default resource.
  159. derived_from_memory_resource d;
  160. polymorphic_allocator<int> p(&d);
  161. BOOST_TEST(get_default_resource() == p.select_on_container_copy_construction().resource());
  162. }
  163. void test_resource()
  164. {
  165. derived_from_memory_resource d;
  166. polymorphic_allocator<int> p(&d);
  167. BOOST_TEST(&d == p.resource());
  168. }
  169. int main()
  170. {
  171. test_default_constructor();
  172. test_resource_constructor();
  173. test_copy_constructor();
  174. test_copy_assignment();
  175. test_allocate();
  176. test_deallocate();
  177. test_construct();
  178. test_destroy();
  179. test_select_on_container_copy_construction();
  180. test_resource();
  181. return ::boost::report_errors();
  182. }