summaryrefslogtreecommitdiff
path: root/memory/test.cpp
blob: f62ce96504e674c07759d3955351e465f251a274 (plain)
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
#include <stdlib.h>
#include <stdio.h>
#include "memory.c"

// ================ ARENA ================  //
void test_arena_init(unsigned char *buffer, u32 buffer_size)
{
  struct Arena a = {0};
  arena_init(&a, buffer, buffer_size);
  assert((a.buffer == buffer) && "Arena does not start at the buffer");
  assert((a.curr_offset == 0) && (a.prev_offset == 0) && "Arena offsets not initialised properly");
  assert((a.capacity == buffer_size) && "Arena capacity does not match buffer size");
}

void test_arena_alloc(unsigned char *buffer, u32 buffer_size)
{
  struct Arena a = {0};
  arena_init(&a, buffer, buffer_size);
  void *qres = arena_alloc(&a, 8);
  assert((qres != NULL) && "Arena Allocation Test Failed: "
      "Reason: Failed to allocate memory");
}

void test_arena_alloc_low_memory(unsigned char *buffer, u32 buffer_size)
{
  struct Arena a = {0};
  arena_init(&a, buffer, buffer_size);
  void *qres = arena_alloc(&a, buffer_size + 20);
  assert((qres == NULL) && "Low Memory Arena Allocation Test Failed: "
      "Reason: Should have allocated the entire buffer since size is larger. \n");
  qres = arena_alloc(&a, 20);
}

void test_arena_alloc_negative_size(unsigned char *buffer, u32 buffer_size)
{
  struct Arena a = {0};
  arena_init(&a, buffer, buffer_size);
  void *qres = arena_alloc(&a, 16);
  u8* n1 = (u8*)qres;

  qres = arena_alloc(&a, -16);

  assert((qres == NULL) && "Low Memory Arena Allocation Test Failed: "
      "Reason: Should have allocated the remaining buffer. \n");
}

void test_arena_resize(unsigned char *buffer, u32 buffer_size)
{
  struct Arena a = {0};
  arena_init(&a, buffer, buffer_size);
  void *qres = arena_alloc(&a, 8);
  u8* n1 = (u8*)qres;
  *n1 = 23;

  qres = arena_alloc(&a, 8);
  u8* n2 = (u8*)qres;
  *n2 = 20;

  qres = arena_resize(&a, n1, 8, 16);
  assert((qres != NULL) && "Arena Resize Test Failed: "
  "Reason: Failed to resize previously allocated memory\n"
  "this should not have happened since we do not have anything causing availability issues\n");
  u8* n1_1 = (u8*)qres;

  assert((*n1 == *n1_1) && "Arena Resize Test Failed: "
  "Reason: Value of resized memory changed. This should not happen in any case!\n");
}

void test_arena_resize_tail(unsigned char *buffer, u32 buffer_size)
{
  struct Arena a = {0};
  arena_init(&a, buffer, buffer_size);
  void *qres = arena_alloc(&a, 16);
  u8* n1 = (u8*)qres;
  *n1 = 23;

  qres = arena_alloc(&a, 16);
  u8* n2 = (u8*)qres;
  *n2 = 20;

  qres = arena_resize(&a, n2, 16, 8);
  /*assert((qres.bytes_count == 8) && "Arena Resize Tail Test Failed: "
  "Reason: Failed to resize previously allocated memory at tail\n");*/
  u8* n2_1 = (u8*)qres;

  assert((*n2 == *n2_1) && "Arena Resize Test Failed: "
  "Reason: Value of resized memory changed. This should not happen in any case!\n");
}

void test_arena_resize_filled_completely(unsigned char *buffer, u32 buffer_size)
{
  struct Arena a = {0};
  arena_init(&a, buffer, buffer_size);
  void *qres = arena_alloc(&a, 8);
  u8* n1 = (u8*)qres;
  *n1 = 23;

  qres = arena_alloc(&a, 8);
  u8* n2 = (u8*)qres;
  *n2 = 20;
  
  qres = arena_resize(&a, n1, 8, buffer_size - 32);
  assert((qres != NULL) && "Arena Resize Filled Completely Failed: \n"
  "Reason: Failed to resize an element even though it perfectly fills the memory Arena");
}

