test_map_hashed.cpp 4.3 KB

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