1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- #include <string.h>
- #include "../Tests.h"
- #include "../src/ErrorSimulator.h"
- #include "core/File.h"
- #include "core/Logger.h"
- static int failIndex = 0;
- static const char* fails[] = {
- "cannot read file 'gdfgdfg'",
- "cannot seek file end of 'testData/someFile'",
- "cannot tell file position of 'testData/someFile'",
- "cannot seek file start of 'testData/someFile'",
- "expected to read 20 bytes from 'testData/someFile' but read 20",
- "cannot close file 'testData/someFile'"};
- static void printReport(
- LogLevel l, const char*, int, void*, const char* message) {
- TEST_INT(LOG_ERROR, (int)l);
- if(!TEST_TRUE(strstr(message, fails[failIndex]) != nullptr)) {
- LOG_ERROR("'%s' does not contain '%s'", message, fails[failIndex]);
- }
- }
- static void testExistingFile() {
- FileContent f = {0};
- if(!TEST_FALSE(readFile(&f, "testData/someFile"))) {
- return;
- }
- TEST_SIZE(20, f.length);
- TEST_STRING("Just\nSome\nTest File\n", f.data);
- destroyFileContent(&f);
- }
- static void testNotExistingFile() {
- FileContent c;
- TEST_TRUE(readFile(&c, "gdfgdfg"));
- }
- static void testFails(int steps) {
- (void)steps;
- #ifdef ERROR_SIMULATOR
- failStep = steps;
- failIndex = steps;
- FileContent c = {0};
- TEST_TRUE(readFile(&c, "testData/someFile"));
- failStep = -1;
- #endif
- }
- void testFile() {
- setReportHandler(printReport, nullptr);
- testExistingFile();
- testNotExistingFile();
- for(int i = 1; i < 6; i++) {
- testFails(i);
- }
- setReportHandler(nullptr, nullptr);
- }
|