OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
PythonExprTest.cpp
Go to the documentation of this file.
1 //
2 // Test PythonExprTest
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/Types.h"
25 #include "Util/OptPilotException.h"
26 #include "Expression/Expression.h"
28 #include "Expression/PythonExpr.h"
29 
30 #include "gtest/gtest.h"
31 
32 #include "boost/smart_ptr.hpp"
33 #include "boost/tuple/tuple.hpp"
34 #include "boost/variant/get.hpp"
35 #include "boost/variant/variant.hpp"
36 
37 
38 namespace {
39 
40  // The fixture for testing class Foo.
41  class PythonExprTest : public ::testing::Test {
42  protected:
43 
44  PythonExprTest() {
45  // You can do set-up work for each test here.
46  }
47 
48  virtual ~PythonExprTest() {
49  // You can do clean-up work that doesn't throw exceptions here.
50  }
51 
52  // If the constructor and destructor are not enough for setting up
53  // and cleaning up each test, you can define the following methods:
54 
55  virtual void SetUp() {
56  // Code here will be called immediately after the constructor (right
57  // before each test).
58  }
59 
60  virtual void TearDown() {
61  // Code here will be called immediately after each test (right
62  // before the destructor).
63  }
64  };
65 
66 
67  TEST_F(PythonExprTest, EvaluatePythonExpr) {
68 
70  double expected = 1.0;
71  expected *= 2.0;
72 
75  python = PythonExpression();
76  funcs.insert(std::pair<std::string, client::function::type>
77  ("python", python));
78 
79  std::string testexpr = "python(\"resources/test.py\", 1.0)";
80  boost::scoped_ptr<Expression> e(new Expression(testexpr, funcs));
81  Expressions::Result_t result;
82  EXPECT_NO_THROW({
83  result = e->evaluate(vars);
84  });
85 
86  ASSERT_EQ(expected, boost::get<0>(result));
87  ASSERT_TRUE(boost::get<1>(result));
88  }
89 
90 }
91 
92 int main(int argc, char **argv) {
93  ::testing::InitGoogleTest(&argc, argv);
94  return RUN_ALL_TESTS();
95 }
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