FileReaderTests.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include "tests/FileReaderTests.h"
  2. #include "io/FileReader.h"
  3. #include "test/Test.h"
  4. static constexpr const char* TEST_FILE = "../src/tests/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 testReadChars() {
  19. Core::FileReader r;
  20. CORE_TEST_ERROR(r.open(TEST_FILE));
  21. char buffer[128];
  22. CORE_TEST_EQUAL(Core::Error::END_OF_FILE,
  23. r.readChars(buffer, sizeof(buffer)));
  24. CORE_TEST_STRING("abc\nBaum\n", buffer);
  25. }
  26. static void testReadCharsOverflow() {
  27. Core::FileReader r;
  28. CORE_TEST_ERROR(r.open(TEST_FILE));
  29. char buffer[6];
  30. CORE_TEST_ERROR(r.readChars(buffer, sizeof(buffer)));
  31. CORE_TEST_STRING("abc\nB", buffer);
  32. }
  33. static void testMoveConstruct() {
  34. Core::FileReader r;
  35. CORE_TEST_ERROR(r.open(TEST_FILE));
  36. Core::FileReader r2 = Core::move(r);
  37. CORE_TEST_STRING("", r.getPath());
  38. CORE_TEST_STRING(TEST_FILE, r2.getPath());
  39. int c = 0;
  40. CORE_TEST_EQUAL(Core::Error::INVALID_STATE, r.readChar(c));
  41. CORE_TEST_ERROR(r2.readChar(c));
  42. CORE_TEST_EQUAL('a', c);
  43. }
  44. static void testMoveAssignment() {
  45. Core::FileReader r;
  46. CORE_TEST_ERROR(r.open(TEST_FILE));
  47. Core::FileReader r2;
  48. r2 = Core::move(r);
  49. CORE_TEST_STRING("", r.getPath());
  50. CORE_TEST_STRING(TEST_FILE, r2.getPath());
  51. int c = 0;
  52. CORE_TEST_EQUAL(Core::Error::INVALID_STATE, r.readChar(c));
  53. CORE_TEST_ERROR(r2.readChar(c));
  54. CORE_TEST_EQUAL('a', c);
  55. }
  56. void Core::FileReaderTests::test() {
  57. testReadChar();
  58. testReadChars();
  59. testReadCharsOverflow();
  60. testMoveConstruct();
  61. testMoveAssignment();
  62. }