OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
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/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 struct my_sqrt {
40 
42 
43  double value = boost::get<double>(args[0]);
44  if (value < 0.0) return boost::make_tuple(0.0, false);
45  return boost::make_tuple(sqrt(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  // The fixture for testing class Foo.
60  class FromFileExpressionTest : public ::testing::Test {
61  protected:
62 
63  FromFileExpressionTest() {
64  // You can do set-up work for each test here.
65  }
66 
67  virtual ~FromFileExpressionTest() {
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 
86  TEST_F(FromFileExpressionTest, EvaluateFromFileExpression) {
87 
89  double expected = 0.3126 + 0.3561 + 0.4242;
90 
93  ff = FromFile();
94  funcs.insert(std::pair<std::string, client::function::type>
95  ("fromFile", ff));
96 
97  std::string testexpr = "fromFile(\"resources/fromfile_test.dat\")";
98  boost::scoped_ptr<Expression> e(new Expression(testexpr, funcs));
99  Expressions::Result_t result;
100  EXPECT_NO_THROW({
101  result = e->evaluate(vars);
102  });
103 
104  ASSERT_NEAR(expected, boost::get<0>(result), 1e-6);
105  ASSERT_TRUE(boost::get<1>(result));
106  }
107 
108  TEST_F(FromFileExpressionTest, EvaluateCombinedFromFileExpression) {
109 
111  double expected = sqrt(pow(0.3126 + 0.3561 + 0.4242, 2) + pow(0.1263 - 0.5613 + 0.2424, 2));
112 
113  functionDictionary_t funcs;
115  ff = FromFile();
116  funcs.insert(std::pair<std::string, client::function::type>
117  ("fromFile", ff));
118  client::function::type sqrt_func;
119  sqrt_func = my_sqrt();
120  funcs.insert(std::pair<std::string, client::function::type>
121  ("sqrt", sqrt_func));
122  client::function::type pow_func;
123  pow_func = my_pow();
124  funcs.insert(std::pair<std::string, client::function::type>
125  ("pow", pow_func));
126 
127  std::string testexpr = "sqrt(pow(fromFile(\"resources/fromfile_test.dat\"), 2) + "
128  "pow(fromFile(\"resources/fromfile_test2.dat\"), 2))";
129  boost::scoped_ptr<Expression> e(new Expression(testexpr, funcs));
130  Expressions::Result_t result;
131  EXPECT_NO_THROW({
132  result = e->evaluate(vars);
133  });
134 
135  ASSERT_NEAR(expected, boost::get<0>(result), 1e-6);
136  ASSERT_TRUE(boost::get<1>(result));
137  }
138 
139 }
140 
141 int main(int argc, char **argv) {
142  ::testing::InitGoogleTest(&argc, argv);
143  return RUN_ALL_TESTS();
144 }
Tps< T > pow(const Tps< T > &x, int y)
Integer power.
Definition: TpsMath.h:76
Tps< T > sqrt(const Tps< T > &x)
Square root.
Definition: TpsMath.h:91
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)
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
double FromFile(std::string file, const std::vector< double > &referencePoint)