Skip to content

Commit eed1390

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

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
}
@@ -291,7 +303,7 @@ static int run_concurrent(struct tf_framework* tf) {
291303
/* Child worker: read jobs from the shared pipe */
292304
close(pipefd[1]); /* children never write */
293305
while (read(pipefd[0], &idx, sizeof(idx)) == sizeof(idx)) {
294-
run_test(tf->args.targets.slots[(int)idx]);
306+
tf->fn_run_test(tf->args.targets.slots[(int)idx]);
295307
}
296308
_exit(EXIT_SUCCESS); /* finish child process */
297309
} else {
@@ -338,6 +350,7 @@ static int tf_init(struct tf_framework* tf, int argc, char** argv)
338350
tf->args.help = 0;
339351
tf->args.targets.size = 0;
340352
tf->args.list_tests = 0;
353+
tf->args.logging = 0;
341354

342355
/* Disable buffering for stdout to improve reliability of getting
343356
* diagnostic information. Happens right at the start of main because
@@ -381,6 +394,7 @@ static int tf_init(struct tf_framework* tf, int argc, char** argv)
381394
}
382395
}
383396

397+
tf->fn_run_test = tf->args.logging ? run_test_log : run_test;
384398
return EXIT_SUCCESS;
385399
}
386400

@@ -393,6 +407,12 @@ static int tf_run(struct tf_framework* tf) {
393407
int it;
394408
/* Initial test time */
395409
int64_t start_time = gettime_i64();
410+
/* Verify 'tf_init' has been called */
411+
if (!tf->fn_run_test) {
412+
fprintf(stderr, "Error: No test runner set. You must call 'tf_init' first to initialize the framework "
413+
"or manually assign 'fn_run_test' before calling 'tf_run'.\n");
414+
return EXIT_FAILURE;
415+
}
396416

397417
/* Populate targets with all tests if none were explicitly specified */
398418
run_all = tf->args.targets.size == 0;
@@ -411,6 +431,8 @@ static int tf_run(struct tf_framework* tf) {
411431
}
412432
}
413433

434+
if (!tf->args.logging) printf("Tests running silently. Use '-log=1' to enable detailed logging\n");
435+
414436
/* Log configuration */
415437
print_args(&tf->args);
416438

@@ -419,7 +441,7 @@ static int tf_run(struct tf_framework* tf) {
419441
/* is really only one test. */
420442
for (it = 0; tf->registry_no_rng && it < tf->registry_no_rng->size; it++) {
421443
if (run_all) { /* future: support filtering */
422-
run_test(&tf->registry_no_rng->data[it]);
444+
tf->fn_run_test(&tf->registry_no_rng->data[it]);
423445
}
424446
}
425447

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)