OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
ExpressionTest.cpp
Go to the documentation of this file.
1 #include <set>
2 #include <string>
3 
8 
9 #include "gtest/gtest.h"
10 
11 #include "boost/smart_ptr.hpp"
12 #include "boost/tuple/tuple.hpp"
13 #include "boost/variant/get.hpp"
14 #include "boost/variant/variant.hpp"
15 
16 
17 namespace {
18 
19 
20 struct my_abs {
21 
23 
24  double value = boost::get<double>(args[0]);
25  return boost::make_tuple(fabs(value), true);
26  }
27 };
28 
29 struct my_pow {
30 
32 
33  double base = boost::get<double>(args[0]);
34  double exponent = boost::get<double>(args[1]);
35  return boost::make_tuple(pow(base, exponent), true);
36  }
37 };
38 
39 
40  // The fixture for testing class Foo.
41  class ExpressionTest : public ::testing::Test {
42  protected:
43 
44  ExpressionTest() {
45  // You can do set-up work for each test here.
46  }
47 
48  virtual ~ExpressionTest() {
49  // You can do clean-up work that doesn't throw exceptions here.
50  }
51 
52  // If the constructor and destructor are not enough for setting up
53  // and cleaning up each test, you can define the following methods:
54 
55  virtual void SetUp() {
56  // Code here will be called immediately after the constructor (right
57  // before each test).
58  }
59 
60  virtual void TearDown() {
61  // Code here will be called immediately after each test (right
62  // before the destructor).
63  }
64  };
65 
66  TEST_F(ExpressionTest, ParseExpression) {
67 
68  EXPECT_NO_THROW({
69  Expression("(1 + A * A * 2 * b + 2)");
70  });
71 
72  }
73 
74  TEST_F(ExpressionTest, RequestedVars) {
75 
76  std::string testexpr = "abs(A * A * 2.0 * b + 2.0)";
77  boost::scoped_ptr<Expression> e(new Expression(testexpr));
78 
79  std::set<std::string> reqVars = e->getReqVars();
80 
81  ASSERT_EQ(static_cast<size_t>(2), reqVars.size());
82 
83  }
84 
85  TEST_F(ExpressionTest, EvaluateExpression) {
86 
88  client::function::type abs_func;
89  abs_func = my_abs();
90  funcs.insert(std::pair<std::string, client::function::type>
91  ("abs", abs_func));
92 
93  std::string testexpr = "abs(1.0 + A * A * 2.0 * B + 2.0)";
94  boost::scoped_ptr<Expression> e(new Expression(testexpr, funcs));
95 
96  double a = 5.2;
97  double b = -10.2;
98 
100  vars.insert(std::pair<std::string, double>("A", a));
101  vars.insert(std::pair<std::string, double>("B", b));
102 
103  Expressions::Result_t result;
104  EXPECT_NO_THROW({
105  result = e->evaluate(vars);
106  });
107 
108  double expected = fabs(1 + a * a * 2 * b + 2);
109  ASSERT_DOUBLE_EQ(expected, boost::get<0>(result));
110  ASSERT_TRUE(boost::get<1>(result));
111 
112  }
113 
114  TEST_F(ExpressionTest, EvaluateCombinedExpression) {
115 
116  functionDictionary_t funcs;
117  client::function::type abs_func;
118  abs_func = my_abs();
119  funcs.insert(std::pair<std::string, client::function::type>
120  ("abs", abs_func));
121 
122  std::string testexpr = "abs(1.0 + A * 2.0) + abs(B + 2.0)";
123  boost::scoped_ptr<Expression> e(new Expression(testexpr, funcs));
124 
125  double a = 5.2;
126  double b = -10.2;
127 
129  vars.insert(std::pair<std::string, double>("A", a));
130  vars.insert(std::pair<std::string, double>("B", b));
131 
132  Expressions::Result_t result;
133  EXPECT_NO_THROW({
134  result = e->evaluate(vars);
135  });
136 
137  double expected = fabs(1 + a * 2) + fabs(b + 2);
138  ASSERT_DOUBLE_EQ(expected, boost::get<0>(result));
139  ASSERT_TRUE(boost::get<1>(result));
140 
141  }
142 
143  TEST_F(ExpressionTest, EvaluateNestedExpression) {
144 
145  functionDictionary_t funcs;
146  client::function::type abs_func;
147  abs_func = my_abs();
148  funcs.insert(std::pair<std::string, client::function::type>
149  ("abs", abs_func));
150  client::function::type pow_func;
151  pow_func = my_pow();
152  funcs.insert(std::pair<std::string, client::function::type>
153  ("pow", pow_func));
154 
155  std::string testexpr = "abs(1.0 + pow(A, 3) * 2.0) + abs(B + 2.0)";
156  boost::scoped_ptr<Expression> e(new Expression(testexpr, funcs));
157 
158  double a = 5.2;
159  double b = -10.2;
160 
162  vars.insert(std::pair<std::string, double>("A", a));
163  vars.insert(std::pair<std::string, double>("B", b));
164 
165  Expressions::Result_t result;
166  EXPECT_NO_THROW({
167  result = e->evaluate(vars);
168  });
169 
170  double expected = fabs(1 + pow(a, 3.0) * 2) + fabs(b + 2);
171  ASSERT_DOUBLE_EQ(expected, boost::get<0>(result));
172  ASSERT_TRUE(boost::get<1>(result));
173 
174  }
175 
176  TEST_F(ExpressionTest, EvaluateBooleanExpression) {
177 
178  std::string testexpr = "a > 5.2 - 1e-6";
179  boost::scoped_ptr<Expression> e(new Expression(testexpr));
180 
181  double a = 5.2;
182 
184  vars.insert(std::pair<std::string, double>("a", a));
185 
186  Expressions::Result_t result;
187  EXPECT_NO_THROW({
188  result = e->evaluate(vars);
189  });
190 
191  bool expected = true;
192  ASSERT_DOUBLE_EQ(expected, boost::get<0>(result));
193  ASSERT_TRUE(boost::get<1>(result));
194 
195  Expressions::OperatorType_t op = e->getOpType();
196  ASSERT_EQ(Expressions::INEQ_RHS, op);
197  }
198 
199 }
200 
201 int main(int argc, char **argv) {
202  ::testing::InitGoogleTest(&argc, argv);
203  return RUN_ALL_TESTS();
204 }
constexpr double e
The value of .
Definition: Physics.h:40
PETE_TUTree< FnFabs, typename T::PETE_Expr_t > fabs(const PETE_Expr< T > &l)
Definition: PETE.h:815
Expression to be evaluated in the framework.
Definition: Expression.h:75
boost::tuple< double, bool > Result_t
Definition: Expression.h:37
std::vector< argument_t > arguments_t
Definition: function.hpp:19
Tps< T > pow(const Tps< T > &x, int y)
Integer power.
Definition: TpsMath.h:76
OperatorType_t
distinguish different constraints
Definition: Expression.h:51
int main(int argc, char *argv[])
Definition: Main.cpp:107
boost::function< boost::tuple< double, bool >arguments_t)> type
Definition: function.hpp:21
std::map< std::string, double > variableDictionary_t
Definition: Expression.h:26
std::map< std::string, client::function::type > functionDictionary_t
Definition: Expression.h:27