test_set_unordered.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #include <boost/serialization/unordered_set.hpp>
  33. #include <functional> // requires changeset [69520]; Ticket #5254
  34. namespace std {
  35. template<>
  36. struct hash<A> {
  37. std::size_t operator()(const A& a) const {
  38. return static_cast<std::size_t>(a);
  39. }
  40. };
  41. } // namespace std
  42. void
  43. test_unordered_set(){
  44. const char * testfile = boost::archive::tmpnam(NULL);
  45. BOOST_REQUIRE(NULL != testfile);
  46. // test array of objects
  47. std::unordered_set<A> anunordered_set;
  48. A a, a1;
  49. anunordered_set.insert(a);
  50. anunordered_set.insert(a1);
  51. {
  52. test_ostream os(testfile, TEST_STREAM_FLAGS);
  53. test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
  54. oa << boost::serialization::make_nvp("anunordered_set", anunordered_set);
  55. }
  56. std::unordered_set<A> anunordered_set1;
  57. {
  58. test_istream is(testfile, TEST_STREAM_FLAGS);
  59. test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
  60. ia >> boost::serialization::make_nvp("anunordered_set", anunordered_set1);
  61. }
  62. std::vector<A> tvec, tvec1;
  63. tvec.clear();
  64. tvec1.clear();
  65. std::copy(anunordered_set.begin(), anunordered_set.end(), std::back_inserter(tvec));
  66. std::sort(tvec.begin(), tvec.end());
  67. std::copy(anunordered_set1.begin(), anunordered_set1.end(), std::back_inserter(tvec1));
  68. std::sort(tvec1.begin(), tvec1.end());
  69. BOOST_CHECK(tvec == tvec1);
  70. std::remove(testfile);
  71. }
  72. void
  73. test_unordered_multiset(){
  74. const char * testfile = boost::archive::tmpnam(NULL);
  75. BOOST_REQUIRE(NULL != testfile);
  76. std::unordered_multiset<A> anunordered_multiset;
  77. anunordered_multiset.insert(A());
  78. anunordered_multiset.insert(A());
  79. {
  80. test_ostream os(testfile, TEST_STREAM_FLAGS);
  81. test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
  82. oa << boost::serialization::make_nvp("anunordered_multiset", anunordered_multiset);
  83. }
  84. std::unordered_multiset<A> anunordered_multiset1;
  85. {
  86. test_istream is(testfile, TEST_STREAM_FLAGS);
  87. test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
  88. ia >> boost::serialization::make_nvp("anunordered_multiset", anunordered_multiset1);
  89. }
  90. std::vector<A> tvec, tvec1;
  91. tvec.clear();
  92. tvec1.clear();
  93. std::copy(anunordered_multiset.begin(), anunordered_multiset.end(), std::back_inserter(tvec));
  94. std::sort(tvec.begin(), tvec.end());
  95. std::copy(anunordered_multiset1.begin(), anunordered_multiset1.end(), std::back_inserter(tvec1));
  96. std::sort(tvec1.begin(), tvec1.end());
  97. BOOST_CHECK(tvec == tvec1);
  98. std::remove(testfile);
  99. }
  100. int test_main( int /* argc */, char* /* argv */[] ){
  101. test_unordered_set();
  102. test_unordered_multiset();
  103. return EXIT_SUCCESS;
  104. }