diff --git a/bflibcpp/makefile b/bflibcpp/makefile index 42911649..fe5f1626 100644 --- a/bflibcpp/makefile +++ b/bflibcpp/makefile @@ -51,8 +51,8 @@ MAIN_FILE = testbench/tests.cpp endif # ($(CONFIG),...) dependencies: - cd ../bflibc/ && make clean && make build && make build CONFIG=debug cd ../bftest/ && make clean && make build && make build CONFIG=debug + cd ../bflibc/ && make clean && make build && make build CONFIG=debug include makefiles/lib.mk LIBS_MAKEFILES_PATH:=$(shell dirname $(CURDIR))/makefiles diff --git a/bftest/src/bftest.c b/bftest/src/bftest.c index 182dbf13..77b18947 100644 --- a/bftest/src/bftest.c +++ b/bftest/src/bftest.c @@ -159,3 +159,51 @@ void _BFTestLogFlush() { } } +long long __BFTestGetCurrentTimeNS__() { + struct timespec ts; + if (clock_gettime(CLOCK_REALTIME, &ts) == -1) { + return 0; + } + + return (long long) ts.tv_sec * 1000000000LL + ts.tv_nsec; +} + +void __BFTestFormatElapsedTime__(long long elapsedTimeNS, char * buf, size_t bufsize) { + if (!buf) return; + + // 0: nano, 1: micro, 2: milli, 3: seconds, 4: minutes + int level = 0; + const int maxLevel = 3; + float ns = elapsedTimeNS; + + while (ns > 1000 && level <= maxLevel) { + ns /= 1000; + level++; + } + + char * unit = NULL; + switch (level) { + case 0: + unit = "ns"; + break; + case 1: + unit = "us"; + break; + case 2: + unit = "ms"; + break; + case 3: + default: + unit = "s"; + break; + } + + if (ns >= 60 && level == 3) { + int min = ns / 60; + int sec = (int) ns % 60; + snprintf(buf, bufsize, "%d min %d s", min, sec); + } else { + snprintf(buf, bufsize, "%.2f %s", ns, unit); + } +} + diff --git a/bftest/src/bftest.h b/bftest/src/bftest.h index 61e6bb45..eba2f627 100644 --- a/bftest/src/bftest.h +++ b/bftest/src/bftest.h @@ -11,6 +11,10 @@ #include #include #include +#include + +long long __BFTestGetCurrentTimeNS__(); +void __BFTestFormatElapsedTime__(long long elapsedTimeNS, char * buf, size_t bufsize); /** TEST SUITE **/ // a test suite is the high level function that will call @@ -20,8 +24,12 @@ int main () {\ int pass = 0, fail = 0;\ float tp = 0, tf = 0;\ + time_t startTime = __BFTestGetCurrentTimeNS__();\ __VA_ARGS__ \ - printf("Grade - %.2f%% (%d/%d)\n", (float) ((tp/(tp+tf)) * 100), (int) tp, (int) (tp+tf));\ + time_t endTime = __BFTestGetCurrentTimeNS__();\ + char buf[64];\ + __BFTestFormatElapsedTime__(endTime - startTime, buf, 64);\ + printf("Grade - %.2f%% (%d/%d) %s\n", (float) ((tp/(tp+tf)) * 100), (int) tp, (int) (tp+tf), buf);\ return 0;\ } @@ -84,13 +92,17 @@ #define BFTEST_UNIT_START \ printf("%s - ", __func__);fflush(stdout);\ fflush(stdout);\ - int result = 0; + int result = 0;\ + time_t startTime = __BFTestGetCurrentTimeNS__(); #define BFTEST_UNIT_END \ - if (result == 0) { printf("PASS\n");fflush(stdout); }\ - else {\ - printf("FAIL\n");fflush(stdout);\ - }\ + time_t endTime = __BFTestGetCurrentTimeNS__();\ + if (result == 0) { printf("PASS"); }\ + else { printf("FAIL"); }\ + char buf[64];\ + __BFTestFormatElapsedTime__(endTime - startTime, buf, 64);\ + printf(" (%s)\n", buf);\ + fflush(stdout);\ _BFTestLogFlush();\ return result;