]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/serialization/test/test_variant.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / serialization / test / test_variant.cpp
1 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
2 // test_variant.cpp
3 // test of non-intrusive serialization of variant types
4 //
5 // copyright (c) 2005
6 // troy d. straszheim <troy@resophonic.com>
7 // http://www.resophonic.com
8 //
9 // Use, modification and distribution is subject to the Boost Software
10 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
11 // http://www.boost.org/LICENSE_1_0.txt)
12 //
13 // See http://www.boost.org for updates, documentation, and revision history.
14 //
15 // thanks to Robert Ramey and Peter Dimov.
16 //
17
18 #include <cstddef> // NULL
19 #include <cstdio> // remove
20 #include <fstream>
21 #include <boost/config.hpp>
22 #include <boost/math/special_functions/next.hpp>
23 #if defined(BOOST_NO_STDC_NAMESPACE)
24 namespace std{
25 using ::remove;
26 }
27 #endif
28
29 #include <boost/type_traits/is_same.hpp>
30 #include <boost/mpl/eval_if.hpp>
31 #include <boost/mpl/identity.hpp>
32 #include <boost/serialization/throw_exception.hpp>
33
34 #if defined(_MSC_VER) && (_MSC_VER <= 1020)
35 # pragma warning (disable : 4786) // too long name, harmless warning
36 #endif
37
38 #include "test_tools.hpp"
39
40 #include <boost/archive/archive_exception.hpp>
41
42 #include <boost/serialization/nvp.hpp>
43 #include <boost/serialization/variant.hpp>
44
45 #include "A.hpp"
46 #include "A.ipp"
47
48 class are_equal
49 : public boost::static_visitor<bool>
50 {
51 public:
52 // note extra rigamorole for compilers which don't support
53 // partial function template ordering - specfically msvc 6.x
54 struct same {
55 template<class T, class U>
56 static bool invoke(const T & t, const U & u){
57 return t == u;
58 }
59 };
60
61 struct not_same {
62 template<class T, class U>
63 static bool invoke(const T &, const U &){
64 return false;
65 }
66 };
67
68 template <class T, class U>
69 bool operator()( const T & t, const U & u) const
70 {
71 typedef typename boost::mpl::eval_if<boost::is_same<T, U>,
72 boost::mpl::identity<same>,
73 boost::mpl::identity<not_same>
74 >::type type;
75 return type::invoke(t, u);
76 }
77
78 bool operator()( const float & lhs, const float & rhs ) const
79 {
80 return std::abs( boost::math::float_distance(lhs, rhs)) < 2;
81 }
82 bool operator()( const double & lhs, const double & rhs ) const
83 {
84 return std::abs( boost::math::float_distance(lhs, rhs)) < 2;
85 }
86 };
87
88 template <class T>
89 void test_type(const T& gets_written){
90 const char * testfile = boost::archive::tmpnam(NULL);
91 BOOST_REQUIRE(testfile != NULL);
92 {
93 test_ostream os(testfile, TEST_STREAM_FLAGS);
94 test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
95 oa << boost::serialization::make_nvp("written", gets_written);
96 }
97
98 T got_read;
99 {
100 test_istream is(testfile, TEST_STREAM_FLAGS);
101 test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
102 ia >> boost::serialization::make_nvp("written", got_read);
103 }
104 BOOST_CHECK(boost::apply_visitor(are_equal(), gets_written, got_read));
105
106 std::remove(testfile);
107 }
108
109 // this verifies that if you try to read in a variant from a file
110 // whose "which" is illegal for the one in memory (that is, you're
111 // reading in to a different variant than you wrote out to) the load()
112 // operation will throw. One could concievably add checking for
113 // sequence length as well, but this would add size to the archive for
114 // dubious benefit.
115 //
116 void do_bad_read()
117 {
118 // Compiling this test invokes and ICE on msvc 6
119 // So, we'll just to skip it for this compiler
120 #if defined(_MSC_VER) && (_MSC_VER <= 1020)
121 boost::variant<bool, float, int, std::string> big_variant;
122 big_variant = std::string("adrenochrome");
123
124 const char * testfile = boost::archive::tmpnam(NULL);
125 BOOST_REQUIRE(testfile != NULL);
126 {
127 test_ostream os(testfile, TEST_STREAM_FLAGS);
128 test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
129 oa << BOOST_SERIALIZATION_NVP(big_variant);
130 }
131 boost::variant<bool, float, int> little_variant;
132 {
133 test_istream is(testfile, TEST_STREAM_FLAGS);
134 test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
135 bool exception_invoked = false;
136 BOOST_TRY {
137 ia >> BOOST_SERIALIZATION_NVP(little_variant);
138 } BOOST_CATCH (boost::archive::archive_exception e) {
139 BOOST_CHECK(boost::archive::archive_exception::unsupported_version == e.code);
140 exception_invoked = true;
141 }
142 BOOST_CATCH_END
143 BOOST_CHECK(exception_invoked);
144 }
145 #endif
146 }
147
148 int test_main( int /* argc */, char* /* argv */[] )
149 {
150 {
151 boost::variant<bool, int, float, double, A, std::string> v;
152 v = false;
153 test_type(v);
154 v = 1;
155 test_type(v);
156 v = (float) 2.3;
157 test_type(v);
158 v = (double) 6.4;
159 test_type(v);
160 v = std::string("we can't stop here, this is Bat Country");
161 test_type(v);
162 v = A();
163 test_type(v);
164 }
165 do_bad_read();
166 return EXIT_SUCCESS;
167 }
168
169 // EOF