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