Added a heapmem reallocation test.
@@ -186,6 +186,50 @@ UNIT_TEST(max_alloc)
UNIT_TEST_END();
}
/*****************************************************************************/
+UNIT_TEST_REGISTER(reallocations, "Heapmem reallocations");
+UNIT_TEST(reallocations)
+{
+#define INITIAL_SIZE 100
+
+ UNIT_TEST_BEGIN();
+
+ uint8_t *ptr1 = heapmem_realloc(NULL, INITIAL_SIZE);
+ UNIT_TEST_ASSERT(ptr1 != NULL);
+
+ for(size_t i = 0; i < INITIAL_SIZE; i++) {
+ ptr1[i] = i + 128;
+ }
+
+ /* Extend the initial array. */
+ uint8_t *ptr2 = heapmem_realloc(ptr1, INITIAL_SIZE * 2);
+ UNIT_TEST_ASSERT(ptr2 != NULL);
+
+ for(size_t i = 0; i < INITIAL_SIZE; i++) {
+ /* Check that the bytes of the lower half have been preserved. */
+ UNIT_TEST_ASSERT(ptr2[i] == (uint8_t)(i + 128));
+ /* Initialize the upper half of the reallocated area. */
+ ptr2[i + INITIAL_SIZE] = i * 2 + 128;
+ }
+
+ /* Reduce the extended array. */
+ uint8_t *ptr3 = heapmem_realloc(ptr2, (2 * INITIAL_SIZE) / 3);
+ UNIT_TEST_ASSERT(ptr3 != NULL);
+
+ /* Check that the array is correctly preserved after
+ the final reallocation. */
+ for(size_t i = 0; i < (2 * INITIAL_SIZE) / 3; i++) {
+ if(i < INITIAL_SIZE) {
+ UNIT_TEST_ASSERT(ptr2[i] == (uint8_t)(i + 128));
+ } else {
+ UNIT_TEST_ASSERT(ptr2[i] == (uint8_t)(i * 2 + 128));
+ }
+ }
+
+ UNIT_TEST_ASSERT(heapmem_realloc(ptr3, 0) == NULL);
+
+ UNIT_TEST_END();
+}
+/*****************************************************************************/
UNIT_TEST_REGISTER(stats_check, "Heapmem statistics validation");
UNIT_TEST(stats_check)
{
@@ -222,6 +266,7 @@ PROCESS_THREAD(test_heapmem_process, ev, data)
UNIT_TEST_RUN(do_many_allocations);
UNIT_TEST_RUN(max_alloc);
UNIT_TEST_RUN(invalid_freeing);
+ UNIT_TEST_RUN(reallocations);
UNIT_TEST_RUN(stats_check);
if(!UNIT_TEST_PASSED(do_many_allocations) ||
