Skip to content

Commit 2f4546c

Browse files
committed
test: add --log option to display tests execution
When enabled (--log=1), shows test start, completion, and execution time.
1 parent 95b9953 commit 2f4546c

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

src/unit_test.c

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ static int parse_jobs_count(const char* key, const char* value, struct tf_framew
2626
static int parse_iterations(const char* key, const char* value, struct tf_framework* tf);
2727
static int parse_seed(const char* key, const char* value, struct tf_framework* tf);
2828
static int parse_target(const char* key, const char* value, struct tf_framework* tf);
29+
static int parse_logging(const char* key, const char* value, struct tf_framework* tf);
2930

3031
/* Mapping table: key -> handler */
3132
typedef int (*ArgHandler)(const char* key, const char* value, struct tf_framework* tf);
@@ -46,6 +47,7 @@ static struct ArgMap arg_map[] = {
4647
{ "j", parse_jobs_count }, { "jobs", parse_jobs_count },
4748
{ "i", parse_iterations }, { "iterations", parse_iterations },
4849
{ "seed", parse_seed },
50+
{ "log", parse_logging },
4951
{ NULL, NULL } /* sentinel */
5052
};
5153

@@ -87,6 +89,7 @@ static void help(void) {
8789
printf(" --seed=<hex> Set a specific RNG seed (default: random)\n");
8890
printf(" --target=<test name>, -t=<name> Run a specific test (can be provided multiple times)\n");
8991
printf(" --target=<module name>, -t=<module> Run all tests within a specific module (can be provided multiple times)\n");
92+
printf(" --log=<0|1> Enable or disable test execution logging (default: 0 = disabled)\n");
9093
printf("\n");
9194
printf("Notes:\n");
9295
printf(" - All arguments must be provided in the form '--key=value', '-key=value' or '-k=value'.\n");
@@ -146,6 +149,12 @@ static int parse_seed(const char* key, const char* value, struct tf_framework* t
146149
return 0;
147150
}
148151

152+
static int parse_logging(const char* key, const char* value, struct tf_framework* tf) {
153+
UNUSED(key);
154+
tf->args.logging = value && strcmp(value, "1") == 0;
155+
return 0;
156+
}
157+
149158
/* Strip up to two leading dashes */
150159
static const char* normalize_key(const char* arg, const char** err_msg) {
151160
const char* key;
@@ -250,17 +259,20 @@ static int read_args(int argc, char** argv, int start, struct tf_framework* tf)
250259
return 0;
251260
}
252261

253-
static void run_test(const struct tf_test_entry* t) {
262+
static void run_test_log(const struct tf_test_entry* t) {
263+
int64_t start_time = gettime_i64();
254264
printf("Running %s..\n", t->name);
255265
t->func();
256-
printf("%s PASSED\n", t->name);
266+
printf("Test %s PASSED (%.3f sec)\n", t->name, (double)(gettime_i64() - start_time) / 1000000);
257267
}
258268

269+
static void run_test(const struct tf_test_entry* t) { t->func(); }
270+
259271
/* Process tests in sequential order */
260272
static int run_sequential(struct tf_framework* tf) {
261273
int it;
262274
for (it = 0; it < tf->args.targets.size; it++) {
263-
run_test(tf->args.targets.slots[it]);
275+
tf->fn_run_test(tf->args.targets.slots[it]);
264276
}
265277
return EXIT_SUCCESS;
266278
}
@@ -301,7 +313,7 @@ static int run_concurrent(struct tf_framework* tf) {
301313
/* Child worker: read jobs from the shared pipe */
302314
close(pipefd[1]); /* children never write */
303315
while (read(pipefd[0], &idx, sizeof(idx)) == sizeof(idx)) {
304-
run_test(tf->args.targets.slots[(int)idx]);
316+
tf->fn_run_test(tf->args.targets.slots[(int)idx]);
305317
}
306318
_exit(EXIT_SUCCESS); /* finish child process */
307319
} else {
@@ -348,6 +360,7 @@ static int tf_init(struct tf_framework* tf, int argc, char** argv)
348360
tf->args.help = 0;
349361
tf->args.targets.size = 0;
350362
tf->args.list_tests = 0;
363+
tf->args.logging = 0;
351364

352365
/* Disable buffering for stdout to improve reliability of getting
353366
* diagnostic information. Happens right at the start of main because
@@ -391,6 +404,7 @@ static int tf_init(struct tf_framework* tf, int argc, char** argv)
391404
}
392405
}
393406

407+
tf->fn_run_test = tf->args.logging ? run_test_log : run_test;
394408
return EXIT_SUCCESS;
395409
}
396410

@@ -403,6 +417,12 @@ static int tf_run(struct tf_framework* tf) {
403417
int it;
404418
/* Initial test time */
405419
int64_t start_time = gettime_i64();
420+
/* Verify 'tf_init' has been called */
421+
if (!tf->fn_run_test) {
422+
fprintf(stderr, "Error: No test runner set. You must call 'tf_init' first to initialize the framework "
423+
"or manually assign 'fn_run_test' before calling 'tf_run'.\n");
424+
return EXIT_FAILURE;
425+
}
406426

407427
/* Populate targets with all tests if none were explicitly specified */
408428
run_all = tf->args.targets.size == 0;
@@ -421,6 +441,8 @@ static int tf_run(struct tf_framework* tf) {
421441
}
422442
}
423443

444+
if (!tf->args.logging) printf("Tests running silently. Use '-log=1' to enable detailed logging\n");
445+
424446
/* Log configuration */
425447
print_args(&tf->args);
426448

@@ -429,7 +451,7 @@ static int tf_run(struct tf_framework* tf) {
429451
/* is really only one test. */
430452
for (it = 0; tf->registry_no_rng && it < tf->registry_no_rng->size; it++) {
431453
if (run_all) { /* future: support filtering */
432-
run_test(&tf->registry_no_rng->data[it]);
454+
tf->fn_run_test(&tf->registry_no_rng->data[it]);
433455
}
434456
}
435457

src/unit_test.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ struct tf_test_module {
6161

6262
typedef int (*setup_ctx_fn)(void);
6363
typedef int (*teardown_fn)(void);
64+
typedef void (*run_test_fn)(const struct tf_test_entry*);
6465

6566
struct tf_targets {
6667
/* Target tests indexes */
@@ -81,6 +82,8 @@ struct tf_args {
8182
int list_tests;
8283
/* Target tests indexes */
8384
struct tf_targets targets;
85+
/* Enable test execution logging */
86+
int logging;
8487
};
8588

8689
/* --------------------------------------------------------- */
@@ -99,6 +102,8 @@ struct tf_framework {
99102
/* Specific context setup and teardown functions */
100103
setup_ctx_fn fn_setup;
101104
teardown_fn fn_teardown;
105+
/* Test runner function (can be customized) */
106+
run_test_fn fn_run_test;
102107
};
103108

104109
/*

0 commit comments

Comments
 (0)