void test_arena_resize_out_of_bounds(unsigned char *buffer, u32 buffer_size)
{
  struct Arena a = {0};
  arena_init(&a, buffer, buffer_size);
  void *qres = arena_alloc(&a, 8);
  u8* n1 = (u8*)qres;
  *n1 = 23;

  qres = arena_alloc(&a, 8);
  u8* n2 = (u8*)qres;
  *n2 = 20;
  
  qres = arena_resize(&a, n1, 8, buffer_size - 8);
  assert(qres == NULL && "Arena Resize OUT OF BOUNDS test Failed: \n"
  "Reason: Allocated a larger than capacity element within Arena capacity.\n");
}

void test_arena_resize_negative_size(unsigned char *buffer, u32 buffer_size)
{
  struct Arena a = {0};
  arena_init(&a, buffer, buffer_size);
  void *qres = arena_alloc(&a, 8);
  u8* n1 = (u8*)qres;
  *n1 = 23;

  qres = arena_alloc(&a, 8);
  u8* n2 = (u8*)qres;
  *n2 = 20;
  
  qres = arena_resize(&a, n1, 8, -35);
  assert(qres == NULL && "Arena Resize -ve size test Failed: \n"
  "Reason: Failed to handle allocating a -ve size (VERY LARGE IN UINT) space.\n");
}

void test_arena_resize_tail_negative_size(unsigned char *buffer, u32 buffer_size)
{
  struct Arena a = {0};
  arena_init(&a, buffer, buffer_size);
  void *qres = arena_alloc(&a, 8);
  u8* n1 = (u8*)qres;
  *n1 = 23;

  qres = arena_alloc(&a, 8);
  u8* n2 = (u8*)qres;
  *n2 = 20;
  
  qres = arena_resize(&a, n2, 8, -35);
  assert(qres == NULL && "Arena Resize OUT OF BOUNDS test Failed: \n"
  "Reason: Failed to fit a larger than capacity element within Arena capacity.\n");
}

void test_arena_clear(unsigned char *buffer, u32 buffer_size)
{
  struct Arena a = {0};
  arena_init(&a, buffer, buffer_size);
  void *qres = arena_alloc(&a, 8);
  arena_clear(&a);
  assert((a.prev_offset == 0) && (a.curr_offset == 0) && "Arena Clear Test Failed: \n"
  "Reason: Failed to clear offsets on an active Arena entity after it was cleared.\n");
}

void test_arena_realloc(unsigned char *buffer, u32 buffer_size)
{
  struct Arena a = {0};
  arena_init(&a, buffer, buffer_size);
  void *qres = arena_alloc(&a, 8);
  u8* n1 = (u8*)qres;
  *n1 = 12;
  u8 n1_value = *n1;

  // free Arena
  arena_clear(&a);

  qres = arena_alloc(&a, 16);
  u8* n1_realloc = (u8*)qres;

  assert((*n1_realloc != n1_value) && "Arena Realloc Test Failed: \n"
      "Reason: Failed to properly clear memory in a region where memory was already allocated.\n");
}

// ================ STACK ================  //

void test_stack_init(unsigned char *buffer, u32 buffer_size)
{
  struct stack s = {0};
  stack_init(&s, buffer, buffer_size);
  assert((s.buffer == buffer) && "stack does not start at the buffer");
  assert((s.curr_offset == 0) && (s.prev_offset == 0) && "stack offsets not initialised properly");
  assert((s.capacity == buffer_size) && "stack capacity does not match buffer size");
}

void test_stack_alloc_n1(unsigned char *buffer, u32 buffer_size)
{
  struct stack s = {0};
  stack_init(&s, buffer, buffer_size);

  u8 ele_sz = 16;
  struct ResVoid qres = stack_alloc(&s, ele_sz);
  assert((qres.status == MEM_OK) && "failed to allocate stack element");

  u8 *ele = (u8 *)qres.memory;
  struct stack_hdr *header = (struct stack_hdr*)((uintptr_t)ele - sizeof(struct stack_hdr));
  assert((header->prev_offset == 0) && "incorrect prev_offset for first stack element");

  size_t pad = header->padding;

  assert((s.curr_offset == pad + ele_sz ) && "incorrect curr offset memory");
}

void test_stack_alloc_n2(unsigned char *buffer, u32 buffer_size)
{
  struct stack s = {0};
  stack_init(&s, buffer, buffer_size);

  u8 ele_sz = 16;
  stack_alloc(&s, ele_sz);

  size_t last_ele_head = s.prev_offset;
  size_t last_ele_tail = s.curr_offset;
  struct ResVoid qres = stack_alloc(&s, ele_sz);
  assert((qres.status == MEM_OK) && "failed to allocate stack element");

  u8 *ele = (u8 *)qres.memory;
  struct stack_hdr *header = (struct stack_hdr*)((uintptr_t)ele - sizeof(struct stack_hdr));
  assert((header->prev_offset == last_ele_head) && "incorrect prev_offset for second stack element");

  size_t pad = header->padding;
  assert((s.curr_offset == last_ele_tail + pad + ele_sz ) && "incorrect curr offset memory");
}

