diff options
author | Edward Thomson <ethomson@edwardthomson.com> | 2023-01-25 09:37:22 +0000 |
---|---|---|
committer | Edward Thomson <ethomson@edwardthomson.com> | 2023-01-25 09:37:22 +0000 |
commit | ed26a908febf143d4639245b679c2c7797160cc9 (patch) | |
tree | b7d2e79cfda2a5e720b393555f628f8d08ffbe97 | |
parent | 4e15c9c87fe238364fd54b01f108c9671093b5d0 (diff) | |
download | libgit2-ed26a908febf143d4639245b679c2c7797160cc9.tar.gz |
clar: cross-platform elapsed time counter
Abstract time counter for tests; use gettimeofday on Unix and
GetTickCount on Windows.
-rw-r--r-- | tests/clar/clar.c | 46 |
1 files changed, 37 insertions, 9 deletions
diff --git a/tests/clar/clar.c b/tests/clar/clar.c index b3fd176d4..c9c3fde38 100644 --- a/tests/clar/clar.c +++ b/tests/clar/clar.c @@ -13,7 +13,6 @@ #include <stdarg.h> #include <wchar.h> #include <time.h> -#include <sys/time.h> /* required for sandboxing */ #include <sys/types.h> @@ -251,6 +250,37 @@ clar_report_all(void) } } +#ifdef WIN32 +# define clar_time DWORD + +static void clar_time_now(clar_time *out) +{ + *out = GetTickCount(); +} + +static double clar_time_diff(clar_time *start, clar_time *end) +{ + return ((double)*end - (double)*start) / 1000; +} +#else +# include <sys/time.h> + +# define clar_time struct timeval + +static void clar_time_now(clar_time *out) +{ + struct timezone tz; + + gettimeofday(out, &tz); +} + +static double clar_time_diff(clar_time *start, clar_time *end) +{ + return ((double)end->tv_sec + (double)end->tv_usec / 1.0E6) - + ((double)start->tv_sec + (double)start->tv_usec / 1.0E6); +} +#endif + static void clar_run_test( const struct clar_suite *suite, @@ -258,34 +288,32 @@ clar_run_test( const struct clar_func *initialize, const struct clar_func *cleanup) { - struct timeval start, end; - struct timezone tz; + clar_time start, end; _clar.trampoline_enabled = 1; CL_TRACE(CL_TRACE__TEST__BEGIN); + _clar.last_report->start = time(NULL); + clar_time_now(&start); + if (setjmp(_clar.trampoline) == 0) { if (initialize->ptr != NULL) initialize->ptr(); - _clar.last_report->start = time(NULL); - gettimeofday(&start, &tz); - CL_TRACE(CL_TRACE__TEST__RUN_BEGIN); test->ptr(); CL_TRACE(CL_TRACE__TEST__RUN_END); } - gettimeofday(&end, &tz); + clar_time_now(&end); _clar.trampoline_enabled = 0; if (_clar.last_report->status == CL_TEST_NOTRUN) _clar.last_report->status = CL_TEST_OK; - _clar.last_report->elapsed = ((double)end.tv_sec + (double)end.tv_usec / 1.0E6) - - ((double)start.tv_sec + (double)start.tv_usec / 1.0E6); + _clar.last_report->elapsed = clar_time_diff(&start, &end); if (_clar.local_cleanup != NULL) _clar.local_cleanup(_clar.local_cleanup_payload); |