Преглед изворни кода

Move test data to folder, file reading, test improvements

Kajetan Johannes Hammerle пре 2 месеци
родитељ
комит
efb752439f

+ 3 - 0
CMakeLists.txt

@@ -7,6 +7,7 @@ set(SRC
     "src/BitArray.c"
     "src/Box.c"
     "src/Buffer.c"
+    "src/File.c"
     "src/Frustum.c"
     "src/HashMap.c"
     "src/Logger.c"
@@ -32,6 +33,7 @@ set(SRC_TESTS
     "test/modules/BoxTests.c"
     "test/modules/BufferTests.c"
     "test/modules/ComponentsTests.c"
+    "test/modules/FileTests.c"
     "test/modules/FrustumTests.c"
     "test/modules/HashMapTests.c"
     "test/modules/ListTests.c"
@@ -100,6 +102,7 @@ target_sources(core PUBLIC
         ./include/core/Buffer.h
         ./include/core/Check.h
         ./include/core/Components.h
+        ./include/core/File.h
         ./include/core/Frustum.h
         ./include/core/Generic.h
         ./include/core/HashMap.h

+ 0 - 2
ignoredCoverageFunctions

@@ -1,2 +0,0 @@
-QueueCL
-QueueDataCL

+ 14 - 0
include/core/File.h

@@ -0,0 +1,14 @@
+#ifndef CORE_FILE_H
+#define CORE_FILE_H
+
+#include <stddef.h>
+
+typedef struct {
+    char* data;
+    size_t length;
+} FileContent;
+
+bool readFile(FileContent* f, const char* path);
+void destroyFileContent(FileContent* f);
+
+#endif

+ 1 - 1
src/ErrorSimulator.c

@@ -2,12 +2,12 @@
 
 #include "ErrorSimulator.h"
 
-bool failFileClose = false;
 bool failTimeGet = false;
 bool failThreadInit = false;
 bool failThreadJoin = false;
 bool failMutexInit = false;
 bool failMutexLock = false;
 bool failMutexUnlock = false;
+int failStep = -1;
 
 #endif

+ 3 - 3
src/ErrorSimulator.h

@@ -2,28 +2,28 @@
 #define CORE_ERROR_SIMULATOR_H
 
 #ifdef ERROR_SIMULATOR
-extern bool failFileClose;
 extern bool failTimeGet;
 extern bool failThreadInit;
 extern bool failThreadJoin;
 extern bool failMutexInit;
 extern bool failMutexLock;
 extern bool failMutexUnlock;
-#define FILE_CLOSE_FAIL failFile
+extern int failStep;
 #define TIME_GET_FAIL failTimeGet
 #define THREAD_INIT_FAIL failThreadInit
 #define THREAD_JOIN_FAIL failThreadJoin
 #define MUTEX_INIT_FAIL failMutexInit
 #define MUTEX_LOCK_FAIL failMutexLock
 #define MUTEX_UNLOCK_FAIL failMutexUnlock
+#define FAIL_STEP (--failStep == 0)
 #else
-#define FILE_CLOSE_FAIL false
 #define TIME_GET_FAIL false
 #define THREAD_INIT_FAIL false
 #define THREAD_JOIN_FAIL false
 #define MUTEX_INIT_FAIL false
 #define MUTEX_LOCK_FAIL false
 #define MUTEX_UNLOCK_FAIL false
+#define FAIL_STEP false
 #endif
 
 #endif

+ 56 - 0
src/File.c

@@ -0,0 +1,56 @@
+#include "core/File.h"
+
+#include <stdio.h>
+
+#include "ErrorSimulator.h"
+#include "core/Logger.h"
+#include "core/Utility.h"
+
+static bool readOpenFile(FILE* file, FileContent* f, const char* path) {
+    if(FAIL_STEP || fseek(file, 0, SEEK_END)) {
+        REPORT(LOG_ERROR, "cannot seek file end of '%s'", path);
+        return true;
+    }
+    long l = ftell(file);
+    if(FAIL_STEP || l < 0) {
+        REPORT(LOG_ERROR, "cannot tell file position of '%s'", path);
+        return true;
+    }
+    f->length = (size_t)l;
+    if(FAIL_STEP || fseek(file, 0, SEEK_SET)) {
+        REPORT(LOG_ERROR, "cannot seek file start of '%s'", path);
+        return true;
+    }
+    f->data = coreAllocate(f->length + 1);
+    size_t read = fread(f->data, 1, f->length, file);
+    f->data[f->length] = 0;
+    if(FAIL_STEP || read != f->length) {
+        REPORT(
+            LOG_ERROR, "expected to read %zu bytes from '%s' but read %zu",
+            f->length, path, read);
+        return true;
+    }
+    return false;
+}
+
+bool readFile(FileContent* f, const char* path) {
+    FILE* file = fopen(path, "rb");
+    if(file == nullptr) {
+        REPORT(LOG_ERROR, "cannot read file '%s'", path);
+        return true;
+    }
+    bool r = readOpenFile(file, f, path);
+    if(FAIL_STEP || fclose(file)) {
+        REPORT(LOG_ERROR, "cannot close file '%s'", path);
+        r = true;
+    }
+    if(r) {
+        destroyFileContent(f);
+    }
+    return r;
+}
+
+void destroyFileContent(FileContent* f) {
+    coreFree(f->data);
+    *f = (FileContent){0};
+}

+ 2 - 8
test/Main.c

@@ -19,12 +19,6 @@ static void onExit(int code, void* data) {
     finalize();
 }
 
-static void printReport(
-    LogLevel l, const char* file, int line, void*, const char* message) {
-    printLog(l, file, line, "", "IGNORE: ", "%s", message);
-    setReportHandler(nullptr, nullptr);
-}
-
 int main(int argAmount, const char** args) {
     if(argAmount >= 2 && strcmp(args[1], "help") == 0) {
         puts("alloc");
@@ -33,7 +27,7 @@ int main(int argAmount, const char** args) {
         puts("post_canary");
         puts("test;ignore");
         puts("terminal");
-        puts("light;readLineTest");
+        puts("light;testData/readLineTest");
         return 0;
     }
     setlocale(LC_ALL, "en_US.utf8");
@@ -61,12 +55,12 @@ int main(int argAmount, const char** args) {
             return 0;
         }
     }
-    setReportHandler(printReport, nullptr);
 
     testBitArray();
     testBox();
     testBuffer(light);
     testComponents();
+    testFile();
     testFrustum();
     testHashMap(light);
     testList(light);

+ 1 - 0
test/Tests.h

@@ -9,6 +9,7 @@ void testBitArray(void);
 void testBox(void);
 void testBuffer(bool light);
 void testComponents(void);
+void testFile(void);
 void testFrustum(void);
 void testHashMap(bool light);
 void testInteractiveTerminal(void);

+ 59 - 0
test/modules/FileTests.c

@@ -0,0 +1,59 @@
+#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);
+}

+ 8 - 0
testData/ignoredCoverageFunctions

@@ -0,0 +1,8 @@
+QueueCL
+QueueDataCL
+testInt
+testI64
+testU32
+testU64
+testSize
+testBool

+ 0 - 0
readLineTest → testData/readLineTest


+ 3 - 0
testData/someFile

@@ -0,0 +1,3 @@
+Just
+Some
+Test File