I’m experimenting with TDD and C. I’d like to write a simple malloc wrapper following a TDD approach. I’m trying to follow Bob Martin’s Three laws of TDD
- Do not write production code unless it is to make a failing unit test pass.
- Do not write more of a unit test than is sufficient to fail, and build failures are failures.
- Do not write more production code than is sufficient to pass the one failing unit test.
This is my code up until now:
J_STATUS MemAlloc(long Size, void **OutPtr) {
J_STATUS ReturnStatus;
void *Ptr;
Ptr = NULL;
if (Size >= 0) {
Ptr = malloc(Size);
*OutPtr = Ptr;
ReturnStatus = SUCCESS;
//TODO controllare malloc error
} else {
ReturnStatus = ERROR;
}
return ReturnStatus;
}
And these are my tests (I’m using Unity test framework):
#include "../unity/unity.h"
#include "../src/jMem.h"
#include "stdlib.h"
static int *ptr;
static J_STATUS Result;
static long Size;
static long Count;
void setUp() {
ptr = NULL;
Size = 10;
Count = 5;
}
void tearDown() {
if (ptr != NULL) {
free(ptr);
}
}
void test_MemAllocShouldAllocateMemoryAndReturnSuccess(void) {
Result = MemAlloc(Size, (void **) &ptr);
TEST_ASSERT_EQUAL(SUCCESS, Result);
TEST_ASSERT_NOT_NULL(ptr);
}
void test_MemAllocShouldReturnErrorIfSizeIsNegative(void) {
Size = -4;
Result = MemAlloc(Size, (void **) &ptr);
TEST_ASSERT_EQUAL(ERROR, Result);
}
Now how do I write a test for a failing malloc? I know I can link a different version of malloc or I could write a macro to make malloc fail, but this should be true only for this test, while in the other I want to use the real malloc.
Any idea?
Thank you
To substitute
malloc()you can use a function pointer like this:And then you use
pMalloc(some size)instead ofmalloc(some size).If you set the pointer to point at
malloc(), as inpMalloc = &malloc;, you’ll be using the realmalloc(). If you set it to point at your own function, you’ll be using that function of yours.In your function you can simulate a
malloc()failure by returningNULL. You can also callmalloc()from it, if so is needed for the test.Similarly you can substitute your own
free().In these substituted functions you can log what’s happening.
For example, if you pass
LONG_MAXto your wrapper function (I’m assuming it still takes the size as alongargument), your fakemalloc()can log the size it has actually received. In the test you can then note thatLONG_MAXhas propagated from the wrapper tomalloc()as(size_t)LONG_MAX, and ifsize_tis shorter thanlong(in terms of the number of bits) you can detect the discrepancy by comparing the values (LONG_MAX != (size_t)LONG_MAX).You can also log the pointer value that your fake
malloc()returns (from inside of itself) and compare it with the value returned by your wrapper and see if these are different.You can elaborate the idea further to come up with more tests (e.g. the wrapper returns a non-NULL value, but the fake
malloc()(and therefore the real one) isn’t called at all).While doing all this you don’t have to log to a file or to
stdout. You can dedicate a data structure for the purpose and then examine it.