furi_memmgr_test.c 1001 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. #include "../minunit.h"
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5. void test_furi_memmgr() {
  6. void* ptr;
  7. // allocate memory case
  8. ptr = malloc(100);
  9. mu_check(ptr != NULL);
  10. // test that memory is zero-initialized after allocation
  11. for(int i = 0; i < 100; i++) {
  12. mu_assert_int_eq(0, ((uint8_t*)ptr)[i]);
  13. }
  14. free(ptr);
  15. // reallocate memory case
  16. ptr = malloc(100);
  17. memset(ptr, 66, 100);
  18. ptr = realloc(ptr, 200);
  19. mu_check(ptr != NULL);
  20. // test that memory is really reallocated
  21. for(int i = 0; i < 100; i++) {
  22. mu_assert_int_eq(66, ((uint8_t*)ptr)[i]);
  23. }
  24. // TODO: fix realloc to copy only old size, and write testcase that leftover of reallocated memory is zero-initialized
  25. free(ptr);
  26. // allocate and zero-initialize array (calloc)
  27. ptr = calloc(100, 2);
  28. mu_check(ptr != NULL);
  29. for(int i = 0; i < 100 * 2; i++) {
  30. mu_assert_int_eq(0, ((uint8_t*)ptr)[i]);
  31. }
  32. free(ptr);
  33. }