From 17e4b7758e14e15d24aa814efa0f10d631584c25 Mon Sep 17 00:00:00 2001 From: Camil Staps Date: Fri, 4 Dec 2015 18:52:39 +0100 Subject: More timing Time the following: - time per test - total time used - total wall time --- minctest.c | 16 ++++++++++++---- minctest.h | 4 +++- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/minctest.c b/minctest.c index d428f0f..66faf53 100644 --- a/minctest.c +++ b/minctest.c @@ -31,16 +31,22 @@ tester* test_initialize(void) { test->passed = 0; test->failed = 0; test->show_pass = true; + gettimeofday(&test->created, NULL); test_start_timer(test); return test; } void test_wrapup(tester* tester) { + struct timeval end; + gettimeofday(&end, NULL); + float time = ((float) end.tv_usec - tester->created.tv_usec) / 1000; + float used = ((float) tester->used) / 1000; + if (tester->failed == 0) { - printf(KGRN "\nAll tests passed (%d)\n" RESET, tester->passed); + printf(KGRN "\nAll tests passed (%d) [%.3fs / %.3fs]\n" RESET, tester->passed, used, time); } else { - printf(KRED "\n%d test%s failed" RESET " (%d passed)\n", - tester->failed, tester->failed > 1 ? "s" : "", tester->passed); + printf(KRED "\n%d test%s failed" RESET " (%d passed) [%.3fs / %.3fs]\n", + tester->failed, tester->failed > 1 ? "s" : "", tester->passed, used, time); } } @@ -61,13 +67,15 @@ void test_start_timer(tester* tester) { void test_true(tester* tester, bool check, const char* text) { struct timeval end; gettimeofday(&end, NULL); + tester->used += end.tv_usec - tester->start_last_test.tv_usec; + float time = ((float) end.tv_usec - tester->start_last_test.tv_usec) / 1000; if (!check || tester->show_pass) { printf(check ? KGRN : KRED); printf(check ? "Test passed" : "Test failed"); if (text != NULL) printf(": %s", text); - printf(" [%.3fs]", ((float) end.tv_usec - tester->start_last_test.tv_usec) / 1000); + printf(" [%.3fs]", time); printf(RESET "\n"); } diff --git a/minctest.h b/minctest.h index 1aaf19c..64b4519 100644 --- a/minctest.h +++ b/minctest.h @@ -26,7 +26,9 @@ typedef struct { uint16_t passed; // Number of passed tests uint16_t failed; // Number of failed tests bool show_pass; // Whether or not to print a message when a test passes - struct timeval start_last_test; + struct timeval start_last_test; // Time last test was started + struct timeval created; // Time this was created + __suseconds_t used; // usecs used for tests } tester; /** -- cgit v1.2.3