--- a/tests/ucxtest.c Thu May 15 15:43:30 2025 +0200 +++ b/tests/ucxtest.c Thu May 15 16:02:54 2025 +0200 @@ -26,6 +26,7 @@ * POSSIBILITY OF SUCH DAMAGE. */ +#include "util_allocator.h" #include "cx/common.h" #include "cx/test.h" @@ -52,16 +53,35 @@ CxTestSuite *cx_test_suite_printf(void); CxTestSuite *cx_test_suite_mempool(void); -#define run_tests(suite) cx_test_run_stdout(suite); success += (suite)->success; failure += (suite)->failure +#define run_tests(suite) cx_test_run_stdout(suite); success += (suite)->success; failure += (suite)->failure; \ + if (!cx_testing_allocator_verify(&testing_allocator)) break; #define execute_test_suites(...) unsigned success = 0, failure = 0; CxTestSuite* test_suites[] = {__VA_ARGS__}; \ for (size_t i = 0; i < cx_nmemb(test_suites) ; i++) {run_tests(test_suites[i]);} (void)0 #define free_test_suites for (size_t i = 0 ; i < cx_nmemb(test_suites) ; i++) {cx_test_suite_free(test_suites[i]);} (void)0 +static int verify_testing_allocator() { + printf("Verify Testing Allocator\n"); + CxTestSuite *suite_ta = cx_test_suite_testing_allocator(); + int result = suite_ta->failure > 0; + cx_test_suite_free(suite_ta); + return result; +} + int main(void) { + if (verify_testing_allocator()) { + fprintf(stderr, "Testing Allocator is not working - cannot perform tests!\n"); + return EXIT_FAILURE; + } + + // Replace the default allocator with the testing allocator + CxTestingAllocator testing_allocator; + cx_testing_allocator_init(&testing_allocator); + cxDefaultAllocator = &testing_allocator.base; + + // Run tests printf("UCX Tests\n---------\n"); execute_test_suites( - cx_test_suite_testing_allocator(), cx_test_suite_szmul(), cx_test_suite_allocator(), cx_test_suite_streams(), @@ -89,6 +109,17 @@ success + failure, success, failure); free_test_suites; + if (cx_testing_allocator_verify(&testing_allocator)) { + printf("\nAllocations verified successfully.\n"); + } else { + printf("\nVerifying allocations failed!\nSome cxMalloc() might not match a cxFree().\n" + "Aborting test execution.\nError must be in a function tested by the last executed test suite.\n" + "Total allocations: %u\n" + "Total frees: %u\n", testing_allocator.alloc_total, testing_allocator.free_total); + failure++; + } + cx_testing_allocator_destroy(&testing_allocator); + return failure > 0 ? 1 : 0; }