aboutsummaryrefslogtreecommitdiff
path: root/utest
diff options
context:
space:
mode:
authorYaroslav de la Peña Smirnov <yps@yaroslavps.com>2025-09-12 12:31:51 +0300
committerYaroslav de la Peña Smirnov <yps@yaroslavps.com>2025-09-12 12:31:51 +0300
commit3620253a9463a9a256a2a031312d175fd23c73c1 (patch)
tree6481bcaacc3ace65bf61686eaf2686153237834f /utest
parentac22f535bcd14f8e201d410265aab268899d7753 (diff)
downloadc-wares-master.tar.gz
c-wares-master.zip
utest: add benchmarksHEADmaster
Also updated clang-format a bit.
Diffstat (limited to 'utest')
-rw-r--r--utest/utest.h41
1 files changed, 33 insertions, 8 deletions
diff --git a/utest/utest.h b/utest/utest.h
index 218b2b7..4222183 100644
--- a/utest/utest.h
+++ b/utest/utest.h
@@ -10,6 +10,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
+#include <time.h>
/*
* TODO?: test runners that run in different processes in order to not crash the
@@ -89,7 +90,7 @@ static setup_fn tests_destroy = NULL;
*/
#define asserteq(a, b) \
({ \
- if ((a) != (b)) { \
+ if ((a) != (b)) { \
FAIL_TEST("assertion " TBLD TBLU #a " == " #b TRST " failed"); \
} \
})
@@ -101,7 +102,7 @@ static setup_fn tests_destroy = NULL;
*/
#define assertneq(a, b) \
({ \
- if ((a) == (b)) { \
+ if ((a) == (b)) { \
FAIL_TEST("assertion " TBLD TBLU #a " != " #b TRST " failed"); \
} \
})
@@ -114,7 +115,7 @@ static setup_fn tests_destroy = NULL;
*/
#define expect(cond, reason...) \
({ \
- if (!(cond)) { \
+ if (!(cond)) { \
FAIL_TEST(reason); \
} \
})
@@ -125,11 +126,11 @@ static setup_fn tests_destroy = NULL;
*
* Should be followed by TEST_OUT and TEST_END.
*/
-#define TEST_BEGIN(name) \
- int name(void) \
- { \
- const char *__this_name = #name; \
- bool __ret = true; \
+#define TEST_BEGIN(name) \
+ int name(void) \
+ { \
+ const char *__this_name = #name; \
+ bool __ret = true; \
printf(" [⏳]\t%s: " TBLU "running..." TRST, __this_name); \
fflush(stdout);
@@ -154,6 +155,30 @@ test_out:;
return __ret; \
}
+#define BENCH_BEGIN(name) \
+ int name(void) \
+ { \
+ const char *__this_name = #name; \
+ struct timespec __t1, __t2; \
+ clock_gettime(CLOCK_MONOTONIC, &__t1); \
+ printf(" [⏳]\t%s: " TBLU "running..." TRST, __this_name); \
+ fflush(stdout);
+
+#define BENCH_END \
+ clock_gettime(CLOCK_MONOTONIC, &__t2); \
+ __t2.tv_sec -= __t1.tv_sec; \
+ if (__t2.tv_nsec < __t1.tv_nsec) { \
+ __t2.tv_nsec = __t1.tv_nsec - __t2.tv_nsec; \
+ __t2.tv_sec -= 1; \
+ } else { \
+ __t2.tv_nsec = __t2.tv_nsec - __t1.tv_nsec; \
+ } \
+ printf(TDEL " [" TBLD TGRN "✓" TRST "]\t%s: ran in\t" TBLD TBLU \
+ "%lu.%09lus\n" TRST, \
+ __this_name, __t2.tv_sec, __t2.tv_nsec); \
+ return true; \
+ }
+
/**
* __run_tests() - runs tests; for internal use.
*/