Merge pull request #15476 from benpicco/tests/malloc-fail

tests/malloc: fail if allocation count does not match
This commit is contained in:
Francisco 2020-11-26 10:27:02 +01:00 committed by GitHub
commit ae2246dc80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -47,7 +47,7 @@ struct node {
static uint32_t total = 0; static uint32_t total = 0;
static void fill_memory(struct node *head) static uint32_t fill_memory(struct node *head)
{ {
uint32_t allocations = 0; uint32_t allocations = 0;
@ -70,6 +70,8 @@ static void fill_memory(struct node *head)
} }
printf("Allocations count: %"PRIu32"\n", allocations); printf("Allocations count: %"PRIu32"\n", allocations);
return allocations;
} }
static void free_memory(struct node *head) static void free_memory(struct node *head)
@ -102,6 +104,8 @@ static void free_memory(struct node *head)
int main(void) int main(void)
{ {
uint32_t allocations = 0;
printf("CHUNK_SIZE: %"PRIu32"\n", (uint32_t)CHUNK_SIZE); printf("CHUNK_SIZE: %"PRIu32"\n", (uint32_t)CHUNK_SIZE);
printf("NUMBER_OF_TESTS: %d\n", NUMBER_OF_TESTS); printf("NUMBER_OF_TESTS: %d\n", NUMBER_OF_TESTS);
@ -110,8 +114,16 @@ int main(void)
struct node *head = malloc(sizeof(struct node)); struct node *head = malloc(sizeof(struct node));
total += sizeof(struct node); total += sizeof(struct node);
fill_memory(head); uint32_t new_allocations = fill_memory(head);
free_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]"); puts("[SUCCESS]");