31#include "gtest/gtest.h"
40 double value = std::get<double>(args[0]);
41 return std::make_tuple(
fabs(value),
true);
49 double base = std::get<double>(args[0]);
50 double exponent = std::get<double>(args[1]);
51 return std::make_tuple(
pow(base, exponent),
true);
57 class ExpressionTest :
public ::testing::Test {
64 virtual ~ExpressionTest() {
71 virtual void SetUp() {
76 virtual void TearDown() {
82 TEST_F(ExpressionTest, ParseExpression) {
90 TEST_F(ExpressionTest, RequestedVars) {
92 std::string testexpr =
"abs(A * A * 2.0 * b + 2.0)";
93 const std::unique_ptr<Expression>
e(
new Expression(testexpr));
95 std::set<std::string> reqVars =
e->getReqVars();
97 ASSERT_EQ(
static_cast<size_t>(2), reqVars.size());
101 TEST_F(ExpressionTest, EvaluateExpression) {
106 funcs.insert(std::pair<std::string, client::function::type>
109 std::string testexpr =
"abs(1.0 + A * A * 2.0 * B + 2.0)";
110 const std::unique_ptr<Expression>
e(
new Expression(testexpr, funcs));
116 vars.insert(std::pair<std::string, double>(
"A",
a));
117 vars.insert(std::pair<std::string, double>(
"B", b));
121 result =
e->evaluate(vars);
124 double expected =
fabs(1 +
a *
a * 2 * b + 2);
125 ASSERT_DOUBLE_EQ(expected, std::get<0>(result));
126 ASSERT_TRUE(std::get<1>(result));
130 TEST_F(ExpressionTest, EvaluateCombinedExpression) {
135 funcs.insert(std::pair<std::string, client::function::type>
138 std::string testexpr =
"abs(1.0 + A * 2.0) + abs(B + 2.0)";
139 const std::unique_ptr<Expression>
e(
new Expression(testexpr, funcs));
145 vars.insert(std::pair<std::string, double>(
"A",
a));
146 vars.insert(std::pair<std::string, double>(
"B", b));
150 result =
e->evaluate(vars);
153 double expected =
fabs(1 +
a * 2) +
fabs(b + 2);
154 ASSERT_DOUBLE_EQ(expected, std::get<0>(result));
155 ASSERT_TRUE(std::get<1>(result));
159 TEST_F(ExpressionTest, EvaluateNestedExpression) {
164 funcs.insert(std::pair<std::string, client::function::type>
168 funcs.insert(std::pair<std::string, client::function::type>
171 std::string testexpr =
"abs(1.0 + pow(A, 3) * 2.0) + abs(B + 2.0)";
172 const std::unique_ptr<Expression>
e(
new Expression(testexpr, funcs));
178 vars.insert(std::pair<std::string, double>(
"A",
a));
179 vars.insert(std::pair<std::string, double>(
"B", b));
183 result =
e->evaluate(vars);
186 double expected =
fabs(1 +
pow(
a, 3.0) * 2) +
fabs(b + 2);
187 ASSERT_DOUBLE_EQ(expected, std::get<0>(result));
188 ASSERT_TRUE(std::get<1>(result));
192 TEST_F(ExpressionTest, EvaluateBooleanExpression) {
194 std::string testexpr =
"a > 5.2 - 1e-6";
195 const std::unique_ptr<Expression>
e(
new Expression(testexpr));
200 vars.insert(std::pair<std::string, double>(
"a",
a));
204 result =
e->evaluate(vars);
207 bool expected =
true;
208 ASSERT_DOUBLE_EQ(expected, std::get<0>(result));
209 ASSERT_TRUE(std::get<1>(result));
217int main(
int argc,
char **argv) {
218 ::testing::InitGoogleTest(&argc, argv);
219 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
std::tuple< double, bool > Result_t
constexpr double e
The value of.
std::function< std::tuple< double, bool >(arguments_t)> type
std::vector< argument_t > arguments_t