dyn_bitset_unit_tests5.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // -----------------------------------------------------------
  2. // Copyright (c) 2001 Jeremy Siek
  3. // Copyright (c) 2003-2006 Gennaro Prota
  4. //
  5. // Copyright (c) 2015 Seth Heeren
  6. //
  7. // Distributed under the Boost Software License, Version 1.0.
  8. // (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. // -----------------------------------------------------------
  12. #include "boost/config.hpp"
  13. #if !defined (BOOST_NO_STRINGSTREAM)
  14. # include <sstream>
  15. #endif
  16. #include "bitset_test.hpp"
  17. #include <boost/dynamic_bitset/serialization.hpp>
  18. #include <boost/config/workaround.hpp>
  19. // Codewarrior 8.3 for Win fails without this.
  20. // Thanks Howard Hinnant ;)
  21. #if defined __MWERKS__ && BOOST_WORKAROUND(__MWERKS__, <= 0x3003) // 8.x
  22. # pragma parse_func_templ off
  23. #endif
  24. #if defined BOOST_NO_STD_WSTRING || defined BOOST_NO_STD_LOCALE
  25. # define BOOST_DYNAMIC_BITSET_NO_WCHAR_T_TESTS
  26. #endif
  27. #include <boost/archive/binary_oarchive.hpp>
  28. #include <boost/archive/binary_iarchive.hpp>
  29. #include <boost/archive/xml_oarchive.hpp>
  30. #include <boost/archive/xml_iarchive.hpp>
  31. #include <sstream>
  32. namespace {
  33. template <typename Block>
  34. struct SerializableType {
  35. boost::dynamic_bitset<Block> x;
  36. private:
  37. friend class boost::serialization::access;
  38. template <class Archive> void serialize(Archive &ar, const unsigned int) {
  39. ar & BOOST_SERIALIZATION_NVP(x);
  40. }
  41. };
  42. template <typename Block, typename IArchive, typename OArchive>
  43. void test_serialization( BOOST_EXPLICIT_TEMPLATE_TYPE(Block) )
  44. {
  45. SerializableType<Block> a;
  46. for (int i=0; i<128; ++i)
  47. a.x.resize(11*i, i%2);
  48. #if !defined (BOOST_NO_STRINGSTREAM)
  49. std::stringstream ss;
  50. // test serialization
  51. {
  52. OArchive oa(ss);
  53. oa << BOOST_SERIALIZATION_NVP(a);
  54. }
  55. // test de-serialization
  56. {
  57. IArchive ia(ss);
  58. SerializableType<Block> b;
  59. ia >> BOOST_SERIALIZATION_NVP(b);
  60. assert(a.x == b.x);
  61. }
  62. #else
  63. # error "TODO implement file-based test path?"
  64. #endif
  65. }
  66. template <typename Block>
  67. void test_binary_archive( BOOST_EXPLICIT_TEMPLATE_TYPE(Block) ) {
  68. test_serialization<Block, boost::archive::binary_iarchive, boost::archive::binary_oarchive>();
  69. }
  70. template <typename Block>
  71. void test_xml_archive( BOOST_EXPLICIT_TEMPLATE_TYPE(Block) ) {
  72. test_serialization<Block, boost::archive::xml_iarchive, boost::archive::xml_oarchive>();
  73. }
  74. }
  75. template <typename Block>
  76. void run_test_cases( BOOST_EXPLICIT_TEMPLATE_TYPE(Block) )
  77. {
  78. test_binary_archive<Block>();
  79. test_xml_archive<Block>();
  80. }
  81. int main()
  82. {
  83. run_test_cases<unsigned char>();
  84. run_test_cases<unsigned short>();
  85. run_test_cases<unsigned int>();
  86. run_test_cases<unsigned long>();
  87. # ifdef BOOST_HAS_LONG_LONG
  88. run_test_cases< ::boost::ulong_long_type>();
  89. # endif
  90. return boost::report_errors();
  91. }