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.

99 lines
5.7 KiB

  1. #include "gtest/gtest.h"
  2. #include "storm-config.h"
  3. #include "src/models/sparse/StandardRewardModel.h"
  4. #include "src/parser/DeterministicModelParser.h"
  5. #include "src/models/sparse/Dtmc.h"
  6. #include "src/models/sparse/Ctmc.h"
  7. #include "src/exceptions/FileIoException.h"
  8. #include "src/exceptions/InvalidArgumentException.h"
  9. #include "src/exceptions/OutOfRangeException.h"
  10. TEST(DeterministicModelParserTest, NonExistingFile) {
  11. // No matter what happens, please do NOT create a file with the name "nonExistingFile.not"!
  12. ASSERT_THROW(storm::parser::DeterministicModelParser::parseDtmc(STORM_CPP_TESTS_BASE_PATH "/nonExistingFile.not", STORM_CPP_TESTS_BASE_PATH "/nonExistingFile.not"), storm::exceptions::FileIoException);
  13. ASSERT_THROW(storm::parser::DeterministicModelParser::parseCtmc(STORM_CPP_TESTS_BASE_PATH "/nonExistingFile.not", STORM_CPP_TESTS_BASE_PATH "/nonExistingFile.not"), storm::exceptions::FileIoException);
  14. }
  15. TEST(DeterministicModelParserTest, BasicDtmcParsing) {
  16. // Parse a Dtmc and check the result.
  17. storm::models::sparse::Dtmc<double> dtmc(storm::parser::DeterministicModelParser::parseDtmc(STORM_CPP_TESTS_BASE_PATH "/functional/parser/tra_files/dtmc_general.tra", STORM_CPP_TESTS_BASE_PATH "/functional/parser/lab_files/dtmc_general.lab", STORM_CPP_TESTS_BASE_PATH "/functional/parser/rew_files/dtmc_general.state.rew", STORM_CPP_TESTS_BASE_PATH "/functional/parser/rew_files/dtmc_general.trans.rew"));
  18. ASSERT_EQ(8ul, dtmc.getNumberOfStates());
  19. ASSERT_EQ(16ul, dtmc.getNumberOfTransitions());
  20. ASSERT_EQ(2ul, dtmc.getInitialStates().getNumberOfSetBits());
  21. ASSERT_TRUE(dtmc.getInitialStates().get(0));
  22. ASSERT_TRUE(dtmc.getInitialStates().get(7));
  23. ASSERT_EQ(5ul, dtmc.getStateLabeling().getNumberOfLabels());
  24. ASSERT_EQ(2ul, dtmc.getLabelsOfState(6).size());
  25. ASSERT_TRUE(dtmc.hasRewardModel());
  26. ASSERT_EQ(42, dtmc.getRewardModel("").getStateRewardVector()[7]);
  27. double rewardSum = 0;
  28. for(uint_fast64_t i = 0; i < dtmc.getRewardModel("").getStateRewardVector().size(); i++) {
  29. rewardSum += dtmc.getRewardModel("").getStateRewardVector()[i];
  30. }
  31. ASSERT_EQ(263.32, rewardSum);
  32. ASSERT_TRUE(dtmc.getRewardModel("").hasTransitionRewards());
  33. ASSERT_EQ(17ul, dtmc.getRewardModel("").getTransitionRewardMatrix().getEntryCount());
  34. rewardSum = 0;
  35. for(uint_fast64_t i = 0; i < dtmc.getRewardModel("").getTransitionRewardMatrix().getRowCount(); i++) {
  36. rewardSum += dtmc.getRewardModel("").getTransitionRewardMatrix().getRowSum(i);
  37. }
  38. ASSERT_EQ(125.4, rewardSum);
  39. }
  40. TEST(DeterministicModelParserTest, BasicCtmcParsing) {
  41. // Parse a Ctmc and check the result.
  42. storm::models::sparse::Ctmc<double> ctmc(storm::parser::DeterministicModelParser::parseCtmc(STORM_CPP_TESTS_BASE_PATH "/functional/parser/tra_files/dtmc_general.tra", STORM_CPP_TESTS_BASE_PATH "/functional/parser/lab_files/dtmc_general.lab", STORM_CPP_TESTS_BASE_PATH "/functional/parser/rew_files/dtmc_general.state.rew", STORM_CPP_TESTS_BASE_PATH "/functional/parser/rew_files/dtmc_general.trans.rew"));
  43. ASSERT_EQ(8ul, ctmc.getNumberOfStates());
  44. ASSERT_EQ(16ul, ctmc.getNumberOfTransitions());
  45. ASSERT_EQ(2ul, ctmc.getInitialStates().getNumberOfSetBits());
  46. ASSERT_TRUE(ctmc.getInitialStates().get(0));
  47. ASSERT_TRUE(ctmc.getInitialStates().get(7));
  48. ASSERT_EQ(5ul, ctmc.getStateLabeling().getNumberOfLabels());
  49. ASSERT_EQ(2ul, ctmc.getLabelsOfState(6).size());
  50. ASSERT_TRUE(ctmc.hasRewardModel());
  51. ASSERT_EQ(42, ctmc.getRewardModel("").getStateRewardVector()[7]);
  52. double rewardSum = 0;
  53. for(uint_fast64_t i = 0; i < ctmc.getRewardModel("").getStateRewardVector().size(); i++) {
  54. rewardSum += ctmc.getRewardModel("").getStateRewardVector()[i];
  55. }
  56. ASSERT_EQ(263.32, rewardSum);
  57. ASSERT_TRUE(ctmc.getRewardModel("").hasTransitionRewards());
  58. ASSERT_EQ(17ul, ctmc.getRewardModel("").getTransitionRewardMatrix().getEntryCount());
  59. rewardSum = 0;
  60. for(uint_fast64_t i = 0; i < ctmc.getRewardModel("").getTransitionRewardMatrix().getRowCount(); i++) {
  61. rewardSum += ctmc.getRewardModel("").getTransitionRewardMatrix().getRowSum(i);
  62. }
  63. ASSERT_EQ(125.4, rewardSum);
  64. }
  65. TEST(DeterministicModelParserTest, MismatchedFiles) {
  66. // Test file combinations that do not match, i.e. differing number of states, transitions, etc.
  67. // The labeling file contains a label for a non existent state.
  68. ASSERT_THROW(storm::parser::DeterministicModelParser::parseDtmc(STORM_CPP_TESTS_BASE_PATH "/functional/parser/tra_files/dtmc_mismatched.tra", STORM_CPP_TESTS_BASE_PATH "/functional/parser/lab_files/dtmc_general.lab"), storm::exceptions::OutOfRangeException);
  69. // The state reward file contains a reward for a non existent state.
  70. ASSERT_THROW(storm::parser::DeterministicModelParser::parseDtmc(STORM_CPP_TESTS_BASE_PATH "/functional/parser/tra_files/dtmc_mismatched.tra", STORM_CPP_TESTS_BASE_PATH "/functional/parser/lab_files/dtmc_mismatched.lab", STORM_CPP_TESTS_BASE_PATH "/functional/parser/rew_files/dtmc_general.state.rew"), storm::exceptions::OutOfRangeException);
  71. // The transition reward file contains rewards for a non existent state.
  72. ASSERT_THROW(storm::parser::DeterministicModelParser::parseDtmc(STORM_CPP_TESTS_BASE_PATH "/functional/parser/tra_files/dtmc_mismatched.tra", STORM_CPP_TESTS_BASE_PATH "/functional/parser/lab_files/dtmc_mismatched.lab", "", STORM_CPP_TESTS_BASE_PATH "/functional/parser/rew_files/dtmc_general.trans.rew"), storm::exceptions::OutOfRangeException);
  73. // The transition reward file contains rewards for a non existent transition
  74. ASSERT_THROW(storm::parser::DeterministicModelParser::parseDtmc(STORM_CPP_TESTS_BASE_PATH "/functional/parser/tra_files/dtmc_mismatched.tra", STORM_CPP_TESTS_BASE_PATH "/functional/parser/lab_files/dtmc_mismatched.lab", "", STORM_CPP_TESTS_BASE_PATH "/functional/parser/rew_files/dtmc_mismatched.trans.rew"), storm::exceptions::OutOfRangeException);
  75. }