test_map_boost_unordered.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. #include <boost/serialization/boost_unordered_map.hpp>
  52. #include <functional> // requires changeset [69520]; Ticket #5254
  53. namespace boost {
  54. template<>
  55. struct hash<random_key>{
  56. std::size_t operator()(const random_key& r) const {
  57. return static_cast<std::size_t>(r);
  58. }
  59. };
  60. } // namespace std
  61. void
  62. test_unordered_map(){
  63. const char * testfile = boost::archive::tmpnam(NULL);
  64. BOOST_REQUIRE(NULL != testfile);
  65. BOOST_CHECKPOINT("unordered_map");
  66. // test unordered_map of objects
  67. boost::unordered_map<random_key, A> anunordered_map;
  68. anunordered_map.insert(std::make_pair(random_key(), A()));
  69. anunordered_map.insert(std::make_pair(random_key(), A()));
  70. {
  71. test_ostream os(testfile, TEST_STREAM_FLAGS);
  72. test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
  73. oa << boost::serialization::make_nvp("anunorderedmap",anunordered_map);
  74. }
  75. boost::unordered_map<random_key, A> anunordered_map1;
  76. {
  77. test_istream is(testfile, TEST_STREAM_FLAGS);
  78. test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
  79. ia >> boost::serialization::make_nvp("anunorderedmap",anunordered_map1);
  80. }
  81. std::vector< std::pair<random_key, A> > tvec, tvec1;
  82. std::copy(anunordered_map.begin(), anunordered_map.end(), std::back_inserter(tvec));
  83. std::sort(tvec.begin(), tvec.end());
  84. std::copy(anunordered_map1.begin(), anunordered_map1.end(), std::back_inserter(tvec1));
  85. std::sort(tvec1.begin(), tvec1.end());
  86. BOOST_CHECK(tvec == tvec1);
  87. std::remove(testfile);
  88. }
  89. void
  90. test_unordered_multimap(){
  91. const char * testfile = boost::archive::tmpnam(NULL);
  92. BOOST_REQUIRE(NULL != testfile);
  93. BOOST_CHECKPOINT("unordered_multimap");
  94. boost::unordered_multimap<random_key, A> anunordered_multimap;
  95. anunordered_multimap.insert(std::make_pair(random_key(), A()));
  96. anunordered_multimap.insert(std::make_pair(random_key(), A()));
  97. {
  98. test_ostream os(testfile, TEST_STREAM_FLAGS);
  99. test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
  100. oa << boost::serialization::make_nvp("anunordered_multimap", anunordered_multimap);
  101. }
  102. boost::unordered_multimap<random_key, A> anunordered_multimap1;
  103. {
  104. test_istream is(testfile, TEST_STREAM_FLAGS);
  105. test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
  106. ia >> boost::serialization::make_nvp("anunordered_multimap", anunordered_multimap1);
  107. }
  108. std::vector< std::pair<random_key, A> > tvec, tvec1;
  109. tvec.clear();
  110. tvec1.clear();
  111. std::copy(anunordered_multimap.begin(), anunordered_multimap.end(), std::back_inserter(tvec));
  112. std::sort(tvec.begin(), tvec.end());
  113. std::copy(anunordered_multimap1.begin(), anunordered_multimap1.end(), std::back_inserter(tvec1));
  114. std::sort(tvec1.begin(), tvec1.end());
  115. BOOST_CHECK(tvec == tvec1);
  116. std::remove(testfile);
  117. }
  118. int test_main( int /* argc */, char* /* argv */[] )
  119. {
  120. test_unordered_map();
  121. test_unordered_multimap();
  122. return EXIT_SUCCESS;
  123. }