test_set.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  2. // test_set.cpp
  3. // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
  4. // (C) Copyright 2014 Jim Bell
  5. // Use, modification and distribution is subject to the Boost Software
  6. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. // should pass compilation and execution
  9. #include <cstddef> // NULLsize_t
  10. #include <cstdio> // remove
  11. #include <fstream>
  12. #include <algorithm> // std::copy
  13. #include <vector>
  14. #include <boost/config.hpp>
  15. #if defined(BOOST_NO_STDC_NAMESPACE)
  16. namespace std{
  17. using ::size_t;
  18. }
  19. #endif
  20. #include <boost/detail/workaround.hpp>
  21. #if defined(BOOST_NO_STDC_NAMESPACE)
  22. namespace std{
  23. using ::remove;
  24. }
  25. #endif
  26. #include <boost/archive/archive_exception.hpp>
  27. #include "test_tools.hpp"
  28. #include <boost/serialization/nvp.hpp>
  29. #include <boost/serialization/set.hpp>
  30. #include "A.hpp"
  31. #include "A.ipp"
  32. void
  33. test_set(){
  34. const char * testfile = boost::archive::tmpnam(NULL);
  35. BOOST_REQUIRE(NULL != testfile);
  36. // test array of objects
  37. std::set<A> aset;
  38. aset.insert(A());
  39. aset.insert(A());
  40. const A * a_ptr = & * aset.begin();
  41. {
  42. test_ostream os(testfile, TEST_STREAM_FLAGS);
  43. test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
  44. oa << boost::serialization::make_nvp("aset", aset);
  45. // serialize a pointer into the set
  46. oa << boost::serialization::make_nvp("a_ptr", a_ptr);
  47. }
  48. std::set<A> aset1;
  49. A * a_ptr1;
  50. {
  51. test_istream is(testfile, TEST_STREAM_FLAGS);
  52. test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
  53. ia >> boost::serialization::make_nvp("aset", aset1);
  54. // deserialize a pointer into the set
  55. ia >> boost::serialization::make_nvp("a_ptr1", a_ptr1);
  56. }
  57. BOOST_CHECK_EQUAL(aset, aset1);
  58. BOOST_CHECK_EQUAL(*a_ptr1, * aset1.begin());
  59. BOOST_CHECK_EQUAL(a_ptr1, & * aset1.begin());
  60. std::remove(testfile);
  61. }
  62. void
  63. test_multiset(){
  64. const char * testfile = boost::archive::tmpnam(NULL);
  65. BOOST_REQUIRE(NULL != testfile);
  66. std::multiset<A> amultiset;
  67. amultiset.insert(A());
  68. amultiset.insert(A());
  69. const A * a_ptr = & * amultiset.begin();
  70. {
  71. test_ostream os(testfile, TEST_STREAM_FLAGS);
  72. test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
  73. oa << boost::serialization::make_nvp("amultiset", amultiset);
  74. // serialize a pointer into the set
  75. oa << boost::serialization::make_nvp("a_ptr", a_ptr);
  76. }
  77. std::multiset<A> amultiset1;
  78. A * a_ptr1;
  79. {
  80. test_istream is(testfile, TEST_STREAM_FLAGS);
  81. test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
  82. ia >> boost::serialization::make_nvp("amultiset", amultiset1);
  83. // deserialize a pointer into the set
  84. ia >> boost::serialization::make_nvp("a_ptr1", a_ptr1);
  85. }
  86. BOOST_CHECK(amultiset == amultiset1);
  87. BOOST_CHECK_EQUAL(*a_ptr1, * amultiset1.begin());
  88. BOOST_CHECK_EQUAL(a_ptr1, & * amultiset1.begin());
  89. std::remove(testfile);
  90. }
  91. int test_main( int /* argc */, char* /* argv */[] ){
  92. test_set();
  93. test_multiset();
  94. return EXIT_SUCCESS;
  95. }