OPAL (Object Oriented Parallel Accelerator Library) 2024.2
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#include <tuple>
24#include <variant>
25
29#include "Expression/FromFile.h"
30
31#include "gtest/gtest.h"
32
33
34namespace {
35
36struct my_sqrt {
37
39
40 double value = std::get<double>(args[0]);
41 if (value < 0.0) return std::make_tuple(0.0, false);
42 return std::make_tuple(sqrt(value), true);
43 }
44};
45
46struct my_pow {
47
49
50 double base = std::get<double>(args[0]);
51 double exponent = std::get<double>(args[1]);
52 return std::make_tuple(pow(base, exponent), true);
53 }
54};
55
56 // The fixture for testing class Foo.
57 class FromFileExpressionTest : public ::testing::Test {
58 protected:
59
60 FromFileExpressionTest() {
61 // You can do set-up work for each test here.
62 }
63
64 virtual ~FromFileExpressionTest() {
65 // You can do clean-up work that doesn't throw exceptions here.
66 }
67
68 // If the constructor and destructor are not enough for setting up
69 // and cleaning up each test, you can define the following methods:
70
71 virtual void SetUp() {
72 // Code here will be called immediately after the constructor (right
73 // before each test).
74 }
75
76 virtual void TearDown() {
77 // Code here will be called immediately after each test (right
78 // before the destructor).
79 }
80 };
81
82
83 TEST_F(FromFileExpressionTest, EvaluateFromFileExpression) {
84
86 double expected = 0.3126 + 0.3561 + 0.4242;
87
90 ff = FromFile();
91 funcs.insert(std::pair<std::string, client::function::type>
92 ("fromFile", ff));
93
94 std::string testexpr = "fromFile(\"resources/fromfile_test.dat\")";
95 const std::unique_ptr<Expression> e(new Expression(testexpr, funcs));
97 EXPECT_NO_THROW({
98 result = e->evaluate(vars);
99 });
100
101 ASSERT_NEAR(expected, std::get<0>(result), 1e-6);
102 ASSERT_TRUE(std::get<1>(result));
103 }
104
105 TEST_F(FromFileExpressionTest, EvaluateCombinedFromFileExpression) {
106
108 double expected = sqrt(pow(0.3126 + 0.3561 + 0.4242, 2) + pow(0.1263 - 0.5613 + 0.2424, 2));
109
112 ff = FromFile();
113 funcs.insert(std::pair<std::string, client::function::type>
114 ("fromFile", ff));
115 client::function::type sqrt_func;
116 sqrt_func = my_sqrt();
117 funcs.insert(std::pair<std::string, client::function::type>
118 ("sqrt", sqrt_func));
119 client::function::type pow_func;
120 pow_func = my_pow();
121 funcs.insert(std::pair<std::string, client::function::type>
122 ("pow", pow_func));
123
124 std::string testexpr = "sqrt(pow(fromFile(\"resources/fromfile_test.dat\"), 2) + "
125 "pow(fromFile(\"resources/fromfile_test2.dat\"), 2))";
126 const std::unique_ptr<Expression> e(new Expression(testexpr, funcs));
128 EXPECT_NO_THROW({
129 result = e->evaluate(vars);
130 });
131
132 ASSERT_NEAR(expected, std::get<0>(result), 1e-6);
133 ASSERT_TRUE(std::get<1>(result));
134 }
135
136}
137
138int main(int argc, char **argv) {
139 ::testing::InitGoogleTest(&argc, argv);
140 return RUN_ALL_TESTS();
141}
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:55
std::map< std::string, double > variableDictionary_t
Definition Expression.h:54
int main(int argc, char **argv)
std::tuple< double, bool > Result_t
Definition Expression.h:65
constexpr double e
The value of.
Definition Physics.h:39
std::function< std::tuple< double, bool >(arguments_t)> type
Definition function.hpp:16
std::vector< argument_t > arguments_t
Definition function.hpp:14
Sampling method that reads design variable values from a text file.
Definition FromFile.h:50