9 #include "gtest/gtest.h"
11 #include "boost/smart_ptr.hpp"
12 #include "boost/tuple/tuple.hpp"
13 #include "boost/variant/get.hpp"
14 #include "boost/variant/variant.hpp"
24 double value = boost::get<double>(args[0]);
25 return boost::make_tuple(
fabs(value),
true);
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);
41 class ExpressionTest :
public ::testing::Test {
48 virtual ~ExpressionTest() {
55 virtual void SetUp() {
60 virtual void TearDown() {
66 TEST_F(ExpressionTest, ParseExpression) {
74 TEST_F(ExpressionTest, RequestedVars) {
76 std::string testexpr =
"abs(A * A * 2.0 * b + 2.0)";
77 boost::scoped_ptr<Expression>
e(
new Expression(testexpr));
79 std::set<std::string> reqVars =
e->getReqVars();
81 ASSERT_EQ(static_cast<size_t>(2), reqVars.size());
85 TEST_F(ExpressionTest, EvaluateExpression) {
90 funcs.insert(std::pair<std::string, client::function::type>
93 std::string testexpr =
"abs(1.0 + A * A * 2.0 * B + 2.0)";
94 boost::scoped_ptr<Expression>
e(
new Expression(testexpr, funcs));
100 vars.insert(std::pair<std::string, double>(
"A", a));
101 vars.insert(std::pair<std::string, double>(
"B", b));
105 result =
e->evaluate(vars);
108 double expected =
fabs(1 + a * a * 2 * b + 2);
109 ASSERT_DOUBLE_EQ(expected, boost::get<0>(result));
110 ASSERT_TRUE(boost::get<1>(result));
114 TEST_F(ExpressionTest, EvaluateCombinedExpression) {
119 funcs.insert(std::pair<std::string, client::function::type>
122 std::string testexpr =
"abs(1.0 + A * 2.0) + abs(B + 2.0)";
123 boost::scoped_ptr<Expression>
e(
new Expression(testexpr, funcs));
129 vars.insert(std::pair<std::string, double>(
"A", a));
130 vars.insert(std::pair<std::string, double>(
"B", b));
134 result =
e->evaluate(vars);
137 double expected =
fabs(1 + a * 2) +
fabs(b + 2);
138 ASSERT_DOUBLE_EQ(expected, boost::get<0>(result));
139 ASSERT_TRUE(boost::get<1>(result));
143 TEST_F(ExpressionTest, EvaluateNestedExpression) {
148 funcs.insert(std::pair<std::string, client::function::type>
152 funcs.insert(std::pair<std::string, client::function::type>
155 std::string testexpr =
"abs(1.0 + pow(A, 3) * 2.0) + abs(B + 2.0)";
156 boost::scoped_ptr<Expression>
e(
new Expression(testexpr, funcs));
162 vars.insert(std::pair<std::string, double>(
"A", a));
163 vars.insert(std::pair<std::string, double>(
"B", b));
167 result =
e->evaluate(vars);
170 double expected =
fabs(1 +
pow(a, 3.0) * 2) +
fabs(b + 2);
171 ASSERT_DOUBLE_EQ(expected, boost::get<0>(result));
172 ASSERT_TRUE(boost::get<1>(result));
176 TEST_F(ExpressionTest, EvaluateBooleanExpression) {
178 std::string testexpr =
"a > 5.2 - 1e-6";
179 boost::scoped_ptr<Expression>
e(
new Expression(testexpr));
184 vars.insert(std::pair<std::string, double>(
"a", a));
188 result =
e->evaluate(vars);
191 bool expected =
true;
192 ASSERT_DOUBLE_EQ(expected, boost::get<0>(result));
193 ASSERT_TRUE(boost::get<1>(result));
201 int main(
int argc,
char **argv) {
202 ::testing::InitGoogleTest(&argc, argv);
203 return RUN_ALL_TESTS();
constexpr double e
The value of .
PETE_TUTree< FnFabs, typename T::PETE_Expr_t > fabs(const PETE_Expr< T > &l)
Expression to be evaluated in the framework.
boost::tuple< double, bool > Result_t
std::vector< argument_t > arguments_t
Tps< T > pow(const Tps< T > &x, int y)
Integer power.
OperatorType_t
distinguish different constraints
int main(int argc, char *argv[])
boost::function< boost::tuple< double, bool >arguments_t)> type
std::map< std::string, double > variableDictionary_t
std::map< std::string, client::function::type > functionDictionary_t