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.

65 lines
2.7 KiB

  1. /*
  2. * SparseStateRewardParserTest.cpp
  3. *
  4. * Created on: 26.02.2014
  5. * Author: Manuel Sascha Weiand
  6. */
  7. #include "gtest/gtest.h"
  8. #include "storm-config.h"
  9. #include <cmath>
  10. #include "src/parser/SparseStateRewardParser.h"
  11. #include "src/exceptions/FileIoException.h"
  12. #include "src/exceptions/WrongFormatException.h"
  13. #include "src/exceptions/OutOfRangeException.h"
  14. TEST(SparseStateRewardParserTest, NonExistingFile) {
  15. // No matter what happens, please do NOT create a file with the name "nonExistingFile.not"!
  16. ASSERT_THROW(storm::parser::SparseStateRewardParser::parseSparseStateReward(42, STORM_CPP_TESTS_BASE_PATH "/nonExistingFile.not"), storm::exceptions::FileIoException);
  17. }
  18. double round(double val, int precision)
  19. {
  20. std::stringstream s;
  21. s << std::setprecision(precision) << std::setiosflags(std::ios_base::fixed) << val;
  22. s >> val;
  23. return val;
  24. }
  25. TEST(SparseStateRewardParserTest, BasicParsing) {
  26. // Get the parsing result.
  27. std::vector<double> result = storm::parser::SparseStateRewardParser::parseSparseStateReward(100, STORM_CPP_TESTS_BASE_PATH "/functional/parser/rew_files/state_reward_parser_basic.state.rew");
  28. // Now test if the correct value were parsed.
  29. for(int i = 0; i < 100; i++) {
  30. ASSERT_EQ(std::round(2*i + 15/13*i*i - 1.5/(i+0.1) + 15.7), std::round(result[i]));
  31. }
  32. }
  33. TEST(SparseStateRewardParserTest, Whitespaces) {
  34. // Get the parsing result.
  35. std::vector<double> result = storm::parser::SparseStateRewardParser::parseSparseStateReward(100, STORM_CPP_TESTS_BASE_PATH "/functional/parser/rew_files/state_reward_parser_whitespaces.state.rew");
  36. // Now test if the correct value were parsed.
  37. for(int i = 0; i < 100; i++) {
  38. ASSERT_EQ(std::round(2*i + 15/13*i*i - 1.5/(i+0.1) + 15.7), std::round(result[i]));
  39. }
  40. }
  41. TEST(SparseStateRewardParserTest, DoubledLines) {
  42. // There are multiple lines attributing a reward to the same state.
  43. ASSERT_THROW(storm::parser::SparseStateRewardParser::parseSparseStateReward(11, STORM_CPP_TESTS_BASE_PATH "/functional/parser/rew_files/state_reward_parser_doubledLines.state.rew"), storm::exceptions::WrongFormatException);
  44. // There is a line for a state that has been skipped.
  45. ASSERT_THROW(storm::parser::SparseStateRewardParser::parseSparseStateReward(11, STORM_CPP_TESTS_BASE_PATH "/functional/parser/rew_files/state_reward_parser_doubledLinesSkipped.state.rew"), storm::exceptions::WrongFormatException);
  46. }
  47. TEST(SparseStateRewardParserTest, RewardForNonExistentState) {
  48. // The index of one of the state that are to be given rewards is higher than the number of states in the model.
  49. ASSERT_THROW(storm::parser::SparseStateRewardParser::parseSparseStateReward(99, STORM_CPP_TESTS_BASE_PATH "/functional/parser/rew_files/state_reward_parser_basic.state.rew"), storm::exceptions::OutOfRangeException);
  50. }