OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
FromFileExpressionTest.cpp
Go to the documentation of this file.
1 #include <set>
2 #include <string>
3 
7 #include "Expression/FromFile.h"
8 
9 #include "gtest/gtest.h"
10 
11 #include "boost/smart_ptr.hpp"
12 #include "boost/tuple/tuple.hpp"
13 #include "boost/variant/get.hpp"
14 #include "boost/variant/variant.hpp"
15 
16 
17 namespace {
18 
19 struct my_sqrt {
20 
22 
23  double value = boost::get<double>(args[0]);
24  if (value < 0.0) return boost::make_tuple(0.0, false);
25  return boost::make_tuple(sqrt(value), true);
26  }
27 };
28 
29 struct my_pow {
30 
32 
33  double base = boost::get<double>(args[0]);
34  double exponent = boost::get<double>(args[1]);
35  return boost::make_tuple(pow(base, exponent), true);
36  }
37 };
38 
39  // The fixture for testing class Foo.
40  class FromFileExpressionTest : public ::testing::Test {
41  protected:
42 
43  FromFileExpressionTest() {
44  // You can do set-up work for each test here.
45  }
46 
47  virtual ~FromFileExpressionTest() {
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(FromFileExpressionTest, EvaluateFromFileExpression) {
67 
69  double expected = 0.3126 + 0.3561 + 0.4242;
70 
73  ff = FromFile();
74  funcs.insert(std::pair<std::string, client::function::type>
75  ("fromFile", ff));
76 
77  std::string testexpr = "fromFile(\"resources/fromfile_test.dat\")";
78  boost::scoped_ptr<Expression> e(new Expression(testexpr, funcs));
79  Expressions::Result_t result;
80  EXPECT_NO_THROW({
81  result = e->evaluate(vars);
82  });
83 
84  ASSERT_NEAR(expected, boost::get<0>(result), 1e-6);
85  ASSERT_TRUE(boost::get<1>(result));
86  }
87 
88  TEST_F(FromFileExpressionTest, EvaluateCombinedFromFileExpression) {
89 
91  double expected = sqrt(pow(0.3126 + 0.3561 + 0.4242, 2) + pow(0.1263 - 0.5613 + 0.2424, 2));
92 
95  ff = FromFile();
96  funcs.insert(std::pair<std::string, client::function::type>
97  ("fromFile", ff));
98  client::function::type sqrt_func;
99  sqrt_func = my_sqrt();
100  funcs.insert(std::pair<std::string, client::function::type>
101  ("sqrt", sqrt_func));
102  client::function::type pow_func;
103  pow_func = my_pow();
104  funcs.insert(std::pair<std::string, client::function::type>
105  ("pow", pow_func));
106 
107  std::string testexpr = "sqrt(pow(fromFile(\"resources/fromfile_test.dat\"), 2) + "
108  "pow(fromFile(\"resources/fromfile_test2.dat\"), 2))";
109  boost::scoped_ptr<Expression> e(new Expression(testexpr, funcs));
110  Expressions::Result_t result;
111  EXPECT_NO_THROW({
112  result = e->evaluate(vars);
113  });
114 
115  ASSERT_NEAR(expected, boost::get<0>(result), 1e-6);
116  ASSERT_TRUE(boost::get<1>(result));
117  }
118 
119 }
120 
121 int main(int argc, char **argv) {
122  ::testing::InitGoogleTest(&argc, argv);
123  return RUN_ALL_TESTS();
124 }
constexpr double e
The value of .
Definition: Physics.h:40
Expression to be evaluated in the framework.
Definition: Expression.h:75
boost::tuple< double, bool > Result_t
Definition: Expression.h:37
std::vector< argument_t > arguments_t
Definition: function.hpp:19
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
int main(int argc, char *argv[])
Definition: Main.cpp:107
boost::function< boost::tuple< double, bool >arguments_t)> type
Definition: function.hpp:21
double FromFile(std::string file, const std::vector< double > &referencePoint)
std::map< std::string, double > variableDictionary_t
Definition: Expression.h:26
std::map< std::string, client::function::type > functionDictionary_t
Definition: Expression.h:27