From 378227c3774436181096512ac3c0199af6a91b64 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Mon, 13 Apr 2026 22:55:59 -0500 Subject: [PATCH] Fixed more memory tests --- test/util/test_memory.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/test/util/test_memory.c b/test/util/test_memory.c index dc8f2581..8cfae254 100644 --- a/test/util/test_memory.c +++ b/test/util/test_memory.c @@ -314,7 +314,7 @@ static void test_memoryCompare(void **state) { assert_int_equal(memoryCompare(b, b, size), 0); // Comparison of 0 bytes is always true - assert_true(memoryCompare(a, b, 0)); + assert_int_equal(memoryCompare(a, b, 0), 0); // Compare different sizes (should assert, so we only test the API contract) // Not possible with current API, as size is explicit, but document intent. @@ -365,6 +365,7 @@ static void test_memoryResize(void **state) { size_t initialSize = 32; void *ptr = memoryAllocate(initialSize); assert_non_null(ptr); + assert_int_equal(memoryGetAllocatedCount(), 1); // Initialize memory for(size_t i = 0; i < initialSize; i++) { @@ -376,12 +377,14 @@ static void test_memoryResize(void **state) { memoryResize(&ptr, initialSize, initialSize); assert_non_null(ptr); assert_ptr_equal(ptr, oldPtr); + assert_int_equal(memoryGetAllocatedCount(), 1); - // Reallocate to a larger size + // Reallocate to a larger size, it should not leak. size_t newSize = 64; memoryResize(&ptr, initialSize, newSize); assert_non_null(ptr); - + assert_int_equal(memoryGetAllocatedCount(), 1); + // Check that old data is preserved for(size_t i = 0; i < initialSize; i++) { assert_int_equal(((uint8_t*)ptr)[i], (uint8_t)(i + 1)); @@ -391,6 +394,7 @@ static void test_memoryResize(void **state) { size_t smallerSize = 16; memoryResize(&ptr, newSize, smallerSize); assert_non_null(ptr); + assert_int_equal(memoryGetAllocatedCount(), 1); // Check that data is still correct up to the smaller size for(size_t i = 0; i < smallerSize; i++) { @@ -399,15 +403,19 @@ static void test_memoryResize(void **state) { // Cannot realloc to size 0 expect_assert_failure(memoryResize(&ptr, smallerSize, 0)); + assert_int_equal(memoryGetAllocatedCount(), 1); // Cannot take NULL expect_assert_failure(memoryResize(NULL, smallerSize, 16)); + assert_int_equal(memoryGetAllocatedCount(), 1); // memoryResize with oldsize of 0 not possible expect_assert_failure(memoryResize(&ptr, 0, 16)); + assert_int_equal(memoryGetAllocatedCount(), 1); // Cannot resize more memory than possible expect_assert_failure(memoryResize(&ptr, smallerSize, SIZE_MAX)); + assert_int_equal(memoryGetAllocatedCount(), 1); memoryFree(ptr);