Skip to content

Commit 726e70b

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

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

src/unit_test.c

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ static int parse_arg(const char* key, const char* value, struct TestFramework* t
5353
if (strcmp(key, "t") == 0 || strcmp(key, "target") == 0) {
5454
return parse_target(value, tf);
5555
}
56+
/* Logging */
57+
if (strcmp(key, "log") == 0) {
58+
tf->args.logging = value && strcmp(value, "1") == 0;
59+
return 0;
60+
}
5661

5762
/* Unknown key: report just so typos don’t silently pass. */
5863
printf("Unknown argument '-%s=%s'\n", key, value);
@@ -70,6 +75,7 @@ static void help(void) {
7075
printf(" -seed=<hex> Set a specific RNG seed (default: random)\n");
7176
printf(" -target=<test name>, -t=<name> Run a specific test (can be provided multiple times)\n");
7277
printf(" -target=<module name>, -t=<module> Run all tests within a specific module (can be provided multiple times)\n");
78+
printf(" -log=<0|1> Enable or disable test execution logging (default: 0 = disabled)\n");
7379
printf("\n");
7480
printf("Notes:\n");
7581
printf(" - All arguments must be provided in the form '-key=value'.\n");
@@ -180,18 +186,21 @@ static int read_args(int argc, char** argv, int start, struct TestFramework* tf)
180186
return 0;
181187
}
182188

183-
static void run_test(const struct TestEntry* t) {
189+
static void run_test_log(const struct TestEntry* t) {
190+
int64_t start_time = gettime_i64();
184191
printf("Running %s..\n", t->name);
185192
t->func();
186-
printf("%s PASSED\n", t->name);
193+
printf("Test %s PASSED (%.3f sec)\n", t->name, (double)(gettime_i64() - start_time) / 1000000);
187194
}
188195

196+
static void run_test(const struct TestEntry* t) { t->func(); }
197+
189198
/* Process tests in sequential order */
190199
static int run_sequential(struct TestFramework* tf) {
191200
int it;
192201
for (it = 0; it < tf->args.targets.size; it++) {
193202
TestRef* index = &tf->args.targets.slots[it];
194-
run_test(&tf->registry_modules[index->group].data[index->idx]);
203+
tf->fn_run_test(&tf->registry_modules[index->group].data[index->idx]);
195204
}
196205
return EXIT_SUCCESS;
197206
}
@@ -229,7 +238,7 @@ static int run_concurrent(struct TestFramework* tf) {
229238
TestRef tref;
230239
close(pipes[it][1]); /* Close write end */
231240
while (read(pipes[it][0], &tref, sizeof(tref)) == sizeof(tref)) {
232-
run_test(&tf->registry_modules[tref.group].data[tref.idx]);
241+
tf->fn_run_test(&tf->registry_modules[tref.group].data[tref.idx]);
233242
}
234243
_exit(EXIT_SUCCESS); /* finish child process */
235244
} else {
@@ -271,6 +280,7 @@ static int tf_init(struct TestFramework* tf, int argc, char** argv)
271280
tf->args.num_processes = 0;
272281
tf->args.custom_seed = NULL;
273282
tf->args.targets.size = 0;
283+
tf->args.logging = 0;
274284

275285
/* Disable buffering for stdout to improve reliability of getting
276286
* diagnostic information. Happens right at the start of main because
@@ -313,6 +323,7 @@ static int tf_init(struct TestFramework* tf, int argc, char** argv)
313323
}
314324
}
315325

326+
tf->fn_run_test = tf->args.logging ? run_test_log : run_test;
316327
return EXIT_SUCCESS;
317328
}
318329

@@ -325,6 +336,12 @@ static int tf_run(struct TestFramework* tf) {
325336
int it;
326337
/* Initial test time */
327338
int64_t start_time = gettime_i64(); /* maybe move this after the slots set */
339+
/* Verify 'tf_init' has been called */
340+
if (!tf->fn_run_test) {
341+
fprintf(stderr, "Error: No test runner set. You must call 'tf_init' first to initialize the framework "
342+
"or manually assign 'fn_run_test' before calling 'tf_run'.\n");
343+
return EXIT_FAILURE;
344+
}
328345

329346
/* Populate targets with all tests if none were explicitly specified */
330347
run_all = tf->args.targets.size == 0;
@@ -345,12 +362,14 @@ static int tf_run(struct TestFramework* tf) {
345362
tf->args.targets.size = slot;
346363
}
347364

365+
if (!tf->args.logging) printf("Tests running silently. Use '-log=1' to enable detailed logging\n");
366+
348367
/* Run test RNG tests (must run before we really initialize the test RNG) */
349368
/* Note: currently, these tests are executed sequentially because there */
350369
/* is really only one test. */
351370
for (it = 0; tf->registry_no_ctx && it < tf->registry_no_ctx->size; it++) {
352371
if (run_all) { /* future: support filtering */
353-
run_test(&tf->registry_no_ctx->data[it]);
372+
tf->fn_run_test(&tf->registry_no_ctx->data[it]);
354373
}
355374
}
356375

src/unit_test.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ struct TestModule {
6262

6363
typedef int (*setup_ctx_fn)(void);
6464
typedef int (*teardown_fn)(void);
65+
typedef void (*run_test_fn)(const struct TestEntry*);
6566

6667
/* Reference to a test in the registry. Group index and test index */
6768
typedef struct {
@@ -84,6 +85,8 @@ struct Args {
8485
const char* custom_seed;
8586
/* Target tests indexes */
8687
struct Targets targets;
88+
/* Enable test execution logging */
89+
int logging;
8790
};
8891

8992
struct TestFramework {
@@ -98,6 +101,8 @@ struct TestFramework {
98101
/* Specific context setup and teardown functions */
99102
setup_ctx_fn fn_setup;
100103
teardown_fn fn_teardown;
104+
/* Test runner function (can be customized) */
105+
run_test_fn fn_run_test;
101106
};
102107

103108
/* --------------------------------------------------------- */

0 commit comments

Comments
 (0)