container_size_test.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2014-2014
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // See http://www.boost.org/libs/intrusive for documentation.
  10. //
  11. /////////////////////////////////////////////////////////////////////////////
  12. #include <boost/detail/lightweight_test.hpp>
  13. #include <cstddef>
  14. #include <boost/intrusive/list.hpp>
  15. #include <boost/intrusive/slist.hpp>
  16. #include <boost/intrusive/set.hpp>
  17. #include <boost/intrusive/avl_set.hpp>
  18. #include <boost/intrusive/bs_set.hpp>
  19. #include <boost/intrusive/sg_set.hpp>
  20. #include <boost/intrusive/splay_set.hpp>
  21. #include <boost/intrusive/treap_set.hpp>
  22. #include <boost/intrusive/unordered_set.hpp>
  23. #include <boost/static_assert.hpp>
  24. #include "itestvalue.hpp"
  25. using namespace boost::intrusive;
  26. BOOST_INTRUSIVE_INSTANTIATE_DEFAULT_TYPE_TMPLT(reverse_iterator)
  27. BOOST_INTRUSIVE_INSTANTIATE_DEFAULT_TYPE_TMPLT(const_reverse_iterator)
  28. template<bool Value>
  29. struct boolean
  30. {
  31. static const bool value = Value;
  32. };
  33. template<class A, class B>
  34. struct pow2_and_equal_sizes
  35. {
  36. static const std::size_t a_size = sizeof(A);
  37. static const std::size_t b_size = sizeof(B);
  38. static const bool a_b_sizes_equal = a_size == b_size;
  39. static const bool value = !(a_size & (a_size - 1u));
  40. };
  41. template<class Hook>
  42. struct node : Hook
  43. {};
  44. //Avoid testing for uncommon architectures
  45. void test_sizes(boolean<false>, std::size_t)
  46. {}
  47. template<class C>
  48. void test_iterator_sizes(std::size_t size)
  49. {
  50. typedef typename C::iterator iterator;
  51. typedef typename C::const_iterator const_iterator;
  52. typedef BOOST_INTRUSIVE_OBTAIN_TYPE_WITH_DEFAULT
  53. (::, C, reverse_iterator, iterator) reverse_iterator;
  54. typedef BOOST_INTRUSIVE_OBTAIN_TYPE_WITH_DEFAULT
  55. (::, C, const_reverse_iterator, const_iterator) const_reverse_iterator;
  56. BOOST_TEST_EQ(sizeof(iterator), size);
  57. BOOST_TEST_EQ(sizeof(const_iterator), size);
  58. BOOST_TEST_EQ(sizeof(iterator), sizeof(reverse_iterator));
  59. BOOST_TEST_EQ(sizeof(const_iterator), size);
  60. BOOST_TEST_EQ(sizeof(const_iterator), sizeof(const_reverse_iterator));
  61. }
  62. //Test sizes for common 32 and 64 bit architectures
  63. void test_sizes(boolean<true>, std::size_t wordsize)
  64. {
  65. { //list
  66. typedef list<node<list_base_hook<> > > c;
  67. BOOST_TEST_EQ(sizeof(c), wordsize*3);
  68. test_iterator_sizes<c>(wordsize);
  69. }
  70. {
  71. typedef list<node<list_base_hook<> >, constant_time_size<false> > c;
  72. BOOST_TEST_EQ(sizeof(c), wordsize*2);
  73. test_iterator_sizes<c>(wordsize);
  74. }
  75. {
  76. typedef list< node< list_base_hook<> >, header_holder_type< heap_node_holder< list_node<void*>* > > > c;
  77. BOOST_TEST_EQ(sizeof(c), wordsize*2);
  78. test_iterator_sizes<c>(wordsize);
  79. }
  80. {
  81. typedef list< node< list_base_hook<> >, constant_time_size<false>, header_holder_type< heap_node_holder< list_node<void*>* > > > c;
  82. BOOST_TEST_EQ(sizeof(c), wordsize*1);
  83. test_iterator_sizes<c>(wordsize);
  84. }
  85. { //slist
  86. typedef slist<node< slist_base_hook<> > > c;
  87. BOOST_TEST_EQ(sizeof(c), wordsize*2);
  88. test_iterator_sizes<c>(wordsize);
  89. }
  90. {
  91. typedef slist<node< slist_base_hook<> >, constant_time_size<false> > c;
  92. BOOST_TEST_EQ(sizeof(c), wordsize*1);
  93. test_iterator_sizes<c>(wordsize);
  94. }
  95. {
  96. typedef slist<node< slist_base_hook<> >, cache_last<true> > c;
  97. BOOST_TEST_EQ(sizeof(c), wordsize*3);
  98. test_iterator_sizes<c>(wordsize);
  99. }
  100. { //set
  101. typedef set<node< set_base_hook<> > > c;
  102. BOOST_TEST_EQ(sizeof(c), wordsize*5);
  103. test_iterator_sizes<c>(wordsize);
  104. }
  105. {
  106. typedef set<node< set_base_hook<> > , constant_time_size<false> > c;
  107. BOOST_TEST_EQ(sizeof(c), wordsize*4);
  108. test_iterator_sizes<c>(wordsize);
  109. }
  110. {
  111. typedef set<node< set_base_hook<optimize_size<true> > > , constant_time_size<false> > c;
  112. BOOST_TEST_EQ(sizeof(c), wordsize*3);
  113. test_iterator_sizes<c>(wordsize);
  114. }
  115. {
  116. typedef set< node< set_base_hook<> >, header_holder_type< heap_node_holder< rbtree_node<void*>* > > > c;
  117. BOOST_TEST_EQ(sizeof(c), wordsize*2);
  118. test_iterator_sizes<c>(wordsize);
  119. }
  120. {
  121. typedef set< node< set_base_hook<> >, constant_time_size<false>, header_holder_type< heap_node_holder< rbtree_node<void*>* > > > c;
  122. BOOST_TEST_EQ(sizeof(c), wordsize*1);
  123. test_iterator_sizes<c>(wordsize);
  124. }
  125. { //avl
  126. typedef avl_set<node< avl_set_base_hook<> > > c;
  127. BOOST_TEST_EQ(sizeof(c), wordsize*5);
  128. test_iterator_sizes<c>(wordsize);
  129. }
  130. {
  131. typedef avl_set<node< avl_set_base_hook<> > , constant_time_size<false> > c;
  132. BOOST_TEST_EQ(sizeof(c), wordsize*4);
  133. test_iterator_sizes<c>(wordsize);
  134. }
  135. {
  136. typedef avl_set<node< avl_set_base_hook<optimize_size<true> > > , constant_time_size<false> > c;
  137. BOOST_TEST_EQ(sizeof(c), wordsize*3);
  138. test_iterator_sizes<c>(wordsize);
  139. }
  140. {
  141. typedef avl_set< node< avl_set_base_hook<> >, header_holder_type< heap_node_holder< avltree_node<void*>* > > > c;
  142. BOOST_TEST_EQ(sizeof(c), wordsize*2);
  143. test_iterator_sizes<c>(wordsize);
  144. }
  145. {
  146. typedef avl_set< node< avl_set_base_hook<> >, constant_time_size<false>, header_holder_type< heap_node_holder< avltree_node<void*>* > > > c;
  147. BOOST_TEST_EQ(sizeof(c), wordsize*1);
  148. test_iterator_sizes<c>(wordsize);
  149. }
  150. { //bs
  151. typedef bs_set<node< bs_set_base_hook<> > > c;
  152. BOOST_TEST_EQ(sizeof(c), wordsize*4);
  153. test_iterator_sizes<c>(wordsize);
  154. }
  155. {
  156. typedef bs_set<node< bs_set_base_hook<> > , constant_time_size<false> > c;
  157. BOOST_TEST_EQ(sizeof(c), wordsize*3);
  158. test_iterator_sizes<c>(wordsize);
  159. }
  160. { //splay
  161. typedef splay_set<node< bs_set_base_hook<> > > c;
  162. BOOST_TEST_EQ(sizeof(c), wordsize*4);
  163. test_iterator_sizes<c>(wordsize);
  164. }
  165. {
  166. typedef splay_set<node< bs_set_base_hook<> > , constant_time_size<false> > c;
  167. BOOST_TEST_EQ(sizeof(c), wordsize*3);
  168. test_iterator_sizes<c>(wordsize);
  169. }
  170. { //scapegoat
  171. typedef sg_set<node< bs_set_base_hook<> > > c;
  172. BOOST_TEST_EQ(sizeof(c), (wordsize*5+sizeof(float)*2));
  173. test_iterator_sizes<c>(wordsize);
  174. }
  175. { //treap
  176. typedef treap_set<node< bs_set_base_hook<> > > c;
  177. BOOST_TEST_EQ(sizeof(c), wordsize*4);
  178. test_iterator_sizes<c>(wordsize);
  179. }
  180. {
  181. typedef treap_set<node< bs_set_base_hook<> > , constant_time_size<false> > c;
  182. BOOST_TEST_EQ(sizeof(c), wordsize*3);
  183. test_iterator_sizes<c>(wordsize);
  184. }
  185. { //unordered
  186. typedef unordered_set<node< unordered_set_base_hook<> > > c;
  187. BOOST_TEST_EQ(sizeof(c), wordsize*3);
  188. test_iterator_sizes<c>(wordsize*2);
  189. }
  190. {
  191. typedef unordered_set<node< unordered_set_base_hook<> > , power_2_buckets<true> > c;
  192. BOOST_TEST_EQ(sizeof(c), wordsize*3);
  193. test_iterator_sizes<c>(wordsize*2);
  194. }
  195. {
  196. typedef unordered_set<node< unordered_set_base_hook<> >, constant_time_size<false> > c;
  197. BOOST_TEST_EQ(sizeof(c), wordsize*2);
  198. test_iterator_sizes<c>(wordsize*2);
  199. }
  200. {
  201. typedef unordered_set<node< unordered_set_base_hook< optimize_multikey<true> > >, constant_time_size<false> > c;
  202. BOOST_TEST_EQ(sizeof(c), wordsize*2);
  203. test_iterator_sizes<c>(wordsize*2);
  204. }
  205. {
  206. typedef unordered_set<node< unordered_set_base_hook< optimize_multikey<true> > >, incremental<true> > c;
  207. BOOST_TEST_EQ(sizeof(c), wordsize*4);
  208. test_iterator_sizes<c>(wordsize*2);
  209. }
  210. }
  211. int main()
  212. {
  213. test_sizes(boolean< pow2_and_equal_sizes<std::size_t, void*>::value >(), sizeof(std::size_t));
  214. return ::boost::report_errors();
  215. }