FileReaderTests.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include "../../src/ErrorSimulator.hpp"
  2. #include "../Tests.hpp"
  3. #include "core/io/FileReader.hpp"
  4. static constexpr const char* TEST_FILE = "../test/modules/resources/test";
  5. static void testReadChar() {
  6. Core::FileReader r;
  7. CORE_TEST_ERROR(r.open(TEST_FILE));
  8. Core::String8<128> s;
  9. while(true) {
  10. int c = 0;
  11. if(r.readChar(c) != Core::Error::NONE) {
  12. break;
  13. }
  14. CORE_TEST_ERROR(s.append(static_cast<c32>(c)));
  15. }
  16. CORE_TEST_STRING("abc\nBaum\n", s);
  17. }
  18. static void testReadCharPath() {
  19. Core::FileReader r;
  20. Core::Path path;
  21. CORE_TEST_ERROR(path.append(TEST_FILE));
  22. CORE_TEST_ERROR(r.open(path));
  23. Core::String8<128> s;
  24. while(true) {
  25. int c = 0;
  26. if(r.readChar(c) != Core::Error::NONE) {
  27. break;
  28. }
  29. CORE_TEST_ERROR(s.append(static_cast<c32>(c)));
  30. }
  31. CORE_TEST_STRING("abc\nBaum\n", s);
  32. }
  33. static void testReadChars() {
  34. Core::FileReader r;
  35. CORE_TEST_ERROR(r.open(TEST_FILE));
  36. char buffer[128];
  37. CORE_TEST_EQUAL(Core::Error::END_OF_FILE,
  38. r.readChars(buffer, sizeof(buffer)));
  39. CORE_TEST_STRING("abc\nBaum\n", buffer);
  40. }
  41. static void testReadCharsOverflow() {
  42. Core::FileReader r;
  43. CORE_TEST_ERROR(r.open(TEST_FILE));
  44. char buffer[6];
  45. CORE_TEST_ERROR(r.readChars(buffer, sizeof(buffer)));
  46. CORE_TEST_STRING("abc\nB", buffer);
  47. }
  48. static void testMoveConstruct() {
  49. Core::FileReader r;
  50. CORE_TEST_ERROR(r.open(TEST_FILE));
  51. Core::FileReader r2 = Core::move(r);
  52. CORE_TEST_STRING("", r.getPath());
  53. CORE_TEST_STRING(TEST_FILE, r2.getPath());
  54. int c = 0;
  55. CORE_TEST_EQUAL(Core::Error::INVALID_STATE, r.readChar(c));
  56. CORE_TEST_ERROR(r2.readChar(c));
  57. CORE_TEST_EQUAL('a', c);
  58. }
  59. static void testMoveAssignment() {
  60. Core::FileReader r;
  61. CORE_TEST_ERROR(r.open(TEST_FILE));
  62. Core::FileReader r2;
  63. r2 = Core::move(r);
  64. CORE_TEST_STRING("", r.getPath());
  65. CORE_TEST_STRING(TEST_FILE, r2.getPath());
  66. int c = 0;
  67. CORE_TEST_EQUAL(Core::Error::INVALID_STATE, r.readChar(c));
  68. CORE_TEST_ERROR(r2.readChar(c));
  69. CORE_TEST_EQUAL('a', c);
  70. }
  71. static void testInvalidAccess() {
  72. char buffer[4];
  73. Core::FileReader r;
  74. CORE_TEST_EQUAL(Core::Error::INVALID_STATE,
  75. r.readChars(buffer, sizeof(buffer)));
  76. CORE_TEST_ERROR(r.open(TEST_FILE));
  77. CORE_TEST_EQUAL(Core::Error::INVALID_ARGUMENT, r.readChars(buffer, -1));
  78. CORE_TEST_EQUAL(Core::Error::INVALID_STATE, r.open(TEST_FILE));
  79. }
  80. static void testInvalidAccessPath() {
  81. Core::FileReader r;
  82. Core::Path path;
  83. CORE_TEST_ERROR(path.append(TEST_FILE));
  84. CORE_TEST_ERROR(r.open(path));
  85. CORE_TEST_EQUAL(Core::Error::INVALID_STATE, r.open(path));
  86. }
  87. static void testCloseFail() {
  88. #ifdef ERROR_SIMULATOR
  89. Core::Fail::fileClose = true;
  90. {
  91. Core::FileReader r;
  92. CORE_TEST_ERROR(r.open(TEST_FILE));
  93. }
  94. Core::Fail::fileClose = false;
  95. #endif
  96. }
  97. void Core::testFileReader() {
  98. testReadChar();
  99. testReadCharPath();
  100. testReadChars();
  101. testReadCharsOverflow();
  102. testMoveConstruct();
  103. testMoveAssignment();
  104. testInvalidAccess();
  105. testInvalidAccessPath();
  106. testCloseFail();
  107. }