|
1 //----------------------------------------------------------------------------- |
|
2 // boost-libs variant/test/test3.cpp source file |
|
3 // See http://www.boost.org for updates, documentation, and revision history. |
|
4 //----------------------------------------------------------------------------- |
|
5 // |
|
6 // Copyright (c) 2003 |
|
7 // Eric Friedman, Itay Maman |
|
8 // |
|
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) |
|
12 /* |
|
13 * © Portions copyright (c) 2006-2007 Nokia Corporation. All rights reserved. |
|
14 */ |
|
15 |
|
16 #include "boost/test/minimal.hpp" |
|
17 #include "boost/variant.hpp" |
|
18 |
|
19 #include <iostream> |
|
20 #include <sstream> |
|
21 #include <string> |
|
22 #ifdef __SYMBIAN32__ |
|
23 #include "std_log_result.h" |
|
24 #define LOG_FILENAME_LINE __FILE__, __LINE__ |
|
25 #endif |
|
26 ///////////////////////////////////////////////////////////////////// |
|
27 |
|
28 using boost::variant; |
|
29 using boost::recursive_wrapper; |
|
30 #ifndef __SYMBIAN32__ |
|
31 using std::cout; |
|
32 #endif |
|
33 using std::endl; |
|
34 |
|
35 ///////////////////////////////////////////////////////////////////// |
|
36 ///////////////////////////////////////////////////////////////////// |
|
37 |
|
38 struct Add; |
|
39 struct Sub; |
|
40 |
|
41 typedef variant<int, recursive_wrapper<Add>, recursive_wrapper<Sub> > Expr; |
|
42 |
|
43 struct Sub |
|
44 { |
|
45 Sub(); |
|
46 Sub(const Expr& l, const Expr& r); |
|
47 Sub(const Sub& other); |
|
48 |
|
49 Expr lhs_; |
|
50 Expr rhs_; |
|
51 }; |
|
52 |
|
53 struct Add |
|
54 { |
|
55 Add() { } |
|
56 Add(const Expr& l, const Expr& r) : lhs_(l), rhs_(r) { } |
|
57 Add(const Add& other) : lhs_(other.lhs_), rhs_(other.rhs_) { } |
|
58 |
|
59 Expr lhs_; |
|
60 Expr rhs_; |
|
61 }; |
|
62 |
|
63 Sub::Sub() { } |
|
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_) { } |
|
66 |
|
67 |
|
68 // |
|
69 // insert-to operators |
|
70 // |
|
71 std::ostream& operator<<(std::ostream& out, const Sub& a); |
|
72 |
|
73 std::ostream& operator<<(std::ostream& out, const Add& a) |
|
74 { |
|
75 out << '(' << a.lhs_ << '+' << a.rhs_ << ')'; |
|
76 return out; |
|
77 } |
|
78 |
|
79 std::ostream& operator<<(std::ostream& out, const Sub& a) |
|
80 { |
|
81 out << '(' << a.lhs_ << '-' << a.rhs_ << ')'; |
|
82 return out; |
|
83 } |
|
84 |
|
85 // |
|
86 // Expression evaluation visitor |
|
87 // |
|
88 struct Calculator : boost::static_visitor<int> |
|
89 { |
|
90 Calculator() { } |
|
91 |
|
92 int operator()(Add& x) const |
|
93 { |
|
94 Calculator calc; |
|
95 int n1 = boost::apply_visitor(calc, x.lhs_); |
|
96 int n2 = boost::apply_visitor(calc, x.rhs_); |
|
97 |
|
98 return n1 + n2; |
|
99 } |
|
100 |
|
101 int operator()(Sub& x) const |
|
102 { |
|
103 return boost::apply_visitor(Calculator(), x.lhs_) |
|
104 - boost::apply_visitor(Calculator(), x.rhs_); |
|
105 } |
|
106 |
|
107 int operator()(Expr& x) const |
|
108 { |
|
109 Calculator calc; |
|
110 return boost::apply_visitor(calc, x); |
|
111 } |
|
112 |
|
113 int operator()(int x) const |
|
114 { |
|
115 return x; |
|
116 } |
|
117 |
|
118 }; // Calculator |
|
119 |
|
120 |
|
121 ///////////////////////////////////////////////////////////////////// |
|
122 |
|
123 |
|
124 int test_main(int, char* []) |
|
125 { |
|
126 std_log(LOG_FILENAME_LINE,"[Test Case for test3]"); |
|
127 int n = 13; |
|
128 Expr e1( Add(n, Sub(Add(40,2),Add(10,4))) ); //n + (40+2)-(10+14) = n+28 |
|
129 |
|
130 std::ostringstream e1_str; |
|
131 e1_str << e1; |
|
132 |
|
133 BOOST_CHECK(e1.type() == typeid(Add)); |
|
134 BOOST_CHECK(e1_str.str() == "(13+((40+2)-(10+4)))"); |
|
135 |
|
136 //Evaluate expression |
|
137 int res = boost::apply_visitor(Calculator(), e1); |
|
138 BOOST_CHECK(res == n + 28); |
|
139 |
|
140 #ifdef __SYMBIAN32__ |
|
141 testResultXml("test3"); |
|
142 close_log_file(); |
|
143 #endif |
|
144 return 0; |
|
145 } |
|
146 |