Do you have a way to fail malloc() for unit tests

1 day ago 7

Hi,

I currently trying to do unit testing on some function using the Unity library, and in some of the functions I'm checking the return of malloc to ensure it doesn't return NULL for example.

But I was wondering how can I try to fail the malloc function without changing the arguments ? Is there a way to cap the memory allocation so it cannot allocate more for example ?

And if it exist do you have a way to do "enable" it for 1 unit test then "disable" it when the test is done ?

I know that usually malloc is safe, and I could "not care about if it has an error" but still just to make sure I want to try testing it

submitted by /u/Valuable-Birthday-10 to r/C_Programming
[link] [comments]
Read Entire Article