Update contrib.
1 //-----------------------------------------------------------------------------
2 // boost-libs variant/test/test7.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"
26 #include "boost/detail/workaround.hpp"
27 #if BOOST_WORKAROUND(BOOST_MSVC, <= 1200)
28 # include "boost/mpl/bool.hpp"
29 # include "boost/type_traits/is_same.hpp"
33 using namespace boost;
40 jas(const jas& other);
43 jas& operator=(const jas& other);
45 void swap(jas& other);
50 static int s_inst_id_;
55 typedef map<const jas*,int> table_type;
56 typedef table_type::iterator iterator_type;
58 static table_type s_this_to_sn_;
60 static void insert(const jas& j)
62 s_this_to_sn_[&j] = j.sn_;
63 cout << "jas( " << j.sn_ << ") Registered" << endl;
66 static void remove(const jas& j)
68 iterator_type iter = s_this_to_sn_.find(&j);
69 BOOST_CHECK(iter != s_this_to_sn_.end());
70 BOOST_CHECK( ((*iter).second) == j.sn_);
72 int sn = (*iter).second;
75 cout << "Mismatch: this = " << (*iter).first << ", sn_ = " << sn
76 << ", other: this = " << &j << ", j.sn_ = " << j.sn_ << endl;
79 BOOST_CHECK(sn == j.sn_);
85 s_this_to_sn_.erase(&j);
86 cout << "jas( " << j.sn_ << ") Removed" << endl;
91 BOOST_CHECK(s_this_to_sn_.empty());
95 Tracker::table_type Tracker::s_this_to_sn_;
99 jas::jas(int n) : n_(n)
104 Tracker::insert(*this);
107 jas::jas(const jas& other) : n_(other.n_)
112 Tracker::insert(*this);
117 Tracker::remove(*this);
120 jas& jas::operator=(const jas& other)
128 void jas::swap(jas& other)
130 Tracker::remove(*this);
131 Tracker::remove(other);
133 std::swap(n_, other.n_);
134 std::swap(sn_, other.sn_);
136 Tracker::insert(*this);
137 Tracker::insert(other);
140 int jas::s_inst_id_ = 0;
143 bool operator==(const jas& a, const jas& b)
148 ostream& operator<<(ostream& out, const jas& a)
150 cout << "jas::n_ = " << a.n_;
155 template<typename ValueType>
156 struct compare_helper : boost::static_visitor<bool>
158 compare_helper(ValueType& expected) : expected_(expected) { }
160 #if !BOOST_WORKAROUND(BOOST_MSVC, <= 1200)
162 bool operator()(const ValueType& value)
164 return value == expected_;
167 template <typename T>
168 bool operator()(const T& )
177 bool compare_impl(const ValueType& value, boost::mpl::true_)
179 return value == expected_;
182 template <typename T>
183 bool compare_impl(const T&, boost::mpl::false_)
190 template <typename T>
191 bool operator()(const T& value)
193 typedef typename boost::is_same<T, ValueType>::type
196 return compare_impl(value, T_is_ValueType());
199 #endif // MSVC6 workaround
201 ValueType& expected_;
205 template<typename VariantType, typename ExpectedType>
206 void var_compare(const VariantType& v, ExpectedType expected)
208 compare_helper<ExpectedType> ch(expected);
210 bool checks = boost::apply_visitor(ch, v);
217 variant<string, short> v0;
219 var_compare(v0, string(""));
222 var_compare(v0, static_cast<short>(8));
225 var_compare(v0, string("penny lane"));
227 variant<jas, string, int> v1, v2 = jas(195);
228 var_compare(v1, jas(364));
233 var_compare(v1, jas(195));
234 var_compare(v2, jas(500));
237 variant<string, int> v3;
238 var_compare(v3, string(""));
242 int test_main(int , char* [])
244 std_log(LOG_FILENAME_LINE,"[Test Case for test7]");
249 testResultXml("test7");