OPAL (Object Oriented Parallel Accelerator Library)  2024.1
OPAL
FromFileExpressionTest.cpp
Go to the documentation of this file.
1 //
2 // Test FromFileExpressionTest
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 "Util/OptPilotException.h"
25 #include "Expression/Expression.h"
27 #include "Expression/FromFile.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 struct my_sqrt {
39 
41 
42  double value = boost::get<double>(args[0]);
43  if (value < 0.0) return boost::make_tuple(0.0, false);
44  return boost::make_tuple(sqrt(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  // The fixture for testing class Foo.
59  class FromFileExpressionTest : public ::testing::Test {
60  protected:
61 
62  FromFileExpressionTest() {
63  // You can do set-up work for each test here.
64  }
65 
66  virtual ~FromFileExpressionTest() {
67  // You can do clean-up work that doesn't throw exceptions here.
68  }
69 
70  // If the constructor and destructor are not enough for setting up
71  // and cleaning up each test, you can define the following methods:
72 
73  virtual void SetUp() {
74  // Code here will be called immediately after the constructor (right
75  // before each test).
76  }
77 
78  virtual void TearDown() {
79  // Code here will be called immediately after each test (right
80  // before the destructor).
81  }
82  };
83 
84 
85  TEST_F(FromFileExpressionTest, EvaluateFromFileExpression) {
86 
88  double expected = 0.3126 + 0.3561 + 0.4242;
89 
92  ff = FromFile();
93  funcs.insert(std::pair<std::string, client::function::type>
94  ("fromFile", ff));
95 
96  std::string testexpr = "fromFile(\"resources/fromfile_test.dat\")";
97  const std::unique_ptr<Expression> e(new Expression(testexpr, funcs));
99  EXPECT_NO_THROW({
100  result = e->evaluate(vars);
101  });
102 
103  ASSERT_NEAR(expected, boost::get<0>(result), 1e-6);
104  ASSERT_TRUE(boost::get<1>(result));
105  }
106 
107  TEST_F(FromFileExpressionTest, EvaluateCombinedFromFileExpression) {
108 
110  double expected = sqrt(pow(0.3126 + 0.3561 + 0.4242, 2) + pow(0.1263 - 0.5613 + 0.2424, 2));
111 
112  functionDictionary_t funcs;
114  ff = FromFile();
115  funcs.insert(std::pair<std::string, client::function::type>
116  ("fromFile", ff));
117  client::function::type sqrt_func;
118  sqrt_func = my_sqrt();
119  funcs.insert(std::pair<std::string, client::function::type>
120  ("sqrt", sqrt_func));
121  client::function::type pow_func;
122  pow_func = my_pow();
123  funcs.insert(std::pair<std::string, client::function::type>
124  ("pow", pow_func));
125 
126  std::string testexpr = "sqrt(pow(fromFile(\"resources/fromfile_test.dat\"), 2) + "
127  "pow(fromFile(\"resources/fromfile_test2.dat\"), 2))";
128  const std::unique_ptr<Expression> e(new Expression(testexpr, funcs));
130  EXPECT_NO_THROW({
131  result = e->evaluate(vars);
132  });
133 
134  ASSERT_NEAR(expected, boost::get<0>(result), 1e-6);
135  ASSERT_TRUE(boost::get<1>(result));
136  }
137 
138 }
139 
140 int main(int argc, char **argv) {
141  ::testing::InitGoogleTest(&argc, argv);
142  return RUN_ALL_TESTS();
143 }
Tps< T > sqrt(const Tps< T > &x)
Square root.
Definition: TpsMath.h:91
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
double FromFile(std::string file, const std::vector< double > &referencePoint)
std::map< std::string, client::function::type > functionDictionary_t
Definition: Expression.h:56
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