add tests for freeing memory with a reallocation of size zero default tip

Tue, 16 Dec 2025 21:33:58 +0100

author
Mike Becker <universe@uap-core.de>
date
Tue, 16 Dec 2025 21:33:58 +0100
changeset 1617
d4385f35f8b0
parent 1616
bdc04a8e0dd3

add tests for freeing memory with a reallocation of size zero

tests/test_allocator.c file | annotate | diff | comparison | revisions
--- a/tests/test_allocator.c	Tue Dec 16 18:33:12 2025 +0100
+++ b/tests/test_allocator.c	Tue Dec 16 21:33:58 2025 +0100
@@ -206,6 +206,26 @@
     free(test);
 }
 
+CX_TEST(test_allocator_reallocate_frees) {
+    CX_TEST_DO {
+        void *test = cxCallocDefault(8, 1);
+        CX_TEST_ASSERT(test != NULL);
+        int ret = cxReallocateDefault(&test, 0);
+        CX_TEST_ASSERT(ret == 0);
+        CX_TEST_ASSERT(test == NULL);
+    }
+}
+
+CX_TEST(test_allocator_reallocate_low_level_frees) {
+    CX_TEST_DO {
+        void *test = calloc(8, 1);
+        CX_TEST_ASSERT(test != NULL);
+        int ret = cx_reallocate(&test, 0);
+        CX_TEST_ASSERT(ret == 0);
+        CX_TEST_ASSERT(test == NULL);
+    }
+}
+
 CX_TEST(test_allocator_reallocatearray) {
     char *test = cxCallocDefault(8, 1);
     memcpy(test, "Test", 5);
@@ -256,6 +276,26 @@
     free(test);
 }
 
+CX_TEST(test_allocator_reallocatearray_frees) {
+    CX_TEST_DO {
+        char *test = cxCallocDefault(8, 1);
+        CX_TEST_ASSERT(test != NULL);
+        int ret = cxReallocateArrayDefault(&test, 0, 1);
+        CX_TEST_ASSERT(ret == 0);
+        CX_TEST_ASSERT(test == NULL);
+    }
+}
+
+CX_TEST(test_allocator_reallocatearray_low_level_frees) {
+    CX_TEST_DO {
+        char *test = calloc(8, 1);
+        CX_TEST_ASSERT(test != NULL);
+        int ret = cx_reallocatearray(&test, 0, 1);
+        CX_TEST_ASSERT(ret == 0);
+        CX_TEST_ASSERT(test == NULL);
+    }
+}
+
 static void *test_allocator_mock_failing_realloc(
         cx_attr_unused void *p,
         cx_attr_unused void *d,
@@ -304,10 +344,14 @@
     cx_test_register(suite, test_allocator_reallocate);
     cx_test_register(suite, test_allocator_reallocate_fails);
     cx_test_register(suite, test_allocator_reallocate_low_level);
+    cx_test_register(suite, test_allocator_reallocate_frees);
+    cx_test_register(suite, test_allocator_reallocate_low_level_frees);
     cx_test_register(suite, test_allocator_reallocatearray);
     cx_test_register(suite, test_allocator_reallocatearray_overflow);
     cx_test_register(suite, test_allocator_reallocatearray_low_level);
     cx_test_register(suite, test_allocator_reallocatearray_low_level_overflow);
+    cx_test_register(suite, test_allocator_reallocatearray_frees);
+    cx_test_register(suite, test_allocator_reallocatearray_low_level_frees);
 
     return suite;
 }

mercurial