void test_stack_free(unsigned char *buffer, u32 buffer_size)
{
  struct stack s = {0};
  stack_init(&s, buffer, buffer_size);

  u8 ele_sz = 16;
  stack_alloc(&s, ele_sz);

  enum MemStatus status = stack_free(&s);
  assert((status == MEM_OK) && "failed to free stack element");

  assert((s.buffer == buffer) && "failed to reset buffer pointer to start of memory");
  assert((s.prev_offset == 0) && (s.curr_offset == 0) && "failed to move offsets back correctly");
}

void test_stack_resize_n0(unsigned char *buffer, u32 buffer_size)
{
  struct stack s = {0};
  stack_init(&s, buffer, buffer_size);

  u8 static_mem[8] = {0,1,2,3,4,5,6,7};
  struct ResVoid qres = stack_resize(&s, static_mem, 8, 16);
  assert((qres.status == MEM_OUT_OF_BOUNDS) && "Stack Resize n0 Failed: \
        Reason: should not have resized an element not belonging to the stack\n");
}

void test_stack_resize_n1(unsigned char *buffer, u32 buffer_size)
{
  struct stack s = {0};
  stack_init(&s, buffer, buffer_size);

  u8 ele_sz = 16;
  struct ResVoid qres =  stack_alloc(&s, ele_sz);
  u8* n1 = (u8*)qres.memory;
  *n1 = 23;

  qres = stack_resize(&s, n1, ele_sz, ele_sz + 20);
  assert((qres.status == MEM_OK) && "Stack Resize n1 Failed: \
      Reason: failed to resize stack element\n");
  n1 = (u8*)qres.memory;

  struct stack_hdr* header = (struct stack_hdr*)(n1 - sizeof(struct stack_hdr));
  assert((s.curr_offset == 52) && "Stack Resize n1 Failed: \
      Reason: incorrectly resized stack element\n");
}

void test_stack_resize_n1_low_memory(unsigned char *buffer, u32 buffer_size)
{
  struct stack s = {0};
  stack_init(&s, buffer, buffer_size);

  u8 ele_sz = 16;
  struct ResVoid qres =  stack_alloc(&s, ele_sz);
  u8* n1 = (u8*)qres.memory;
  *n1 = 23;

  qres = stack_resize(&s, n1, ele_sz, ele_sz + 256);
  n1 = (u8*)qres.memory;

  assert((qres.status == MEM_FULL) && "Stack Resize n1 low memory Failed: \
      Reason: Should not be allowed to resize beyond the allocated stack capacity\n");
}

void test_stack_resize_n2(unsigned char *buffer, u32 buffer_size)
{
  struct stack s = {0};
  stack_init(&s, buffer, buffer_size);

  u8 ele_sz = 16;
  struct ResVoid qres =  stack_alloc(&s, ele_sz);
  u8* n1 = (u8*)qres.memory;
  *n1 = 1;

  qres = stack_alloc(&s, ele_sz);
  u8* n2 = (u8*)qres.memory;
  *n2 = 2;

  size_t last_ele_head = s.prev_offset;

  qres = stack_resize(&s, n1, ele_sz, 8);
  u8* n3 = (u8*)qres.memory;
  assert((qres.status == MEM_OK) && "Stack Resize n2 Failed: "
      "Reason: failed to resize first element in a two element array\n");

  assert((*n3 == *n1) && "Stack Resize n2 Failed: "
      "Reason: failed to move over data properly after resizing elements");

  struct stack_hdr* header = (struct stack_hdr*)(n3 - sizeof(struct stack_hdr));
  assert((header->prev_offset == last_ele_head) && "Stack Resize 2 Failed: "
      "Reason: failed to set the previous offset of the header element properly after resize");
}

void test_stack_resize_tail(unsigned char *buffer, u32 buffer_size)
{
  struct stack s = {0};
  stack_init(&s, buffer, buffer_size);

  u8 ele_sz = 16;
  struct ResVoid qres =  stack_alloc(&s, ele_sz);
  u8* n1 = (u8*)qres.memory;
  *n1 = 1;

  qres = stack_resize(&s, n1, ele_sz, 8);
  assert((qres.status == MEM_OK) && "Stack Resize tail Failed: "
      "Reason: failed to resize last element to be smaller\n");
}

