test_map.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  2. // test_map.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 <algorithm> // std::copy
  10. #include <vector>
  11. #include <fstream>
  12. #include <cstddef> // size_t, NULL
  13. #include <boost/config.hpp>
  14. #include <boost/detail/workaround.hpp>
  15. #include <cstdio>
  16. #if defined(BOOST_NO_STDC_NAMESPACE)
  17. namespace std{
  18. using ::rand;
  19. using ::size_t;
  20. }
  21. #endif
  22. #include "test_tools.hpp"
  23. #include <boost/serialization/nvp.hpp>
  24. #include <boost/serialization/map.hpp>
  25. #include "A.hpp"
  26. #include "A.ipp"
  27. ///////////////////////////////////////////////////////
  28. // a key value initialized with a random value for use
  29. // in testing STL map serialization
  30. struct random_key {
  31. friend class boost::serialization::access;
  32. template<class Archive>
  33. void serialize(
  34. Archive & ar,
  35. const unsigned int /* file_version */
  36. ){
  37. ar & boost::serialization::make_nvp("random_key", m_i);
  38. }
  39. int m_i;
  40. random_key() : m_i(std::rand()){};
  41. bool operator<(const random_key &rhs) const {
  42. return m_i < rhs.m_i;
  43. }
  44. bool operator==(const random_key &rhs) const {
  45. return m_i == rhs.m_i;
  46. }
  47. operator std::size_t () const { // required by hash_map
  48. return m_i;
  49. }
  50. };
  51. void
  52. test_map(){
  53. const char * testfile = boost::archive::tmpnam(NULL);
  54. BOOST_REQUIRE(NULL != testfile);
  55. BOOST_MESSAGE("map");
  56. // test map of objects
  57. std::map<random_key, A> amap;
  58. amap.insert(std::make_pair(random_key(), A()));
  59. amap.insert(std::make_pair(random_key(), A()));
  60. {
  61. test_ostream os(testfile, TEST_STREAM_FLAGS);
  62. test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
  63. oa << boost::serialization::make_nvp("amap", amap);
  64. }
  65. std::map<random_key, A> amap1;
  66. {
  67. test_istream is(testfile, TEST_STREAM_FLAGS);
  68. test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
  69. ia >> boost::serialization::make_nvp("amap", amap1);
  70. }
  71. BOOST_CHECK(amap == amap1);
  72. std::remove(testfile);
  73. }
  74. void
  75. test_map_2(){
  76. const char * testfile = boost::archive::tmpnam(NULL);
  77. BOOST_REQUIRE(NULL != testfile);
  78. BOOST_MESSAGE("map_2");
  79. std::pair<int, int> a(11, 22);
  80. std::map<int, int> b;
  81. b[0] = 0;
  82. b[-1] = -1;
  83. b[1] = 1;
  84. {
  85. test_ostream os(testfile, TEST_STREAM_FLAGS);
  86. std::pair<int, int> * const pa = &a;
  87. std::map<int, int> * const pb = &b;
  88. test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
  89. oa << BOOST_SERIALIZATION_NVP(pb);
  90. oa << BOOST_SERIALIZATION_NVP(pa);
  91. }
  92. {
  93. test_istream is(testfile, TEST_STREAM_FLAGS);
  94. std::pair<int, int> *pa = 0;
  95. std::map<int, int> *pb = 0;
  96. test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
  97. ia >> BOOST_SERIALIZATION_NVP(pb);
  98. ia >> BOOST_SERIALIZATION_NVP(pa);
  99. delete pa;
  100. delete pb;
  101. }
  102. std::remove(testfile);
  103. }
  104. void
  105. test_multimap(){
  106. const char * testfile = boost::archive::tmpnam(NULL);
  107. BOOST_REQUIRE(NULL != testfile);
  108. BOOST_MESSAGE("multimap");
  109. std::multimap<random_key, A> amultimap;
  110. amultimap.insert(std::make_pair(random_key(), A()));
  111. amultimap.insert(std::make_pair(random_key(), A()));
  112. {
  113. test_ostream os(testfile, TEST_STREAM_FLAGS);
  114. test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
  115. oa << boost::serialization::make_nvp("amultimap", amultimap);
  116. }
  117. std::multimap<random_key, A> amultimap1;
  118. {
  119. test_istream is(testfile, TEST_STREAM_FLAGS);
  120. test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
  121. ia >> boost::serialization::make_nvp("amultimap", amultimap1);
  122. }
  123. BOOST_CHECK(amultimap == amultimap1);
  124. std::remove(testfile);
  125. }
  126. int test_main( int /* argc */, char* /* argv */[] )
  127. {
  128. test_map();
  129. test_map_2();
  130. test_multimap();
  131. return EXIT_SUCCESS;
  132. }