提交 92f6d5dd 编写于 作者: J jsalling

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.
上级 d837342b
...@@ -479,6 +479,10 @@ TEST(LeakDetection, PointerSettingMax) ...@@ -479,6 +479,10 @@ TEST(LeakDetection, PointerSettingMax)
//------------------------------------------------------------ //------------------------------------------------------------
TEST_GROUP(InternalMalloc); 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_SETUP(InternalMalloc) { }
TEST_TEAR_DOWN(InternalMalloc) { } TEST_TEAR_DOWN(InternalMalloc) { }
...@@ -491,6 +495,7 @@ TEST(InternalMalloc, MallocPastBufferFails) ...@@ -491,6 +495,7 @@ TEST(InternalMalloc, MallocPastBufferFails)
free(m); free(m);
TEST_ASSERT_NOT_NULL(m); TEST_ASSERT_NOT_NULL(m);
TEST_ASSERT_NULL(n); TEST_ASSERT_NULL(n);
TEST_ASSERT_MEMORY_ALL_FREE_LIFO_ORDER(m, n);
#endif #endif
} }
...@@ -502,6 +507,7 @@ TEST(InternalMalloc, CallocPastBufferFails) ...@@ -502,6 +507,7 @@ TEST(InternalMalloc, CallocPastBufferFails)
free(m); free(m);
TEST_ASSERT_NOT_NULL(m); TEST_ASSERT_NOT_NULL(m);
TEST_ASSERT_NULL(n); TEST_ASSERT_NULL(n);
TEST_ASSERT_MEMORY_ALL_FREE_LIFO_ORDER(m, n);
#endif #endif
} }
...@@ -513,6 +519,7 @@ TEST(InternalMalloc, MallocThenReallocGrowsMemoryInPlace) ...@@ -513,6 +519,7 @@ TEST(InternalMalloc, MallocThenReallocGrowsMemoryInPlace)
free(n); free(n);
TEST_ASSERT_NOT_NULL(m); TEST_ASSERT_NOT_NULL(m);
TEST_ASSERT_EQUAL(m, n); TEST_ASSERT_EQUAL(m, n);
TEST_ASSERT_MEMORY_ALL_FREE_LIFO_ORDER(m, n);
#endif #endif
} }
...@@ -523,6 +530,7 @@ TEST(InternalMalloc, ReallocFailDoesNotFreeMem) ...@@ -523,6 +530,7 @@ TEST(InternalMalloc, ReallocFailDoesNotFreeMem)
void* n1 = malloc(10); void* n1 = malloc(10);
void* out_of_mem = realloc(n1, UNITY_INTERNAL_HEAP_SIZE_BYTES/2 + 1); void* out_of_mem = realloc(n1, UNITY_INTERNAL_HEAP_SIZE_BYTES/2 + 1);
void* n2 = malloc(10); void* n2 = malloc(10);
free(n2); free(n2);
if (out_of_mem == NULL) free(n1); if (out_of_mem == NULL) free(n1);
free(m); free(m);
...@@ -530,5 +538,6 @@ TEST(InternalMalloc, ReallocFailDoesNotFreeMem) ...@@ -530,5 +538,6 @@ TEST(InternalMalloc, ReallocFailDoesNotFreeMem)
TEST_ASSERT_NOT_NULL(m); // Got a real memory location TEST_ASSERT_NOT_NULL(m); // Got a real memory location
TEST_ASSERT_NULL(out_of_mem); // The realloc should have failed 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_NOT_EQUAL(n2, n1); // If n1 != n2 then realloc did not free n1
TEST_ASSERT_MEMORY_ALL_FREE_LIFO_ORDER(m, n2);
#endif #endif
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册