Update contrib.
1 //-----------------------------------------------------------------------------
2 // boost-libs variant/test/test3.cpp source 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"
23 #include "std_log_result.h"
24 #define LOG_FILENAME_LINE __FILE__, __LINE__
26 /////////////////////////////////////////////////////////////////////
29 using boost::recursive_wrapper;
35 /////////////////////////////////////////////////////////////////////
36 /////////////////////////////////////////////////////////////////////
41 typedef variant<int, recursive_wrapper<Add>, recursive_wrapper<Sub> > Expr;
46 Sub(const Expr& l, const Expr& r);
47 Sub(const Sub& other);
56 Add(const Expr& l, const Expr& r) : lhs_(l), rhs_(r) { }
57 Add(const Add& other) : lhs_(other.lhs_), rhs_(other.rhs_) { }
64 Sub::Sub(const Expr& l, const Expr& r) : lhs_(l), rhs_(r) { }
65 Sub::Sub(const Sub& other) : lhs_(other.lhs_), rhs_(other.rhs_) { }
69 // insert-to operators
71 std::ostream& operator<<(std::ostream& out, const Sub& a);
73 std::ostream& operator<<(std::ostream& out, const Add& a)
75 out << '(' << a.lhs_ << '+' << a.rhs_ << ')';
79 std::ostream& operator<<(std::ostream& out, const Sub& a)
81 out << '(' << a.lhs_ << '-' << a.rhs_ << ')';
86 // Expression evaluation visitor
88 struct Calculator : boost::static_visitor<int>
92 int operator()(Add& x) const
95 int n1 = boost::apply_visitor(calc, x.lhs_);
96 int n2 = boost::apply_visitor(calc, x.rhs_);
101 int operator()(Sub& x) const
103 return boost::apply_visitor(Calculator(), x.lhs_)
104 - boost::apply_visitor(Calculator(), x.rhs_);
107 int operator()(Expr& x) const
110 return boost::apply_visitor(calc, x);
113 int operator()(int x) const
121 /////////////////////////////////////////////////////////////////////
124 int test_main(int, char* [])
126 std_log(LOG_FILENAME_LINE,"[Test Case for test3]");
128 Expr e1( Add(n, Sub(Add(40,2),Add(10,4))) ); //n + (40+2)-(10+14) = n+28
130 std::ostringstream e1_str;
133 BOOST_CHECK(e1.type() == typeid(Add));
134 BOOST_CHECK(e1_str.str() == "(13+((40+2)-(10+4)))");
136 //Evaluate expression
137 int res = boost::apply_visitor(Calculator(), e1);
138 BOOST_CHECK(res == n + 28);
141 testResultXml("test3");