test_copy_assignment.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /* Boost.MultiIndex test for copying and assignment.
  2. *
  3. * Copyright 2003-2018 Joaquin M Lopez Munoz.
  4. * Distributed under the Boost Software License, Version 1.0.
  5. * (See accompanying file LICENSE_1_0.txt or copy at
  6. * http://www.boost.org/LICENSE_1_0.txt)
  7. *
  8. * See http://www.boost.org/libs/multi_index for library home page.
  9. */
  10. #include "test_copy_assignment.hpp"
  11. #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
  12. #include <algorithm>
  13. #include <boost/move/utility_core.hpp>
  14. #include <list>
  15. #include <numeric>
  16. #include <vector>
  17. #include "pre_multi_index.hpp"
  18. #include "employee.hpp"
  19. #include "small_allocator.hpp"
  20. #include <boost/detail/lightweight_test.hpp>
  21. using namespace boost::multi_index;
  22. #if BOOST_WORKAROUND(__MWERKS__,<=0x3003)
  23. /* The "ISO C++ Template Parser" option makes CW8.3 incorrectly fail at
  24. * expressions of the form sizeof(x) where x is an array local to a
  25. * template function.
  26. */
  27. #pragma parse_func_templ off
  28. #endif
  29. typedef multi_index_container<int> copyable_and_movable;
  30. struct holder
  31. {
  32. copyable_and_movable c;
  33. };
  34. template<typename Sequence>
  35. static void test_assign()
  36. {
  37. Sequence s;
  38. int a[]={0,1,2,3,4,5};
  39. std::size_t sa=sizeof(a)/sizeof(a[0]);
  40. s.assign(&a[0],&a[sa]);
  41. BOOST_TEST(s.size()==sa&&std::equal(s.begin(),s.end(),&a[0]));
  42. s.assign((const int*)(&a[0]),(const int*)(&a[sa]));
  43. BOOST_TEST(s.size()==sa&&std::equal(s.begin(),s.end(),&a[0]));
  44. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  45. s.assign({0,1,2,3,4,5});
  46. #else
  47. s.assign(&a[0],&a[sa]);
  48. #endif
  49. BOOST_TEST(s.size()==sa&&std::equal(s.begin(),s.end(),&a[0]));
  50. s.assign((std::size_t)18,37);
  51. BOOST_TEST(s.size()==18&&std::accumulate(s.begin(),s.end(),0)==666);
  52. s.assign((std::size_t)12,167);
  53. BOOST_TEST(s.size()==12&&std::accumulate(s.begin(),s.end(),0)==2004);
  54. }
  55. #if BOOST_WORKAROUND(__MWERKS__,<=0x3003)
  56. #pragma parse_func_templ reset
  57. #endif
  58. template<typename Sequence>
  59. static void test_integral_assign()
  60. {
  61. /* Special cases described in 23.1.1/9: integral types must not
  62. * be taken as iterators in assign(f,l) and insert(p,f,l).
  63. */
  64. Sequence s;
  65. s.assign(5,10);
  66. BOOST_TEST(s.size()==5&&std::accumulate(s.begin(),s.end(),0)==50);
  67. s.assign(2u,5u);
  68. BOOST_TEST(s.size()==2&&std::accumulate(s.begin(),s.end(),0)==10);
  69. s.clear();
  70. s.insert(s.begin(),5,10);
  71. BOOST_TEST(s.size()==5&&std::accumulate(s.begin(),s.end(),0)==50);
  72. s.insert(s.begin(),2u,5u);
  73. BOOST_TEST(s.size()==7&&std::accumulate(s.begin(),s.end(),0)==60);
  74. }
  75. employee_set produce_employee_set()
  76. {
  77. employee_set es;
  78. es.insert(employee(0,"Petr",60,2837));
  79. es.insert(employee(1,"Jiri",25,2143));
  80. es.insert(employee(2,"Radka",40,4875));
  81. es.insert(employee(3,"Milan",38,3474));
  82. return es;
  83. }
  84. void test_copy_assignment()
  85. {
  86. employee_set es;
  87. employee_set es2(es);
  88. employee_set::allocator_type al=es.get_allocator();
  89. al=get<1>(es).get_allocator();
  90. al=get<2>(es).get_allocator();
  91. al=get<3>(es).get_allocator();
  92. al=get<4>(es).get_allocator();
  93. al=get<5>(es).get_allocator();
  94. BOOST_TEST(es2.empty());
  95. es2.insert(employee(0,"Joe",31,1123));
  96. es2.insert(employee(1,"Robert",27,5601));
  97. es2.insert(employee(2,"John",40,7889));
  98. es2.insert(employee(2,"Aristotle",2388,3357)); /* clash */
  99. es2.insert(employee(3,"Albert",20,9012));
  100. es2.insert(employee(4,"John",57,1002));
  101. es2.insert(employee(0,"Andrew",60,2302)); /* clash */
  102. employee_set es3(es2);
  103. BOOST_TEST(es2==es3);
  104. BOOST_TEST(get<2>(es2)==get<2>(es3));
  105. BOOST_TEST(get<3>(es2)==get<3>(es3));
  106. BOOST_TEST(get<5>(es2)==get<5>(es3));
  107. employee_set es4=employee_set(non_std_allocator<employee>());
  108. employee_set_by_name& i1=get<name>(es4);
  109. i1=get<1>(es2);
  110. BOOST_TEST(es4==es2);
  111. employee_set es5=employee_set(employee_set::ctor_args_list());
  112. employee_set_by_age& i2=get<age>(es5);
  113. i2=get<2>(es2);
  114. BOOST_TEST(i2==get<2>(es2));
  115. employee_set es6;
  116. employee_set_as_inserted& i3=get<as_inserted>(es6);
  117. i3=get<3>(es2);
  118. BOOST_TEST(i3==get<3>(es2));
  119. employee_set es7;
  120. employee_set_randomly& i5=get<randomly>(es7);
  121. i5=get<5>(es2);
  122. BOOST_TEST(i5==get<5>(es2));
  123. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)&&\
  124. !BOOST_WORKAROUND(BOOST_MSVC,==1800) /* MSVC 12.0 chokes on what follows */
  125. employee_set es8({{0,"Rose",40,4512},{1,"Mary",38,3345},{2,"Jo",25,7102}});
  126. employee_set es9;
  127. es9={{0,"Rose",40,4512},{1,"Mary",38,3345},{2,"Jo",25,7102},
  128. {0,"Rose",40,4512}};
  129. BOOST_TEST(es8.size()==3);
  130. BOOST_TEST(es9==es8);
  131. es9.clear();
  132. get<0>(es9)={{0,"Rose",40,4512},{1,"Mary",38,3345},{2,"Jo",25,7102},
  133. {0,"Rose",40,4512}};
  134. BOOST_TEST(es9==es8);
  135. es9.clear();
  136. get<0>(es9)={{0,"Rose",40,4512},{2,"Jo",25,7102},{1,"Mary",38,3345}};
  137. BOOST_TEST(es9==es8);
  138. es9.clear();
  139. get<1>(es9)={{1,"Mary",38,3345},{0,"Rose",40,4512},{2,"Jo",25,7102},
  140. {0,"Rose",40,4512}};
  141. BOOST_TEST(es9==es8);
  142. es9.clear();
  143. get<2>(es9)={{2,"Jo",25,7102},{0,"Rose",40,4512},{1,"Mary",38,3345}};
  144. BOOST_TEST(es9==es8);
  145. es9.clear();
  146. get<3>(es9)={{0,"Rose",40,4512},{1,"Mary",38,3345},{1,"Mary",38,3345},
  147. {2,"Jo",25,7102}};
  148. BOOST_TEST(es9==es8);
  149. es9.clear();
  150. get<4>(es9)={{1,"Mary",38,3345},{2,"Jo",25,7102},{0,"Rose",40,4512}};
  151. BOOST_TEST(es9==es8);
  152. es9.clear();
  153. get<5>(es9)={{1,"Mary",38,3345},{2,"Jo",25,7102},{0,"Rose",40,4512},
  154. {2,"Jo",25,7102}};
  155. BOOST_TEST(es9==es8);
  156. #endif
  157. employee_set es10(produce_employee_set()),es11(produce_employee_set());
  158. BOOST_TEST(es10==es11);
  159. employee_set es12(boost::move(es10));
  160. BOOST_TEST(es10.empty());
  161. BOOST_TEST(es11==es12);
  162. es10=boost::move(es12);
  163. BOOST_TEST(es12.empty());
  164. BOOST_TEST(es11==es10);
  165. std::list<employee> l;
  166. l.push_back(employee(3,"Anna",31,5388));
  167. l.push_back(employee(1,"Rachel",27,9012));
  168. l.push_back(employee(2,"Agatha",40,1520));
  169. employee_set es13(l.begin(),l.end());
  170. l.sort();
  171. BOOST_TEST(es13.size()==l.size()&&
  172. std::equal(es13.begin(),es13.end(),l.begin()));
  173. test_assign<multi_index_container<int,indexed_by<sequenced<> > > >();
  174. test_integral_assign<
  175. multi_index_container<int,indexed_by<sequenced<> > > >();
  176. test_assign<multi_index_container<int,indexed_by<random_access<> > > >();
  177. test_integral_assign<
  178. multi_index_container<int,indexed_by<random_access<> > > >();
  179. /* Testcase for problem described at http://www.boost.org/doc/html/move/
  180. * emulation_limitations.html#move.emulation_limitations.assignment_operator
  181. */
  182. holder h((holder()));
  183. h=holder();
  184. #if !defined(BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX)
  185. {
  186. /* testcase for https://svn.boost.org/trac10/ticket/13518 */
  187. multi_index_container<int> x={};
  188. }
  189. #endif
  190. typedef small_allocator<int> small_alloc;
  191. typedef multi_index_container<
  192. int,
  193. indexed_by<
  194. ordered_unique<identity<int> >,
  195. ranked_unique<identity<int> >,
  196. hashed_unique<identity<int> >,
  197. sequenced<>,
  198. random_access<>
  199. >,
  200. small_alloc
  201. > small_container;
  202. typedef small_container::nth_index<0>::type small_container_0;
  203. typedef small_container::nth_index<1>::type small_container_1;
  204. typedef small_container::nth_index<2>::type small_container_2;
  205. typedef small_container::nth_index<3>::type small_container_3;
  206. typedef small_container::nth_index<4>::type small_container_4;
  207. BOOST_STATIC_ASSERT((
  208. boost::is_same<small_container::size_type,small_alloc::size_type>::value));
  209. BOOST_STATIC_ASSERT((
  210. boost::is_same<
  211. small_container::difference_type,small_alloc::difference_type>::value));
  212. BOOST_STATIC_ASSERT((
  213. boost::is_same<small_container_0::size_type,small_alloc::size_type>::value));
  214. BOOST_STATIC_ASSERT((
  215. boost::is_same<
  216. small_container_0::difference_type,small_alloc::difference_type>::value));
  217. BOOST_STATIC_ASSERT((
  218. boost::is_same<small_container_1::size_type,small_alloc::size_type>::value));
  219. BOOST_STATIC_ASSERT((
  220. boost::is_same<
  221. small_container_1::difference_type,small_alloc::difference_type>::value));
  222. BOOST_STATIC_ASSERT((
  223. boost::is_same<small_container_2::size_type,small_alloc::size_type>::value));
  224. BOOST_STATIC_ASSERT((
  225. boost::is_same<
  226. small_container_2::difference_type,small_alloc::difference_type>::value));
  227. BOOST_STATIC_ASSERT((
  228. boost::is_same<small_container_3::size_type,small_alloc::size_type>::value));
  229. BOOST_STATIC_ASSERT((
  230. boost::is_same<
  231. small_container_3::difference_type,small_alloc::difference_type>::value));
  232. BOOST_STATIC_ASSERT((
  233. boost::is_same<small_container_4::size_type,small_alloc::size_type>::value));
  234. BOOST_STATIC_ASSERT((
  235. boost::is_same<
  236. small_container_4::difference_type,small_alloc::difference_type>::value));
  237. small_container sc;
  238. small_container_0& sc0=sc.get<0>();
  239. small_container_1& sc1=sc.get<1>();
  240. small_container_2& sc2=sc.get<2>();
  241. small_container_3& sc3=sc.get<3>();
  242. small_container_4& sc4=sc.get<4>();
  243. small_alloc::size_type s,
  244. ms=(small_alloc::size_type)(-1);
  245. sc.insert(0);
  246. s=sc0.size();
  247. BOOST_TEST(sc0.max_size()<=ms);
  248. s=sc0.erase(1);
  249. s=sc0.count(0);
  250. s=sc0.count(0,std::less<int>());
  251. s=sc1.size();
  252. BOOST_TEST(sc1.max_size()<=ms);
  253. s=sc1.erase(1);
  254. s=sc1.count(0);
  255. s=sc1.count(0,std::less<int>());
  256. (void)sc1.nth(s);
  257. s=sc1.rank(sc1.begin());
  258. s=sc1.find_rank(0);
  259. s=sc1.find_rank(0,std::less<int>());
  260. s=sc1.lower_bound_rank(0);
  261. s=sc1.lower_bound_rank(0,std::less<int>());
  262. s=sc1.upper_bound_rank(0);
  263. s=sc1.upper_bound_rank(0,std::less<int>());
  264. s=sc1.equal_range_rank(0).first;
  265. s=sc1.equal_range_rank(0).second;
  266. s=sc1.range_rank(unbounded,unbounded).first;
  267. s=sc1.range_rank(unbounded,unbounded).second;
  268. s=sc2.size();
  269. BOOST_TEST(sc2.max_size()<=ms);
  270. s=sc2.erase(1);
  271. s=sc2.count(0);
  272. s=sc2.count(0,boost::hash<int>(),std::equal_to<int>());
  273. s=sc2.bucket_count();
  274. BOOST_TEST(sc2.max_bucket_count()<=ms);
  275. BOOST_TEST(sc2.bucket_size((small_alloc::size_type)(0))<=ms);
  276. BOOST_TEST(sc2.bucket_size(0)<=ms);
  277. (void)sc2.begin(0);
  278. (void)sc2.end(0);
  279. (void)sc2.cbegin(0);
  280. (void)sc2.cend(0);
  281. sc2.rehash(2);
  282. sc2.reserve(2);
  283. s=sc3.size();
  284. BOOST_TEST(sc3.max_size()<=ms);
  285. sc3.resize(0);
  286. sc3.resize(0,0);
  287. sc3.assign((small_alloc::size_type)(1),0);
  288. sc3.insert(sc3.begin(),(small_alloc::size_type)(0),0);
  289. s=sc4.size();
  290. BOOST_TEST(sc4.max_size()<=ms);
  291. BOOST_TEST(sc4.capacity()<=ms);
  292. sc4.reserve(0);
  293. sc4.resize(0);
  294. sc4.resize(0,0);
  295. sc4.assign((small_alloc::size_type)(1),0);
  296. (void)sc4[0];
  297. (void)sc4.at(0);
  298. sc4.insert(sc4.begin(),(small_alloc::size_type)(0),0);
  299. }