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.

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