]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/serialization/test/test_variant.cpp
1 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
3 // test of non-intrusive serialization of variant types
6 // troy d. straszheim <troy@resophonic.com>
7 // http://www.resophonic.com
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)
13 // See http://www.boost.org for updates, documentation, and revision history.
15 // thanks to Robert Ramey and Peter Dimov.
18 #include <cstddef> // NULL
19 #include <cstdio> // remove
21 #include <boost/config.hpp>
22 #include <boost/math/special_functions/next.hpp> // float_distance
23 #if defined(BOOST_NO_STDC_NAMESPACE)
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>
34 #if defined(_MSC_VER) && (_MSC_VER <= 1020)
35 # pragma warning (disable : 4786) // too long name, harmless warning
38 #include "test_tools.hpp"
40 #include <boost/archive/archive_exception.hpp>
42 #include <boost/serialization/nvp.hpp>
43 #include <boost/serialization/variant.hpp>
49 : public boost::static_visitor
<bool>
52 // note extra rigamorole for compilers which don't support
53 // partial function template ordering - specfically msvc 6.x
55 template<class T
, class U
>
56 static bool invoke(const T
& t
, const U
& u
){
62 template<class T
, class U
>
63 static bool invoke(const T
&, const U
&){
68 template <class T
, class U
>
69 bool operator()( const T
& t
, const U
& u
) const
71 typedef typename
boost::mpl::eval_if
<boost::is_same
<T
, U
>,
72 boost::mpl::identity
<same
>,
73 boost::mpl::identity
<not_same
>
75 return type::invoke(t
, u
);
78 bool operator()( const float & lhs
, const float & rhs
) const
80 return std::abs( boost::math::float_distance(lhs
, rhs
)) < 2;
82 bool operator()( const double & lhs
, const double & rhs
) const
84 return std::abs( boost::math::float_distance(lhs
, rhs
)) < 2;
89 void test_type(const T
& gets_written
){
90 const char * testfile
= boost::archive::tmpnam(NULL
);
91 BOOST_REQUIRE(testfile
!= NULL
);
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
);
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
);
104 BOOST_CHECK(boost::apply_visitor(are_equal(), gets_written
, got_read
));
106 std::remove(testfile
);
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
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");
124 const char * testfile
= boost::archive::tmpnam(NULL
);
125 BOOST_REQUIRE(testfile
!= NULL
);
127 test_ostream
os(testfile
, TEST_STREAM_FLAGS
);
128 test_oarchive
oa(os
, TEST_ARCHIVE_FLAGS
);
129 oa
<< BOOST_SERIALIZATION_NVP(big_variant
);
131 boost::variant
<bool, float, int> little_variant
;
133 test_istream
is(testfile
, TEST_STREAM_FLAGS
);
134 test_iarchive
ia(is
, TEST_ARCHIVE_FLAGS
);
135 bool exception_invoked
= false;
137 ia
>> BOOST_SERIALIZATION_NVP(little_variant
);
138 } BOOST_CATCH (boost::archive::archive_exception
const& e
) {
139 BOOST_CHECK(boost::archive::archive_exception::unsupported_version
== e
.code
);
140 exception_invoked
= true;
143 BOOST_CHECK(exception_invoked
);
153 namespace serialization
{
155 template<class Archive
>
156 void serialize(Archive
&ar
, H
& h
, const unsigned int /*file_version*/){
157 ar
& boost::serialization::make_nvp("h", h
.i
);
160 } // namespace serialization
163 inline bool operator==(H
const & lhs
, H
const & rhs
) {
164 return lhs
.i
== rhs
.i
;
167 inline bool operator!=(H
const & lhs
, H
const & rhs
) {
168 return !(lhs
== rhs
);
171 inline bool operator<(H
const & lhs
, H
const & rhs
) {
172 return lhs
.i
< rhs
.i
;
175 inline std::size_t hash_value(H
const & val
) {
180 const char * testfile
= boost::archive::tmpnam(NULL
);
181 BOOST_REQUIRE(testfile
!= NULL
);
182 typedef boost::variant
<H
, int> variant_t
;
186 test_ostream
os(testfile
, TEST_STREAM_FLAGS
);
187 test_oarchive
oa(os
, TEST_ARCHIVE_FLAGS
);
188 oa
<< boost::serialization::make_nvp("written", v
);
189 const H
* h_ptr
= & boost::strict_get
<H
const &>(v
);
190 oa
<< boost::serialization::make_nvp("written", h_ptr
);
194 test_istream
is(testfile
, TEST_STREAM_FLAGS
);
195 test_iarchive
ia(is
, TEST_ARCHIVE_FLAGS
);
196 ia
>> boost::serialization::make_nvp("written", v2
);
198 ia
>> boost::serialization::make_nvp("written", h2_ptr
);
199 BOOST_CHECK_EQUAL(h
, boost::strict_get
<H
const>(v2
));
200 BOOST_CHECK_EQUAL(h2_ptr
, & boost::strict_get
<H
const &>(v2
));
202 BOOST_CHECK_EQUAL(v
, v2
);
205 #include <boost/serialization/map.hpp>
206 #include <boost/serialization/set.hpp>
208 // test a pointer to an object contained into a variant that is an
210 void test_variant_set()
212 const char * testfile
= boost::archive::tmpnam(NULL
);
213 BOOST_REQUIRE(testfile
!= NULL
);
214 typedef boost::variant
<H
, int> variant_t
;
215 typedef std::set
<variant_t
> uset_t
;
218 test_ostream
os(testfile
, TEST_STREAM_FLAGS
);
219 test_oarchive
oa(os
, TEST_ARCHIVE_FLAGS
);
223 oa
<< boost::serialization::make_nvp("written", set
);
224 H
const * const h_ptr
= boost::strict_get
<H
const>(&(*set
.begin()));
225 oa
<< boost::serialization::make_nvp("written", h_ptr
);
229 test_istream
is(testfile
, TEST_STREAM_FLAGS
);
230 test_iarchive
ia(is
, TEST_ARCHIVE_FLAGS
);
231 ia
>> boost::serialization::make_nvp("written", set2
);
233 ia
>> boost::serialization::make_nvp("written", h_ptr
);
234 const H
* h_ptr2
= & boost::strict_get
<H
const>(*set2
.begin());
235 BOOST_CHECK_EQUAL(h_ptr
, h_ptr2
);
237 BOOST_CHECK_EQUAL(set
, set2
);
240 // test a pointer to an object contained into a variant that is an
242 void test_variant_map()
244 const char * testfile
= boost::archive::tmpnam(NULL
);
245 BOOST_REQUIRE(testfile
!= NULL
);
246 typedef boost::variant
<H
, int> variant_t
;
247 typedef std::map
<int, variant_t
> map_t
;
250 test_ostream
os(testfile
, TEST_STREAM_FLAGS
);
251 test_oarchive
oa(os
, TEST_ARCHIVE_FLAGS
);
255 BOOST_ASSERT(1 == map
.size());
256 oa
<< boost::serialization::make_nvp("written", map
);
257 H
const * const h_ptr
= boost::strict_get
<H
const>(&map
[0]);
258 BOOST_CHECK_EQUAL(h_ptr
, boost::strict_get
<H
const>(&map
[0]));
259 oa
<< boost::serialization::make_nvp("written", h_ptr
);
263 test_istream
is(testfile
, TEST_STREAM_FLAGS
);
264 test_iarchive
ia(is
, TEST_ARCHIVE_FLAGS
);
265 ia
>> boost::serialization::make_nvp("written", map2
);
266 BOOST_ASSERT(1 == map2
.size());
268 ia
>> boost::serialization::make_nvp("written", h_ptr
);
269 H
const * const h_ptr2
= boost::strict_get
<H
const>(&map2
[0]);
270 BOOST_CHECK_EQUAL(h_ptr
, h_ptr2
);
272 BOOST_CHECK_EQUAL(map
, map2
);
275 int test_main( int /* argc */, char* /* argv */[] )
278 boost::variant
<bool, int, float, double, A
, std::string
> v
;
287 v
= std::string("we can't stop here, this is Bat Country");