MP-Gadget  5.0.1.dev1-76bc7d4726-dirty
test_memory.c
Go to the documentation of this file.
1 #include <stdarg.h>
2 #include <stddef.h>
3 #include <setjmp.h>
4 #include <cmocka.h>
5 #include <math.h>
6 #include <stdio.h>
7 
8 #include "stub.h"
9 
10 static void
11 test_allocator(void ** state)
12 {
13  Allocator A0[1];
14  allocator_init(A0, "Default", 4096 * 1024, 1, NULL);
15 
16  int * p1 = allocator_alloc_bot(A0, "M+1", 1024*sizeof(int));
17  int * p2 = allocator_alloc_bot(A0, "M+2", 2048*sizeof(int));
18 
19  int * q1 = allocator_alloc_top(A0, "M-1", 1024*sizeof(int));
20  int * q2 = allocator_alloc_top(A0, "M-2", 2048*sizeof(int));
21 
22  p1[0] = 1;
23  p2[1] = 1;
24  allocator_print(A0);
25 
26  q2[2000] = 1;
27  q2 = allocator_realloc(A0, q2, 3072*sizeof(int));
28  assert_int_equal(q2[2000] ,1);
29  /*Realloc to something smaller*/
30  q2 = allocator_realloc(A0, q2, 2048*sizeof(int));
31  assert_int_equal(q2[2000] ,1);
32 
33  /* Assert that reallocing does not move the base pointer.
34  * Note this is true only for bottom allocations*/
35  int * p2new = allocator_realloc(A0, p2, 3072*sizeof(int));
36  assert_int_equal(p2new, p2);
37 
38  assert_int_equal(allocator_dealloc(A0, p1), ALLOC_EMISMATCH);
39  assert_int_equal(allocator_dealloc(A0, q1), ALLOC_EMISMATCH);
40 
41  assert_int_equal(allocator_dealloc(A0, p2), 0);
42  assert_int_equal(allocator_dealloc(A0, q2), 0);
43 
44  allocator_free(p1);
45  allocator_free(q1);
46 
47  allocator_print(A0);
48 
50 }
51 
52 static void
53 test_sub_allocator(void ** state)
54 {
55  Allocator A0[1];
56  Allocator A1[1];
57  allocator_init(A0, "Default", 4096 * 1024 * 2, 1, NULL);
58  allocator_init(A1, "A1", 4096 * 1024, 1, A0);
59 
60  allocator_print(A0);
61  void * p1 = allocator_alloc_bot(A1, "M+1", 1024);
62  void * p2 = allocator_alloc_bot(A0, "M+2", 2048);
63 
64  allocator_print(A0);
65  allocator_print(A1);
66 
67  allocator_free(p2);
68  allocator_free(p1);
69 
72 }
73 static void
74 test_allocator_malloc(void ** state)
75 {
76  Allocator A0[1];
77  allocator_malloc_init(A0, "libc based", 4096 * 1024, 1, NULL);
78 
79  int * p1 = allocator_alloc_bot(A0, "M+1", 2048);
80  int * p2 = allocator_alloc_bot(A0, "M+2", 2048);
81 
82  int * q1 = allocator_alloc_top(A0, "M-1", 2048);
83  int * q2 = allocator_alloc_top(A0, "M-2", 128*sizeof(int));
84 
85  allocator_print(A0);
86  p1[0] = 1;
87  p2[1] = 1;
88  q2[100] = 1;
89 
90  p2 = allocator_realloc(A0, p2, 3072);
91  assert_int_equal(p2[1],1);
92  q2 = allocator_realloc(A0, q2, 1000*sizeof(int));
93  q2[500] = 1;
94  assert_int_equal(q2[100] ,1);
95 
96  assert_int_equal(allocator_dealloc(A0, p1), ALLOC_EMISMATCH);
97  assert_int_equal(allocator_dealloc(A0, q1), ALLOC_EMISMATCH);
98 
99  assert_int_equal(allocator_dealloc(A0, p2), 0);
100  assert_int_equal(allocator_dealloc(A0, q2), 0);
101 
102  allocator_free(p1);
103  allocator_free(q1);
104 
105  allocator_print(A0);
106 
107  allocator_destroy(A0);
108 }
109 
110 int main(void)
111 {
112  const struct CMUnitTest tests[] = {
113  cmocka_unit_test(test_allocator),
114  cmocka_unit_test(test_allocator_malloc),
115  cmocka_unit_test(test_sub_allocator),
116  };
117  return cmocka_run_group_tests_mpi(tests, NULL, NULL);
118 }
int allocator_init(Allocator *alloc, const char *name, const size_t request_size, const int zero, Allocator *parent)
Definition: memory.c:24
void allocator_free(void *ptr)
Definition: memory.c:358
int allocator_malloc_init(Allocator *alloc, const char *name, const size_t request_size, const int zero, Allocator *parent)
Definition: memory.c:54
int allocator_dealloc(Allocator *alloc, void *ptr)
Definition: memory.c:376
void allocator_print(Allocator *alloc)
Definition: memory.c:284
int allocator_destroy(Allocator *alloc)
Definition: memory.c:176
#define allocator_alloc_bot(alloc, name, size)
Definition: memory.h:65
#define allocator_alloc_top(alloc, name, size)
Definition: memory.h:68
#define allocator_realloc(alloc, ptr, size)
Definition: memory.h:71
#define ALLOC_EMISMATCH
Definition: memory.h:9
static void test_allocator_malloc(void **state)
Definition: test_memory.c:74
int main(void)
Definition: test_memory.c:110
static void test_sub_allocator(void **state)
Definition: test_memory.c:53
static void test_allocator(void **state)
Definition: test_memory.c:11