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