default_allocator_test.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. Copyright 2019 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/core/default_allocator.hpp>
  8. #include <boost/core/lightweight_test_trait.hpp>
  9. #include <vector>
  10. #include <list>
  11. class type {
  12. public:
  13. explicit type(double value)
  14. : value_(value) { }
  15. private:
  16. type(const type&);
  17. type& operator=(const type&);
  18. double value_;
  19. };
  20. void test_value_type()
  21. {
  22. BOOST_TEST_TRAIT_SAME(int,
  23. boost::default_allocator<int>::value_type);
  24. BOOST_TEST_TRAIT_SAME(type,
  25. boost::default_allocator<type>::value_type);
  26. BOOST_TEST_TRAIT_SAME(int[5],
  27. boost::default_allocator<int[5]>::value_type);
  28. BOOST_TEST_TRAIT_SAME(void,
  29. boost::default_allocator<void>::value_type);
  30. }
  31. void test_pointer()
  32. {
  33. BOOST_TEST_TRAIT_SAME(int*,
  34. boost::default_allocator<int>::pointer);
  35. BOOST_TEST_TRAIT_SAME(type*,
  36. boost::default_allocator<type>::pointer);
  37. BOOST_TEST_TRAIT_SAME(int(*)[5],
  38. boost::default_allocator<int[5]>::pointer);
  39. BOOST_TEST_TRAIT_SAME(void*,
  40. boost::default_allocator<void>::pointer);
  41. }
  42. void test_const_pointer()
  43. {
  44. BOOST_TEST_TRAIT_SAME(const int*,
  45. boost::default_allocator<int>::const_pointer);
  46. BOOST_TEST_TRAIT_SAME(const type*,
  47. boost::default_allocator<type>::const_pointer);
  48. BOOST_TEST_TRAIT_SAME(const int(*)[5],
  49. boost::default_allocator<int[5]>::const_pointer);
  50. BOOST_TEST_TRAIT_SAME(const void*,
  51. boost::default_allocator<void>::const_pointer);
  52. }
  53. void test_reference()
  54. {
  55. BOOST_TEST_TRAIT_SAME(int&,
  56. boost::default_allocator<int>::reference);
  57. BOOST_TEST_TRAIT_SAME(type&,
  58. boost::default_allocator<type>::reference);
  59. BOOST_TEST_TRAIT_SAME(int(&)[5],
  60. boost::default_allocator<int[5]>::reference);
  61. BOOST_TEST_TRAIT_SAME(void,
  62. boost::default_allocator<void>::reference);
  63. }
  64. void test_const_reference()
  65. {
  66. BOOST_TEST_TRAIT_SAME(const int&,
  67. boost::default_allocator<int>::const_reference);
  68. BOOST_TEST_TRAIT_SAME(const type&,
  69. boost::default_allocator<type>::const_reference);
  70. BOOST_TEST_TRAIT_SAME(const int(&)[5],
  71. boost::default_allocator<int[5]>::const_reference);
  72. BOOST_TEST_TRAIT_SAME(const void,
  73. boost::default_allocator<void>::const_reference);
  74. }
  75. void test_size_type()
  76. {
  77. BOOST_TEST_TRAIT_SAME(std::size_t,
  78. boost::default_allocator<int>::size_type);
  79. BOOST_TEST_TRAIT_SAME(std::size_t,
  80. boost::default_allocator<type>::size_type);
  81. BOOST_TEST_TRAIT_SAME(std::size_t,
  82. boost::default_allocator<int[5]>::size_type);
  83. BOOST_TEST_TRAIT_SAME(std::size_t,
  84. boost::default_allocator<void>::size_type);
  85. }
  86. void test_difference_type()
  87. {
  88. BOOST_TEST_TRAIT_SAME(std::ptrdiff_t,
  89. boost::default_allocator<int>::difference_type);
  90. BOOST_TEST_TRAIT_SAME(std::ptrdiff_t,
  91. boost::default_allocator<type>::difference_type);
  92. BOOST_TEST_TRAIT_SAME(std::ptrdiff_t,
  93. boost::default_allocator<int[5]>::difference_type);
  94. BOOST_TEST_TRAIT_SAME(std::ptrdiff_t,
  95. boost::default_allocator<void>::difference_type);
  96. }
  97. void test_propagate_on_container_move_assignment()
  98. {
  99. BOOST_TEST_TRAIT_TRUE((boost::default_allocator<int>::
  100. propagate_on_container_move_assignment));
  101. BOOST_TEST_TRAIT_TRUE((boost::default_allocator<type>::
  102. propagate_on_container_move_assignment));
  103. BOOST_TEST_TRAIT_TRUE((boost::default_allocator<int[5]>::
  104. propagate_on_container_move_assignment));
  105. BOOST_TEST_TRAIT_TRUE((boost::default_allocator<void>::
  106. propagate_on_container_move_assignment));
  107. }
  108. void test_is_always_equal()
  109. {
  110. BOOST_TEST_TRAIT_TRUE((boost::default_allocator<int>::is_always_equal));
  111. BOOST_TEST_TRAIT_TRUE((boost::default_allocator<type>::is_always_equal));
  112. BOOST_TEST_TRAIT_TRUE((boost::default_allocator<int[5]>::is_always_equal));
  113. BOOST_TEST_TRAIT_TRUE((boost::default_allocator<void>::is_always_equal));
  114. }
  115. void test_rebind()
  116. {
  117. BOOST_TEST_TRAIT_SAME(boost::default_allocator<type>,
  118. boost::default_allocator<int>::rebind<type>::other);
  119. BOOST_TEST_TRAIT_SAME(boost::default_allocator<int[5]>,
  120. boost::default_allocator<type>::rebind<int[5]>::other);
  121. BOOST_TEST_TRAIT_SAME(boost::default_allocator<void>,
  122. boost::default_allocator<int[5]>::rebind<void>::other);
  123. BOOST_TEST_TRAIT_SAME(boost::default_allocator<int>,
  124. boost::default_allocator<void>::rebind<int>::other);
  125. }
  126. void test_default_construct()
  127. {
  128. boost::default_allocator<int> a1;
  129. (void)a1;
  130. boost::default_allocator<type> a2;
  131. (void)a2;
  132. boost::default_allocator<int[5]> a3;
  133. (void)a3;
  134. boost::default_allocator<void> a4;
  135. (void)a4;
  136. }
  137. void test_copy()
  138. {
  139. boost::default_allocator<int> a1;
  140. boost::default_allocator<int> a2(a1);
  141. (void)a2;
  142. boost::default_allocator<int[5]> a3;
  143. boost::default_allocator<int[5]> a4(a3);
  144. (void)a4;
  145. boost::default_allocator<void> a5;
  146. boost::default_allocator<void> a6(a5);
  147. (void)a6;
  148. }
  149. void test_construct_other()
  150. {
  151. boost::default_allocator<int> a1;
  152. boost::default_allocator<type> a2(a1);
  153. boost::default_allocator<int[5]> a3(a2);
  154. boost::default_allocator<void> a4(a3);
  155. boost::default_allocator<int> a5(a4);
  156. (void)a5;
  157. }
  158. #if defined(PTRDIFF_MAX) && defined(SIZE_MAX)
  159. template<class T>
  160. std::size_t max_size()
  161. {
  162. return PTRDIFF_MAX < SIZE_MAX / sizeof(T)
  163. ? PTRDIFF_MAX : SIZE_MAX / sizeof(T);
  164. }
  165. #else
  166. template<class T>
  167. std::size_t max_size()
  168. {
  169. return ~static_cast<std::size_t>(0) / sizeof(T);
  170. }
  171. #endif
  172. void test_max_size()
  173. {
  174. BOOST_TEST_EQ(max_size<int>(),
  175. boost::default_allocator<int>().max_size());
  176. BOOST_TEST_EQ(max_size<type>(),
  177. boost::default_allocator<type>().max_size());
  178. BOOST_TEST_EQ(max_size<int[5]>(),
  179. boost::default_allocator<int[5]>().max_size());
  180. }
  181. template<class T>
  182. void test_allocate()
  183. {
  184. boost::default_allocator<T> a;
  185. T* p = a.allocate(1);
  186. BOOST_TEST(p != 0);
  187. a.deallocate(p, 1);
  188. p = a.allocate(0);
  189. a.deallocate(p, 0);
  190. BOOST_TEST_THROWS(a.allocate(a.max_size() + 1), std::bad_alloc);
  191. }
  192. void test_allocate_deallocate()
  193. {
  194. test_allocate<int>();
  195. test_allocate<type>();
  196. test_allocate<int[5]>();
  197. }
  198. void test_equals()
  199. {
  200. BOOST_TEST(boost::default_allocator<int>() ==
  201. boost::default_allocator<type>());
  202. BOOST_TEST(boost::default_allocator<type>() ==
  203. boost::default_allocator<int[5]>());
  204. BOOST_TEST(boost::default_allocator<int[5]>() ==
  205. boost::default_allocator<void>());
  206. BOOST_TEST(boost::default_allocator<void>() ==
  207. boost::default_allocator<int>());
  208. }
  209. void test_not_equals()
  210. {
  211. BOOST_TEST(!(boost::default_allocator<int>() !=
  212. boost::default_allocator<type>()));
  213. BOOST_TEST(!(boost::default_allocator<type>() !=
  214. boost::default_allocator<int[5]>()));
  215. BOOST_TEST(!(boost::default_allocator<int[5]>() !=
  216. boost::default_allocator<void>()));
  217. BOOST_TEST(!(boost::default_allocator<void>() !=
  218. boost::default_allocator<int>()));
  219. }
  220. void test_container()
  221. {
  222. std::vector<int, boost::default_allocator<int> > v;
  223. v.push_back(1);
  224. BOOST_TEST(v.size() == 1);
  225. BOOST_TEST(v.front() == 1);
  226. std::list<int, boost::default_allocator<int> > l;
  227. l.push_back(1);
  228. BOOST_TEST(l.size() == 1);
  229. BOOST_TEST(l.front() == 1);
  230. }
  231. int main()
  232. {
  233. test_value_type();
  234. test_pointer();
  235. test_const_pointer();
  236. test_reference();
  237. test_const_reference();
  238. test_size_type();
  239. test_difference_type();
  240. test_propagate_on_container_move_assignment();
  241. test_is_always_equal();
  242. test_rebind();
  243. test_default_construct();
  244. test_copy();
  245. test_construct_other();
  246. test_max_size();
  247. test_allocate_deallocate();
  248. test_equals();
  249. test_not_equals();
  250. test_container();
  251. return boost::report_errors();
  252. }