-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_darray.c
More file actions
153 lines (121 loc) · 4.19 KB
/
Copy pathtest_darray.c
File metadata and controls
153 lines (121 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "standard.h"
#include <assert.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
void test_darray_basics(Arena *arena) {
printf("--- Test: Basics & Alignment ---\n");
// Create an array of doubles (8-byte alignment)
DArray *arr = darray_create(sizeof(double), 4, arena);
double val1 = 1.1, val2 = 2.2;
arr = darray_push(arr, &val1);
arr = darray_push(arr, &val2);
printf("Length: %zu (Expected: 2)\n", darray_length(arr));
double *res = darray_at(arr, 0);
if (*res == 1.1)
printf("Index 0: Success\n");
// Test 8-byte alignment of the data payload
if ((uintptr_t)res % 8 == 0) {
printf("Alignment: Success (8-byte aligned)\n");
} else {
printf("Alignment: FAILURE\n");
}
}
void test_negative_indexing(Arena *arena) {
printf("\n--- Test: Negative Index Wrapping ---\n");
DArray *arr = darray_create(sizeof(int), 5, arena);
for (int i = 0; i < 5; i++) {
arr = darray_push(arr, &i);
}
int *last = darray_at(arr, -1);
int *first = darray_at(arr, -5);
printf("Index -1: %d (Expected: 4)\n", *last);
printf("Index -5: %d (Expected: 0)\n", *first);
if (*last == 4 && *first == 0) {
printf("Wrapping: Success\n");
}
}
void test_growth_and_pointer_stability(Arena *arena) {
printf("\n--- Test: Growth & Arena Reallocation ---\n");
// Small capacity to force multiple growths
DArray *arr = darray_create(sizeof(int), 2, arena);
void *original_ptr = (void *)arr;
for (int i = 0; i < 10; i++) {
arr = darray_push(arr, &i);
}
printf("Final Length: %zu\n", darray_length(arr));
if ((void *)arr != original_ptr) {
printf("Growth: Success (Pointer relocated in Arena)\n");
}
// Verify data integrity after multiple relocations
int sum = 0;
for (int i = 0; i < 10; i++) {
sum += *(int *)darray_at(arr, i);
}
printf("Data Sum: %d (Expected: 45)\n", sum);
}
void test_pop_and_clear(Arena *arena) {
printf("\n--- Test: Pop & Clear ---\n");
DArray *arr = darray_create(sizeof(int), 4, arena);
int val = 42;
arr = darray_push(arr, &val);
int *popped = darray_pop(arr);
printf("Popped Value: %d (Expected: 42)\n", *popped);
printf("Length after pop: %zu\n", darray_length(arr));
darray_clear_zero(arr);
printf("Length after clear: %zu\n", darray_length(arr));
}
void test_heap_vs_arena() {
LOG_INFO("--- Starting Intensive Allocation Test ---");
// 1. Arena Array Test
Arena *arena = arena_create(1024);
DArray *a_arr = darray_create(sizeof(int), 2, arena);
// Force multiple reallocations in Arena
for (int i = 0; i < 50; i++) {
a_arr = darray_push(a_arr, &i);
}
LOG_INFO("Arena Array Length: %zu", darray_length(a_arr));
assert(*(int *)darray_at(a_arr, -1) == 49);
// 2. Heap Array Test (Arena = NULL)
DArray *h_arr = darray_create(sizeof(double), 1, NULL);
// Force multiple malloc/free cycles during growth
for (double d = 0.0; d < 10.0; d += 0.5) {
h_arr = darray_push(h_arr, &d);
}
LOG_INFO("Heap Array Length: %zu", darray_length(h_arr));
// Verify data integrity
assert(*(double *)darray_at(h_arr, 0) == 0.0);
assert(*(double *)darray_at(h_arr, -1) == 9.5);
// 3. Negative Index Wrapping Extremes
// Array length is 20, index -20 should be index 0
assert(darray_at(h_arr, -20) == darray_at(h_arr, 0));
darray_destroy(h_arr); // Must free heap array
arena_destroy(arena); // Arena handles its own
LOG_INFO("Allocation Test Passed.");
}
void test_zeroing_and_clearing() {
LOG_INFO("--- Starting Zero/Clear Test ---");
Arena *arena = arena_create(512);
// 1. Create zeroed array
size_t test_cap = 10;
DArray *arr = darray_create_zeroed(sizeof(long), test_cap, 0, arena);
// For now, let's just push 10 zeros to test it properly:
long zero = 0;
for (int i = 0; i < 10; i++) {
arr = darray_push(arr, &zero);
}
for (int i = 0; i < 10; i++) {
long val = *(long *)darray_at(arr, i);
assert(val == 0);
}
// 2. Fill with data and then Clear
long filling = 99;
darray_clear(arr); // Reset length to 0
for (int i = 0; i < 10; i++)
arr = darray_push(arr, &filling);
darray_clear_zero(arr); // Reset length AND zero memory
assert(darray_length(arr) == 0);
arena_destroy(arena);
LOG_INFO("Zero/Clear Test Passed.");
}