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