mirror of
https://github.com/ThrowTheSwitch/Unity.git
synced 2026-01-28 10:44:26 +01:00
Add configuration option UNITY_EXCLUDE_STDLIB_MALLOC to Fixture
This feature removes the dependency on malloc/free for constrained embedded systems without a heap. It uses a static heap inside Unity Fixture. Setting UNITY_INTERNAL_HEAP_SIZE_BYTES sizes the heap. Add tests for new option, add targets to makefile for running tests. UNITY_FIXTURE_MALLOC for Fixture use only, remove from unity_output_Spy.c.
This commit is contained in:
@@ -82,7 +82,7 @@ TEST(UnityFixture, ReallocLargerNeeded)
|
||||
CHECK(m1);
|
||||
strcpy((char*)m1, "123456789");
|
||||
m2 = realloc(m1, 15);
|
||||
CHECK(m1 != m2);
|
||||
// CHECK(m1 != m2); //Depends on implementation
|
||||
STRCMP_EQUAL("123456789", m2);
|
||||
free(m2);
|
||||
}
|
||||
@@ -136,7 +136,7 @@ TEST(UnityFixture, PointerSet)
|
||||
|
||||
TEST(UnityFixture, FreeNULLSafety)
|
||||
{
|
||||
unity_free(NULL);
|
||||
free(NULL);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------
|
||||
@@ -290,7 +290,11 @@ TEST_GROUP(LeakDetection);
|
||||
|
||||
TEST_SETUP(LeakDetection)
|
||||
{
|
||||
#ifdef UNITY_EXCLUDE_STDLIB_MALLOC
|
||||
UnityOutputCharSpy_Create(200);
|
||||
#else
|
||||
UnityOutputCharSpy_Create(1000);
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST_TEAR_DOWN(LeakDetection)
|
||||
@@ -376,3 +380,57 @@ TEST(LeakDetection, BufferOverrunFoundDuringRealloc)
|
||||
CHECK(strstr(UnityOutputCharSpy_Get(), "Buffer overrun detected during realloc()"));
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST_GROUP(InternalMalloc);
|
||||
|
||||
TEST_SETUP(InternalMalloc) { }
|
||||
TEST_TEAR_DOWN(InternalMalloc) { }
|
||||
|
||||
TEST(InternalMalloc, MallocPastBufferFails)
|
||||
{
|
||||
#ifdef UNITY_EXCLUDE_STDLIB_MALLOC
|
||||
void* m = malloc(UNITY_INTERNAL_HEAP_SIZE_BYTES/2 + 1);
|
||||
TEST_ASSERT_NOT_NULL(m);
|
||||
void* n = malloc(UNITY_INTERNAL_HEAP_SIZE_BYTES/2);
|
||||
TEST_ASSERT_NULL(n);
|
||||
free(m);
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST(InternalMalloc, CallocPastBufferFails)
|
||||
{
|
||||
#ifdef UNITY_EXCLUDE_STDLIB_MALLOC
|
||||
void* m = calloc(1, UNITY_INTERNAL_HEAP_SIZE_BYTES/2 + 1);
|
||||
TEST_ASSERT_NOT_NULL(m);
|
||||
void* n = calloc(1, UNITY_INTERNAL_HEAP_SIZE_BYTES/2);
|
||||
TEST_ASSERT_NULL(n);
|
||||
free(m);
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST(InternalMalloc, MallocThenReallocGrowsMemoryInPlace)
|
||||
{
|
||||
#ifdef UNITY_EXCLUDE_STDLIB_MALLOC
|
||||
void* m = malloc(UNITY_INTERNAL_HEAP_SIZE_BYTES/2 + 1);
|
||||
TEST_ASSERT_NOT_NULL(m);
|
||||
void* n = realloc(m, UNITY_INTERNAL_HEAP_SIZE_BYTES/2 + 9);
|
||||
TEST_ASSERT_EQUAL(m, n);
|
||||
free(n);
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST(InternalMalloc, ReallocFailDoesNotFreeMem)
|
||||
{
|
||||
#ifdef UNITY_EXCLUDE_STDLIB_MALLOC
|
||||
void* m = malloc(UNITY_INTERNAL_HEAP_SIZE_BYTES/2);
|
||||
TEST_ASSERT_NOT_NULL(m);
|
||||
void* n1 = malloc(10);
|
||||
void* out_of_mem = realloc(n1, UNITY_INTERNAL_HEAP_SIZE_BYTES/2 + 1);
|
||||
TEST_ASSERT_NULL(out_of_mem);
|
||||
void* n2 = malloc(10);
|
||||
TEST_ASSERT_NOT_EQUAL(n2, n1);
|
||||
free(n2);
|
||||
free(n1);
|
||||
free(m);
|
||||
#endif
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user