Browse Source

Clock utils

Kajetan Johannes Hammerle 11 months ago
parent
commit
82e864921f
8 changed files with 51 additions and 21 deletions
  1. 7 10
      CMakeLists.txt
  2. 3 2
      include/core/Utility.h
  3. 17 0
      src/Utility.c
  4. 1 4
      test/Main.c
  5. 1 0
      test/Test.c
  6. 2 0
      test/Test.h
  7. 1 4
      test/Tests.h
  8. 19 1
      test/modules/UtilityTests.c

+ 7 - 10
CMakeLists.txt

@@ -10,7 +10,6 @@ set(SRC
     "src/Utility.c"
     #"src/BitArray.cpp"
     #"src/Box.cpp"
-    #"src/Clock.cpp"
     #"src/Frustum.cpp"
     #"src/Matrix.cpp"
     #"src/Mutex.cpp"
@@ -30,7 +29,6 @@ set(SRC_TESTS
     "test/modules/UtilityTests.c"
     #"test/modules/BitArrayTests.cpp"
     #"test/modules/BoxTests.cpp"
-    #"test/modules/ClockTests.cpp"
     #"test/modules/ColorTests.cpp"
     #"test/modules/ComponentsTests.cpp"
     #"test/modules/FrustumTests.cpp"
@@ -166,24 +164,23 @@ target_sources(core PUBLIC
         ./include/core/Types.h
         ./include/core/Utility.h
 #        ./include/core/BitArray.hpp
+#        ./include/core/Box.hpp
+#        ./include/core/Color.hpp
 #        ./include/core/Components.hpp
+#        ./include/core/Frustum.hpp
 #        ./include/core/HashMap.hpp
 #        ./include/core/LinkedList.hpp
 #        ./include/core/List.hpp
-#        ./include/core/ProbingHashMap.hpp
-#        ./include/core/RingBuffer.hpp
-#        ./include/core/Stack.hpp
-#        ./include/core/Box.hpp
-#        ./include/core/Frustum.hpp
 #        ./include/core/Matrix.hpp
 #        ./include/core/MatrixStack.hpp
 #        ./include/core/Plane.hpp
+#        ./include/core/ProbingHashMap.hpp
 #        ./include/core/Quaternion.hpp
+#        ./include/core/RingBuffer.hpp
+#        ./include/core/Stack.hpp
+#        ./include/core/Thread.hpp
 #        ./include/core/Vector.hpp
 #        ./include/core/View.hpp
-#        ./include/core/Thread.hpp
-#        ./include/core/Clock.hpp
-#        ./include/core/Color.hpp
 )
 install(TARGETS core FILE_SET HEADERS)
 

+ 3 - 2
include/core/Utility.h

@@ -23,11 +23,9 @@ void* coreDebugAllocate(const char* file, int line, size_t n);
 void* coreDebugReallocate(const char* file, int line, void* p, size_t n);
 void coreFreeDebug(const char* file, int line, void* p);
 void corePrintMemoryReport();
-
 #define coreAllocate(n) coreDebugAllocate(__FILE__, __LINE__, n)
 #define coreReallocate(p, n) coreDebugReallocate(__FILE__, __LINE__, p, n)
 #define coreFree(p) coreFreeDebug(__FILE__, __LINE__, p)
-
 #else
 void* coreAllocate(size_t n);
 void* coreReallocate(void* p, size_t n);
@@ -35,4 +33,7 @@ void coreFree(void* p);
 #define corePrintMemoryReport()
 #endif
 
+bool coreSleep(i64 nanos);
+i64 coreNanos(void);
+
 #endif

+ 17 - 0
src/Utility.c

@@ -3,6 +3,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <threads.h>
+#include <time.h>
 
 #include "ErrorSimulator.h"
 #include "core/Logger.h"
@@ -196,3 +198,18 @@ void coreFree(void* p) {
 }
 
 #endif
+
+bool coreSleep(i64 nanos) {
+    struct timespec t;
+    t.tv_nsec = nanos % 1'000'000'000;
+    t.tv_sec = nanos / 1'000'000'000;
+    return thrd_sleep(&t, nullptr) != 0;
+}
+
+i64 coreNanos(void) {
+    struct timespec ts;
+    if(timespec_get(&ts, TIME_UTC) == 0 || CORE_TIME_GET_FAIL) {
+        return -1;
+    }
+    return (i64)ts.tv_sec * 1'000'000'000L + (i64)ts.tv_nsec;
+}

+ 1 - 4
test/Main.c

@@ -34,14 +34,12 @@ int main(int argAmount, const char** args) {
     // coreTestBitArray();
     // coreTestBox();
     coreTestBuffer(light);
-    // coreTestClock(light);
     // coreTestColor();
     // coreTestComponents();
     // coreTestFrustum();
     // coreTestHashMap(light);
     // coreTestLinkedList(light);
     // coreTestList(light);
-    // coreTestMath();
     // coreTestMatrixStack(light);
     // coreTestMatrix();
     // coreTestPlane();
@@ -50,8 +48,7 @@ int main(int argAmount, const char** args) {
     // coreTestRingBuffer();
     // coreTestStack(light);
     // coreTestThread();
-    // coreTestUniquePointer();
-    coreTestUtility();
+    coreTestUtility(light);
     // coreTestVector();
     // coreTestView();
 

+ 1 - 0
test/Test.c

@@ -82,6 +82,7 @@ static bool addToResult(const char* file, bool comparison) {
     }
 
 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")

+ 2 - 0
test/Test.h

@@ -10,6 +10,7 @@ void coreFinalizeTests(void);
     bool coreTest##name(CORE_TEST_ARGS, type wanted, type actual)
 
 CORE_TEST_FUNCTION(Int, int);
+CORE_TEST_FUNCTION(I64, i64);
 CORE_TEST_FUNCTION(U64, u64);
 CORE_TEST_FUNCTION(Size, size_t);
 CORE_TEST_FUNCTION(Bool, bool);
@@ -28,6 +29,7 @@ bool coreTestNotNull(CORE_TEST_ARGS, const void* p);
 
 #define CORE_TEST_BOOL(wanted, actual) CORE_TEST(wanted, actual, Bool)
 #define CORE_TEST_INT(wanted, actual) CORE_TEST(wanted, actual, Int)
+#define CORE_TEST_I64(wanted, actual) CORE_TEST(wanted, actual, I64)
 #define CORE_TEST_U64(wanted, actual) CORE_TEST(wanted, actual, U64)
 #define CORE_TEST_SIZE(wanted, actual) CORE_TEST(wanted, actual, Size)
 #define CORE_TEST_STRING(wanted, actual) CORE_TEST(wanted, actual, String)

+ 1 - 4
test/Tests.h

@@ -6,8 +6,6 @@
 void coreTestBitArray(void);
 void coreTestBox(void);
 void coreTestBuffer(bool light);
-void coreTestBufferedValue(void);
-void coreTestClock(bool light);
 void coreTestColor(void);
 void coreTestComponents(void);
 void coreTestFileReader(void);
@@ -15,7 +13,6 @@ void coreTestFrustum(void);
 void coreTestHashMap(bool light);
 void coreTestLinkedList(bool light);
 void coreTestList(bool light);
-void coreTestMath(void);
 void coreTestMatrixStack(bool light);
 void coreTestMatrix(void);
 void coreTestPlane(void);
@@ -24,7 +21,7 @@ void coreTestRandom(bool light);
 void coreTestRingBuffer(void);
 void coreTestStack(bool light);
 void coreTestThread(void);
-void coreTestUtility(void);
+void coreTestUtility(bool light);
 void coreTestInvalidAllocate(void);
 void coreTestInvalidReallocate(void);
 void coreTestPreCanary(void);

+ 19 - 1
test/modules/UtilityTests.c

@@ -56,13 +56,31 @@ static void testDegreeToRadian() {
     CORE_TEST_FLOAT(CORE_PI * 2.0f, coreDegreeToRadian(360.0f), eps);
 }
 
-void coreTestUtility() {
+static void testSleep(i64 nanos) {
+    i64 time = -coreNanos();
+    CORE_TEST_FALSE(coreSleep(nanos));
+    time += coreNanos();
+    CORE_TEST_TRUE(time >= nanos && time <= (nanos * 13) / 10);
+}
+
+static void testFail() {
+#ifdef ERROR_SIMULATOR
+    coreFailTimeGet = true;
+    CORE_TEST_I64(-1, coreNanos());
+    coreFailTimeGet = false;
+#endif
+}
+
+void coreTestUtility(bool light) {
     testPopCount();
     testZeroRellocate();
     testMemoryInfoList();
     testInterpolate();
     testRadianToDegree();
     testDegreeToRadian();
+    testSleep(light ? 5'000'000 : 50'000'000);
+    testSleep(light ? 50'000'000 : 1'300'000'000);
+    testFail();
 }
 
 static void outOfMemory(void*) {