From 3257710fb759258f440aa5b67e6aff5900cb7fa5 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Sun, 28 Apr 2019 21:49:54 -0400 Subject: [PATCH] Fixed bad timers on GTK+ tests. Turns out that clock() doesn't count when the process isn't actively running code (for instance, if it's waiting for I/O) :| --- test/meson.build | 9 +++++++++ test/testing_darwinunix.c | 1 - test/testing_unix.c | 36 +++++++++++++----------------------- 3 files changed, 22 insertions(+), 24 deletions(-) diff --git a/test/meson.build b/test/meson.build index a9d49b38..1faec94b 100644 --- a/test/meson.build +++ b/test/meson.build @@ -42,6 +42,15 @@ if libui_OS == 'windows' required: true), ] endif +elif libui_OS == 'darwin' + # do nothing +else + libui_test_deps += [ + # satisfy old versions of glibc + # TODO is this really needed? + meson.get_compiler('c').find_library('rt', + required: false), + ] endif # TODO once we upgrade to 0.49.0, add pie: true diff --git a/test/testing_darwinunix.c b/test/testing_darwinunix.c index a3f21ff9..5f20562f 100644 --- a/test/testing_darwinunix.c +++ b/test/testing_darwinunix.c @@ -10,7 +10,6 @@ #include #include "testing.h" -// TODO testingTimers after this fails are wrong on GTK+ // TODO don't start the timer on any platform until after we call setjmp(); also decide whether to start the timer before or after resuming the thread on Windows static jmp_buf timeout_ret; diff --git a/test/testing_unix.c b/test/testing_unix.c index 9cdd8c46..2e9492ab 100644 --- a/test/testing_unix.c +++ b/test/testing_unix.c @@ -5,8 +5,8 @@ #include "testing.h" struct testingTimer { - clock_t start; - clock_t end; + struct timespec start; + struct timespec end; }; testingTimer *testingNewTimer(void) @@ -26,34 +26,24 @@ void testingFreeTimer(testingTimer *t) void testingTimerStart(testingTimer *t) { - t->start = clock(); + // TODO check errors + clock_gettime(CLOCK_MONOTONIC, &(t->start)); } void testingTimerEnd(testingTimer *t) { - t->end = clock(); + // TODO check errors + clock_gettime(CLOCK_MONOTONIC, &(t->end)); } +// TODO replace with proper subtraction code int64_t testingTimerNsec(testingTimer *t) { - clock_t c; - clock_t sec; - int64_t sec64; - clock_t subsec; - double subsecf; - int64_t subsec64; + int64_t nstart, nend; - c = t->end - t->start; - - sec = c / CLOCKS_PER_SEC; - sec64 = (int64_t) sec; - sec64 *= testingNsecPerSec; - - subsec = c % CLOCKS_PER_SEC; - subsecf = (double) subsec; - subsecf /= CLOCKS_PER_SEC; - subsecf *= testingNsecPerSec; - subsec64 = (int64_t) subsecf; - - return sec64 + subsec64; + nstart = ((int64_t) (t->start.tv_sec)) * testingNsecPerSec; + nstart += t->start.tv_nsec; + nend = ((int64_t) (t->end.tv_sec)) * testingNsecPerSec; + nend += t->end.tv_nsec; + return nend - nstart; }