1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/stdcpp/tsrc/Boost_test/variant/src/test7.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,254 @@
1.4 +//-----------------------------------------------------------------------------
1.5 +// boost-libs variant/test/test7.cpp header file
1.6 +// See http://www.boost.org for updates, documentation, and revision history.
1.7 +//-----------------------------------------------------------------------------
1.8 +//
1.9 +// Copyright (c) 2003
1.10 +// Eric Friedman, Itay Maman
1.11 +//
1.12 +// Distributed under the Boost Software License, Version 1.0. (See
1.13 +// accompanying file LICENSE_1_0.txt or copy at
1.14 +// http://www.boost.org/LICENSE_1_0.txt)
1.15 +/*
1.16 + * © Portions copyright (c) 2006-2007 Nokia Corporation. All rights reserved.
1.17 +*/
1.18 +
1.19 +#include "boost/test/minimal.hpp"
1.20 +#include "boost/variant.hpp"
1.21 +
1.22 +#include "jobs.h"
1.23 +
1.24 +#include <iostream>
1.25 +#include <algorithm>
1.26 +#include <string>
1.27 +#include <map>
1.28 +
1.29 +#include "boost/detail/workaround.hpp"
1.30 +#if BOOST_WORKAROUND(BOOST_MSVC, <= 1200)
1.31 +# include "boost/mpl/bool.hpp"
1.32 +# include "boost/type_traits/is_same.hpp"
1.33 +#endif
1.34 +
1.35 +
1.36 +using namespace boost;
1.37 +using namespace std;
1.38 +
1.39 +
1.40 +struct jas
1.41 +{
1.42 + jas(int n = 364);
1.43 + jas(const jas& other);
1.44 +
1.45 + ~jas();
1.46 + jas& operator=(const jas& other);
1.47 +
1.48 + void swap(jas& other);
1.49 +
1.50 + int n_;
1.51 +
1.52 + int sn_;
1.53 + static int s_inst_id_;
1.54 +};
1.55 +
1.56 +struct Tracker
1.57 +{
1.58 + typedef map<const jas*,int> table_type;
1.59 + typedef table_type::iterator iterator_type;
1.60 +
1.61 + static table_type s_this_to_sn_;
1.62 +
1.63 + static void insert(const jas& j)
1.64 + {
1.65 + s_this_to_sn_[&j] = j.sn_;
1.66 + cout << "jas( " << j.sn_ << ") Registered" << endl;
1.67 + }
1.68 +
1.69 + static void remove(const jas& j)
1.70 + {
1.71 + iterator_type iter = s_this_to_sn_.find(&j);
1.72 + BOOST_CHECK(iter != s_this_to_sn_.end());
1.73 + BOOST_CHECK( ((*iter).second) == j.sn_);
1.74 +
1.75 + int sn = (*iter).second;
1.76 + if(sn != j.sn_)
1.77 + {
1.78 + cout << "Mismatch: this = " << (*iter).first << ", sn_ = " << sn
1.79 + << ", other: this = " << &j << ", j.sn_ = " << j.sn_ << endl;
1.80 + }
1.81 +
1.82 + BOOST_CHECK(sn == j.sn_);
1.83 +
1.84 +
1.85 +
1.86 +
1.87 +
1.88 + s_this_to_sn_.erase(&j);
1.89 + cout << "jas( " << j.sn_ << ") Removed" << endl;
1.90 + }
1.91 +
1.92 + static void check()
1.93 + {
1.94 + BOOST_CHECK(s_this_to_sn_.empty());
1.95 + }
1.96 +};
1.97 +
1.98 +Tracker::table_type Tracker::s_this_to_sn_;
1.99 +
1.100 +
1.101 +
1.102 +jas::jas(int n) : n_(n)
1.103 +{
1.104 + sn_ = s_inst_id_;
1.105 + s_inst_id_ += 1;
1.106 +
1.107 + Tracker::insert(*this);
1.108 +}
1.109 +
1.110 +jas::jas(const jas& other) : n_(other.n_)
1.111 +{
1.112 + sn_ = s_inst_id_;
1.113 + s_inst_id_ += 1;
1.114 +
1.115 + Tracker::insert(*this);
1.116 +}
1.117 +
1.118 +jas::~jas()
1.119 +{
1.120 + Tracker::remove(*this);
1.121 +}
1.122 +
1.123 +jas& jas::operator=(const jas& other)
1.124 +{
1.125 + jas temp(other);
1.126 + swap(temp);
1.127 +
1.128 + return *this;
1.129 +}
1.130 +
1.131 +void jas::swap(jas& other)
1.132 +{
1.133 + Tracker::remove(*this);
1.134 + Tracker::remove(other);
1.135 +
1.136 + std::swap(n_, other.n_);
1.137 + std::swap(sn_, other.sn_);
1.138 +
1.139 + Tracker::insert(*this);
1.140 + Tracker::insert(other);
1.141 +}
1.142 +
1.143 +int jas::s_inst_id_ = 0;
1.144 +
1.145 +
1.146 +bool operator==(const jas& a, const jas& b)
1.147 +{
1.148 + return a.n_ == b.n_;
1.149 +}
1.150 +
1.151 +ostream& operator<<(ostream& out, const jas& a)
1.152 +{
1.153 + cout << "jas::n_ = " << a.n_;
1.154 + return out;
1.155 +}
1.156 +
1.157 +
1.158 +template<typename ValueType>
1.159 +struct compare_helper : boost::static_visitor<bool>
1.160 +{
1.161 + compare_helper(ValueType& expected) : expected_(expected) { }
1.162 +
1.163 +#if !BOOST_WORKAROUND(BOOST_MSVC, <= 1200)
1.164 +
1.165 + bool operator()(const ValueType& value)
1.166 + {
1.167 + return value == expected_;
1.168 + }
1.169 +
1.170 + template <typename T>
1.171 + bool operator()(const T& )
1.172 + {
1.173 + return false;
1.174 + }
1.175 +
1.176 +#else // MSVC6
1.177 +
1.178 +private:
1.179 +
1.180 + bool compare_impl(const ValueType& value, boost::mpl::true_)
1.181 + {
1.182 + return value == expected_;
1.183 + }
1.184 +
1.185 + template <typename T>
1.186 + bool compare_impl(const T&, boost::mpl::false_)
1.187 + {
1.188 + return false;
1.189 + }
1.190 +
1.191 +public:
1.192 +
1.193 + template <typename T>
1.194 + bool operator()(const T& value)
1.195 + {
1.196 + typedef typename boost::is_same<T, ValueType>::type
1.197 + T_is_ValueType;
1.198 +
1.199 + return compare_impl(value, T_is_ValueType());
1.200 + }
1.201 +
1.202 +#endif // MSVC6 workaround
1.203 +
1.204 + ValueType& expected_;
1.205 +
1.206 +};
1.207 +
1.208 +template<typename VariantType, typename ExpectedType>
1.209 +void var_compare(const VariantType& v, ExpectedType expected)
1.210 +{
1.211 + compare_helper<ExpectedType> ch(expected);
1.212 +
1.213 + bool checks = boost::apply_visitor(ch, v);
1.214 + BOOST_CHECK(checks);
1.215 +}
1.216 +
1.217 +
1.218 +void run()
1.219 +{
1.220 + variant<string, short> v0;
1.221 +
1.222 + var_compare(v0, string(""));
1.223 +
1.224 + v0 = 8;
1.225 + var_compare(v0, static_cast<short>(8));
1.226 +
1.227 + v0 = "penny lane";
1.228 + var_compare(v0, string("penny lane"));
1.229 +
1.230 + variant<jas, string, int> v1, v2 = jas(195);
1.231 + var_compare(v1, jas(364));
1.232 +
1.233 + v1 = jas(500);
1.234 + v1.swap(v2);
1.235 +
1.236 + var_compare(v1, jas(195));
1.237 + var_compare(v2, jas(500));
1.238 +
1.239 +
1.240 + variant<string, int> v3;
1.241 + var_compare(v3, string(""));
1.242 +}
1.243 +
1.244 +
1.245 +int test_main(int , char* [])
1.246 +{
1.247 +std_log(LOG_FILENAME_LINE,"[Test Case for test7]");
1.248 + run();
1.249 + Tracker::check();
1.250 +
1.251 +#ifdef __SYMBIAN32__
1.252 + testResultXml("test7");
1.253 + close_log_file();
1.254 +#endif
1.255 + return 0;
1.256 +}
1.257 +