|
| 1 | +#include "include/test.h" |
| 2 | +#include "../../src/include/util/hb_allocator.h" |
| 3 | + |
| 4 | +#include <string.h> |
| 5 | + |
| 6 | +TEST(test_malloc_realloc_null_pointer) |
| 7 | + hb_allocator_T allocator = hb_allocator_with_malloc(); |
| 8 | + |
| 9 | + void* pointer = hb_allocator_realloc(&allocator, NULL, 0, 64); |
| 10 | + ck_assert_ptr_nonnull(pointer); |
| 11 | + |
| 12 | + hb_allocator_dealloc(&allocator, pointer); |
| 13 | +END |
| 14 | + |
| 15 | +TEST(test_malloc_realloc_grow) |
| 16 | + hb_allocator_T allocator = hb_allocator_with_malloc(); |
| 17 | + |
| 18 | + char* pointer = hb_allocator_alloc(&allocator, 16); |
| 19 | + ck_assert_ptr_nonnull(pointer); |
| 20 | + memcpy(pointer, "hello, world!!!", 16); |
| 21 | + |
| 22 | + char* new_pointer = hb_allocator_realloc(&allocator, pointer, 16, 64); |
| 23 | + ck_assert_ptr_nonnull(new_pointer); |
| 24 | + ck_assert_int_eq(memcmp(new_pointer, "hello, world!!!", 16), 0); |
| 25 | + |
| 26 | + hb_allocator_dealloc(&allocator, new_pointer); |
| 27 | +END |
| 28 | + |
| 29 | +TEST(test_malloc_realloc_shrink) |
| 30 | + hb_allocator_T allocator = hb_allocator_with_malloc(); |
| 31 | + |
| 32 | + char* pointer = hb_allocator_alloc(&allocator, 64); |
| 33 | + ck_assert_ptr_nonnull(pointer); |
| 34 | + memcpy(pointer, "shrink me", 10); |
| 35 | + |
| 36 | + char* new_pointer = hb_allocator_realloc(&allocator, pointer, 64, 16); |
| 37 | + ck_assert_ptr_nonnull(new_pointer); |
| 38 | + ck_assert_int_eq(memcmp(new_pointer, "shrink me", 10), 0); |
| 39 | + |
| 40 | + hb_allocator_dealloc(&allocator, new_pointer); |
| 41 | +END |
| 42 | + |
| 43 | +TEST(test_arena_realloc_null_pointer) |
| 44 | + hb_allocator_T allocator; |
| 45 | + hb_allocator_init(&allocator, HB_ALLOCATOR_ARENA); |
| 46 | + |
| 47 | + void* pointer = hb_allocator_realloc(&allocator, NULL, 0, 64); |
| 48 | + ck_assert_ptr_nonnull(pointer); |
| 49 | + |
| 50 | + hb_allocator_destroy(&allocator); |
| 51 | +END |
| 52 | + |
| 53 | +TEST(test_arena_realloc_grow) |
| 54 | + hb_allocator_T allocator; |
| 55 | + hb_allocator_init(&allocator, HB_ALLOCATOR_ARENA); |
| 56 | + |
| 57 | + char* pointer = hb_allocator_alloc(&allocator, 16); |
| 58 | + ck_assert_ptr_nonnull(pointer); |
| 59 | + memcpy(pointer, "hello, world!!!", 16); |
| 60 | + |
| 61 | + char* new_pointer = hb_allocator_realloc(&allocator, pointer, 16, 64); |
| 62 | + ck_assert_ptr_nonnull(new_pointer); |
| 63 | + ck_assert_ptr_ne(new_pointer, pointer); |
| 64 | + ck_assert_int_eq(memcmp(new_pointer, "hello, world!!!", 16), 0); |
| 65 | + |
| 66 | + hb_allocator_destroy(&allocator); |
| 67 | +END |
| 68 | + |
| 69 | +TEST(test_arena_realloc_shrink) |
| 70 | + hb_allocator_T allocator; |
| 71 | + hb_allocator_init(&allocator, HB_ALLOCATOR_ARENA); |
| 72 | + |
| 73 | + char* pointer = hb_allocator_alloc(&allocator, 64); |
| 74 | + ck_assert_ptr_nonnull(pointer); |
| 75 | + memcpy(pointer, "shrink me", 10); |
| 76 | + |
| 77 | + char* new_pointer = hb_allocator_realloc(&allocator, pointer, 64, 16); |
| 78 | + ck_assert_ptr_nonnull(new_pointer); |
| 79 | + ck_assert_int_eq(memcmp(new_pointer, "shrink me", 10), 0); |
| 80 | + |
| 81 | + hb_allocator_destroy(&allocator); |
| 82 | +END |
| 83 | + |
| 84 | +TEST(test_arena_realloc_preserves_data_across_pages) |
| 85 | + hb_allocator_T allocator; |
| 86 | + hb_allocator_init_with_size(&allocator, HB_ALLOCATOR_ARENA, 64); |
| 87 | + |
| 88 | + char* pointer = hb_allocator_alloc(&allocator, 32); |
| 89 | + ck_assert_ptr_nonnull(pointer); |
| 90 | + memset(pointer, 'A', 32); |
| 91 | + |
| 92 | + char* new_pointer = hb_allocator_realloc(&allocator, pointer, 32, 128); |
| 93 | + ck_assert_ptr_nonnull(new_pointer); |
| 94 | + |
| 95 | + for (size_t i = 0; i < 32; i++) { |
| 96 | + ck_assert_int_eq(new_pointer[i], 'A'); |
| 97 | + } |
| 98 | + |
| 99 | + hb_allocator_destroy(&allocator); |
| 100 | +END |
| 101 | + |
| 102 | +TEST(test_tracking_realloc_null_pointer) |
| 103 | + hb_allocator_T allocator = hb_allocator_with_tracking(); |
| 104 | + |
| 105 | + void* pointer = hb_allocator_realloc(&allocator, NULL, 0, 64); |
| 106 | + ck_assert_ptr_nonnull(pointer); |
| 107 | + |
| 108 | + hb_allocator_tracking_stats_T* stats = hb_allocator_tracking_stats(&allocator); |
| 109 | + ck_assert_int_eq(stats->allocation_count, 1); |
| 110 | + ck_assert_int_eq(stats->bytes_allocated, 64); |
| 111 | + |
| 112 | + hb_allocator_dealloc(&allocator, pointer); |
| 113 | + hb_allocator_destroy(&allocator); |
| 114 | +END |
| 115 | + |
| 116 | +TEST(test_tracking_realloc_grow) |
| 117 | + hb_allocator_T allocator = hb_allocator_with_tracking(); |
| 118 | + |
| 119 | + char* pointer = hb_allocator_alloc(&allocator, 16); |
| 120 | + memcpy(pointer, "hello, world!!!", 16); |
| 121 | + |
| 122 | + char* new_pointer = hb_allocator_realloc(&allocator, pointer, 16, 64); |
| 123 | + ck_assert_ptr_nonnull(new_pointer); |
| 124 | + ck_assert_int_eq(memcmp(new_pointer, "hello, world!!!", 16), 0); |
| 125 | + |
| 126 | + hb_allocator_tracking_stats_T* stats = hb_allocator_tracking_stats(&allocator); |
| 127 | + ck_assert_int_eq(stats->allocation_count, 2); |
| 128 | + ck_assert_int_eq(stats->deallocation_count, 1); |
| 129 | + ck_assert_int_eq(stats->bytes_allocated, 16 + 64); |
| 130 | + ck_assert_int_eq(stats->bytes_deallocated, 16); |
| 131 | + |
| 132 | + hb_allocator_dealloc(&allocator, new_pointer); |
| 133 | + hb_allocator_destroy(&allocator); |
| 134 | +END |
| 135 | + |
| 136 | +TCase *hb_allocator_realloc_tests(void) { |
| 137 | + TCase *allocator = tcase_create("Herb Allocator"); |
| 138 | + |
| 139 | + tcase_add_test(allocator, test_malloc_realloc_null_pointer); |
| 140 | + tcase_add_test(allocator, test_malloc_realloc_grow); |
| 141 | + tcase_add_test(allocator, test_malloc_realloc_shrink); |
| 142 | + |
| 143 | + tcase_add_test(allocator, test_arena_realloc_null_pointer); |
| 144 | + tcase_add_test(allocator, test_arena_realloc_grow); |
| 145 | + tcase_add_test(allocator, test_arena_realloc_shrink); |
| 146 | + tcase_add_test(allocator, test_arena_realloc_preserves_data_across_pages); |
| 147 | + |
| 148 | + tcase_add_test(allocator, test_tracking_realloc_null_pointer); |
| 149 | + tcase_add_test(allocator, test_tracking_realloc_grow); |
| 150 | + |
| 151 | + return allocator; |
| 152 | +} |
0 commit comments