FileTests.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include <cstring>
  2. #include "../Tests.hpp"
  3. #include "../src/ErrorSimulator.hpp"
  4. #include "core/File.hpp"
  5. #include "core/Logger.hpp"
  6. #include "core/Test.hpp"
  7. static int failIndex = 0;
  8. static const char* fails[] = {
  9. "cannot read file 'gdfgdfg'",
  10. "cannot seek file end of 'testData/someFile'",
  11. "cannot tell file position of 'testData/someFile'",
  12. "cannot seek file start of 'testData/someFile'",
  13. "expected to read 20 bytes from 'testData/someFile' but read 20",
  14. "cannot close file 'testData/someFile'"};
  15. static void printReport(
  16. Core::LogLevel l, const char*, int, void*, const char* message) {
  17. TEST(static_cast<int>(Core::LogLevel::ERROR), static_cast<int>(l));
  18. if(!TEST_TRUE(strstr(message, fails[failIndex]) != nullptr)) {
  19. LOG_ERROR("'#' does not contain '#'", message, fails[failIndex]);
  20. }
  21. }
  22. static void testExistingFile() {
  23. Core::List<char> f;
  24. if(!TEST_FALSE(readFile(f, "testData/someFile"))) {
  25. return;
  26. }
  27. TEST(21lu, f.getLength());
  28. TEST_STRING("Just\nSome\nTest File\n", &f[0]);
  29. }
  30. static void testNotExistingFile() {
  31. Core::List<char> f;
  32. TEST_TRUE(readFile(f, "gdfgdfg"));
  33. }
  34. static void testFails(int steps) {
  35. (void)steps;
  36. #ifdef ERROR_SIMULATOR
  37. failStep = steps;
  38. failIndex = steps;
  39. Core::List<char> f;
  40. TEST_TRUE(readFile(f, "testData/someFile"));
  41. failStep = -1;
  42. #endif
  43. }
  44. void testFile() {
  45. Core::setReportHandler(printReport, nullptr);
  46. testExistingFile();
  47. testNotExistingFile();
  48. for(int i = 1; i < 6; i++) {
  49. testFails(i);
  50. }
  51. Core::setReportHandler(nullptr, nullptr);
  52. }