From 7e631879e735d0c4761c208439533983ce520fa1 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Tue, 23 Apr 2019 12:26:52 -0400 Subject: [PATCH] Wrote the Unix clock function. It's primitive, but it works. --- test/meson.build | 2 ++ test/testing_unix.c | 59 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 test/testing_unix.c diff --git a/test/meson.build b/test/meson.build index e2502dd2..12386512 100644 --- a/test/meson.build +++ b/test/meson.build @@ -8,6 +8,8 @@ libui_test_sources = [ if libui_OS == 'darwin' libui_test_sources += ['testing_darwin.c'] +else + libui_test_sources += ['testing_unix.c'] endif if libui_OS == 'windows' diff --git a/test/testing_unix.c b/test/testing_unix.c new file mode 100644 index 00000000..85956b60 --- /dev/null +++ b/test/testing_unix.c @@ -0,0 +1,59 @@ +// 23 april 2019 +#include +#include +#include +#include "testing.h" + +struct testingTimer { + clock_t start; + clock_t end; +}; + +testingTimer *testingNewTimer(void) +{ + testingTimer *t; + + t = (testingTimer *) malloc(sizeof (testingTimer)); + // TODO handle failure + memset(t, 0, sizeof (testingTimer)); + return t; +} + +void testingFreeTimer(testingTimer *t) +{ + free(t); +} + +void testingTimerStart(testingTimer *t) +{ + t->start = clock(); +} + +void testingTimerEnd(testingTimer *t) +{ + t->end = clock(); +} + +int64_t testingTimerNsec(testingTimer *t) +{ + clock_t c; + clock_t sec; + int64_t sec64; + clock_t subsec; + double subsecf; + int64_t subsec64; + + c = t->end - t->start; + + sec = c / CLOCKS_PER_SEC; + sec64 = (int64_t) sec; + sec64 *= testingTimerNsecPerSec; + + subsec = c % CLOCKS_PER_SEC; + subsecf = (double) subsec; + subsecf /= CLOCKS_PER_SEC; + subsecf *= testingTimerNsecPerSec; + subsec64 = (int64_t) subsecf; + + return sec64 + subsec64; +}