unit test: malloc

This commit is contained in:
SG
2024-04-10 21:08:32 +03:00
parent e4107040e9
commit ece7a2d868
2 changed files with 63 additions and 4 deletions

View File

@@ -1,8 +1,5 @@
#include "../minunit.h"
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <stdint.h>
#include <furi.h>
void test_furi_memmgr(void) {
void* ptr;
@@ -37,3 +34,63 @@ void test_furi_memmgr(void) {
}
free(ptr);
}
static void test_memmgr_malloc(const size_t allocation_size) {
uint8_t* ptr = NULL;
const char* error_message = NULL;
FURI_CRITICAL_ENTER();
ptr = malloc(allocation_size);
// test that we can allocate memory
if(ptr == NULL) {
error_message = "malloc failed";
}
// test that memory is zero-initialized after allocation
for(size_t i = 0; i < allocation_size; i++) {
if(ptr[i] != 0) {
error_message = "memory is not zero-initialized after malloc";
break;
}
}
memset(ptr, 0x55, allocation_size);
free(ptr);
// test that memory is zero-initialized after free
// allocator can use this memory for inner purposes
// so we check that memory at least partially zero-initialized
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wuse-after-free"
size_t zero_count = 0;
for(size_t i = 0; i < allocation_size; i++) {
if(ptr[i] == 0) {
zero_count++;
}
}
#pragma GCC diagnostic pop
// chech that at least 75% of memory is zero-initialized
if(zero_count < (allocation_size * 0.75)) {
error_message = "seems memory is not zero-initialized after free";
}
FURI_CRITICAL_EXIT();
if(error_message != NULL) {
mu_fail(error_message);
}
}
void test_furi_memmgr_advanced(void) {
test_memmgr_malloc(50);
test_memmgr_malloc(100);
test_memmgr_malloc(500);
test_memmgr_malloc(1000);
test_memmgr_malloc(5000);
test_memmgr_malloc(10000);
}

View File

@@ -9,6 +9,7 @@ void test_furi_concurrent_access(void);
void test_furi_pubsub(void);
void test_furi_memmgr(void);
void test_furi_memmgr_advanced(void);
static int foo = 0;
@@ -37,6 +38,7 @@ MU_TEST(mu_test_furi_memmgr) {
// this test is not accurate, but gives a basic understanding
// that memory management is working fine
test_furi_memmgr();
test_furi_memmgr_advanced();
}
MU_TEST_SUITE(test_suite) {