test_set_hashed.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. void
  33. test_set(){
  34. const char * testfile = boost::archive::tmpnam(NULL);
  35. BOOST_REQUIRE(NULL != testfile);
  36. // test array of objects
  37. std::set<A> aset;
  38. aset.insert(A());
  39. aset.insert(A());
  40. {
  41. test_ostream os(testfile, TEST_STREAM_FLAGS);
  42. test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
  43. oa << boost::serialization::make_nvp("aset", aset);
  44. }
  45. std::set<A> aset1;
  46. {
  47. test_istream is(testfile, TEST_STREAM_FLAGS);
  48. test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
  49. ia >> boost::serialization::make_nvp("aset", aset1);
  50. }
  51. BOOST_CHECK(aset == aset1);
  52. std::remove(testfile);
  53. }
  54. void
  55. test_multiset(){
  56. const char * testfile = boost::archive::tmpnam(NULL);
  57. BOOST_REQUIRE(NULL != testfile);
  58. std::multiset<A> amultiset;
  59. amultiset.insert(A());
  60. amultiset.insert(A());
  61. {
  62. test_ostream os(testfile, TEST_STREAM_FLAGS);
  63. test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
  64. oa << boost::serialization::make_nvp("amultiset", amultiset);
  65. }
  66. std::multiset<A> amultiset1;
  67. {
  68. test_istream is(testfile, TEST_STREAM_FLAGS);
  69. test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
  70. ia >> boost::serialization::make_nvp("amultiset", amultiset1);
  71. }
  72. BOOST_CHECK(amultiset == amultiset1);
  73. std::remove(testfile);
  74. }
  75. #ifdef BOOST_HAS_HASH
  76. #include <boost/serialization/hash_set.hpp>
  77. namespace BOOST_STD_EXTENSION_NAMESPACE {
  78. template<>
  79. struct hash<A> {
  80. std::size_t operator()(const A& a) const {
  81. return static_cast<std::size_t>(a);
  82. }
  83. };
  84. }
  85. void
  86. test_hash_set(){
  87. const char * testfile = boost::archive::tmpnam(NULL);
  88. BOOST_REQUIRE(NULL != testfile);
  89. // test array of objects
  90. BOOST_STD_EXTENSION_NAMESPACE::hash_set<A> ahash_set;
  91. A a, a1;
  92. ahash_set.insert(a);
  93. ahash_set.insert(a1);
  94. {
  95. test_ostream os(testfile, TEST_STREAM_FLAGS);
  96. test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
  97. oa << boost::serialization::make_nvp("ahash_set", ahash_set);
  98. }
  99. BOOST_STD_EXTENSION_NAMESPACE::hash_set<A> ahash_set1;
  100. {
  101. test_istream is(testfile, TEST_STREAM_FLAGS);
  102. test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
  103. ia >> boost::serialization::make_nvp("ahash_set", ahash_set1);
  104. }
  105. std::vector<A> tvec, tvec1;
  106. tvec.clear();
  107. tvec1.clear();
  108. std::copy(ahash_set.begin(), ahash_set.end(), std::back_inserter(tvec));
  109. std::sort(tvec.begin(), tvec.end());
  110. std::copy(ahash_set1.begin(), ahash_set1.end(), std::back_inserter(tvec1));
  111. std::sort(tvec1.begin(), tvec1.end());
  112. BOOST_CHECK(tvec == tvec1);
  113. std::remove(testfile);
  114. }
  115. void
  116. test_hash_multiset(){
  117. const char * testfile = boost::archive::tmpnam(NULL);
  118. BOOST_REQUIRE(NULL != testfile);
  119. BOOST_STD_EXTENSION_NAMESPACE::hash_multiset<A> ahash_multiset;
  120. ahash_multiset.insert(A());
  121. ahash_multiset.insert(A());
  122. {
  123. test_ostream os(testfile, TEST_STREAM_FLAGS);
  124. test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
  125. oa << boost::serialization::make_nvp("ahash_multiset", ahash_multiset);
  126. }
  127. BOOST_STD_EXTENSION_NAMESPACE::hash_multiset<A> ahash_multiset1;
  128. {
  129. test_istream is(testfile, TEST_STREAM_FLAGS);
  130. test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
  131. ia >> boost::serialization::make_nvp("ahash_multiset", ahash_multiset1);
  132. }
  133. std::vector<A> tvec, tvec1;
  134. tvec.clear();
  135. tvec1.clear();
  136. std::copy(ahash_multiset.begin(), ahash_multiset.end(), std::back_inserter(tvec));
  137. std::sort(tvec.begin(), tvec.end());
  138. std::copy(ahash_multiset1.begin(), ahash_multiset1.end(), std::back_inserter(tvec1));
  139. std::sort(tvec1.begin(), tvec1.end());
  140. BOOST_CHECK(tvec == tvec1);
  141. std::remove(testfile);
  142. }
  143. #endif
  144. #ifndef BOOST_NO_CXX11_HDR_UNORDERED_SET
  145. #include <boost/serialization/unordered_set.hpp>
  146. #include <functional> // requires changeset [69520]; Ticket #5254
  147. namespace std {
  148. template<>
  149. struct hash<A> {
  150. std::size_t operator()(const A& a) const {
  151. return static_cast<std::size_t>(a);
  152. }
  153. };
  154. } // namespace std
  155. void
  156. test_unordered_set(){
  157. const char * testfile = boost::archive::tmpnam(NULL);
  158. BOOST_REQUIRE(NULL != testfile);
  159. // test array of objects
  160. std::unordered_set<A> anunordered_set;
  161. A a, a1;
  162. anunordered_set.insert(a);
  163. anunordered_set.insert(a1);
  164. {
  165. test_ostream os(testfile, TEST_STREAM_FLAGS);
  166. test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
  167. oa << boost::serialization::make_nvp("anunordered_set", anunordered_set);
  168. }
  169. std::unordered_set<A> anunordered_set1;
  170. {
  171. test_istream is(testfile, TEST_STREAM_FLAGS);
  172. test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
  173. ia >> boost::serialization::make_nvp("anunordered_set", anunordered_set1);
  174. }
  175. std::vector<A> tvec, tvec1;
  176. tvec.clear();
  177. tvec1.clear();
  178. std::copy(anunordered_set.begin(), anunordered_set.end(), std::back_inserter(tvec));
  179. std::sort(tvec.begin(), tvec.end());
  180. std::copy(anunordered_set1.begin(), anunordered_set1.end(), std::back_inserter(tvec1));
  181. std::sort(tvec1.begin(), tvec1.end());
  182. BOOST_CHECK(tvec == tvec1);
  183. std::remove(testfile);
  184. }
  185. void
  186. test_unordered_multiset(){
  187. const char * testfile = boost::archive::tmpnam(NULL);
  188. BOOST_REQUIRE(NULL != testfile);
  189. std::unordered_multiset<A> anunordered_multiset;
  190. anunordered_multiset.insert(A());
  191. anunordered_multiset.insert(A());
  192. {
  193. test_ostream os(testfile, TEST_STREAM_FLAGS);
  194. test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
  195. oa << boost::serialization::make_nvp("anunordered_multiset", anunordered_multiset);
  196. }
  197. std::unordered_multiset<A> anunordered_multiset1;
  198. {
  199. test_istream is(testfile, TEST_STREAM_FLAGS);
  200. test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
  201. ia >> boost::serialization::make_nvp("anunordered_multiset", anunordered_multiset1);
  202. }
  203. std::vector<A> tvec, tvec1;
  204. tvec.clear();
  205. tvec1.clear();
  206. std::copy(anunordered_multiset.begin(), anunordered_multiset.end(), std::back_inserter(tvec));
  207. std::sort(tvec.begin(), tvec.end());
  208. std::copy(anunordered_multiset1.begin(), anunordered_multiset1.end(), std::back_inserter(tvec1));
  209. std::sort(tvec1.begin(), tvec1.end());
  210. BOOST_CHECK(tvec == tvec1);
  211. std::remove(testfile);
  212. }
  213. #endif
  214. int test_main( int /* argc */, char* /* argv */[] ){
  215. test_set();
  216. test_multiset();
  217. #ifdef BOOST_HAS_HASH
  218. test_hash_set();
  219. test_hash_multiset();
  220. #endif
  221. #ifndef BOOST_NO_CXX11_HDR_UNORDERED_SET
  222. test_unordered_set();
  223. test_unordered_multiset();
  224. #endif
  225. return EXIT_SUCCESS;
  226. }