test_realloc.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /*
  2. Generic test for realloc
  3. */
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "unity.h"
  7. #include "sdkconfig.h"
  8. #include "esp_heap_caps.h"
  9. #ifndef CONFIG_HEAP_POISONING_COMPREHENSIVE
  10. /* (can't realloc in place if comprehensive is enabled) */
  11. TEST_CASE("realloc shrink buffer in place", "[heap]")
  12. {
  13. void *x = malloc(64);
  14. TEST_ASSERT(x);
  15. void *y = realloc(p, 48);
  16. TEST_ASSERT_EQUAL_PTR(x, y);
  17. }
  18. #endif
  19. TEST_CASE_ESP32("realloc move data to a new heap type", "[heap]")
  20. {
  21. const char *test = "I am some test content to put in the heap";
  22. char buf[64];
  23. memset(buf, 0xEE, 64);
  24. strlcpy(buf, test, 64);
  25. char *a = malloc(64);
  26. memcpy(a, buf, 64);
  27. // move data from 'a' to IRAM
  28. char *b = heap_caps_realloc(a, 64, MALLOC_CAP_EXEC);
  29. TEST_ASSERT_NOT_NULL(b);
  30. TEST_ASSERT_NOT_EQUAL(a, b);
  31. TEST_ASSERT(heap_caps_check_integrity(MALLOC_CAP_INVALID, true));
  32. TEST_ASSERT_EQUAL_HEX32_ARRAY(buf, b, 64/sizeof(uint32_t));
  33. // Move data back to DRAM
  34. char *c = heap_caps_realloc(b, 48, MALLOC_CAP_8BIT);
  35. TEST_ASSERT_NOT_NULL(c);
  36. TEST_ASSERT_NOT_EQUAL(b, c);
  37. TEST_ASSERT(heap_caps_check_integrity(MALLOC_CAP_INVALID, true));
  38. TEST_ASSERT_EQUAL_HEX8_ARRAY(buf, c, 48);
  39. free(c);
  40. }