29 #include "gtest/gtest.h"
31 #include "boost/tuple/tuple.hpp"
32 #include "boost/variant/get.hpp"
33 #include "boost/variant/variant.hpp"
43 double value = boost::get<double>(args[0]);
44 return boost::make_tuple(
fabs(value),
true);
52 double base = boost::get<double>(args[0]);
53 double exponent = boost::get<double>(args[1]);
54 return boost::make_tuple(
pow(base, exponent),
true);
60 class ExpressionTest :
public ::testing::Test {
67 virtual ~ExpressionTest() {
74 virtual void SetUp() {
79 virtual void TearDown() {
85 TEST_F(ExpressionTest, ParseExpression) {
93 TEST_F(ExpressionTest, RequestedVars) {
95 std::string testexpr =
"abs(A * A * 2.0 * b + 2.0)";
96 const std::unique_ptr<Expression>
e(
new Expression(testexpr));
98 std::set<std::string> reqVars =
e->getReqVars();
100 ASSERT_EQ(static_cast<size_t>(2), reqVars.size());
104 TEST_F(ExpressionTest, EvaluateExpression) {
109 funcs.insert(std::pair<std::string, client::function::type>
112 std::string testexpr =
"abs(1.0 + A * A * 2.0 * B + 2.0)";
113 const std::unique_ptr<Expression>
e(
new Expression(testexpr, funcs));
119 vars.insert(std::pair<std::string, double>(
"A", a));
120 vars.insert(std::pair<std::string, double>(
"B", b));
124 result =
e->evaluate(vars);
127 double expected =
fabs(1 + a * a * 2 * b + 2);
128 ASSERT_DOUBLE_EQ(expected, boost::get<0>(result));
129 ASSERT_TRUE(boost::get<1>(result));
133 TEST_F(ExpressionTest, EvaluateCombinedExpression) {
138 funcs.insert(std::pair<std::string, client::function::type>
141 std::string testexpr =
"abs(1.0 + A * 2.0) + abs(B + 2.0)";
142 const std::unique_ptr<Expression>
e(
new Expression(testexpr, funcs));
148 vars.insert(std::pair<std::string, double>(
"A", a));
149 vars.insert(std::pair<std::string, double>(
"B", b));
153 result =
e->evaluate(vars);
156 double expected =
fabs(1 + a * 2) +
fabs(b + 2);
157 ASSERT_DOUBLE_EQ(expected, boost::get<0>(result));
158 ASSERT_TRUE(boost::get<1>(result));
162 TEST_F(ExpressionTest, EvaluateNestedExpression) {
167 funcs.insert(std::pair<std::string, client::function::type>
171 funcs.insert(std::pair<std::string, client::function::type>
174 std::string testexpr =
"abs(1.0 + pow(A, 3) * 2.0) + abs(B + 2.0)";
175 const std::unique_ptr<Expression>
e(
new Expression(testexpr, funcs));
181 vars.insert(std::pair<std::string, double>(
"A", a));
182 vars.insert(std::pair<std::string, double>(
"B", b));
186 result =
e->evaluate(vars);
189 double expected =
fabs(1 +
pow(a, 3.0) * 2) +
fabs(b + 2);
190 ASSERT_DOUBLE_EQ(expected, boost::get<0>(result));
191 ASSERT_TRUE(boost::get<1>(result));
195 TEST_F(ExpressionTest, EvaluateBooleanExpression) {
197 std::string testexpr =
"a > 5.2 - 1e-6";
198 const std::unique_ptr<Expression>
e(
new Expression(testexpr));
203 vars.insert(std::pair<std::string, double>(
"a", a));
207 result =
e->evaluate(vars);
210 bool expected =
true;
211 ASSERT_DOUBLE_EQ(expected, boost::get<0>(result));
212 ASSERT_TRUE(boost::get<1>(result));
220 int main(
int argc,
char **argv) {
221 ::testing::InitGoogleTest(&argc, argv);
222 return RUN_ALL_TESTS();
int main(int argc, char *argv[])
boost::function< boost::tuple< double, bool >arguments_t)> type
std::vector< argument_t > arguments_t
std::map< std::string, double > variableDictionary_t
PETE_TUTree< FnFabs, typename T::PETE_Expr_t > fabs(const PETE_Expr< T > &l)
std::map< std::string, client::function::type > functionDictionary_t
OperatorType_t
distinguish different constraints
constexpr double e
The value of .
Tps< T > pow(const Tps< T > &x, int y)
Integer power.
boost::tuple< double, bool > Result_t