test_serialization3.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /* Boost.MultiIndex test for serialization, part 3.
  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_serialization3.hpp"
  11. #include "test_serialization_template.hpp"
  12. #include <boost/move/core.hpp>
  13. #include <boost/multi_index/hashed_index.hpp>
  14. #include <boost/multi_index/sequenced_index.hpp>
  15. #include <boost/multi_index/ordered_index.hpp>
  16. #include <boost/multi_index/key_extractors.hpp>
  17. #include "non_std_allocator.hpp"
  18. struct non_default_ctble
  19. {
  20. non_default_ctble(int n_):n(n_){}
  21. bool operator==(const non_default_ctble& x)const{return n==x.n;}
  22. int n;
  23. };
  24. #if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
  25. namespace boost{
  26. namespace serialization{
  27. #endif
  28. template<class Archive>
  29. void save_construct_data(
  30. Archive& ar,const non_default_ctble* p,const unsigned int version)
  31. {
  32. if(version<3)return;
  33. ar<<boost::serialization::make_nvp("n",p->n);
  34. }
  35. template<class Archive>
  36. void load_construct_data(
  37. Archive& ar,non_default_ctble* p,const unsigned int version)
  38. {
  39. if(version<3)return;
  40. int n=0;
  41. ar>>boost::serialization::make_nvp("n",n);
  42. ::new(p)non_default_ctble(n);
  43. }
  44. template<class Archive>
  45. void serialize(Archive&,non_default_ctble&,const unsigned int)
  46. {
  47. }
  48. #if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
  49. } /* namespace serialization */
  50. } /* namespace boost*/
  51. #endif
  52. namespace boost{
  53. namespace serialization{
  54. template<> struct version<non_default_ctble>
  55. {
  56. BOOST_STATIC_CONSTANT(int,value=3);
  57. };
  58. } /* namespace serialization */
  59. } /* namespace boost*/
  60. struct non_copyable
  61. {
  62. non_copyable(int n_=0):n(n_){}
  63. non_copyable(BOOST_RV_REF(non_copyable) x):n(x.n){}
  64. bool operator==(const non_copyable& x)const{return n==x.n;}
  65. bool operator<(const non_copyable& x)const{return n<x.n;}
  66. int n;
  67. private:
  68. BOOST_MOVABLE_BUT_NOT_COPYABLE(non_copyable)
  69. };
  70. #if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
  71. namespace boost{
  72. namespace serialization{
  73. #endif
  74. template<class Archive>
  75. void serialize(Archive& ar,non_copyable& x,const unsigned int)
  76. {
  77. ar&boost::serialization::make_nvp("n",x.n);
  78. }
  79. #if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
  80. } /* namespace serialization */
  81. } /* namespace boost*/
  82. #endif
  83. using namespace boost::multi_index;
  84. void test_serialization3()
  85. {
  86. const int N=100;
  87. const int SHUFFLE=10232;
  88. typedef multi_index_container<
  89. int,
  90. indexed_by<
  91. hashed_unique<identity<int> >,
  92. sequenced<>
  93. >,
  94. non_std_allocator<int>
  95. > hashed_set;
  96. typedef hashed_set::iterator iterator;
  97. typedef hashed_set::local_iterator local_iterator;
  98. hashed_set hs;
  99. for(int i=0;i<N;++i){
  100. hs.insert(i*SHUFFLE);
  101. }
  102. std::ostringstream oss;
  103. {
  104. boost::archive::text_oarchive oa(oss);
  105. oa<<const_cast<const hashed_set&>(hs);
  106. std::vector<iterator> its(N);
  107. for(int i=0;i<N;++i){
  108. iterator it=hs.find(i*SHUFFLE);
  109. its.push_back(it);
  110. oa<<const_cast<const iterator&>(its.back());
  111. }
  112. iterator it=hs.end();
  113. oa<<const_cast<const iterator&>(it);
  114. std::vector<local_iterator> lits(2*N);
  115. for(std::size_t buc=0;buc<hs.bucket_count();++buc){
  116. for(local_iterator lit=hs.begin(buc),lit_end=hs.end(buc);
  117. lit!=lit_end;++lit){
  118. oa<<*lit;
  119. lits.push_back(lit);
  120. oa<<const_cast<const local_iterator&>(lits.back());
  121. }
  122. local_iterator lit2=hs.end(buc);
  123. lits.push_back(lit2);
  124. oa<<const_cast<const local_iterator&>(lits.back());
  125. }
  126. }
  127. hashed_set hs2;
  128. std::istringstream iss(oss.str());
  129. boost::archive::text_iarchive ia(iss);
  130. ia>>hs2;
  131. BOOST_TEST(boost::multi_index::get<1>(hs)==boost::multi_index::get<1>(hs2));
  132. for(int j=0;j<N;++j){
  133. iterator it;
  134. ia>>it;
  135. BOOST_TEST(*it==j*SHUFFLE);
  136. }
  137. iterator it;
  138. ia>>it;
  139. BOOST_TEST(it==hs2.end());
  140. for(std::size_t buc=0;buc<hs2.bucket_count();++buc){
  141. for(std::size_t k=0;k<hs2.bucket_size(buc);++k){
  142. int n;
  143. local_iterator it_;
  144. ia>>n;
  145. ia>>it_;
  146. BOOST_TEST(*it_==n);
  147. }
  148. local_iterator it2;
  149. ia>>it2;
  150. BOOST_TEST(it2==hs2.end(buc));
  151. }
  152. {
  153. typedef multi_index_container<
  154. non_default_ctble,
  155. indexed_by<
  156. ordered_unique<
  157. BOOST_MULTI_INDEX_MEMBER(non_default_ctble,int,n)
  158. >
  159. >
  160. > multi_index_t;
  161. multi_index_t m;
  162. for(int i=0;i<100;++i)m.insert(non_default_ctble(i));
  163. test_serialization(m);
  164. }
  165. {
  166. /* testcase for https://svn.boost.org/trac10/ticket/13478 */
  167. typedef multi_index_container<
  168. non_copyable,
  169. indexed_by<
  170. ordered_unique<
  171. BOOST_MULTI_INDEX_MEMBER(non_copyable,int,n)
  172. >
  173. >
  174. > multi_index_t;
  175. multi_index_t m;
  176. for(int i=0;i<100;++i)m.emplace(i);
  177. test_serialization(m);
  178. }
  179. }