diff -r 000000000000 -r e4d67989cc36 stdcpp/tsrc/Boost_test/variant/src/test8.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stdcpp/tsrc/Boost_test/variant/src/test8.cpp Tue Feb 02 02:01:42 2010 +0200 @@ -0,0 +1,118 @@ +//----------------------------------------------------------------------------- +// boost-libs variant/test/test8.cpp header file +// See http://www.boost.org for updates, documentation, and revision history. +//----------------------------------------------------------------------------- +// +// Copyright (c) 2003 +// Eric Friedman, Itay Maman +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +/* + * © Portions copyright (c) 2006-2007 Nokia Corporation. All rights reserved. +*/ + +#include "boost/test/minimal.hpp" +#include "boost/variant.hpp" + +#include +#include +#include + +using namespace std; +using namespace boost; + +typedef variant > t_var1; + +struct int_sum : static_visitor<> +{ + int_sum() : result_(0) { } + + void operator()(int t) + { + result_ += t; + } + + result_type operator()(float ) { } + result_type operator()(const std::string& ) { } + result_type operator()(const std::vector& ) { } + + int result_; +}; + +template +T& check_pass(Variant& v, T value) +{ + BOOST_CHECK(get(&v)); + + try + { + T& r = get(v); + BOOST_CHECK(r == value); + return r; + } + catch(boost::bad_get&) + { + throw; // must never reach + } +} + +template +void check_fail(Variant& v) +{ + BOOST_CHECK(!get(&v)); + + try + { + T& r = get(v); + BOOST_CHECK(false && &r); // should never reach + } + catch(boost::bad_get&) + { + // (do nothing here) + } +} + +int test_main(int , char* []) +{ + int_sum acc; + t_var1 v1 = 800; +std_log(LOG_FILENAME_LINE,"[Test Case for test8]"); + // check get on non-const variant + { + int& r1 = check_pass(v1, 800); + const int& cr1 = check_pass(v1, 800); + + check_fail(v1); + check_fail(v1); + check_fail(v1); + check_fail(v1); + + apply_visitor(acc, v1); + BOOST_CHECK(acc.result_ == 800); + + r1 = 920; // NOTE: modifies content of v1 + apply_visitor(acc, v1); + BOOST_CHECK(cr1 == 920); + BOOST_CHECK(acc.result_ == 800 + 920); + } + + // check const correctness: + { + const t_var1& c = v1; + + check_pass(c, 920); + + //check_fail(c); + check_fail(c); + //check_fail(c); + check_fail(c); + //check_fail(c); + } +#ifdef __SYMBIAN32__ + testResultXml("test8"); + close_log_file(); +#endif + return boost::exit_success; +}