Reputation: 1
I would like to write a test for a routine of data structure initialization. E.g.,
struct book_t
{
float price;
int page_num;
char title[100];
int hardcover:1;
int on_sale:1;
int language:3;
}
void init_book(struct book_t *b)
{
/* use memset or assign value to each field */
};
A test of init_book() would be helpful to prevent a developer from forgetting to update init_book() if they modify struct book_t. It is not helpful to check the field values one by one, since new fields would not be tested.
I am considering using some sort of Boolean operation for this case, but it is insufficient to determine uninitialized fields from padding. Is there a better way to do this?
Upvotes: 0
Views: 857
Reputation: 135
You might consider adding a hard coded struct size validation in the end of the init function, and whoever adds a parameter and haven't updated the hard coded size compared to the struct size will be asserted in the first run
Upvotes: 0
Reputation: 59997
Adding new fields cannot be check for by unit testing.
This should be done by code review.
Upvotes: 1