Skip to content

Commit ed26a90

Browse files
committed
clar: cross-platform elapsed time counter
Abstract time counter for tests; use gettimeofday on Unix and GetTickCount on Windows.
1 parent 4e15c9c commit ed26a90

File tree

1 file changed

+37
-9
lines changed

1 file changed

+37
-9
lines changed

tests/clar/clar.c

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include <stdarg.h>
1414
#include <wchar.h>
1515
#include <time.h>
16-
#include <sys/time.h>
1716

1817
/* required for sandboxing */
1918
#include <sys/types.h>
@@ -251,41 +250,70 @@ clar_report_all(void)
251250
}
252251
}
253252

253+
#ifdef WIN32
254+
# define clar_time DWORD
255+
256+
static void clar_time_now(clar_time *out)
257+
{
258+
*out = GetTickCount();
259+
}
260+
261+
static double clar_time_diff(clar_time *start, clar_time *end)
262+
{
263+
return ((double)*end - (double)*start) / 1000;
264+
}
265+
#else
266+
# include <sys/time.h>
267+
268+
# define clar_time struct timeval
269+
270+
static void clar_time_now(clar_time *out)
271+
{
272+
struct timezone tz;
273+
274+
gettimeofday(out, &tz);
275+
}
276+
277+
static double clar_time_diff(clar_time *start, clar_time *end)
278+
{
279+
return ((double)end->tv_sec + (double)end->tv_usec / 1.0E6) -
280+
((double)start->tv_sec + (double)start->tv_usec / 1.0E6);
281+
}
282+
#endif
283+
254284
static void
255285
clar_run_test(
256286
const struct clar_suite *suite,
257287
const struct clar_func *test,
258288
const struct clar_func *initialize,
259289
const struct clar_func *cleanup)
260290
{
261-
struct timeval start, end;
262-
struct timezone tz;
291+
clar_time start, end;
263292

264293
_clar.trampoline_enabled = 1;
265294

266295
CL_TRACE(CL_TRACE__TEST__BEGIN);
267296

297+
_clar.last_report->start = time(NULL);
298+
clar_time_now(&start);
299+
268300
if (setjmp(_clar.trampoline) == 0) {
269301
if (initialize->ptr != NULL)
270302
initialize->ptr();
271303

272-
_clar.last_report->start = time(NULL);
273-
gettimeofday(&start, &tz);
274-
275304
CL_TRACE(CL_TRACE__TEST__RUN_BEGIN);
276305
test->ptr();
277306
CL_TRACE(CL_TRACE__TEST__RUN_END);
278307
}
279308

280-
gettimeofday(&end, &tz);
309+
clar_time_now(&end);
281310

282311
_clar.trampoline_enabled = 0;
283312

284313
if (_clar.last_report->status == CL_TEST_NOTRUN)
285314
_clar.last_report->status = CL_TEST_OK;
286315

287-
_clar.last_report->elapsed = ((double)end.tv_sec + (double)end.tv_usec / 1.0E6) -
288-
((double)start.tv_sec + (double)start.tv_usec / 1.0E6);
316+
_clar.last_report->elapsed = clar_time_diff(&start, &end);
289317

290318
if (_clar.local_cleanup != NULL)
291319
_clar.local_cleanup(_clar.local_cleanup_payload);

0 commit comments

Comments
 (0)