void test_stack_alloc_free(unsigned char *buffer, u32 buffer_size)
{
  struct stack s = {0};
  stack_init(&s, buffer, buffer_size);

  u8 ele_sz = 16;
  struct ResVoid qres = stack_alloc(&s, ele_sz);
  u8* n1 = (u8*)qres.memory;
  *n1 = 1;
  size_t head_n1 = s.prev_offset;
  size_t tail_n1 = s.curr_offset;

  qres = stack_alloc(&s, ele_sz);
  u8* n2 = (u8*)qres.memory;
  *n2 = 2;
  size_t head_n2 = s.prev_offset;
  size_t tail_n2 = s.curr_offset;

  /* @note: elements are allocated, now we will be testing a few things
   *
   * 1. can we free all elements up till the stack UNTIL the point where, 
   * we can not free the stack since it is empty
   *
   * 2. when we free elements, does the offset reset to the point we expect it to
   * By which I mean,
   *
   *                . . . . . ele_1 . . . . . ele_2 . . . . .
   *                              |               |
   *                              |               |-> curr_offset is here at the end of ele2
   *                              |
   *                              |-> this is the end of ele1, once we free, we expect the curr_offset
   *                              to return to this point and we expect to start considering allocations from
   *                              this point on
   *
   *
   * */
  stack_free(&s);

  assert((s.prev_offset == head_n1) && "Stack alloc free error"
      "Reason: failed to move the prev_offset back to the correct position");
  assert((s.curr_offset == tail_n1) && "Stack alloc free error"
      "Reason: failed to move the curr_offset back to the correct position");

  /*
   * @note: we now want to test after allocating something, whether the memory was overwritten properly
   * and there was no garbage data present
   */

  qres = stack_alloc(&s, ele_sz);
  u8* n2_1 = (u8*)qres.memory;
  assert((*n2_1 == 0) && "Stack Alloc Free error"
      "Reason: failed to free up memory properly on allocating memory that was cleared");
}

void test_stack_alloc_free_resize(unsigned char *buffer, u32 buffer_size)
{
  struct stack s = {0};
  stack_init(&s, buffer, buffer_size);

  u8 ele_sz = 16;
  struct ResVoid qres = stack_alloc(&s, ele_sz);
  u8* n1 = (u8*)qres.memory;
  *n1 = 1;
  size_t head_n1 = s.prev_offset;
  size_t tail_n1 = s.curr_offset;

  qres = stack_alloc(&s, ele_sz);
  u8* n2 = (u8*)qres.memory;
  *n2 = 2;
  size_t head_n2 = s.prev_offset;
  size_t tail_n2 = s.curr_offset;

  qres = stack_alloc(&s, ele_sz);
  u8* n3 = (u8*)qres.memory;
  *n3 = 3;
  size_t head_n3 = s.prev_offset;
  size_t tail_n3 = s.curr_offset;

  stack_free(&s);

  qres = stack_resize(&s, n1, ele_sz, 32);
  u8* n1_1 = (u8*)qres.memory;
  assert((*n1_1 != 3) && "Stack Alloc Free Resize Test Failed: "
      "Reason: the newly resized memory was not setup properly.\n"
      "It had the memory contents of a previously allocated statement\n");
  assert((n1_1 == n3) && "Stack Alloc Free Resize Test Failed: "
      "Reason: the newly resized memory was not allocated to the correct next region\n"
      "It should have the same memory address as memory allocation #3\n");
}

void test_stack_resize_n2_low_space(unsigned char *buffer, u32 buffer_size)
{
  struct stack s = {0};
  stack_init(&s, buffer, buffer_size);

  u8 ele_sz = 16;
  struct ResVoid qres = stack_alloc(&s, ele_sz);
  u8* n1 = (u8*)qres.memory;
  *n1 = 1;
  size_t head_n1 = s.prev_offset;
  size_t tail_n1 = s.curr_offset;

  qres = stack_alloc(&s, s.capacity - 64);
  u8* n2 = (u8*)qres.memory;
  *n2 = 2;
  size_t head_n2 = s.prev_offset;
  size_t tail_n2 = s.curr_offset;

  qres = stack_resize(&s, n1, ele_sz, 30);
  
  assert((qres.status == MEM_FULL) && "Test Stack Resize Low Space with 2 elements Failed: "
      "Reason: Failed to catch resize with size larger than available space. \n");
}

