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.

63 lines
2.8 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. std::stringstream s;
  20. s << std::setprecision(precision) << std::setiosflags(std::ios_base::fixed) << val;
  21. s >> val;
  22. return val;
  23. }
  24. TEST(SparseStateRewardParserTest, BasicParsing) {
  25. // Get the parsing result.
  26. std::vector<double> result = storm::parser::SparseStateRewardParser<>::parseSparseStateReward(100, STORM_CPP_TESTS_BASE_PATH "/functional/parser/rew_files/state_reward_parser_basic.state.rew");
  27. // Now test if the correct value were parsed.
  28. for (int i = 0; i < 100; i++) {
  29. ASSERT_EQ(std::round(2 * i + 15 / 13 * i * i - 1.5 / (i + 0.1) + 15.7), std::round(result[i]));
  30. }
  31. }
  32. TEST(SparseStateRewardParserTest, Whitespaces) {
  33. // Get the parsing result.
  34. std::vector<double> result = storm::parser::SparseStateRewardParser<>::parseSparseStateReward(100, STORM_CPP_TESTS_BASE_PATH "/functional/parser/rew_files/state_reward_parser_whitespaces.state.rew");
  35. // Now test if the correct value were parsed.
  36. for (int i = 0; i < 100; i++) {
  37. ASSERT_EQ(std::round(2 * i + 15 / 13 * i * i - 1.5 / (i + 0.1) + 15.7), std::round(result[i]));
  38. }
  39. }
  40. TEST(SparseStateRewardParserTest, DoubledLines) {
  41. // There are multiple lines attributing a reward to the same state.
  42. 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);
  43. // There is a line for a state that has been skipped.
  44. 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);
  45. }
  46. TEST(SparseStateRewardParserTest, RewardForNonExistentState) {
  47. // The index of one of the state that are to be given rewards is higher than the number of states in the model.
  48. 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);
  49. }