First public contribution.
1 //-----------------------------------------------------------------------------
2 // boost-libs variant/test/test8.cpp header file
3 // See http://www.boost.org for updates, documentation, and revision history.
4 //-----------------------------------------------------------------------------
7 // Eric Friedman, Itay Maman
9 // Distributed under the Boost Software License, Version 1.0. (See
10 // accompanying file LICENSE_1_0.txt or copy at
11 // http://www.boost.org/LICENSE_1_0.txt)
13 * © Portions copyright (c) 2006-2007 Nokia Corporation. All rights reserved.
16 #include "boost/test/minimal.hpp"
17 #include "boost/variant.hpp"
24 using namespace boost;
26 typedef variant<float, std::string, int, std::vector<std::string> > t_var1;
28 struct int_sum : static_visitor<>
30 int_sum() : result_(0) { }
32 void operator()(int t)
37 result_type operator()(float ) { }
38 result_type operator()(const std::string& ) { }
39 result_type operator()(const std::vector<std::string>& ) { }
44 template <typename T, typename Variant>
45 T& check_pass(Variant& v, T value)
47 BOOST_CHECK(get<T>(&v));
52 BOOST_CHECK(r == value);
55 catch(boost::bad_get&)
57 throw; // must never reach
61 template <typename T, typename Variant>
62 void check_fail(Variant& v)
64 BOOST_CHECK(!get<T>(&v));
69 BOOST_CHECK(false && &r); // should never reach
71 catch(boost::bad_get&)
77 int test_main(int , char* [])
81 std_log(LOG_FILENAME_LINE,"[Test Case for test8]");
82 // check get on non-const variant
84 int& r1 = check_pass<int>(v1, 800);
85 const int& cr1 = check_pass<const int>(v1, 800);
87 check_fail<float>(v1);
88 check_fail<const float>(v1);
89 check_fail<short>(v1);
90 check_fail<const short>(v1);
92 apply_visitor(acc, v1);
93 BOOST_CHECK(acc.result_ == 800);
95 r1 = 920; // NOTE: modifies content of v1
96 apply_visitor(acc, v1);
97 BOOST_CHECK(cr1 == 920);
98 BOOST_CHECK(acc.result_ == 800 + 920);
101 // check const correctness:
103 const t_var1& c = v1;
105 check_pass<const int>(c, 920);
107 //check_fail<int>(c);
108 check_fail<const float>(c);
109 //check_fail<float>(c);
110 check_fail<const short>(c);
111 //check_fail<short>(c);
114 testResultXml("test8");
117 return boost::exit_success;