Эх сурвалжийг харах

Add namespace import to Test

Kajetan Johannes Hammerle 11 сар өмнө
parent
commit
65014b82b8
3 өөрчлөгдсөн 72 нэмэгдсэн , 49 устгасан
  1. 21 0
      include/core/Test.h
  2. 37 35
      src/Test.c
  3. 14 14
      test/modules/TestTests.c

+ 21 - 0
include/core/Test.h

@@ -60,4 +60,25 @@ bool coreTestIntVectorN(CORE_TEST_ARGS, const int* wanted, const int* actual,
 #define CORE_TEST_IV4(wanted, actual)                                          \
     CORE_TEST_IVN((CoreIntVector4*)(wanted), (CoreIntVector4*)(actual), 4)
 
+#ifdef IMPORT_CORE
+#define finalizeTests coreFinalizeTests
+#define TEST_FLOAT CORE_TEST_FLOAT
+#define TEST_BOOL CORE_TEST_BOOL
+#define TEST_INT CORE_TEST_INT
+#define TEST_I64 CORE_TEST_I64
+#define TEST_U64 CORE_TEST_U64
+#define TEST_SIZE CORE_TEST_SIZE
+#define TEST_STRING CORE_TEST_STRING
+#define TEST_FALSE CORE_TEST_FALSE
+#define TEST_TRUE CORE_TEST_TRUE
+#define TEST_NULL CORE_TEST_NULL
+#define TEST_NOT_NULL CORE_TEST_NOT_NULL
+#define TEST_V2 CORE_TEST_V2
+#define TEST_V3 CORE_TEST_V3
+#define TEST_V4 CORE_TEST_V4
+#define TEST_IV2 CORE_TEST_IV2
+#define TEST_IV3 CORE_TEST_IV3
+#define TEST_IV4 CORE_TEST_IV4
+#endif
+
 #endif

+ 37 - 35
src/Test.c

@@ -7,6 +7,8 @@
 #include "core/Logger.h"
 #include "core/Utility.h"
 
+#define TEST_ARGS CORE_TEST_ARGS
+
 typedef struct {
     char* file;
     int tests;
@@ -26,7 +28,7 @@ static Result* getResult(const char* file) {
     }
     while(resultsIndex >= resultsCapacity) {
         size_t newCapacity = resultsCapacity == 0 ? 8 : resultsCapacity * 2;
-        results = coreReallocate(results, newCapacity * sizeof(Result));
+        results = cReallocate(results, newCapacity * sizeof(Result));
         resultsCapacity = newCapacity;
     }
     Result* r = results + (resultsIndex++);
@@ -36,17 +38,17 @@ static Result* getResult(const char* file) {
     return r;
 }
 
-void coreFinalizeTests(void) {
+void finalizeTests(void) {
     for(size_t i = 0; i < resultsIndex; i++) {
         Result* r = results + i;
         bool c = r->successTests == r->tests;
-        fputs(c ? CORE_TERMINAL_GREEN : CORE_TERMINAL_RED, stdout);
+        fputs(c ? TERMINAL_GREEN : TERMINAL_RED, stdout);
         printf("%s - %d / %d tests succeeded", r->file, r->successTests,
                r->tests);
-        puts(CORE_TERMINAL_RESET);
+        puts(TERMINAL_RESET);
         free(r->file);
     }
-    coreFree(results);
+    cFree(results);
     results = nullptr;
     resultsIndex = 0;
     resultsCapacity = 0;
@@ -62,64 +64,64 @@ static bool addToResult(const char* file, bool comparison) {
     return false;
 }
 
-#define CORE_TEST_SUCCESS(result)                                              \
-    file = coreGetShortFileName(file);                                         \
+#define TEST_SUCCESS(result)                                                   \
+    file = getShortFileName(file);                                             \
     if(addToResult(file, result)) {                                            \
         return true;                                                           \
     }
 
-#define CORE_TEST_NAMED_COMPARE(name, type, format)                            \
-    bool coreTest##name(CORE_TEST_ARGS, type wanted, type actual) {            \
-        CORE_TEST_SUCCESS(wanted == actual)                                    \
-        fputs(CORE_TERMINAL_RED, stdout);                                      \
+#define TEST_NAMED_COMPARE(name, type, format)                                 \
+    bool coreTest##name(TEST_ARGS, type wanted, type actual) {                 \
+        TEST_SUCCESS(wanted == actual)                                         \
+        fputs(TERMINAL_RED, stdout);                                           \
         printf("%s:%d - expected '" format "' got '" format "'", file, line,   \
                wanted, actual);                                                \
-        puts(CORE_TERMINAL_RESET);                                             \
+        puts(TERMINAL_RESET);                                                  \
         return false;                                                          \
     }
 
-CORE_TEST_NAMED_COMPARE(Int, int, "%d")
-CORE_TEST_NAMED_COMPARE(I64, i64, "%ld")
-CORE_TEST_NAMED_COMPARE(U64, u64, "%lu")
-CORE_TEST_NAMED_COMPARE(Size, size_t, "%zu")
-CORE_TEST_NAMED_COMPARE(Bool, bool, "%d")
+TEST_NAMED_COMPARE(Int, int, "%d")
+TEST_NAMED_COMPARE(I64, i64, "%ld")
+TEST_NAMED_COMPARE(U64, u64, "%lu")
+TEST_NAMED_COMPARE(Size, size_t, "%zu")
+TEST_NAMED_COMPARE(Bool, bool, "%d")
 
-bool coreTestString(CORE_TEST_ARGS, const char* wanted, const char* actual) {
-    CORE_TEST_SUCCESS(strcmp(wanted, actual) == 0)
-    fputs(CORE_TERMINAL_RED, stdout);
+bool coreTestString(TEST_ARGS, const char* wanted, const char* actual) {
+    TEST_SUCCESS(strcmp(wanted, actual) == 0)
+    fputs(TERMINAL_RED, stdout);
     printf("%s:%d - expected '%s' got '%s'", file, line, wanted, actual);
-    puts(CORE_TERMINAL_RESET);
+    puts(TERMINAL_RESET);
     return false;
 }
 
-bool coreTestFloat(CORE_TEST_ARGS, float wanted, float actual, float error) {
+bool coreTestFloat(TEST_ARGS, float wanted, float actual, float error) {
     float diff = wanted - actual;
     diff = diff < 0.0f ? -diff : diff;
-    CORE_TEST_SUCCESS(diff <= error)
-    fputs(CORE_TERMINAL_RED, stdout);
+    TEST_SUCCESS(diff <= error)
+    fputs(TERMINAL_RED, stdout);
     printf("%s:%d - expected '%.3f' got '%.3f'", file, line, (double)wanted,
            (double)actual);
-    puts(CORE_TERMINAL_RESET);
+    puts(TERMINAL_RESET);
     return false;
 }
 
-bool coreTestNull(CORE_TEST_ARGS, const void* actual) {
-    CORE_TEST_SUCCESS(actual == nullptr)
-    fputs(CORE_TERMINAL_RED, stdout);
+bool coreTestNull(TEST_ARGS, const void* actual) {
+    TEST_SUCCESS(actual == nullptr)
+    fputs(TERMINAL_RED, stdout);
     printf("%s:%d - expected null", file, line);
-    puts(CORE_TERMINAL_RESET);
+    puts(TERMINAL_RESET);
     return false;
 }
 
-bool coreTestNotNull(CORE_TEST_ARGS, const void* actual) {
-    CORE_TEST_SUCCESS(actual != nullptr)
-    fputs(CORE_TERMINAL_RED, stdout);
+bool coreTestNotNull(TEST_ARGS, const void* actual) {
+    TEST_SUCCESS(actual != nullptr)
+    fputs(TERMINAL_RED, stdout);
     printf("%s:%d - expected valid pointer", file, line);
-    puts(CORE_TERMINAL_RESET);
+    puts(TERMINAL_RESET);
     return false;
 }
 
-bool coreTestVectorN(CORE_TEST_ARGS, const float* wanted, const float* actual,
+bool coreTestVectorN(TEST_ARGS, const float* wanted, const float* actual,
                      size_t n) {
     for(size_t i = 0; i < n; i++) {
         if(!coreTestFloat(file, line, wanted[i], actual[i], 0.01f)) {
@@ -129,7 +131,7 @@ bool coreTestVectorN(CORE_TEST_ARGS, const float* wanted, const float* actual,
     return true;
 }
 
-bool coreTestIntVectorN(CORE_TEST_ARGS, const int* wanted, const int* actual,
+bool coreTestIntVectorN(TEST_ARGS, const int* wanted, const int* actual,
                         size_t n) {
     for(size_t i = 0; i < n; i++) {
         if(!coreTestInt(file, line, wanted[i], actual[i])) {

+ 14 - 14
test/modules/TestTests.c

@@ -2,24 +2,24 @@
 #include "core/Vector.h"
 
 void coreTestTest() {
-    CORE_TEST_BOOL(false, true);
-    CORE_TEST_INT(0, 1);
-    CORE_TEST_I64(0, 1);
-    CORE_TEST_U64(0, 1);
-    CORE_TEST_SIZE(0, 1);
-    CORE_TEST_STRING("a", "b");
-    CORE_TEST_FALSE(true);
-    CORE_TEST_TRUE(false);
-    CORE_TEST_NULL(&(int){0});
-    CORE_TEST_NOT_NULL(nullptr);
-    CORE_TEST_FLOAT(0.0f, 1.0f, 0.1f);
+    TEST_BOOL(false, true);
+    TEST_INT(0, 1);
+    TEST_I64(0, 1);
+    TEST_U64(0, 1);
+    TEST_SIZE(0, 1);
+    TEST_STRING("a", "b");
+    TEST_FALSE(true);
+    TEST_TRUE(false);
+    TEST_NULL(&(int){0});
+    TEST_NOT_NULL(nullptr);
+    TEST_FLOAT(0.0f, 1.0f, 0.1f);
 
     CoreVector2 a = {{0, 1}};
     CoreVector2 b = {{2, 3}};
-    CORE_TEST_V2(&a, &b);
+    TEST_V2(&a, &b);
 
     CoreIntVector2 ia = {{0, 1}};
     CoreIntVector2 ib = {{2, 3}};
-    CORE_TEST_IV2(&ia, &ib);
-    coreFinalizeTests();
+    TEST_IV2(&ia, &ib);
+    finalizeTests();
 }