29 #include "gtest/gtest.h"
31 #include "boost/smart_ptr.hpp"
32 #include "boost/tuple/tuple.hpp"
33 #include "boost/variant/get.hpp"
34 #include "boost/variant/variant.hpp"
44 double value = boost::get<double>(args[0]);
45 return boost::make_tuple(
fabs(value),
true);
53 double base = boost::get<double>(args[0]);
54 double exponent = boost::get<double>(args[1]);
55 return boost::make_tuple(
pow(base, exponent),
true);
61 class ExpressionTest :
public ::testing::Test {
68 virtual ~ExpressionTest() {
75 virtual void SetUp() {
80 virtual void TearDown() {
86 TEST_F(ExpressionTest, ParseExpression) {
94 TEST_F(ExpressionTest, RequestedVars) {
96 std::string testexpr =
"abs(A * A * 2.0 * b + 2.0)";
97 boost::scoped_ptr<Expression>
e(
new Expression(testexpr));
99 std::set<std::string> reqVars =
e->getReqVars();
101 ASSERT_EQ(
static_cast<size_t>(2), reqVars.size());
105 TEST_F(ExpressionTest, EvaluateExpression) {
110 funcs.insert(std::pair<std::string, client::function::type>
113 std::string testexpr =
"abs(1.0 + A * A * 2.0 * B + 2.0)";
114 boost::scoped_ptr<Expression>
e(
new Expression(testexpr, funcs));
120 vars.insert(std::pair<std::string, double>(
"A",
a));
121 vars.insert(std::pair<std::string, double>(
"B", b));
125 result =
e->evaluate(vars);
128 double expected =
fabs(1 +
a *
a * 2 * b + 2);
129 ASSERT_DOUBLE_EQ(expected, boost::get<0>(result));
130 ASSERT_TRUE(boost::get<1>(result));
134 TEST_F(ExpressionTest, EvaluateCombinedExpression) {
139 funcs.insert(std::pair<std::string, client::function::type>
142 std::string testexpr =
"abs(1.0 + A * 2.0) + abs(B + 2.0)";
143 boost::scoped_ptr<Expression>
e(
new Expression(testexpr, funcs));
149 vars.insert(std::pair<std::string, double>(
"A",
a));
150 vars.insert(std::pair<std::string, double>(
"B", b));
154 result =
e->evaluate(vars);
157 double expected =
fabs(1 +
a * 2) +
fabs(b + 2);
158 ASSERT_DOUBLE_EQ(expected, boost::get<0>(result));
159 ASSERT_TRUE(boost::get<1>(result));
163 TEST_F(ExpressionTest, EvaluateNestedExpression) {
168 funcs.insert(std::pair<std::string, client::function::type>
172 funcs.insert(std::pair<std::string, client::function::type>
175 std::string testexpr =
"abs(1.0 + pow(A, 3) * 2.0) + abs(B + 2.0)";
176 boost::scoped_ptr<Expression>
e(
new Expression(testexpr, funcs));
182 vars.insert(std::pair<std::string, double>(
"A",
a));
183 vars.insert(std::pair<std::string, double>(
"B", b));
187 result =
e->evaluate(vars);
190 double expected =
fabs(1 +
pow(
a, 3.0) * 2) +
fabs(b + 2);
191 ASSERT_DOUBLE_EQ(expected, boost::get<0>(result));
192 ASSERT_TRUE(boost::get<1>(result));
196 TEST_F(ExpressionTest, EvaluateBooleanExpression) {
198 std::string testexpr =
"a > 5.2 - 1e-6";
199 boost::scoped_ptr<Expression>
e(
new Expression(testexpr));
204 vars.insert(std::pair<std::string, double>(
"a",
a));
208 result =
e->evaluate(vars);
211 bool expected =
true;
212 ASSERT_DOUBLE_EQ(expected, boost::get<0>(result));
213 ASSERT_TRUE(boost::get<1>(result));
221 int main(
int argc,
char **argv) {
222 ::testing::InitGoogleTest(&argc, argv);
223 return RUN_ALL_TESTS();
Tps< T > pow(const Tps< T > &x, int y)
Integer power.
PETE_TUTree< FnFabs, typename T::PETE_Expr_t > fabs(const PETE_Expr< T > &l)
std::map< std::string, client::function::type > functionDictionary_t
std::map< std::string, double > variableDictionary_t
int main(int argc, char **argv)
OperatorType_t
distinguish different constraints
boost::tuple< double, bool > Result_t
constexpr double e
The value of.
boost::function< boost::tuple< double, bool >arguments_t)> type
std::vector< argument_t > arguments_t