mirror of
https://github.com/ThrowTheSwitch/Unity.git
synced 2025-06-25 01:59:21 +08:00
Verify the tests for Internal Malloc implementation free all the heap
Make it more clear that each test of the internal heap implementation should free in LIFO order. Without this check, memory can be stranded but still pass.
This commit is contained in:
@ -479,6 +479,10 @@ TEST(LeakDetection, PointerSettingMax)
|
||||
//------------------------------------------------------------
|
||||
|
||||
TEST_GROUP(InternalMalloc);
|
||||
#define TEST_ASSERT_MEMORY_ALL_FREE_LIFO_ORDER(first_mem_ptr, ptr) \
|
||||
ptr = malloc(10); free(ptr); \
|
||||
TEST_ASSERT_EQUAL_PTR_MESSAGE(first_mem_ptr, ptr, "Memory was stranded, free in LIFO order");
|
||||
|
||||
|
||||
TEST_SETUP(InternalMalloc) { }
|
||||
TEST_TEAR_DOWN(InternalMalloc) { }
|
||||
@ -491,6 +495,7 @@ TEST(InternalMalloc, MallocPastBufferFails)
|
||||
free(m);
|
||||
TEST_ASSERT_NOT_NULL(m);
|
||||
TEST_ASSERT_NULL(n);
|
||||
TEST_ASSERT_MEMORY_ALL_FREE_LIFO_ORDER(m, n);
|
||||
#endif
|
||||
}
|
||||
|
||||
@ -502,6 +507,7 @@ TEST(InternalMalloc, CallocPastBufferFails)
|
||||
free(m);
|
||||
TEST_ASSERT_NOT_NULL(m);
|
||||
TEST_ASSERT_NULL(n);
|
||||
TEST_ASSERT_MEMORY_ALL_FREE_LIFO_ORDER(m, n);
|
||||
#endif
|
||||
}
|
||||
|
||||
@ -513,6 +519,7 @@ TEST(InternalMalloc, MallocThenReallocGrowsMemoryInPlace)
|
||||
free(n);
|
||||
TEST_ASSERT_NOT_NULL(m);
|
||||
TEST_ASSERT_EQUAL(m, n);
|
||||
TEST_ASSERT_MEMORY_ALL_FREE_LIFO_ORDER(m, n);
|
||||
#endif
|
||||
}
|
||||
|
||||
@ -523,6 +530,7 @@ TEST(InternalMalloc, ReallocFailDoesNotFreeMem)
|
||||
void* n1 = malloc(10);
|
||||
void* out_of_mem = realloc(n1, UNITY_INTERNAL_HEAP_SIZE_BYTES/2 + 1);
|
||||
void* n2 = malloc(10);
|
||||
|
||||
free(n2);
|
||||
if (out_of_mem == NULL) free(n1);
|
||||
free(m);
|
||||
@ -530,5 +538,6 @@ TEST(InternalMalloc, ReallocFailDoesNotFreeMem)
|
||||
TEST_ASSERT_NOT_NULL(m); // Got a real memory location
|
||||
TEST_ASSERT_NULL(out_of_mem); // The realloc should have failed
|
||||
TEST_ASSERT_NOT_EQUAL(n2, n1); // If n1 != n2 then realloc did not free n1
|
||||
TEST_ASSERT_MEMORY_ALL_FREE_LIFO_ORDER(m, n2);
|
||||
#endif
|
||||
}
|
||||
|
Reference in New Issue
Block a user