I am working on a fairly large C library, which currently has no tests. As the API begins to be final, I would like to start writing unit tests.
Almost all my functions act on the first parameter (structure).
The naive approach to unit test is to have the structure of the preliminary function call in a known state, call the function, and then compare the structure of the preliminary call with the expected result.
Now this works with a structure consisting of scalar types, but as for allocated memory, I was wondering what approach you used.
For example, imagine the image structure when you do:
CreateImage(&img, x, y);
you expect img-> x to be x, img-> y to be y and img-> pixels, a pointer to something big enough to hold x * y * sizeof(pixel)
.
Checking the first two is trivial, but what about img-> pixels? I don't want to check if the malloc call was successful, since I can overload malloc, but I want to know if malloc was called correctly.
This is especially important in the case of:
CreateImage(*img, x, y) { img->x = x; img->y = y; img->pixels = malloc(x * y * sizeof(pixel)); if(!img->pixels) error("no memory"); }
Hope my question is clear.
Thanks.
Nicolas goy
source share