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.

238 lines
11 KiB

  1. /*
  2. * DeterministicSparseTransitionParserTest.cpp
  3. *
  4. * Created on: Feb 24, 2014
  5. * Author: Manuel Sascha Weiand
  6. */
  7. #include "gtest/gtest.h"
  8. #include "storm-config.h"
  9. #include "src/parser/DeterministicSparseTransitionParser.h"
  10. #include "src/storage/SparseMatrix.h"
  11. #include "src/settings/SettingMemento.h"
  12. #include "src/exceptions/FileIoException.h"
  13. #include "src/exceptions/WrongFormatException.h"
  14. TEST(DeterministicSparseTransitionParserTest, NonExistingFile) {
  15. // No matter what happens, please do NOT create a file with the name "nonExistingFile.not"!
  16. ASSERT_THROW(storm::parser::DeterministicSparseTransitionParser::parseDeterministicTransitions<double>(STORM_CPP_TESTS_BASE_PATH "/nonExistingFile.not"), storm::exceptions::FileIoException);
  17. storm::storage::SparseMatrix<double> nullMatrix;
  18. ASSERT_THROW(storm::parser::DeterministicSparseTransitionParser::parseDeterministicTransitionRewards(STORM_CPP_TESTS_BASE_PATH "/nonExistingFile.not", nullMatrix), storm::exceptions::FileIoException);
  19. }
  20. TEST(DeterministicSparseTransitionParserTest, BasicTransitionsParsing) {
  21. // Parse a deterministic transitions file and test the resulting matrix.
  22. storm::storage::SparseMatrix<double> transitionMatrix = storm::parser::DeterministicSparseTransitionParser::parseDeterministicTransitions<double>(STORM_CPP_TESTS_BASE_PATH "/functional/parser/tra_files/dtmc_general.tra");
  23. ASSERT_EQ(8, transitionMatrix.getColumnCount());
  24. ASSERT_EQ(21, transitionMatrix.getEntryCount());
  25. // Test every entry of the matrix.
  26. storm::storage::SparseMatrix<double>::const_iterator cIter = transitionMatrix.begin(0);
  27. ASSERT_EQ(0, cIter->getColumn());
  28. ASSERT_EQ(0, cIter->getValue());
  29. cIter++;
  30. ASSERT_EQ(1, cIter->getColumn());
  31. ASSERT_EQ(1, cIter->getValue());
  32. cIter++;
  33. ASSERT_EQ(1, cIter->getColumn());
  34. ASSERT_EQ(0, cIter->getValue());
  35. cIter++;
  36. ASSERT_EQ(2, cIter->getColumn());
  37. ASSERT_EQ(0.5, cIter->getValue());
  38. cIter++;
  39. ASSERT_EQ(3, cIter->getColumn());
  40. ASSERT_EQ(0.5, cIter->getValue());
  41. cIter++;
  42. ASSERT_EQ(2, cIter->getColumn());
  43. ASSERT_EQ(0, cIter->getValue());
  44. cIter++;
  45. ASSERT_EQ(3, cIter->getColumn());
  46. ASSERT_EQ(0.4, cIter->getValue());
  47. cIter++;
  48. ASSERT_EQ(4, cIter->getColumn());
  49. ASSERT_EQ(0.4, cIter->getValue());
  50. cIter++;
  51. ASSERT_EQ(5, cIter->getColumn());
  52. ASSERT_EQ(0.2, cIter->getValue());
  53. cIter++;
  54. ASSERT_EQ(3, cIter->getColumn());
  55. ASSERT_EQ(1, cIter->getValue());
  56. cIter++;
  57. ASSERT_EQ(3, cIter->getColumn());
  58. ASSERT_EQ(1, cIter->getValue());
  59. cIter++;
  60. ASSERT_EQ(4, cIter->getColumn());
  61. ASSERT_EQ(0, cIter->getValue());
  62. cIter++;
  63. ASSERT_EQ(3, cIter->getColumn());
  64. ASSERT_EQ(0.1, cIter->getValue());
  65. cIter++;
  66. ASSERT_EQ(4, cIter->getColumn());
  67. ASSERT_EQ(0.1, cIter->getValue());
  68. cIter++;
  69. ASSERT_EQ(5, cIter->getColumn());
  70. ASSERT_EQ(0.1, cIter->getValue());
  71. cIter++;
  72. ASSERT_EQ(6, cIter->getColumn());
  73. ASSERT_EQ(0.7, cIter->getValue());
  74. cIter++;
  75. ASSERT_EQ(0, cIter->getColumn());
  76. ASSERT_EQ(0.9, cIter->getValue());
  77. cIter++;
  78. ASSERT_EQ(5, cIter->getColumn());
  79. ASSERT_EQ(0, cIter->getValue());
  80. cIter++;
  81. ASSERT_EQ(6, cIter->getColumn());
  82. ASSERT_EQ(0.1, cIter->getValue());
  83. cIter++;
  84. ASSERT_EQ(6, cIter->getColumn());
  85. ASSERT_EQ(0.224653, cIter->getValue());
  86. cIter++;
  87. ASSERT_EQ(7, cIter->getColumn());
  88. ASSERT_EQ(0.775347, cIter->getValue());
  89. }
  90. TEST(DeterministicSparseTransitionParserTest, BasicTransitionsRewardsParsing) {
  91. // First parse a transition file. Then parse a transition reward file for the resulting transition matrix.
  92. storm::storage::SparseMatrix<double> transitionMatrix = storm::parser::DeterministicSparseTransitionParser::parseDeterministicTransitions<double>(STORM_CPP_TESTS_BASE_PATH "/functional/parser/tra_files/dtmc_general.tra");
  93. storm::storage::SparseMatrix<double> rewardMatrix = storm::parser::DeterministicSparseTransitionParser::parseDeterministicTransitionRewards(STORM_CPP_TESTS_BASE_PATH "/functional/parser/rew_files/dtmc_general.trans.rew", transitionMatrix);
  94. ASSERT_EQ(8, rewardMatrix.getColumnCount());
  95. ASSERT_EQ(17, rewardMatrix.getEntryCount());
  96. // Test every entry of the matrix.
  97. storm::storage::SparseMatrix<double>::const_iterator cIter = rewardMatrix.begin(0);
  98. ASSERT_EQ(1, cIter->getColumn());
  99. ASSERT_EQ(10, cIter->getValue());
  100. cIter++;
  101. ASSERT_EQ(2, cIter->getColumn());
  102. ASSERT_EQ(5, cIter->getValue());
  103. cIter++;
  104. ASSERT_EQ(3, cIter->getColumn());
  105. ASSERT_EQ(5.5, cIter->getValue());
  106. cIter++;
  107. ASSERT_EQ(3, cIter->getColumn());
  108. ASSERT_EQ(21.4, cIter->getValue());
  109. cIter++;
  110. ASSERT_EQ(4, cIter->getColumn());
  111. ASSERT_EQ(4, cIter->getValue());
  112. cIter++;
  113. ASSERT_EQ(5, cIter->getColumn());
  114. ASSERT_EQ(2, cIter->getValue());
  115. cIter++;
  116. ASSERT_EQ(3, cIter->getColumn());
  117. ASSERT_EQ(1, cIter->getValue());
  118. cIter++;
  119. ASSERT_EQ(3, cIter->getColumn());
  120. ASSERT_EQ(1, cIter->getValue());
  121. cIter++;
  122. ASSERT_EQ(3, cIter->getColumn());
  123. ASSERT_EQ(0.1, cIter->getValue());
  124. cIter++;
  125. ASSERT_EQ(4, cIter->getColumn());
  126. ASSERT_EQ(1.1, cIter->getValue());
  127. cIter++;
  128. ASSERT_EQ(5, cIter->getColumn());
  129. ASSERT_EQ(9.5, cIter->getValue());
  130. cIter++;
  131. ASSERT_EQ(6, cIter->getColumn());
  132. ASSERT_EQ(6.7, cIter->getValue());
  133. cIter++;
  134. ASSERT_EQ(0, cIter->getColumn());
  135. ASSERT_EQ(1, cIter->getValue());
  136. cIter++;
  137. ASSERT_EQ(5, cIter->getColumn());
  138. ASSERT_EQ(0, cIter->getValue());
  139. cIter++;
  140. ASSERT_EQ(6, cIter->getColumn());
  141. ASSERT_EQ(12, cIter->getValue());
  142. cIter++;
  143. ASSERT_EQ(6, cIter->getColumn());
  144. ASSERT_EQ(35.224653, cIter->getValue());
  145. cIter++;
  146. ASSERT_EQ(7, cIter->getColumn());
  147. ASSERT_EQ(9.875347, cIter->getValue());
  148. }
  149. TEST(DeterministicSparseTransitionParserTest, Whitespaces) {
  150. // Test the resilience of the parser against whitespaces.
  151. // Do so by comparing the hash of the matrix resulting from the file without whitespaces with the hash of the matrix resulting from the file with whitespaces.
  152. uint_fast64_t correctHash = storm::parser::DeterministicSparseTransitionParser::parseDeterministicTransitions<double>(STORM_CPP_TESTS_BASE_PATH "/functional/parser/tra_files/dtmc_general.tra").hash();
  153. storm::storage::SparseMatrix<double> transitionMatrix = storm::parser::DeterministicSparseTransitionParser::parseDeterministicTransitions<double>(STORM_CPP_TESTS_BASE_PATH "/functional/parser/tra_files/dtmc_whitespaces.tra");
  154. ASSERT_EQ(correctHash, transitionMatrix.hash());
  155. // Do the same for the corresponding transition rewards file (with and without whitespaces)
  156. correctHash = storm::parser::DeterministicSparseTransitionParser::parseDeterministicTransitionRewards(STORM_CPP_TESTS_BASE_PATH "/functional/parser/rew_files/dtmc_general.trans.rew", transitionMatrix).hash();
  157. ASSERT_EQ(correctHash, storm::parser::DeterministicSparseTransitionParser::parseDeterministicTransitionRewards(STORM_CPP_TESTS_BASE_PATH "/functional/parser/rew_files/dtmc_whitespaces.trans.rew", transitionMatrix).hash());
  158. }
  159. TEST(DeterministicSparseTransitionParserTest, MixedTransitionOrder) {
  160. // Since the MatrixBuilder needs sequential input of new elements reordering of transitions or states should throw an exception.
  161. ASSERT_THROW(storm::parser::DeterministicSparseTransitionParser::parseDeterministicTransitions<double>(STORM_CPP_TESTS_BASE_PATH "/functional/parser/tra_files/dtmc_mixedTransitionOrder.tra"), storm::exceptions::InvalidArgumentException);
  162. ASSERT_THROW(storm::parser::DeterministicSparseTransitionParser::parseDeterministicTransitions<double>(STORM_CPP_TESTS_BASE_PATH "/functional/parser/tra_files/dtmc_mixedStateOrder.tra"), storm::exceptions::InvalidArgumentException);
  163. storm::storage::SparseMatrix<double> transitionMatrix = storm::parser::DeterministicSparseTransitionParser::parseDeterministicTransitions<double>(STORM_CPP_TESTS_BASE_PATH "/functional/parser/tra_files/dtmc_general.tra");
  164. ASSERT_THROW(storm::parser::DeterministicSparseTransitionParser::parseDeterministicTransitionRewards(STORM_CPP_TESTS_BASE_PATH "/functional/parser/rew_files/dtmc_mixedTransitionOrder.trans.rew", transitionMatrix), storm::exceptions::InvalidArgumentException);
  165. ASSERT_THROW(storm::parser::DeterministicSparseTransitionParser::parseDeterministicTransitionRewards(STORM_CPP_TESTS_BASE_PATH "/functional/parser/rew_files/dtmc_mixedStateOrder.trans.rew", transitionMatrix), storm::exceptions::InvalidArgumentException);
  166. }
  167. TEST(DeterministicSparseTransitionParserTest, FixDeadlocks) {
  168. // Set the fixDeadlocks flag temporarily. It is set to its old value once the deadlockOption object is destructed.
  169. std::unique_ptr<storm::settings::SettingMemento> fixDeadlocks = storm::settings::mutableGeneralSettings().overrideDontFixDeadlocksSet(false);
  170. // Parse a transitions file with the fixDeadlocks Flag set and test if it works.
  171. storm::storage::SparseMatrix<double> transitionMatrix = storm::parser::DeterministicSparseTransitionParser::parseDeterministicTransitions<double>(STORM_CPP_TESTS_BASE_PATH "/functional/parser/tra_files/dtmc_deadlock.tra");
  172. ASSERT_EQ(9, transitionMatrix.getColumnCount());
  173. ASSERT_EQ(23, transitionMatrix.getEntryCount());
  174. storm::storage::SparseMatrix<double>::const_iterator cIter = transitionMatrix.begin(7);
  175. ASSERT_EQ(7, cIter->getColumn());
  176. ASSERT_EQ(1, cIter->getValue());
  177. cIter++;
  178. ASSERT_EQ(6, cIter->getColumn());
  179. ASSERT_EQ(0.224653, cIter->getValue());
  180. cIter++;
  181. ASSERT_EQ(7, cIter->getColumn());
  182. ASSERT_EQ(0.775347, cIter->getValue());
  183. cIter++;
  184. ASSERT_EQ(8, cIter->getColumn());
  185. ASSERT_EQ(0, cIter->getValue());
  186. }
  187. TEST(DeterministicSparseTransitionParserTest, DontFixDeadlocks) {
  188. // Try to parse a transitions file containing a deadlock state with the fixDeadlocksFlag unset. This should throw an exception.
  189. std::unique_ptr<storm::settings::SettingMemento> dontFixDeadlocks = storm::settings::mutableGeneralSettings().overrideDontFixDeadlocksSet(true);
  190. ASSERT_THROW(storm::parser::DeterministicSparseTransitionParser::parseDeterministicTransitions<double>(STORM_CPP_TESTS_BASE_PATH "/functional/parser/tra_files/dtmc_deadlock.tra"), storm::exceptions::WrongFormatException);
  191. }
  192. TEST(DeterministicSparseTransitionParserTest, DoubledLines) {
  193. // There is a redundant line in the transition file. As the transition already exists this should throw an exception.
  194. // Note: If two consecutive lines are doubled no exception is thrown.
  195. ASSERT_THROW(storm::parser::DeterministicSparseTransitionParser::parseDeterministicTransitions<double>(STORM_CPP_TESTS_BASE_PATH "/functional/parser/tra_files/dtmc_doubledLines.tra"), storm::exceptions::InvalidArgumentException);
  196. }
  197. TEST(DeterministicSparseTransitionParserTest, RewardForNonExistentTransition) {
  198. // First parse a transition file. Then parse a transition reward file for the resulting transition matrix.
  199. storm::storage::SparseMatrix<double> transitionMatrix = storm::parser::DeterministicSparseTransitionParser::parseDeterministicTransitions<double>(STORM_CPP_TESTS_BASE_PATH "/functional/parser/tra_files/dtmc_general.tra");
  200. // There is a reward for a transition that does not exist in the transition matrix.
  201. ASSERT_THROW(storm::parser::DeterministicSparseTransitionParser::parseDeterministicTransitionRewards(STORM_CPP_TESTS_BASE_PATH "/functional/parser/rew_files/dtmc_rewardForNonExTrans.trans.rew", transitionMatrix), storm::exceptions::WrongFormatException);
  202. }