From 95b9953ea44ad6273dd8ddd2040844a54936ed71 Mon Sep 17 00:00:00 2001 From: furszy Date: Mon, 8 Sep 2025 14:17:12 -0400 Subject: [PATCH] test: Add option to display all available tests Useful option to avoid opening the large tests.c file just to find the test case you want to run. --- src/unit_test.c | 31 ++++++++++++++++++++++++++++++- src/unit_test.h | 2 ++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/unit_test.c b/src/unit_test.c index 513bc2cc..4eace526 100644 --- a/src/unit_test.c +++ b/src/unit_test.c @@ -81,6 +81,7 @@ static void help(void) { printf("Run the test suite for the project with optional configuration.\n\n"); printf("Options:\n"); printf(" --help, -h Show this help message\n"); + printf(" --list_tests, -l Display list of all available tests and modules\n"); printf(" --jobs=, -j= Number of parallel worker processes (default: 0 = sequential)\n"); printf(" --iterations=, -i= Number of iterations for each test (default: 16)\n"); printf(" --seed= Set a specific RNG seed (default: random)\n"); @@ -95,6 +96,24 @@ static void help(void) { printf(" - Iterations and seed can also be passed as positional arguments before any other argument for backward compatibility.\n"); } +/* Print all tests in registry */ +static void print_test_list(struct tf_framework* tf) { + int m, t, total = 0; + printf("\nAvailable tests (%d modules):\n", tf->num_modules); + printf("========================================\n"); + for (m = 0; m < tf->num_modules; m++) { + const struct tf_test_module* mod = &tf->registry_modules[m]; + printf("Module: %s (%d tests)\n", mod->name, mod->size); + for (t = 0; t < mod->size; t++) { + printf("\t[%3d] %s\n", total + 1, mod->data[t].name); + total++; + } + printf("----------------------------------------\n"); + } + printf("\nRun specific module: ./tests -t=\n"); + printf("Run specific test: ./tests -t=\n\n"); +} + static int parse_jobs_count(const char* key, const char* value, struct tf_framework* tf) { char* ptr_val; long val = strtol(value, &ptr_val, 10); /* base 10 */ @@ -180,7 +199,7 @@ static int parse_target(const char* key, const char* value, struct tf_framework* if (add_all) return 0; } fprintf(stderr, "Error: target '%s' not found (missing or module disabled).\n" - "Run program with -print_tests option to display available tests and modules.\n", value); + "Run program with -list_tests option to display available tests and modules.\n", value); return -1; } @@ -211,6 +230,10 @@ static int read_args(int argc, char** argv, int start, struct tf_framework* tf) tf->args.help = 1; return 0; } + if (strcmp(key, "l") == 0 || strcmp(key, "list_tests") == 0) { + tf->args.list_tests = 1; + return 0; + } fprintf(stderr, "Invalid arg '%s': must be -k=value or --key=value\n", raw_arg); return -1; } @@ -324,6 +347,7 @@ static int tf_init(struct tf_framework* tf, int argc, char** argv) tf->args.custom_seed = NULL; tf->args.help = 0; tf->args.targets.size = 0; + tf->args.list_tests = 0; /* Disable buffering for stdout to improve reliability of getting * diagnostic information. Happens right at the start of main because @@ -360,6 +384,11 @@ static int tf_init(struct tf_framework* tf, int argc, char** argv) help(); exit(EXIT_SUCCESS); } + + if (tf->args.list_tests) { + print_test_list(tf); + exit(EXIT_SUCCESS); + } } return EXIT_SUCCESS; diff --git a/src/unit_test.h b/src/unit_test.h index e76b8c01..b41e9f75 100644 --- a/src/unit_test.h +++ b/src/unit_test.h @@ -77,6 +77,8 @@ struct tf_args { const char* custom_seed; /* Whether to print the help msg */ int help; + /* Whether to print the tests list msg */ + int list_tests; /* Target tests indexes */ struct tf_targets targets; };