test_bimap_serialization.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Boost.Bimap
  2. //
  3. // Copyright (c) 2006-2007 Matias Capeletto
  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. // VC++ 8.0 warns on usage of certain Standard Library and API functions that
  9. // can be cause buffer overruns or other possible security issues if misused.
  10. // See https://web.archive.org/web/20071014014301/http://msdn.microsoft.com/msdnmag/issues/05/05/SafeCandC/default.aspx
  11. // But the wording of the warning is misleading and unsettling, there are no
  12. // portable alternative functions, and VC++ 8.0's own libraries use the
  13. // functions in question. So turn off the warnings.
  14. #define _CRT_SECURE_NO_DEPRECATE
  15. #define _SCL_SECURE_NO_DEPRECATE
  16. #include <boost/config.hpp>
  17. // std
  18. #include <set>
  19. #include <map>
  20. #include <cstddef>
  21. #include <cassert>
  22. #include <algorithm>
  23. #include <sstream>
  24. #include <algorithm>
  25. // Boost.Test
  26. #include <boost/test/minimal.hpp>
  27. // Boost
  28. #include <boost/archive/text_oarchive.hpp>
  29. #include <boost/archive/text_iarchive.hpp>
  30. // Boost.Bimap
  31. #include <boost/bimap/bimap.hpp>
  32. template< class Bimap, class Archive >
  33. void save_bimap(const Bimap & b, Archive & ar)
  34. {
  35. using namespace boost::bimaps;
  36. ar << b;
  37. const typename Bimap::left_const_iterator left_iter = b.left.begin();
  38. ar << left_iter;
  39. const typename Bimap::const_iterator iter = ++b.begin();
  40. ar << iter;
  41. }
  42. void test_bimap_serialization()
  43. {
  44. using namespace boost::bimaps;
  45. typedef bimap<int,double> bm;
  46. std::set< bm::value_type > data;
  47. data.insert( bm::value_type(1,0.1) );
  48. data.insert( bm::value_type(2,0.2) );
  49. data.insert( bm::value_type(3,0.3) );
  50. data.insert( bm::value_type(4,0.4) );
  51. std::ostringstream oss;
  52. // Save it
  53. {
  54. bm b;
  55. b.insert(data.begin(),data.end());
  56. boost::archive::text_oarchive oa(oss);
  57. save_bimap(b,oa);
  58. }
  59. // Reload it
  60. {
  61. bm b;
  62. std::istringstream iss(oss.str());
  63. boost::archive::text_iarchive ia(iss);
  64. ia >> b;
  65. BOOST_CHECK( std::equal( b.begin(), b.end(), data.begin() ) );
  66. bm::left_const_iterator left_iter;
  67. ia >> left_iter;
  68. BOOST_CHECK( left_iter == b.left.begin() );
  69. bm::const_iterator iter;
  70. ia >> iter;
  71. BOOST_CHECK( iter == ++b.begin() );
  72. }
  73. }
  74. int test_main( int, char* [] )
  75. {
  76. test_bimap_serialization();
  77. return 0;
  78. }