void test_stack_alloc_negative_size(unsigned char *buffer, u32 buffer_size)
{
  struct stack s = {0};
  stack_init(&s, buffer, buffer_size);

  // @note: the way this works is that size_t is unsigned, which is what the function takes
  // so we get a really large number, that actually ends up looping around and becomes less than the array.
  // so we have a bounds check for that.
  stack_alloc(&s, 16);
  struct ResVoid qres = stack_alloc(&s, -16);
  assert((qres.status == MEM_FULL) && "Test Stack Alloc Negative Size Failed\n"
      "Reason: Failed to catch allocation with -ve size. \n"
      "That will translate to be a very large size since the size variable is a size_t (unsigned)\n");
}

void test_stack_resize_negative_size(unsigned char *buffer, u32 buffer_size)
{
  struct stack s = {0};
  stack_init(&s, buffer, buffer_size);

  struct ResVoid qres = stack_alloc(&s, 16);
  s8* n1 = (s8*)qres.memory;
  *n1 = -20;

  qres = stack_resize(&s, n1, 16, -16);
  assert((qres.status == MEM_FULL) && "Test Stack Alloc Negative Size Failed\n"
      "Reason: Failed to catch resize with -ve size. \n"
      "That will translate to be a very large size since the size variable is a size_t (unsigned)\n");
}

void test_stack_resize_tail_negative_size(unsigned char *buffer, u32 buffer_size)
{
  struct stack s = {0};
  stack_init(&s, buffer, buffer_size);

  u8 ele_sz = 16;
  struct ResVoid qres =  stack_alloc(&s, ele_sz);
  u8* n1 = (u8*)qres.memory;
  *n1 = 1;

  qres = stack_resize(&s, n1, ele_sz, -8);
  u8* n2 = (u8*)qres.memory;
  assert((qres.status == MEM_FULL) && "Stack Resize tail Negative Size Failed: "
      "Reason: Failed to catch resize with -ve size. \n"
      "That will translate to be a very large size since the size variable is a size_t (unsigned)\n");
}

int main(int argc, char** argv) {
  u32 sz = 256*sizeof(u8);
  u8 *buffer = (u8 *)malloc(sz);
  // --------- ARENA --------- //
  printf("\n===== Testing Arena Allocator =====\n");
  test_arena_init(buffer, sz);
  printf("- Arena initialization test passed\n");
  test_arena_alloc(buffer, sz);
  test_arena_alloc_low_memory(buffer, sz);
  test_arena_alloc_negative_size(buffer, sz);
  printf("- Arena allocation tests passed\n");
  test_arena_resize(buffer, sz);
  test_arena_resize_tail(buffer, sz);
  test_arena_resize_negative_size(buffer, sz);
  test_arena_resize_tail_negative_size(buffer, sz);
  test_arena_resize_filled_completely(buffer, sz);
  test_arena_resize_out_of_bounds(buffer, sz);
  printf("- Arena resize tests passed\n");
  test_arena_clear(buffer, sz);
  printf("- Arena clear tests passed\n");
  test_arena_realloc(buffer, sz);
  printf("- Arena reallocation tests passed\n");

  // --------- STACK --------- //
  printf("\n===== Testing Stack Allocator =====\n");

  test_stack_init(buffer, sz);
  printf("- stack initialization passed\n");

  test_stack_alloc_n1(buffer, sz);
  test_stack_alloc_n2(buffer, sz);
  printf("- Stack allocation tests passed\n");

  test_stack_free(buffer, sz);
  printf("- stack free passed\n");

  test_stack_resize_n0(buffer, sz);
  test_stack_resize_n1(buffer, sz);
  test_stack_resize_n1_low_memory(buffer, sz);
  test_stack_resize_n2(buffer, sz);
  test_stack_resize_tail(buffer, sz);
  printf("- stack resize passed\n");

  printf("- testing stack alloc free edge cases \n");
  printf("  - edge cases passed:\n");
  test_stack_alloc_free(buffer, sz);
  printf("    1. alloc then free\n");
  test_stack_alloc_free_resize(buffer, sz);
  printf("    2. alloc then free then resize\n");
  test_stack_resize_n2_low_space(buffer, sz);
  printf("    3. alloc two elements and then resize when there is low space\n");
  test_stack_alloc_negative_size(buffer, sz);
  printf("    4. allocating negative size detection\n");
  test_stack_resize_negative_size(buffer, sz);
  printf("    5. resizing negative size detection\n");
  test_stack_resize_tail_negative_size(buffer, sz);
  printf("    6. resizing tail to negative size detection\n");

  printf("\n===== Memory tests completed successfully =====\n");
  free(buffer);
  return 1;
}