Hi all,
Let's assume I have a C file, with a very simple function:
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include "code.h"
char *return_str_dup(const char *str)
{
size_t len = strlen(str);
char *dup = malloc(len + 1);
if (dup == NULL) {
return NULL;
}
strncpy(dup, str, len + 1);
return dup;
}
And I want to write a unit test that checks to make sure the function return NULL if malloc fails (doesn't crash with NULL pointer access). Is this possible with CppUTest? I have tried following along with TestMemoryAllocatorTest.cpp without much luck.
Basically it looks like this:
TEST(tests, test_dup_malloc_fails)
{
failableMallocAllocator->failAllocNumber(1);
void *mem = return_str_dup("Why, hello there!");
POINTERS_EQUAL(NULLPTR, mem);
};
But I can't get malloc to return NULL.
Any help would be appreciated. Thanks!