Merge pull request #15476 from benpicco/tests/malloc-fail
tests/malloc: fail if allocation count does not match
This commit is contained in:
commit
ae2246dc80
@ -47,7 +47,7 @@ struct node {
|
||||
|
||||
static uint32_t total = 0;
|
||||
|
||||
static void fill_memory(struct node *head)
|
||||
static uint32_t fill_memory(struct node *head)
|
||||
{
|
||||
uint32_t allocations = 0;
|
||||
|
||||
@ -70,6 +70,8 @@ static void fill_memory(struct node *head)
|
||||
}
|
||||
|
||||
printf("Allocations count: %"PRIu32"\n", allocations);
|
||||
|
||||
return allocations;
|
||||
}
|
||||
|
||||
static void free_memory(struct node *head)
|
||||
@ -102,6 +104,8 @@ static void free_memory(struct node *head)
|
||||
|
||||
int main(void)
|
||||
{
|
||||
uint32_t allocations = 0;
|
||||
|
||||
printf("CHUNK_SIZE: %"PRIu32"\n", (uint32_t)CHUNK_SIZE);
|
||||
printf("NUMBER_OF_TESTS: %d\n", NUMBER_OF_TESTS);
|
||||
|
||||
@ -110,8 +114,16 @@ int main(void)
|
||||
struct node *head = malloc(sizeof(struct node));
|
||||
total += sizeof(struct node);
|
||||
|
||||
fill_memory(head);
|
||||
uint32_t new_allocations = fill_memory(head);
|
||||
free_memory(head);
|
||||
|
||||
/* test if all memory was freed/can be allocated again */
|
||||
if (allocations == 0) {
|
||||
allocations = new_allocations;
|
||||
} else if (allocations != new_allocations) {
|
||||
puts("[FAILED]");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
puts("[SUCCESS]");
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user