You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
2.5 KiB

  1. #include "gtest/gtest.h"
  2. #include "src/storage/expressions/Expression.h"
  3. #include "src/storage/expressions/ExpressionManager.h"
  4. #include "src/storage/expressions/SimpleValuation.h"
  5. #include "src/storage/expressions/ExprtkExpressionEvaluator.h"
  6. TEST(ExpressionEvaluation, NaiveEvaluation) {
  7. std::shared_ptr<storm::expressions::ExpressionManager> manager(new storm::expressions::ExpressionManager());
  8. storm::expressions::Variable x;
  9. storm::expressions::Variable y;
  10. storm::expressions::Variable z;
  11. ASSERT_NO_THROW(x = manager->declareBooleanVariable("x"));
  12. ASSERT_NO_THROW(y = manager->declareIntegerVariable("y"));
  13. ASSERT_NO_THROW(z = manager->declareRationalVariable("z"));
  14. storm::expressions::SimpleValuation eval(manager);
  15. storm::expressions::Expression iteExpression = storm::expressions::ite(x, y + z, manager->integer(3) * z);
  16. eval.setRationalValue(z, 5.5);
  17. eval.setBooleanValue(x, true);
  18. for (int_fast64_t i = 0; i < 1000; ++i) {
  19. eval.setIntegerValue(y, 3 + i);
  20. EXPECT_NEAR(8.5 + i, iteExpression.evaluateAsDouble(&eval), 1e-6);
  21. }
  22. eval.setBooleanValue(x, false);
  23. for (int_fast64_t i = 0; i < 1000; ++i) {
  24. double zValue = i / static_cast<double>(10);
  25. eval.setRationalValue(z, zValue);
  26. EXPECT_NEAR(3 * zValue, iteExpression.evaluateAsDouble(&eval), 1e-6);
  27. }
  28. }
  29. TEST(ExpressionEvaluation, ExprTkEvaluation) {
  30. std::shared_ptr<storm::expressions::ExpressionManager> manager(new storm::expressions::ExpressionManager());
  31. storm::expressions::Variable x;
  32. storm::expressions::Variable y;
  33. storm::expressions::Variable z;
  34. ASSERT_NO_THROW(x = manager->declareBooleanVariable("x"));
  35. ASSERT_NO_THROW(y = manager->declareIntegerVariable("y"));
  36. ASSERT_NO_THROW(z = manager->declareRationalVariable("z"));
  37. storm::expressions::Expression iteExpression = storm::expressions::ite(x, y + z, manager->integer(3) * z);
  38. storm::expressions::ExprtkExpressionEvaluator eval(*manager);
  39. eval.setRationalValue(z, 5.5);
  40. eval.setBooleanValue(x, true);
  41. for (int_fast64_t i = 0; i < 1000; ++i) {
  42. eval.setIntegerValue(y, 3 + i);
  43. EXPECT_NEAR(8.5 + i, eval.asRational(iteExpression), 1e-6);
  44. }
  45. eval.setBooleanValue(x, false);
  46. for (int_fast64_t i = 0; i < 1000; ++i) {
  47. double zValue = i / static_cast<double>(10);
  48. eval.setRationalValue(z, zValue);
  49. EXPECT_NEAR(3 * zValue, eval.asRational(iteExpression), 1e-6);
  50. }
  51. }