Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/serialization/test/test_variant.cpp @ 29

Last change on this file since 29 was 29, checked in by landauf, 16 years ago

updated boost from 1_33_1 to 1_34_1

File size: 4.9 KB
Line 
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 <fstream>
19#include <cstdio> // remove
20#include <cmath> // for fabs()
21#include <boost/config.hpp>
22#if defined(BOOST_NO_STDC_NAMESPACE)
23namespace std{ 
24    using ::remove;
25}
26#endif
27
28#include <boost/type_traits/is_same.hpp>
29#include <boost/mpl/eval_if.hpp>
30#include <boost/mpl/identity.hpp>
31#include <boost/throw_exception.hpp>
32
33#if defined(_MSC_VER) && (_MSC_VER <= 1020)
34#  pragma warning (disable : 4786) // too long name, harmless warning
35#endif
36
37#include "test_tools.hpp"
38#include <boost/preprocessor/stringize.hpp>
39#include BOOST_PP_STRINGIZE(BOOST_ARCHIVE_TEST)
40
41#include <boost/archive/archive_exception.hpp>
42
43#include <boost/serialization/nvp.hpp>
44#include <boost/serialization/variant.hpp>
45
46#include "A.hpp"
47
48class are_equal
49    : public boost::static_visitor<bool>
50{
51public:
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 BOOST_DEDUCED_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::fabs(lhs- rhs) < std::numeric_limits<float>::round_error();
81    }
82    bool operator()( const double & lhs, const double & rhs ) const
83    {
84        return std::fabs(lhs - rhs) < std::numeric_limits<float>::round_error();
85    }
86};
87
88template <class T>
89void 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);
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);
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//
116void 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);
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);
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
148int 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
Note: See TracBrowser for help on using the repository browser.