MP-Gadget  5.0.1.dev1-76bc7d4726-dirty
Functions
test_memory.c File Reference
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include <math.h>
#include <stdio.h>
#include "stub.h"
Include dependency graph for test_memory.c:

Go to the source code of this file.

Functions

static void test_allocator (void **state)
 
static void test_sub_allocator (void **state)
 
static void test_allocator_malloc (void **state)
 
int main (void)
 

Function Documentation

◆ main()

int main ( void  )

Definition at line 110 of file test_memory.c.

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 }
static void test_allocator_malloc(void **state)
Definition: test_memory.c:74
static void test_sub_allocator(void **state)
Definition: test_memory.c:53
static void test_allocator(void **state)
Definition: test_memory.c:11

References test_allocator(), test_allocator_malloc(), and test_sub_allocator().

Here is the call graph for this function:

◆ test_allocator()

static void test_allocator ( void **  state)
static

Definition at line 11 of file test_memory.c.

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 }
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_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

References ALLOC_EMISMATCH, allocator_alloc_bot, allocator_alloc_top, allocator_dealloc(), allocator_destroy(), allocator_free(), allocator_init(), allocator_print(), and allocator_realloc.

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ test_allocator_malloc()

static void test_allocator_malloc ( void **  state)
static

Definition at line 74 of file test_memory.c.

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 }
int allocator_malloc_init(Allocator *alloc, const char *name, const size_t request_size, const int zero, Allocator *parent)
Definition: memory.c:54

References ALLOC_EMISMATCH, allocator_alloc_bot, allocator_alloc_top, allocator_dealloc(), allocator_destroy(), allocator_free(), allocator_malloc_init(), allocator_print(), and allocator_realloc.

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ test_sub_allocator()

static void test_sub_allocator ( void **  state)
static

Definition at line 53 of file test_memory.c.

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 }

References allocator_alloc_bot, allocator_destroy(), allocator_free(), allocator_init(), and allocator_print().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function: