OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
ExpressionTest.cpp
Go to the documentation of this file.
1 //
2 // Test ExpressionTest
3 //
4 // Copyright (c) 2010 - 2013, Yves Ineichen, ETH Zürich
5 // All rights reserved
6 //
7 // Implemented as part of the PhD thesis
8 // "Toward massively parallel multi-objective optimization with application to
9 // particle accelerators" (https://doi.org/10.3929/ethz-a-009792359)
10 //
11 // This file is part of OPAL.
12 //
13 // OPAL is free software: you can redistribute it and/or modify
14 // it under the terms of the GNU General Public License as published by
15 // the Free Software Foundation, either version 3 of the License, or
16 // (at your option) any later version.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with OPAL. If not, see <https://www.gnu.org/licenses/>.
20 //
21 #include <set>
22 #include <string>
23 
24 #include "Expression/Expression.h"
25 #include "Util/OptPilotException.h"
26 #include "Expression/Expression.h"
28 
29 #include "gtest/gtest.h"
30 
31 #include "boost/smart_ptr.hpp"
32 #include "boost/tuple/tuple.hpp"
33 #include "boost/variant/get.hpp"
34 #include "boost/variant/variant.hpp"
35 
36 
37 namespace {
38 
39 
40 struct my_abs {
41 
43 
44  double value = boost::get<double>(args[0]);
45  return boost::make_tuple(fabs(value), true);
46  }
47 };
48 
49 struct my_pow {
50 
52 
53  double base = boost::get<double>(args[0]);
54  double exponent = boost::get<double>(args[1]);
55  return boost::make_tuple(pow(base, exponent), true);
56  }
57 };
58 
59 
60  // The fixture for testing class Foo.
61  class ExpressionTest : public ::testing::Test {
62  protected:
63 
64  ExpressionTest() {
65  // You can do set-up work for each test here.
66  }
67 
68  virtual ~ExpressionTest() {
69  // You can do clean-up work that doesn't throw exceptions here.
70  }
71 
72  // If the constructor and destructor are not enough for setting up
73  // and cleaning up each test, you can define the following methods:
74 
75  virtual void SetUp() {
76  // Code here will be called immediately after the constructor (right
77  // before each test).
78  }
79 
80  virtual void TearDown() {
81  // Code here will be called immediately after each test (right
82  // before the destructor).
83  }
84  };
85 
86  TEST_F(ExpressionTest, ParseExpression) {
87 
88  EXPECT_NO_THROW({
89  Expression("(1 + A * A * 2 * b + 2)");
90  });
91 
92  }
93 
94  TEST_F(ExpressionTest, RequestedVars) {
95 
96  std::string testexpr = "abs(A * A * 2.0 * b + 2.0)";
97  boost::scoped_ptr<Expression> e(new Expression(testexpr));
98 
99  std::set<std::string> reqVars = e->getReqVars();
100 
101  ASSERT_EQ(static_cast<size_t>(2), reqVars.size());
102 
103  }
104 
105  TEST_F(ExpressionTest, EvaluateExpression) {
106 
107  functionDictionary_t funcs;
108  client::function::type abs_func;
109  abs_func = my_abs();
110  funcs.insert(std::pair<std::string, client::function::type>
111  ("abs", abs_func));
112 
113  std::string testexpr = "abs(1.0 + A * A * 2.0 * B + 2.0)";
114  boost::scoped_ptr<Expression> e(new Expression(testexpr, funcs));
115 
116  double a = 5.2;
117  double b = -10.2;
118 
120  vars.insert(std::pair<std::string, double>("A", a));
121  vars.insert(std::pair<std::string, double>("B", b));
122 
123  Expressions::Result_t result;
124  EXPECT_NO_THROW({
125  result = e->evaluate(vars);
126  });
127 
128  double expected = fabs(1 + a * a * 2 * b + 2);
129  ASSERT_DOUBLE_EQ(expected, boost::get<0>(result));
130  ASSERT_TRUE(boost::get<1>(result));
131 
132  }
133 
134  TEST_F(ExpressionTest, EvaluateCombinedExpression) {
135 
136  functionDictionary_t funcs;
137  client::function::type abs_func;
138  abs_func = my_abs();
139  funcs.insert(std::pair<std::string, client::function::type>
140  ("abs", abs_func));
141 
142  std::string testexpr = "abs(1.0 + A * 2.0) + abs(B + 2.0)";
143  boost::scoped_ptr<Expression> e(new Expression(testexpr, funcs));
144 
145  double a = 5.2;
146  double b = -10.2;
147 
149  vars.insert(std::pair<std::string, double>("A", a));
150  vars.insert(std::pair<std::string, double>("B", b));
151 
152  Expressions::Result_t result;
153  EXPECT_NO_THROW({
154  result = e->evaluate(vars);
155  });
156 
157  double expected = fabs(1 + a * 2) + fabs(b + 2);
158  ASSERT_DOUBLE_EQ(expected, boost::get<0>(result));
159  ASSERT_TRUE(boost::get<1>(result));
160 
161  }
162 
163  TEST_F(ExpressionTest, EvaluateNestedExpression) {
164 
165  functionDictionary_t funcs;
166  client::function::type abs_func;
167  abs_func = my_abs();
168  funcs.insert(std::pair<std::string, client::function::type>
169  ("abs", abs_func));
170  client::function::type pow_func;
171  pow_func = my_pow();
172  funcs.insert(std::pair<std::string, client::function::type>
173  ("pow", pow_func));
174 
175  std::string testexpr = "abs(1.0 + pow(A, 3) * 2.0) + abs(B + 2.0)";
176  boost::scoped_ptr<Expression> e(new Expression(testexpr, funcs));
177 
178  double a = 5.2;
179  double b = -10.2;
180 
182  vars.insert(std::pair<std::string, double>("A", a));
183  vars.insert(std::pair<std::string, double>("B", b));
184 
185  Expressions::Result_t result;
186  EXPECT_NO_THROW({
187  result = e->evaluate(vars);
188  });
189 
190  double expected = fabs(1 + pow(a, 3.0) * 2) + fabs(b + 2);
191  ASSERT_DOUBLE_EQ(expected, boost::get<0>(result));
192  ASSERT_TRUE(boost::get<1>(result));
193 
194  }
195 
196  TEST_F(ExpressionTest, EvaluateBooleanExpression) {
197 
198  std::string testexpr = "a > 5.2 - 1e-6";
199  boost::scoped_ptr<Expression> e(new Expression(testexpr));
200 
201  double a = 5.2;
202 
204  vars.insert(std::pair<std::string, double>("a", a));
205 
206  Expressions::Result_t result;
207  EXPECT_NO_THROW({
208  result = e->evaluate(vars);
209  });
210 
211  bool expected = true;
212  ASSERT_DOUBLE_EQ(expected, boost::get<0>(result));
213  ASSERT_TRUE(boost::get<1>(result));
214 
215  Expressions::OperatorType_t op = e->getOpType();
216  ASSERT_EQ(Expressions::INEQ_RHS, op);
217  }
218 
219 }
220 
221 int main(int argc, char **argv) {
222  ::testing::InitGoogleTest(&argc, argv);
223  return RUN_ALL_TESTS();
224 }
Tps< T > pow(const Tps< T > &x, int y)
Integer power.
Definition: TpsMath.h:76
std::complex< double > a
PETE_TUTree< FnFabs, typename T::PETE_Expr_t > fabs(const PETE_Expr< T > &l)
Definition: PETE.h:732
std::map< std::string, client::function::type > functionDictionary_t
Definition: Expression.h:56
std::map< std::string, double > variableDictionary_t
Definition: Expression.h:55
int main(int argc, char **argv)
OperatorType_t
distinguish different constraints
Definition: Expression.h:80
boost::tuple< double, bool > Result_t
Definition: Expression.h:66
constexpr double e
The value of.
Definition: Physics.h:39
boost::function< boost::tuple< double, bool >arguments_t)> type
Definition: function.hpp:21
std::vector< argument_t > arguments_t
Definition: function.hpp:19