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.

389 lines
26 KiB

  1. #include <map>
  2. #include <string>
  3. #include "gtest/gtest.h"
  4. #include "src/storage/expressions/Expression.h"
  5. #include "src/storage/expressions/SimpleValuation.h"
  6. #include "src/exceptions/InvalidTypeException.h"
  7. TEST(Expression, FactoryMethodTest) {
  8. EXPECT_NO_THROW(storm::expressions::Expression::createBooleanLiteral(true));
  9. EXPECT_NO_THROW(storm::expressions::Expression::createTrue());
  10. EXPECT_NO_THROW(storm::expressions::Expression::createFalse());
  11. EXPECT_NO_THROW(storm::expressions::Expression::createIntegerLiteral(3));
  12. EXPECT_NO_THROW(storm::expressions::Expression::createDoubleLiteral(3.14));
  13. EXPECT_NO_THROW(storm::expressions::Expression::createBooleanVariable("x"));
  14. EXPECT_NO_THROW(storm::expressions::Expression::createIntegerVariable("y"));
  15. EXPECT_NO_THROW(storm::expressions::Expression::createDoubleVariable("z"));
  16. EXPECT_NO_THROW(storm::expressions::Expression::createBooleanConstant("a"));
  17. EXPECT_NO_THROW(storm::expressions::Expression::createIntegerConstant("b"));
  18. EXPECT_NO_THROW(storm::expressions::Expression::createDoubleConstant("c"));
  19. }
  20. TEST(Expression, AccessorTest) {
  21. storm::expressions::Expression trueExpression;
  22. storm::expressions::Expression falseExpression;
  23. storm::expressions::Expression threeExpression;
  24. storm::expressions::Expression piExpression;
  25. storm::expressions::Expression boolVarExpression;
  26. storm::expressions::Expression intVarExpression;
  27. storm::expressions::Expression doubleVarExpression;
  28. storm::expressions::Expression boolConstExpression;
  29. storm::expressions::Expression intConstExpression;
  30. storm::expressions::Expression doubleConstExpression;
  31. ASSERT_NO_THROW(trueExpression = storm::expressions::Expression::createTrue());
  32. ASSERT_NO_THROW(falseExpression = storm::expressions::Expression::createFalse());
  33. ASSERT_NO_THROW(threeExpression = storm::expressions::Expression::createIntegerLiteral(3));
  34. ASSERT_NO_THROW(piExpression = storm::expressions::Expression::createDoubleLiteral(3.14));
  35. ASSERT_NO_THROW(boolVarExpression = storm::expressions::Expression::createBooleanVariable("x"));
  36. ASSERT_NO_THROW(intVarExpression = storm::expressions::Expression::createIntegerVariable("y"));
  37. ASSERT_NO_THROW(doubleVarExpression = storm::expressions::Expression::createDoubleVariable("z"));
  38. ASSERT_NO_THROW(boolConstExpression = storm::expressions::Expression::createBooleanConstant("a"));
  39. ASSERT_NO_THROW(intConstExpression = storm::expressions::Expression::createIntegerConstant("b"));
  40. ASSERT_NO_THROW(doubleConstExpression = storm::expressions::Expression::createDoubleConstant("c"));
  41. EXPECT_TRUE(trueExpression.getReturnType() == storm::expressions::ExpressionReturnType::Bool);
  42. EXPECT_TRUE(trueExpression.isConstant());
  43. EXPECT_TRUE(trueExpression.isTrue());
  44. EXPECT_FALSE(trueExpression.isFalse());
  45. EXPECT_TRUE(trueExpression.getVariables() == std::set<std::string>());
  46. EXPECT_TRUE(trueExpression.getConstants() == std::set<std::string>());
  47. EXPECT_TRUE(falseExpression.getReturnType() == storm::expressions::ExpressionReturnType::Bool);
  48. EXPECT_TRUE(falseExpression.isConstant());
  49. EXPECT_FALSE(falseExpression.isTrue());
  50. EXPECT_TRUE(falseExpression.isFalse());
  51. EXPECT_TRUE(falseExpression.getVariables() == std::set<std::string>());
  52. EXPECT_TRUE(falseExpression.getConstants() == std::set<std::string>());
  53. EXPECT_TRUE(threeExpression.getReturnType() == storm::expressions::ExpressionReturnType::Int);
  54. EXPECT_TRUE(threeExpression.isConstant());
  55. EXPECT_FALSE(threeExpression.isTrue());
  56. EXPECT_FALSE(threeExpression.isFalse());
  57. EXPECT_TRUE(threeExpression.getVariables() == std::set<std::string>());
  58. EXPECT_TRUE(threeExpression.getConstants() == std::set<std::string>());
  59. EXPECT_TRUE(piExpression.getReturnType() == storm::expressions::ExpressionReturnType::Double);
  60. EXPECT_TRUE(piExpression.isConstant());
  61. EXPECT_FALSE(piExpression.isTrue());
  62. EXPECT_FALSE(piExpression.isFalse());
  63. EXPECT_TRUE(piExpression.getVariables() == std::set<std::string>());
  64. EXPECT_TRUE(piExpression.getConstants() == std::set<std::string>());
  65. EXPECT_TRUE(boolVarExpression.getReturnType() == storm::expressions::ExpressionReturnType::Bool);
  66. EXPECT_FALSE(boolVarExpression.isConstant());
  67. EXPECT_FALSE(boolVarExpression.isTrue());
  68. EXPECT_FALSE(boolVarExpression.isFalse());
  69. EXPECT_TRUE(boolVarExpression.getVariables() == std::set<std::string>({"x"}));
  70. EXPECT_TRUE(boolVarExpression.getConstants() == std::set<std::string>());
  71. EXPECT_TRUE(intVarExpression.getReturnType() == storm::expressions::ExpressionReturnType::Int);
  72. EXPECT_FALSE(intVarExpression.isConstant());
  73. EXPECT_FALSE(intVarExpression.isTrue());
  74. EXPECT_FALSE(intVarExpression.isFalse());
  75. EXPECT_TRUE(intVarExpression.getVariables() == std::set<std::string>({"y"}));
  76. EXPECT_TRUE(intVarExpression.getConstants() == std::set<std::string>());
  77. EXPECT_TRUE(doubleVarExpression.getReturnType() == storm::expressions::ExpressionReturnType::Double);
  78. EXPECT_FALSE(doubleVarExpression.isConstant());
  79. EXPECT_FALSE(doubleVarExpression.isTrue());
  80. EXPECT_FALSE(doubleVarExpression.isFalse());
  81. EXPECT_TRUE(doubleVarExpression.getVariables() == std::set<std::string>({"z"}));
  82. EXPECT_TRUE(doubleVarExpression.getConstants() == std::set<std::string>());
  83. EXPECT_TRUE(boolConstExpression.getReturnType() == storm::expressions::ExpressionReturnType::Bool);
  84. EXPECT_FALSE(boolConstExpression.isConstant());
  85. EXPECT_FALSE(boolConstExpression.isTrue());
  86. EXPECT_FALSE(boolConstExpression.isFalse());
  87. EXPECT_TRUE(boolConstExpression.getVariables() == std::set<std::string>());
  88. EXPECT_TRUE(boolConstExpression.getConstants() == std::set<std::string>({"a"}));
  89. EXPECT_TRUE(intConstExpression.getReturnType() == storm::expressions::ExpressionReturnType::Int);
  90. EXPECT_FALSE(intConstExpression.isConstant());
  91. EXPECT_FALSE(intConstExpression.isTrue());
  92. EXPECT_FALSE(intConstExpression.isFalse());
  93. EXPECT_TRUE(intConstExpression.getVariables() == std::set<std::string>());
  94. EXPECT_TRUE(intConstExpression.getConstants() == std::set<std::string>({"b"}));
  95. EXPECT_TRUE(doubleConstExpression.getReturnType() == storm::expressions::ExpressionReturnType::Double);
  96. EXPECT_FALSE(doubleConstExpression.isConstant());
  97. EXPECT_FALSE(doubleConstExpression.isTrue());
  98. EXPECT_FALSE(doubleConstExpression.isFalse());
  99. EXPECT_TRUE(doubleConstExpression.getVariables() == std::set<std::string>());
  100. EXPECT_TRUE(doubleConstExpression.getConstants() == std::set<std::string>({"c"}));
  101. }
  102. TEST(Expression, OperatorTest) {
  103. storm::expressions::Expression trueExpression;
  104. storm::expressions::Expression falseExpression;
  105. storm::expressions::Expression threeExpression;
  106. storm::expressions::Expression piExpression;
  107. storm::expressions::Expression boolVarExpression;
  108. storm::expressions::Expression intVarExpression;
  109. storm::expressions::Expression doubleVarExpression;
  110. storm::expressions::Expression boolConstExpression;
  111. storm::expressions::Expression intConstExpression;
  112. storm::expressions::Expression doubleConstExpression;
  113. ASSERT_NO_THROW(trueExpression = storm::expressions::Expression::createTrue());
  114. ASSERT_NO_THROW(falseExpression = storm::expressions::Expression::createFalse());
  115. ASSERT_NO_THROW(threeExpression = storm::expressions::Expression::createIntegerLiteral(3));
  116. ASSERT_NO_THROW(piExpression = storm::expressions::Expression::createDoubleLiteral(3.14));
  117. ASSERT_NO_THROW(boolVarExpression = storm::expressions::Expression::createBooleanVariable("x"));
  118. ASSERT_NO_THROW(intVarExpression = storm::expressions::Expression::createIntegerVariable("y"));
  119. ASSERT_NO_THROW(doubleVarExpression = storm::expressions::Expression::createDoubleVariable("z"));
  120. ASSERT_NO_THROW(boolConstExpression = storm::expressions::Expression::createBooleanConstant("a"));
  121. ASSERT_NO_THROW(intConstExpression = storm::expressions::Expression::createIntegerConstant("b"));
  122. ASSERT_NO_THROW(doubleConstExpression = storm::expressions::Expression::createDoubleConstant("c"));
  123. storm::expressions::Expression tempExpression;
  124. ASSERT_THROW(tempExpression = trueExpression.ite(falseExpression, piExpression), storm::exceptions::InvalidTypeException);
  125. ASSERT_NO_THROW(tempExpression = boolConstExpression.ite(threeExpression, doubleVarExpression));
  126. EXPECT_TRUE(tempExpression.getReturnType() == storm::expressions::ExpressionReturnType::Double);
  127. ASSERT_NO_THROW(tempExpression = boolConstExpression.ite(threeExpression, intVarExpression));
  128. EXPECT_TRUE(tempExpression.getReturnType() == storm::expressions::ExpressionReturnType::Int);
  129. ASSERT_NO_THROW(tempExpression = boolConstExpression.ite(trueExpression, falseExpression));
  130. EXPECT_TRUE(tempExpression.getReturnType() == storm::expressions::ExpressionReturnType::Bool);
  131. ASSERT_THROW(tempExpression = trueExpression + piExpression, storm::exceptions::InvalidTypeException);
  132. ASSERT_NO_THROW(tempExpression = threeExpression + threeExpression);
  133. EXPECT_TRUE(tempExpression.getReturnType() == storm::expressions::ExpressionReturnType::Int);
  134. ASSERT_NO_THROW(tempExpression = threeExpression + piExpression);
  135. EXPECT_TRUE(tempExpression.getReturnType() == storm::expressions::ExpressionReturnType::Double);
  136. ASSERT_NO_THROW(tempExpression = doubleVarExpression + doubleConstExpression);
  137. EXPECT_TRUE(tempExpression.getReturnType() == storm::expressions::ExpressionReturnType::Double);
  138. ASSERT_THROW(tempExpression = trueExpression - piExpression, storm::exceptions::InvalidTypeException);
  139. ASSERT_NO_THROW(tempExpression = threeExpression - threeExpression);
  140. EXPECT_TRUE(tempExpression.getReturnType() == storm::expressions::ExpressionReturnType::Int);
  141. ASSERT_NO_THROW(tempExpression = threeExpression - piExpression);
  142. EXPECT_TRUE(tempExpression.getReturnType() == storm::expressions::ExpressionReturnType::Double);
  143. ASSERT_NO_THROW(tempExpression = doubleVarExpression - doubleConstExpression);
  144. EXPECT_TRUE(tempExpression.getReturnType() == storm::expressions::ExpressionReturnType::Double);
  145. ASSERT_THROW(tempExpression = -trueExpression, storm::exceptions::InvalidTypeException);
  146. ASSERT_NO_THROW(tempExpression = -threeExpression);
  147. EXPECT_TRUE(tempExpression.getReturnType() == storm::expressions::ExpressionReturnType::Int);
  148. ASSERT_NO_THROW(tempExpression = -piExpression);
  149. EXPECT_TRUE(tempExpression.getReturnType() == storm::expressions::ExpressionReturnType::Double);
  150. ASSERT_NO_THROW(tempExpression = -doubleVarExpression);
  151. EXPECT_TRUE(tempExpression.getReturnType() == storm::expressions::ExpressionReturnType::Double);
  152. ASSERT_THROW(tempExpression = trueExpression * piExpression, storm::exceptions::InvalidTypeException);
  153. ASSERT_NO_THROW(tempExpression = threeExpression * threeExpression);
  154. EXPECT_TRUE(tempExpression.getReturnType() == storm::expressions::ExpressionReturnType::Int);
  155. ASSERT_NO_THROW(tempExpression = threeExpression * piExpression);
  156. EXPECT_TRUE(tempExpression.getReturnType() == storm::expressions::ExpressionReturnType::Double);
  157. ASSERT_NO_THROW(tempExpression = intVarExpression * intConstExpression);
  158. EXPECT_TRUE(tempExpression.getReturnType() == storm::expressions::ExpressionReturnType::Int);
  159. ASSERT_THROW(tempExpression = trueExpression / piExpression, storm::exceptions::InvalidTypeException);
  160. ASSERT_NO_THROW(tempExpression = threeExpression / threeExpression);
  161. EXPECT_TRUE(tempExpression.getReturnType() == storm::expressions::ExpressionReturnType::Int);
  162. ASSERT_NO_THROW(tempExpression = threeExpression / piExpression);
  163. EXPECT_TRUE(tempExpression.getReturnType() == storm::expressions::ExpressionReturnType::Double);
  164. ASSERT_NO_THROW(tempExpression = doubleVarExpression / intConstExpression);
  165. EXPECT_TRUE(tempExpression.getReturnType() == storm::expressions::ExpressionReturnType::Double);
  166. ASSERT_THROW(tempExpression = trueExpression && piExpression, storm::exceptions::InvalidTypeException);
  167. ASSERT_NO_THROW(tempExpression = trueExpression && falseExpression);
  168. EXPECT_TRUE(tempExpression.getReturnType() == storm::expressions::ExpressionReturnType::Bool);
  169. ASSERT_NO_THROW(tempExpression = boolVarExpression && boolConstExpression);
  170. EXPECT_TRUE(tempExpression.getReturnType() == storm::expressions::ExpressionReturnType::Bool);
  171. ASSERT_THROW(tempExpression = trueExpression || piExpression, storm::exceptions::InvalidTypeException);
  172. ASSERT_NO_THROW(tempExpression = trueExpression || falseExpression);
  173. EXPECT_TRUE(tempExpression.getReturnType() == storm::expressions::ExpressionReturnType::Bool);
  174. ASSERT_NO_THROW(tempExpression = boolVarExpression || boolConstExpression);
  175. EXPECT_TRUE(tempExpression.getReturnType() == storm::expressions::ExpressionReturnType::Bool);
  176. ASSERT_THROW(tempExpression = !threeExpression, storm::exceptions::InvalidTypeException);
  177. ASSERT_NO_THROW(tempExpression = !trueExpression);
  178. EXPECT_TRUE(tempExpression.getReturnType() == storm::expressions::ExpressionReturnType::Bool);
  179. ASSERT_NO_THROW(tempExpression = !boolVarExpression);
  180. EXPECT_TRUE(tempExpression.getReturnType() == storm::expressions::ExpressionReturnType::Bool);
  181. ASSERT_THROW(tempExpression = trueExpression == piExpression, storm::exceptions::InvalidTypeException);
  182. ASSERT_NO_THROW(tempExpression = threeExpression == threeExpression);
  183. EXPECT_TRUE(tempExpression.getReturnType() == storm::expressions::ExpressionReturnType::Bool);
  184. ASSERT_NO_THROW(tempExpression = intVarExpression == doubleConstExpression);
  185. EXPECT_TRUE(tempExpression.getReturnType() == storm::expressions::ExpressionReturnType::Bool);
  186. ASSERT_THROW(tempExpression = trueExpression != piExpression, storm::exceptions::InvalidTypeException);
  187. ASSERT_NO_THROW(tempExpression = threeExpression != threeExpression);
  188. EXPECT_TRUE(tempExpression.getReturnType() == storm::expressions::ExpressionReturnType::Bool);
  189. ASSERT_NO_THROW(tempExpression = intVarExpression != doubleConstExpression);
  190. EXPECT_TRUE(tempExpression.getReturnType() == storm::expressions::ExpressionReturnType::Bool);
  191. ASSERT_THROW(tempExpression = trueExpression > piExpression, storm::exceptions::InvalidTypeException);
  192. ASSERT_NO_THROW(tempExpression = threeExpression > threeExpression);
  193. EXPECT_TRUE(tempExpression.getReturnType() == storm::expressions::ExpressionReturnType::Bool);
  194. ASSERT_NO_THROW(tempExpression = intVarExpression > doubleConstExpression);
  195. EXPECT_TRUE(tempExpression.getReturnType() == storm::expressions::ExpressionReturnType::Bool);
  196. ASSERT_THROW(tempExpression = trueExpression >= piExpression, storm::exceptions::InvalidTypeException);
  197. ASSERT_NO_THROW(tempExpression = threeExpression >= threeExpression);
  198. EXPECT_TRUE(tempExpression.getReturnType() == storm::expressions::ExpressionReturnType::Bool);
  199. ASSERT_NO_THROW(tempExpression = intVarExpression >= doubleConstExpression);
  200. EXPECT_TRUE(tempExpression.getReturnType() == storm::expressions::ExpressionReturnType::Bool);
  201. ASSERT_THROW(tempExpression = trueExpression < piExpression, storm::exceptions::InvalidTypeException);
  202. ASSERT_NO_THROW(tempExpression = threeExpression < threeExpression);
  203. EXPECT_TRUE(tempExpression.getReturnType() == storm::expressions::ExpressionReturnType::Bool);
  204. ASSERT_NO_THROW(tempExpression = intVarExpression < doubleConstExpression);
  205. EXPECT_TRUE(tempExpression.getReturnType() == storm::expressions::ExpressionReturnType::Bool);
  206. ASSERT_THROW(tempExpression = trueExpression <= piExpression, storm::exceptions::InvalidTypeException);
  207. ASSERT_NO_THROW(tempExpression = threeExpression <= threeExpression);
  208. EXPECT_TRUE(tempExpression.getReturnType() == storm::expressions::ExpressionReturnType::Bool);
  209. ASSERT_NO_THROW(tempExpression = intVarExpression <= doubleConstExpression);
  210. EXPECT_TRUE(tempExpression.getReturnType() == storm::expressions::ExpressionReturnType::Bool);
  211. ASSERT_THROW(tempExpression = storm::expressions::Expression::minimum(trueExpression, piExpression), storm::exceptions::InvalidTypeException);
  212. ASSERT_NO_THROW(tempExpression = storm::expressions::Expression::minimum(threeExpression, threeExpression));
  213. EXPECT_TRUE(tempExpression.getReturnType() == storm::expressions::ExpressionReturnType::Int);
  214. ASSERT_NO_THROW(tempExpression = storm::expressions::Expression::minimum(intVarExpression, doubleConstExpression));
  215. EXPECT_TRUE(tempExpression.getReturnType() == storm::expressions::ExpressionReturnType::Double);
  216. ASSERT_THROW(tempExpression = storm::expressions::Expression::maximum(trueExpression, piExpression), storm::exceptions::InvalidTypeException);
  217. ASSERT_NO_THROW(tempExpression = storm::expressions::Expression::maximum(threeExpression, threeExpression));
  218. EXPECT_TRUE(tempExpression.getReturnType() == storm::expressions::ExpressionReturnType::Int);
  219. ASSERT_NO_THROW(tempExpression = storm::expressions::Expression::maximum(intVarExpression, doubleConstExpression));
  220. EXPECT_TRUE(tempExpression.getReturnType() == storm::expressions::ExpressionReturnType::Double);
  221. ASSERT_THROW(tempExpression = trueExpression.implies(piExpression), storm::exceptions::InvalidTypeException);
  222. ASSERT_NO_THROW(tempExpression = trueExpression.implies(falseExpression));
  223. EXPECT_TRUE(tempExpression.getReturnType() == storm::expressions::ExpressionReturnType::Bool);
  224. ASSERT_NO_THROW(tempExpression = boolVarExpression.implies(boolConstExpression));
  225. EXPECT_TRUE(tempExpression.getReturnType() == storm::expressions::ExpressionReturnType::Bool);
  226. ASSERT_THROW(tempExpression = trueExpression.iff(piExpression), storm::exceptions::InvalidTypeException);
  227. ASSERT_NO_THROW(tempExpression = trueExpression.iff(falseExpression));
  228. EXPECT_TRUE(tempExpression.getReturnType() == storm::expressions::ExpressionReturnType::Bool);
  229. ASSERT_NO_THROW(tempExpression = boolVarExpression.iff(boolConstExpression));
  230. EXPECT_TRUE(tempExpression.getReturnType() == storm::expressions::ExpressionReturnType::Bool);
  231. ASSERT_THROW(tempExpression = trueExpression ^ piExpression, storm::exceptions::InvalidTypeException);
  232. ASSERT_NO_THROW(tempExpression = trueExpression ^ falseExpression);
  233. EXPECT_TRUE(tempExpression.getReturnType() == storm::expressions::ExpressionReturnType::Bool);
  234. ASSERT_NO_THROW(tempExpression = boolVarExpression ^ boolConstExpression);
  235. EXPECT_TRUE(tempExpression.getReturnType() == storm::expressions::ExpressionReturnType::Bool);
  236. ASSERT_THROW(tempExpression = trueExpression.floor(), storm::exceptions::InvalidTypeException);
  237. ASSERT_NO_THROW(tempExpression = threeExpression.floor());
  238. EXPECT_TRUE(tempExpression.getReturnType() == storm::expressions::ExpressionReturnType::Int);
  239. ASSERT_NO_THROW(tempExpression = doubleConstExpression.floor());
  240. EXPECT_TRUE(tempExpression.getReturnType() == storm::expressions::ExpressionReturnType::Int);
  241. ASSERT_THROW(tempExpression = trueExpression.ceil(), storm::exceptions::InvalidTypeException);
  242. ASSERT_NO_THROW(tempExpression = threeExpression.ceil());
  243. EXPECT_TRUE(tempExpression.getReturnType() == storm::expressions::ExpressionReturnType::Int);
  244. ASSERT_NO_THROW(tempExpression = doubleConstExpression.ceil());
  245. EXPECT_TRUE(tempExpression.getReturnType() == storm::expressions::ExpressionReturnType::Int);
  246. }
  247. TEST(Expression, SubstitutionTest) {
  248. storm::expressions::Expression trueExpression;
  249. storm::expressions::Expression falseExpression;
  250. storm::expressions::Expression threeExpression;
  251. storm::expressions::Expression piExpression;
  252. storm::expressions::Expression boolVarExpression;
  253. storm::expressions::Expression intVarExpression;
  254. storm::expressions::Expression doubleVarExpression;
  255. storm::expressions::Expression boolConstExpression;
  256. storm::expressions::Expression intConstExpression;
  257. storm::expressions::Expression doubleConstExpression;
  258. ASSERT_NO_THROW(trueExpression = storm::expressions::Expression::createTrue());
  259. ASSERT_NO_THROW(falseExpression = storm::expressions::Expression::createFalse());
  260. ASSERT_NO_THROW(threeExpression = storm::expressions::Expression::createIntegerLiteral(3));
  261. ASSERT_NO_THROW(piExpression = storm::expressions::Expression::createDoubleLiteral(3.14));
  262. ASSERT_NO_THROW(boolVarExpression = storm::expressions::Expression::createBooleanVariable("x"));
  263. ASSERT_NO_THROW(intVarExpression = storm::expressions::Expression::createIntegerVariable("y"));
  264. ASSERT_NO_THROW(doubleVarExpression = storm::expressions::Expression::createDoubleVariable("z"));
  265. ASSERT_NO_THROW(boolConstExpression = storm::expressions::Expression::createBooleanConstant("a"));
  266. ASSERT_NO_THROW(intConstExpression = storm::expressions::Expression::createIntegerConstant("b"));
  267. ASSERT_NO_THROW(doubleConstExpression = storm::expressions::Expression::createDoubleConstant("c"));
  268. storm::expressions::Expression tempExpression;
  269. ASSERT_NO_THROW(tempExpression = (intVarExpression < threeExpression || boolVarExpression) && boolConstExpression);
  270. std::map<std::string, storm::expressions::Expression> substution = { std::make_pair("y", doubleConstExpression), std::make_pair("x", storm::expressions::Expression::createTrue()), std::make_pair("a", storm::expressions::Expression::createTrue()) };
  271. storm::expressions::Expression substitutedExpression;
  272. ASSERT_NO_THROW(substitutedExpression = tempExpression.substitute(substution));
  273. EXPECT_TRUE(substitutedExpression.simplify().isTrue());
  274. }
  275. TEST(Expression, SimplificationTest) {
  276. storm::expressions::Expression trueExpression;
  277. storm::expressions::Expression falseExpression;
  278. storm::expressions::Expression threeExpression;
  279. storm::expressions::Expression intVarExpression;
  280. ASSERT_NO_THROW(trueExpression = storm::expressions::Expression::createTrue());
  281. ASSERT_NO_THROW(falseExpression = storm::expressions::Expression::createFalse());
  282. ASSERT_NO_THROW(threeExpression = storm::expressions::Expression::createIntegerLiteral(3));
  283. ASSERT_NO_THROW(intVarExpression = storm::expressions::Expression::createIntegerVariable("y"));
  284. storm::expressions::Expression tempExpression;
  285. storm::expressions::Expression simplifiedExpression;
  286. ASSERT_NO_THROW(tempExpression = trueExpression || intVarExpression > threeExpression);
  287. ASSERT_NO_THROW(simplifiedExpression = tempExpression.simplify());
  288. EXPECT_TRUE(simplifiedExpression.isTrue());
  289. ASSERT_NO_THROW(tempExpression = falseExpression && intVarExpression > threeExpression);
  290. ASSERT_NO_THROW(simplifiedExpression = tempExpression.simplify());
  291. EXPECT_TRUE(simplifiedExpression.isFalse());
  292. }
  293. TEST(Expression, SimpleEvaluationTest) {
  294. storm::expressions::Expression trueExpression;
  295. storm::expressions::Expression falseExpression;
  296. storm::expressions::Expression threeExpression;
  297. storm::expressions::Expression piExpression;
  298. storm::expressions::Expression boolVarExpression;
  299. storm::expressions::Expression intVarExpression;
  300. storm::expressions::Expression doubleVarExpression;
  301. storm::expressions::Expression boolConstExpression;
  302. storm::expressions::Expression intConstExpression;
  303. storm::expressions::Expression doubleConstExpression;
  304. ASSERT_NO_THROW(trueExpression = storm::expressions::Expression::createTrue());
  305. ASSERT_NO_THROW(falseExpression = storm::expressions::Expression::createFalse());
  306. ASSERT_NO_THROW(threeExpression = storm::expressions::Expression::createIntegerLiteral(3));
  307. ASSERT_NO_THROW(piExpression = storm::expressions::Expression::createDoubleLiteral(3.14));
  308. ASSERT_NO_THROW(boolVarExpression = storm::expressions::Expression::createBooleanVariable("x"));
  309. ASSERT_NO_THROW(intVarExpression = storm::expressions::Expression::createIntegerVariable("y"));
  310. ASSERT_NO_THROW(doubleVarExpression = storm::expressions::Expression::createDoubleVariable("z"));
  311. ASSERT_NO_THROW(boolConstExpression = storm::expressions::Expression::createBooleanConstant("a"));
  312. ASSERT_NO_THROW(intConstExpression = storm::expressions::Expression::createIntegerConstant("b"));
  313. ASSERT_NO_THROW(doubleConstExpression = storm::expressions::Expression::createDoubleConstant("c"));
  314. storm::expressions::Expression tempExpression;
  315. ASSERT_NO_THROW(tempExpression = (intVarExpression < threeExpression || boolVarExpression) && boolConstExpression);
  316. ASSERT_NO_THROW(storm::expressions::SimpleValuation valuation);
  317. storm::expressions::SimpleValuation valuation;
  318. ASSERT_NO_THROW(valuation.addBooleanIdentifier("x"));
  319. ASSERT_NO_THROW(valuation.addBooleanIdentifier("a"));
  320. ASSERT_NO_THROW(valuation.addIntegerIdentifier("y"));
  321. ASSERT_NO_THROW(valuation.addIntegerIdentifier("b"));
  322. ASSERT_NO_THROW(valuation.addDoubleIdentifier("z"));
  323. ASSERT_NO_THROW(valuation.addDoubleIdentifier("c"));
  324. ASSERT_THROW(tempExpression.evaluateAsDouble(&valuation), storm::exceptions::InvalidTypeException);
  325. ASSERT_THROW(tempExpression.evaluateAsInt(&valuation), storm::exceptions::InvalidTypeException);
  326. EXPECT_FALSE(tempExpression.evaluateAsBool(&valuation));
  327. ASSERT_NO_THROW(valuation.setBooleanValue("a", true));
  328. EXPECT_TRUE(tempExpression.evaluateAsBool(&valuation));
  329. ASSERT_NO_THROW(valuation.setIntegerValue("y", 3));
  330. EXPECT_FALSE(tempExpression.evaluateAsBool(&valuation));
  331. ASSERT_NO_THROW(tempExpression = ((intVarExpression < threeExpression).ite(trueExpression, falseExpression)));
  332. ASSERT_THROW(tempExpression.evaluateAsDouble(&valuation), storm::exceptions::InvalidTypeException);
  333. ASSERT_THROW(tempExpression.evaluateAsInt(&valuation), storm::exceptions::InvalidTypeException);
  334. EXPECT_FALSE(tempExpression.evaluateAsBool(&valuation));
  335. }