From: Niki Roo Date: Tue, 25 Jun 2024 18:10:48 +0000 (+0200) Subject: timing and launcher changes X-Git-Url: http://git.nikiroo.be/?a=commitdiff_plain;h=493c8a6cd70e7e4b520d15edcf9922576506ca59;p=cutils.git timing and launcher changes --- diff --git a/check/launcher.c b/check/launcher.c index 855bf25..cc39935 100644 --- a/check/launcher.c +++ b/check/launcher.c @@ -109,6 +109,31 @@ void test_failure() { stop_color()); } +void assert_str(const char title[], const char expected[], + const char received[]) { + if (strcmp(expected, received)) { + failure("%s\n\tExpected: <%s>\n\tReceived: <%s>", + title, expected, received + ); + } +} + +void assert_int(const char title[], long long expected, long long received) { + if (received != expected) { + failure("%s\n\tExpected: %lld\n\tReceived: %lld", + title, expected, received + ); + } +} + +void assert_sz(const char title[], size_t expected, size_t received) { + if (received != expected) { + failure("%s\n\tExpected: %zu\n\tReceived: %zu", + title, expected, received + ); + } +} + int test_start(int more) { int failed; SRunner *runner; diff --git a/check/launcher.h b/check/launcher.h index 8df774a..ffc45e5 100644 --- a/check/launcher.h +++ b/check/launcher.h @@ -12,24 +12,17 @@ START_TEST(name) {\ }\ END_TEST\ -#define FAIL(...) \ -/* TODO: test_failure(); ??? */ \ -ck_abort_msg(__VA_ARGS__)\ - -#define ASSERT_EQUALS_STR(title, expected, received) \ - do { if (strcmp(expected, received)) { \ -ck_abort_msg("%s\n\tExpected: <%s>\n\tReceived: <%s>", title, expected, received); \ -}} while(0) - -#define ASSERT_EQUALS_INT(title, expected, received) \ - do { if (expected != received) { \ -ck_abort_msg("%s\n\tExpected: %lld\n\tReceived: %lld", title, (long long)expected, (long long)received); \ -}} while(0) - -#define ASSERT_EQUALS_SIZE(title, expected, received) \ - do { if (expected != received) { \ -ck_abort_msg("%s\n\tExpected: %zu\n\tReceived: %zu", title, expected, received); \ -}} while(0) +#define failure(...) do { \ + test_failure(); \ + ck_abort_msg(__VA_ARGS__); \ +} while(0) + +void assert_str(const char title[], const char expected[], + const char received[]); + +void assert_int(const char title[], long long expected, long long received); + +void assert_sz(const char title[], size_t expected, size_t received); extern int launcher_color; diff --git a/timing.h b/timing.h index bd190ef..81cb8cf 100644 --- a/timing.h +++ b/timing.h @@ -36,23 +36,50 @@ extern "C" { #include +typedef struct { + struct timeval start; + struct timeval stop; + size_t sec; + int msec; + int usec; +} timing_t; + +/** + * Start the timing. + */ +#define TIMING_START(timing) \ + timing_t timing; \ + gettimeofday(&(timing.start), NULL); \ + timing.sec = timing.msec = timing.usec = 0; \ + timing.stop.tv_sec = 0; \ + timing.stop.tv_usec = 0; \ +while(0) + /** - * Start the timer. + * Stop the timing and return the elapsed time in milliseconds into msec. */ -#define TIMING_START struct timeval TIMING_start, TIMING_stop; \ - /* 1 usec = 0.000001 s */ \ - char cusec[7]; \ - gettimeofday(&TIMING_start, NULL); +#define TIMING_STOP(timing) \ + gettimeofday(&(timing.stop), NULL); \ + if (timing.stop.tv_usec >= timing.start.tv_usec) { \ + timing.sec = timing.stop.tv_sec - timing.start.tv_sec; \ + timing.usec = timing.stop.tv_usec - timing.start.tv_usec; \ + } else { \ + timing.sec = (timing.stop.tv_sec - timing.start.tv_sec) - 1; \ + timing.usec = timing.start.tv_usec - timing.stop.tv_usec; \ + } \ + timing.msec = timing.usec / 1000; \ + timing.usec = timing.usec % 1000; \ + gettimeofday(&(timing.start), NULL); \ +while(0) /** - * Stop the timer and print the elapsed time to stdout. + * Stop the timing and print the elapsed time. */ -#define TIMING_STOP gettimeofday(&TIMING_stop, NULL); \ - TIMING_stop.tv_sec = TIMING_stop.tv_sec - TIMING_start.tv_sec; \ - TIMING_stop.tv_usec = TIMING_stop.tv_usec - TIMING_start.tv_usec; \ - sprintf(cusec, "%0.6d", TIMING_stop.tv_usec); \ - printf("TIME: %d.%s sec\n", TIMING_stop.tv_sec, cusec); \ - gettimeofday(&TIMING_start, NULL); +#define TIMING_PRINT(timing) \ + TIMING_STOP(timing); \ + printf("TIME: %zu.%03d%03d sec\n", \ + timing.sec, timing.msec, timing.usec); \ +while(0) #endif // TIMING_H