瀏覽代碼

Add more min/max/clamp functions

Kajetan Johannes Hammerle 3 月之前
父節點
當前提交
cb86be8970
共有 5 個文件被更改,包括 18 次插入10 次删除
  1. 2 0
      include/core/Test.h
  2. 2 0
      include/core/Utility.h
  3. 1 0
      src/Test.c
  4. 2 0
      src/Utility.c
  5. 11 10
      test/modules/UtilityTests.c

+ 2 - 0
include/core/Test.h

@@ -10,6 +10,7 @@ void finalizeTests(void);
     bool test##name(TEST_ARGS, type wanted, type actual)
 
 TEST_FUNCTION(Int, int);
+TEST_FUNCTION(I32, i32);
 TEST_FUNCTION(I64, i64);
 TEST_FUNCTION(U32, u32);
 TEST_FUNCTION(U64, u64);
@@ -30,6 +31,7 @@ bool testNotNull(TEST_ARGS, const void* p);
 
 #define TEST_BOOL(wanted, actual) TEST(wanted, actual, Bool)
 #define TEST_INT(wanted, actual) TEST(wanted, actual, Int)
+#define TEST_I32(wanted, actual) TEST(wanted, actual, I32)
 #define TEST_I64(wanted, actual) TEST(wanted, actual, I64)
 #define TEST_U32(wanted, actual) TEST(wanted, actual, U32)
 #define TEST_U64(wanted, actual) TEST(wanted, actual, U64)

+ 2 - 0
include/core/Utility.h

@@ -23,6 +23,8 @@ size_t popCount(u64 u);
     }
 MIN_MAX(size_t, Size)
 MIN_MAX(u32, U32)
+MIN_MAX(i32, I32)
+MIN_MAX(float, Float)
 
 typedef void (*ExitHandler)(int, void*);
 [[noreturn]] void exitWithHandler(const char* file, int line, int value);

+ 1 - 0
src/Test.c

@@ -81,6 +81,7 @@ static bool addToResult(const char* file, bool comparison) {
     }
 
 TEST_NAMED_COMPARE(Int, int, "%d")
+TEST_NAMED_COMPARE(I32, i32, "%" PRId32)
 TEST_NAMED_COMPARE(I64, i64, "%" PRId64)
 TEST_NAMED_COMPARE(U32, u32, "%" PRIu32)
 TEST_NAMED_COMPARE(U64, u64, "%" PRIu64)

+ 2 - 0
src/Utility.c

@@ -15,6 +15,8 @@
     extern inline type clamp##name(type t, type from, type to)
 MIN_MAX_DEF(size_t, Size);
 MIN_MAX_DEF(u32, U32);
+MIN_MAX_DEF(i32, I32);
+MIN_MAX_DEF(float, Float);
 
 static ExitHandler exitHandler = nullptr;
 static void* exitData = nullptr;

+ 11 - 10
test/modules/UtilityTests.c

@@ -119,17 +119,18 @@ static void testSort() {
     }
 }
 
+#define TEST_MIN_MAX(Name, NAME, ...)                                \
+    TEST_##NAME(5, min##Name(5, 7) __VA_OPT__(, ) __VA_ARGS__);      \
+    TEST_##NAME(7, max##Name(5, 7) __VA_OPT__(, ) __VA_ARGS__);      \
+    TEST_##NAME(5, clamp##Name(3, 5, 7) __VA_OPT__(, ) __VA_ARGS__); \
+    TEST_##NAME(7, clamp##Name(9, 5, 7) __VA_OPT__(, ) __VA_ARGS__); \
+    TEST_##NAME(6, clamp##Name(6, 5, 7) __VA_OPT__(, ) __VA_ARGS__)
+
 static void testMinMax() {
-    TEST_SIZE(5, minSize(5, 7));
-    TEST_SIZE(7, maxSize(5, 7));
-    TEST_SIZE(5, clampSize(3, 5, 7));
-    TEST_SIZE(7, clampSize(9, 5, 7));
-    TEST_SIZE(6, clampSize(6, 5, 7));
-    TEST_U32(4, minU32(4, 6));
-    TEST_U32(6, maxU32(4, 6));
-    TEST_SIZE(4, clampU32(2, 4, 6));
-    TEST_SIZE(6, clampU32(8, 4, 6));
-    TEST_SIZE(5, clampU32(5, 4, 6));
+    TEST_MIN_MAX(Size, SIZE);
+    TEST_MIN_MAX(U32, U32);
+    TEST_MIN_MAX(I32, I32);
+    TEST_MIN_MAX(Float, FLOAT, 0.0f);
 }
 
 void testUtility(bool light) {