FileTests.c 1.5 KB

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