Storm 1.11.1.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
ExpressionEvalutionTest.cpp
Go to the documentation of this file.
1#include "storm-config.h"
2#include "test/storm_gtest.h"
3
11
12TEST(ExpressionEvaluation, NaiveEvaluation) {
13 std::shared_ptr<storm::expressions::ExpressionManager> manager(new storm::expressions::ExpressionManager());
14
18 ASSERT_NO_THROW(x = manager->declareBooleanVariable("x"));
19 ASSERT_NO_THROW(y = manager->declareIntegerVariable("y"));
20 ASSERT_NO_THROW(z = manager->declareRationalVariable("z"));
21
23
24 storm::expressions::Expression iteExpression = storm::expressions::ite(x, y + z, manager->integer(3) * z);
25
26 eval.setRationalValue(z, 5.5);
27 eval.setBooleanValue(x, true);
28 for (int_fast64_t i = 0; i < 1000; ++i) {
29 eval.setIntegerValue(y, 3 + i);
30 EXPECT_NEAR(8.5 + i, iteExpression.evaluateAsDouble(&eval), 1e-6);
31 }
32
33 eval.setBooleanValue(x, false);
34 for (int_fast64_t i = 0; i < 1000; ++i) {
35 double zValue = i / static_cast<double>(10);
36 eval.setRationalValue(z, zValue);
37 EXPECT_NEAR(3 * zValue, iteExpression.evaluateAsDouble(&eval), 1e-6);
38 }
39}
40
41TEST(ExpressionEvaluation, ExprTkEvaluation) {
42 std::shared_ptr<storm::expressions::ExpressionManager> manager(new storm::expressions::ExpressionManager());
43
47 ASSERT_NO_THROW(x = manager->declareBooleanVariable("x"));
48 ASSERT_NO_THROW(y = manager->declareIntegerVariable("y"));
49 ASSERT_NO_THROW(z = manager->declareRationalVariable("z"));
50
51 storm::expressions::Expression iteExpression = storm::expressions::ite(x, y + z, manager->integer(3) * z);
53
54 eval.setRationalValue(z, 5.5);
55 eval.setBooleanValue(x, true);
56 for (int_fast64_t i = 0; i < 1000; ++i) {
57 eval.setIntegerValue(y, 3 + i);
58 EXPECT_NEAR(8.5 + i, eval.asRational(iteExpression), 1e-6);
59 }
60
61 eval.setBooleanValue(x, false);
62 for (int_fast64_t i = 0; i < 1000; ++i) {
63 double zValue = i / static_cast<double>(10);
64 eval.setRationalValue(z, zValue);
65 EXPECT_NEAR(3 * zValue, eval.asRational(iteExpression), 1e-6);
66 }
67}
68
69TEST(ExpressionEvaluation, NegativeModulo) {
70 std::shared_ptr<storm::expressions::ExpressionManager> manager(new storm::expressions::ExpressionManager());
71
72 storm::parser::ExpressionCreator creator(*manager);
73 std::unordered_map<std::string, storm::expressions::Expression> mapping;
74 creator.setIdentifierMapping(mapping);
75
76 storm::expressions::Expression n = manager->integer(-1);
77 storm::expressions::Expression mod = manager->integer(4);
78
79 bool pass = true;
81
82 auto positiveModulo = [](int a, int b) { return a >= 0 ? a % b : (a % b) + b; };
83
84 int expectedInt = positiveModulo(-1, 4);
85 double expectedDouble(expectedInt);
86
88 int result1 = evaluator.asInt(expr);
89 int result2 = expr.evaluateAsInt();
90 double result3 = evaluator.asRational(expr);
91 double result4 = expr.evaluateAsDouble();
92
93 EXPECT_EQ(result1, expectedInt);
94 EXPECT_EQ(result2, expectedInt);
95 EXPECT_NEAR(result3, expectedDouble, 1e-6);
96 EXPECT_NEAR(result4, expectedDouble, 1e-6);
97}
TEST(ExpressionEvaluation, NaiveEvaluation)
int_fast64_t evaluateAsInt(Valuation const *valuation=nullptr) const
Evaluates the expression under the valuation of variables given by the valuation and returns the resu...
double evaluateAsDouble(Valuation const *valuation=nullptr) const
Evaluates the expression under the valuation of variables given by the valuation and returns the resu...
This class is responsible for managing a set of typed variables and all expressions using these varia...
void setIntegerValue(storm::expressions::Variable const &variable, int_fast64_t value) override
void setBooleanValue(storm::expressions::Variable const &variable, bool value) override
int_fast64_t asInt(Expression const &expression) const override
void setRationalValue(storm::expressions::Variable const &variable, double value) override
double asRational(Expression const &expression) const override
A simple implementation of the valuation interface.
virtual void setIntegerValue(Variable const &integerVariable, int_fast64_t value) override
Sets the value of the given integer variable to the provided value.
virtual void setBooleanValue(Variable const &booleanVariable, bool value) override
Sets the value of the given boolean variable to the provided value.
virtual void setRationalValue(Variable const &rationalVariable, double value) override
Sets the value of the given boolean variable to the provided value.
void setIdentifierMapping(qi::symbols< char, storm::expressions::Expression > const *identifiers_)
Sets an identifier mapping that is used to determine valid variables in the expression.
storm::expressions::Expression createPowerModuloLogarithmExpression(storm::expressions::Expression const &e1, storm::expressions::OperatorType const &operatorType, storm::expressions::Expression const &e2, bool &pass) const
Expression ite(Expression const &condition, Expression const &thenExpression, Expression const &elseExpression)