fix(test): register full ctest suite

This commit is contained in:
2026-06-17 10:36:14 -05:00
parent 8f8bd32c19
commit 776a2dc55b
2 changed files with 75 additions and 20 deletions

View File

@@ -75,7 +75,19 @@ if(IKV_BUILD_TESTS)
${CMAKE_CURRENT_SOURCE_DIR}/src
)
target_compile_features(ikv_tests PRIVATE c_std_99)
add_test(NAME ikv_tests COMMAND ikv_tests)
file(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/demo/unit_test.c" IKV_TEST_CASE_ROWS
REGEX "^ \\{\"[^\"]+\", test_[^}]+\\},?$"
)
foreach(IKV_TEST_CASE_ROW IN LISTS IKV_TEST_CASE_ROWS)
string(REGEX REPLACE "^ \\{\"([^\"]+)\", test_[^}]+\\},?$" "\\1" IKV_TEST_CASE_NAME "${IKV_TEST_CASE_ROW}")
string(MAKE_C_IDENTIFIER "${IKV_TEST_CASE_NAME}" IKV_TEST_CASE_ID)
add_test(
NAME "ikv_tests.${IKV_TEST_CASE_ID}"
COMMAND ikv_tests --case "${IKV_TEST_CASE_NAME}"
)
endforeach()
endif()
if(IKV_INSTALL)

View File

@@ -1258,32 +1258,75 @@ static void log_case(bool passed, unsigned int index, unsigned int total, long e
putchar('\n');
}
int main(void)
static int run_test_case(unsigned int index, unsigned int total)
{
test_context_t context;
clock_t start_time = 0;
clock_t end_time = 0;
long elapsed_ms = 0;
int result = 0;
memset(&context, 0, sizeof(context));
#ifdef IKV_TESTING
ikv_test_hooks_reset();
#endif
start_time = clock();
result = test_cases[index].fn(&context);
end_time = clock();
elapsed_ms = (long)(((end_time - start_time) * 1000) / CLOCKS_PER_SEC);
log_case(result == 0, index + 1u, total, elapsed_ms, test_cases[index].name, context.message);
return result;
}
static const test_case_t *find_test_case(const char *name, unsigned int *index_out)
{
const unsigned int total = (unsigned int)(sizeof(test_cases) / sizeof(test_cases[0]));
if (!name)
return NULL;
for (unsigned int i = 0; i < total; ++i)
{
if (strcmp(test_cases[i].name, name) == 0)
{
if (index_out)
*index_out = i;
return &test_cases[i];
}
}
return NULL;
}
int main(int argc, char **argv)
{
const unsigned int total = (unsigned int)(sizeof(test_cases) / sizeof(test_cases[0]));
unsigned int passed = 0u;
if (argc == 3 && strcmp(argv[1], "--case") == 0)
{
unsigned int index = 0u;
if (!find_test_case(argv[2], &index))
{
fprintf(stderr, "unknown test case: %s\n", argv[2]);
return 2;
}
return run_test_case(index, total);
}
if (argc != 1)
{
fprintf(stderr, "usage: %s [--case <test name>]\n", argv[0]);
return 2;
}
for (unsigned int i = 0; i < total; ++i)
{
test_context_t context;
clock_t start_time = 0;
clock_t end_time = 0;
long elapsed_ms = 0;
int result = 0;
memset(&context, 0, sizeof(context));
#ifdef IKV_TESTING
ikv_test_hooks_reset();
#endif
start_time = clock();
result = test_cases[i].fn(&context);
end_time = clock();
elapsed_ms = (long)(((end_time - start_time) * 1000) / CLOCKS_PER_SEC);
if (result == 0)
if (run_test_case(i, total) == 0)
++passed;
log_case(result == 0, i + 1u, total, elapsed_ms, test_cases[i].name, context.message);
}
if (passed != total)