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