]>
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>
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 e
) {
139 BOOST_CHECK(boost::archive::archive_exception::unsupported_version
== e
.code
);
140 exception_invoked
= true;
143 BOOST_CHECK(exception_invoked
);
148 int test_main( int /* argc */, char* /* argv */[] )
151 boost::variant
<bool, int, float, double, A
, std::string
> v
;
160 v
= std::string("we can't stop here, this is Bat Country");