Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bflibcpp/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
48 changes: 48 additions & 0 deletions bftest/src/bftest.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

24 changes: 18 additions & 6 deletions bftest/src/bftest.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <time.h>

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
Expand All @@ -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;\
}

Expand Down Expand Up @@ -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;

Expand Down