• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
13 #include "boost/config.hpp"
14 #if !defined (BOOST_NO_STRINGSTREAM)
15 # include <sstream>
16 #endif
17 
18 #include "bitset_test.hpp"
19 #include <boost/dynamic_bitset/serialization.hpp>
20 #include <boost/config/workaround.hpp>
21 
22 
23 // Codewarrior 8.3 for Win fails without this.
24 // Thanks Howard Hinnant ;)
25 #if defined __MWERKS__ && BOOST_WORKAROUND(__MWERKS__, <= 0x3003) // 8.x
26 # pragma parse_func_templ off
27 #endif
28 
29 
30 #if defined BOOST_NO_STD_WSTRING || defined BOOST_NO_STD_LOCALE
31 # define BOOST_DYNAMIC_BITSET_NO_WCHAR_T_TESTS
32 #endif
33 
34 #include <boost/serialization/vector.hpp>
35 #include <boost/archive/binary_oarchive.hpp>
36 #include <boost/archive/binary_iarchive.hpp>
37 #include <boost/archive/xml_oarchive.hpp>
38 #include <boost/archive/xml_iarchive.hpp>
39 #include <sstream>
40 
41 namespace {
42     template <typename Block>
43         struct SerializableType {
44             boost::dynamic_bitset<Block> x;
45 
46           private:
47             friend class boost::serialization::access;
serialize__anon4b326b6b0111::SerializableType48             template <class Archive> void serialize(Archive &ar, const unsigned int) {
49                 ar & BOOST_SERIALIZATION_NVP(x);
50             }
51         };
52 
53     template <typename Block, typename IArchive, typename OArchive>
test_serialization(BOOST_EXPLICIT_TEMPLATE_TYPE (Block))54         void test_serialization( BOOST_EXPLICIT_TEMPLATE_TYPE(Block) )
55         {
56             SerializableType<Block> a;
57 
58             for (int i=0; i<128; ++i)
59                 a.x.resize(11*i, i%2);
60 
61 #if !defined (BOOST_NO_STRINGSTREAM)
62             std::stringstream ss;
63 
64             // test serialization
65             {
66                 OArchive oa(ss);
67                 oa << BOOST_SERIALIZATION_NVP(a);
68             }
69 
70             // test de-serialization
71             {
72                 IArchive ia(ss);
73                 SerializableType<Block> b;
74                 ia >> BOOST_SERIALIZATION_NVP(b);
75 
76                 assert(a.x == b.x);
77             }
78 #else
79 #           error "TODO implement file-based test path?"
80 #endif
81         }
82 
83     template <typename Block>
test_binary_archive(BOOST_EXPLICIT_TEMPLATE_TYPE (Block))84         void test_binary_archive( BOOST_EXPLICIT_TEMPLATE_TYPE(Block) ) {
85             test_serialization<Block, boost::archive::binary_iarchive, boost::archive::binary_oarchive>();
86         }
87 
88     template <typename Block>
test_xml_archive(BOOST_EXPLICIT_TEMPLATE_TYPE (Block))89         void test_xml_archive( BOOST_EXPLICIT_TEMPLATE_TYPE(Block) ) {
90             test_serialization<Block, boost::archive::xml_iarchive, boost::archive::xml_oarchive>();
91         }
92 }
93 
94 template <typename Block>
run_test_cases(BOOST_EXPLICIT_TEMPLATE_TYPE (Block))95 void run_test_cases( BOOST_EXPLICIT_TEMPLATE_TYPE(Block) )
96 {
97     test_binary_archive<Block>();
98     test_xml_archive<Block>();
99 }
100 
main()101 int main()
102 {
103     run_test_cases<unsigned char>();
104     run_test_cases<unsigned short>();
105     run_test_cases<unsigned int>();
106     run_test_cases<unsigned long>();
107 # ifdef BOOST_HAS_LONG_LONG
108     run_test_cases< ::boost::ulong_long_type>();
109 # endif
110 
111     return boost::report_errors();
112 }
113