Browse Source

Add clamp, min and max makro

Kajetan Johannes Hammerle 1 month ago
parent
commit
bd10bf4017
5 changed files with 35 additions and 9 deletions
  1. 2 0
      include/core/Test.h
  2. 12 7
      include/core/Utility.h
  3. 1 0
      src/Test.c
  4. 6 2
      src/Utility.c
  5. 14 0
      test/modules/UtilityTests.c

+ 2 - 0
include/core/Test.h

@@ -11,6 +11,7 @@ void finalizeTests(void);
 
 TEST_FUNCTION(Int, int);
 TEST_FUNCTION(I64, i64);
+TEST_FUNCTION(U32, u32);
 TEST_FUNCTION(U64, u64);
 TEST_FUNCTION(Size, size_t);
 TEST_FUNCTION(Bool, bool);
@@ -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_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)
 #define TEST_SIZE(wanted, actual) TEST(wanted, actual, Size)
 #define TEST_STRING(wanted, actual) TEST(wanted, actual, String)

+ 12 - 7
include/core/Utility.h

@@ -11,13 +11,18 @@ size_t popCount(u64 u);
 #define radianToDegree(radians) ((radians) * (180.0f / PI))
 #define degreeToRadian(degrees) ((degrees) * (PI / 180.0f))
 
-inline size_t maxSize(size_t a, size_t b) {
-    return a > b ? a : b;
-}
-
-inline size_t minSize(size_t a, size_t b) {
-    return a < b ? a : b;
-}
+#define MIN_MAX(type, name)                               \
+    inline type max##name(type a, type b) {               \
+        return a > b ? a : b;                             \
+    }                                                     \
+    inline type min##name(type a, type b) {               \
+        return a < b ? a : b;                             \
+    }                                                     \
+    inline type clamp##name(type t, type from, type to) { \
+        return max##name(min##name(t, to), from);         \
+    }
+MIN_MAX(size_t, Size)
+MIN_MAX(u32, U32)
 
 typedef void (*ExitHandler)(int, void*);
 [[noreturn]] void exitWithHandler(const char* file, int line, int value);

+ 1 - 0
src/Test.c

@@ -82,6 +82,7 @@ static bool addToResult(const char* file, bool comparison) {
 
 TEST_NAMED_COMPARE(Int, int, "%d")
 TEST_NAMED_COMPARE(I64, i64, "%" PRId64)
+TEST_NAMED_COMPARE(U32, u32, "%" PRIu32)
 TEST_NAMED_COMPARE(U64, u64, "%" PRIu64)
 TEST_NAMED_COMPARE(Size, size_t, "%zu")
 TEST_NAMED_COMPARE(Bool, bool, "%d")

+ 6 - 2
src/Utility.c

@@ -9,8 +9,12 @@
 #include "ErrorSimulator.h"
 #include "core/Logger.h"
 
-extern inline size_t maxSize(size_t a, size_t b);
-extern inline size_t minSize(size_t a, size_t b);
+#define MIN_MAX_DEF(type, name)                                \
+    extern inline type max##name(type a, type b);              \
+    extern inline type min##name(type a, type b);              \
+    extern inline type clamp##name(type t, type from, type to)
+MIN_MAX_DEF(size_t, Size);
+MIN_MAX_DEF(u32, U32);
 
 static ExitHandler exitHandler = nullptr;
 static void* exitData = nullptr;

+ 14 - 0
test/modules/UtilityTests.c

@@ -111,6 +111,19 @@ static void testSort() {
     }
 }
 
+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));
+}
+
 void testUtility(bool light) {
     testPopCount();
     testZeroRellocate();
@@ -124,6 +137,7 @@ void testUtility(bool light) {
     testSwap();
     testFail();
     testSort();
+    testMinMax();
 }
 
 static void outOfMemory(void*) {