test_reset_object_address.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  2. // test_reset_object_address.cpp
  3. // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
  4. // Use, modification and distribution is subject to the Boost Software
  5. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. #include <sstream>
  8. #include <cassert>
  9. #include <cstdlib> // for rand()
  10. #include <cstddef> // size_t
  11. #include <boost/config.hpp>
  12. #if defined(BOOST_NO_STDC_NAMESPACE)
  13. namespace std{
  14. using ::rand;
  15. using ::size_t;
  16. }
  17. #endif
  18. #include "test_tools.hpp"
  19. #include <boost/archive/text_oarchive.hpp>
  20. #include <boost/archive/text_iarchive.hpp>
  21. #include <boost/archive/polymorphic_text_oarchive.hpp>
  22. #include <boost/archive/polymorphic_text_iarchive.hpp>
  23. #include <boost/serialization/list.hpp>
  24. #include <boost/serialization/access.hpp>
  25. // Someday, maybe all tests will be converted to the unit test framework.
  26. // but for now use the text execution monitor to be consistent with all
  27. // the other tests.
  28. // simple test of untracked value
  29. #include "A.hpp"
  30. #include "A.ipp"
  31. void test1(){
  32. std::stringstream ss;
  33. const A a;
  34. {
  35. boost::archive::text_oarchive oa(ss);
  36. oa << a;
  37. }
  38. A a1;
  39. {
  40. boost::archive::text_iarchive ia(ss);
  41. // load to temporary
  42. A a2;
  43. ia >> a2;
  44. BOOST_CHECK_EQUAL(a, a2);
  45. // move to final destination
  46. a1 = a2;
  47. ia.reset_object_address(& a1, & a2);
  48. }
  49. BOOST_CHECK_EQUAL(a, a1);
  50. }
  51. // simple test of tracked value
  52. class B {
  53. friend class boost::serialization::access;
  54. int m_i;
  55. template<class Archive>
  56. void serialize(Archive &ar, const unsigned int /*file_version*/){
  57. ar & m_i;
  58. }
  59. public:
  60. bool operator==(const B & rhs) const {
  61. return m_i == rhs.m_i;
  62. }
  63. B() :
  64. m_i(std::rand())
  65. {}
  66. };
  67. //BOOST_TEST_DONT_PRINT_LOG_VALUE( B )
  68. void test2(){
  69. std::stringstream ss;
  70. B const b;
  71. B const * const b_ptr = & b;
  72. BOOST_CHECK_EQUAL(& b, b_ptr);
  73. {
  74. boost::archive::text_oarchive oa(ss);
  75. oa << b;
  76. oa << b_ptr;
  77. }
  78. B b1;
  79. B * b1_ptr;
  80. {
  81. boost::archive::text_iarchive ia(ss);
  82. // load to temporary
  83. B b2;
  84. ia >> b2;
  85. BOOST_CHECK_EQUAL(b, b2);
  86. // move to final destination
  87. b1 = b2;
  88. ia.reset_object_address(& b1, & b2);
  89. ia >> b1_ptr;
  90. }
  91. BOOST_CHECK_EQUAL(b, b1);
  92. BOOST_CHECK_EQUAL(& b1, b1_ptr);
  93. }
  94. // check that nested member values are properly moved
  95. class D {
  96. friend class boost::serialization::access;
  97. template<class Archive>
  98. void serialize(Archive &ar, const unsigned int /*file_version*/){
  99. ar & m_b;
  100. }
  101. public:
  102. B m_b;
  103. bool operator==(const D & rhs) const {
  104. return m_b == rhs.m_b;
  105. }
  106. D(){}
  107. };
  108. //BOOST_TEST_DONT_PRINT_LOG_VALUE( D )
  109. void test3(){
  110. std::stringstream ss;
  111. D const d;
  112. B const * const b_ptr = & d.m_b;
  113. {
  114. boost::archive::text_oarchive oa(ss);
  115. oa << d;
  116. oa << b_ptr;
  117. }
  118. D d1;
  119. B * b1_ptr;
  120. {
  121. boost::archive::text_iarchive ia(ss);
  122. D d2;
  123. ia >> d2;
  124. d1 = d2;
  125. ia.reset_object_address(& d1, & d2);
  126. ia >> b1_ptr;
  127. }
  128. BOOST_CHECK_EQUAL(d, d1);
  129. BOOST_CHECK_EQUAL(* b_ptr, * b1_ptr);
  130. }
  131. // check that data pointed to by pointer members is NOT moved
  132. class E {
  133. int m_i;
  134. friend class boost::serialization::access;
  135. template<class Archive>
  136. void serialize(Archive &ar, const unsigned int /*file_version*/){
  137. ar & m_i;
  138. }
  139. public:
  140. bool operator==(const E &rhs) const {
  141. return m_i == rhs.m_i;
  142. }
  143. E() :
  144. m_i(std::rand())
  145. {}
  146. E(const E & rhs) :
  147. m_i(rhs.m_i)
  148. {}
  149. };
  150. //BOOST_TEST_DONT_PRINT_LOG_VALUE( E )
  151. // check that moves don't move stuff pointed to
  152. class F {
  153. friend class boost::serialization::access;
  154. E * m_eptr;
  155. template<class Archive>
  156. void serialize(Archive &ar, const unsigned int /*file_version*/){
  157. ar & m_eptr;
  158. }
  159. public:
  160. bool operator==(const F &rhs) const {
  161. return *m_eptr == *rhs.m_eptr;
  162. }
  163. F & operator=(const F & rhs) {
  164. * m_eptr = * rhs.m_eptr;
  165. return *this;
  166. }
  167. F(){
  168. m_eptr = new E;
  169. }
  170. F(const F & rhs){
  171. *this = rhs;
  172. }
  173. ~F(){
  174. delete m_eptr;
  175. }
  176. };
  177. //BOOST_TEST_DONT_PRINT_LOG_VALUE( F )
  178. void test4(){
  179. std::stringstream ss;
  180. const F f;
  181. {
  182. boost::archive::text_oarchive oa(ss);
  183. oa << f;
  184. }
  185. F f1;
  186. {
  187. boost::archive::text_iarchive ia(ss);
  188. F f2;
  189. ia >> f2;
  190. f1 = f2;
  191. ia.reset_object_address(& f1, & f2);
  192. }
  193. BOOST_CHECK_EQUAL(f, f1);
  194. }
  195. // check that multiple moves keep track of the correct target
  196. class G {
  197. friend class boost::serialization::access;
  198. A m_a1;
  199. A m_a2;
  200. A *m_pa2;
  201. template<class Archive>
  202. void save(Archive &ar, const unsigned int /*file_version*/) const {
  203. ar << m_a1;
  204. ar << m_a2;
  205. ar << m_pa2;
  206. }
  207. template<class Archive>
  208. void load(Archive &ar, const unsigned int /*file_version*/){
  209. A a; // temporary A
  210. ar >> a;
  211. m_a1 = a;
  212. ar.reset_object_address(& m_a1, & a);
  213. ar >> a;
  214. m_a2 = a;
  215. ar.reset_object_address(& m_a2, & a);
  216. ar & m_pa2;
  217. }
  218. BOOST_SERIALIZATION_SPLIT_MEMBER()
  219. public:
  220. bool operator==(const G &rhs) const {
  221. return
  222. m_a1 == rhs.m_a1
  223. && m_a2 == rhs.m_a2
  224. && *m_pa2 == *rhs.m_pa2;
  225. }
  226. G & operator=(const G & rhs) {
  227. m_a1 = rhs.m_a1;
  228. m_a2 = rhs.m_a2;
  229. m_pa2 = & m_a2;
  230. return *this;
  231. }
  232. G(){
  233. m_pa2 = & m_a2;
  234. }
  235. G(const G & rhs){
  236. *this = rhs;
  237. }
  238. ~G(){}
  239. };
  240. //BOOST_TEST_DONT_PRINT_LOG_VALUE( G )
  241. void test5(){
  242. std::stringstream ss;
  243. const G g;
  244. {
  245. boost::archive::text_oarchive oa(ss);
  246. oa << g;
  247. }
  248. G g1;
  249. {
  250. boost::archive::text_iarchive ia(ss);
  251. ia >> g1;
  252. }
  253. BOOST_CHECK_EQUAL(g, g1);
  254. }
  255. // joaquin's test - this tests the case where rest_object_address
  256. // is applied to an item which in fact is not tracked so that
  257. // the call is in fact superfluous.
  258. struct foo
  259. {
  260. int x;
  261. private:
  262. friend class boost::serialization::access;
  263. template<class Archive>
  264. void serialize(Archive &,const unsigned int)
  265. {
  266. }
  267. };
  268. struct bar
  269. {
  270. foo f[2];
  271. foo* pf[2];
  272. private:
  273. friend class boost::serialization::access;
  274. BOOST_SERIALIZATION_SPLIT_MEMBER()
  275. template<class Archive>
  276. void save(Archive& ar,const unsigned int)const
  277. {
  278. for(int i=0;i<2;++i){
  279. ar<<f[i].x;
  280. ar<<f[i];
  281. }
  282. for(int j=0;j<2;++j){
  283. ar<<pf[j];
  284. }
  285. }
  286. template<class Archive>
  287. void load(Archive& ar,const unsigned int)
  288. {
  289. for(int i=0;i<2;++i){
  290. int x;
  291. ar>>x;
  292. f[i].x=x;
  293. ar.reset_object_address(&f[i].x,&x);
  294. ar>>f[i];
  295. }
  296. for(int j=0;j<2;++j){
  297. ar>>pf[j];
  298. }
  299. }
  300. };
  301. int test6()
  302. {
  303. bar b;
  304. b.f[0].x=0;
  305. b.f[1].x=1;
  306. b.pf[0]=&b.f[0];
  307. b.pf[1]=&b.f[1];
  308. std::ostringstream oss;
  309. {
  310. boost::archive::text_oarchive oa(oss);
  311. oa<<const_cast<const bar&>(b);
  312. }
  313. bar b1;
  314. b1.pf[0]=0;
  315. b1.pf[1]=0;
  316. std::istringstream iss(oss.str());
  317. boost::archive::text_iarchive ia(iss);
  318. ia>>b1;
  319. BOOST_CHECK(b1.pf[0]==&b1.f[0]&&b1.pf[1]==&b1.f[1]);
  320. return 0;
  321. }
  322. // test one of the collections
  323. void test7(){
  324. std::stringstream ss;
  325. B const b;
  326. B const * const b_ptr = & b;
  327. BOOST_CHECK_EQUAL(& b, b_ptr);
  328. {
  329. std::list<const B *> l;
  330. l.push_back(b_ptr);
  331. boost::archive::text_oarchive oa(ss);
  332. oa << const_cast<const std::list<const B *> &>(l);
  333. }
  334. B b1;
  335. {
  336. std::list<B *> l;
  337. boost::archive::text_iarchive ia(ss);
  338. ia >> l;
  339. delete l.front(); // prevent memory leak
  340. }
  341. }
  342. // test one of the collections with polymorphic archive
  343. void test8(){
  344. std::stringstream ss;
  345. B const b;
  346. B const * const b_ptr = & b;
  347. BOOST_CHECK_EQUAL(& b, b_ptr);
  348. {
  349. std::list<const B *> l;
  350. l.push_back(b_ptr);
  351. boost::archive::polymorphic_text_oarchive oa(ss);
  352. boost::archive::polymorphic_oarchive & poa = oa;
  353. poa << const_cast<const std::list<const B *> &>(l);
  354. }
  355. B b1;
  356. {
  357. std::list<B *> l;
  358. boost::archive::polymorphic_text_iarchive ia(ss);
  359. boost::archive::polymorphic_iarchive & pia = ia;
  360. pia >> l;
  361. delete l.front(); // prevent memory leak
  362. }
  363. }
  364. int test_main(int /* argc */, char * /* argv */[])
  365. {
  366. test1();
  367. test2();
  368. test3();
  369. test4();
  370. test5();
  371. test6();
  372. test7();
  373. test8();
  374. return EXIT_SUCCESS;
  375. }