HIPC  0.5
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
test_client_memope.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 
5 #include "client.c"
6 
7 static void print_pllist(FILE * fp,
8  struct HIPC_ptr_link_list const *pllist_p)
9 {
10  if (NULL == fp) {
11  return;
12  }
13 
14  while (NULL != pllist_p) {
15  fprintf(stderr, "p %p/%p\n", (void *) pllist_p,
16  (void *) pllist_p->ptr);
17  pllist_p = pllist_p->next;
18  }
19 
20  return;
21 }
22 
27 {
28  struct HIPC_client cl;
29 
30  cl.pllist_p = NULL;
31  print_pllist(stdout, cl.pllist_p);
33  print_pllist(stdout, cl.pllist_p);
34  assert(NULL == cl.pllist_p);
35 
36  return;
37 }
38 
42 static void test_client_memope_malloc(void)
43 {
44  struct HIPC_client cl;
45  void *tmp_p;
46 
47  cl.pllist_p = NULL;
48  tmp_p = hipcClientMalloc(&cl, 5);
49  assert(NULL != cl.pllist_p);
50  assert(tmp_p == cl.pllist_p->ptr);
51  assert(NULL == cl.pllist_p->next);
52  print_pllist(stdout, cl.pllist_p);
53 
55  assert(NULL == cl.pllist_p);
56  print_pllist(stdout, cl.pllist_p);
57 
58  return;
59 }
60 
64 static void test_client_memope_calloc(void)
65 {
66  struct HIPC_client cl;
67  unsigned char *tmp_p;
68  int i;
69 
70  cl.pllist_p = NULL;
71  tmp_p = hipcClientCalloc(&cl, 5, 7);
72  for (i = 0; i < (5 * 7); ++i) {
73  assert(0 == tmp_p[i]);
74  }
75  assert(NULL != cl.pllist_p);
76  assert(tmp_p == cl.pllist_p->ptr);
77  assert(NULL == cl.pllist_p->next);
78  print_pllist(stdout, cl.pllist_p);
79 
81  assert(NULL == cl.pllist_p);
82  print_pllist(stdout, cl.pllist_p);
83 
84  return;
85 }
86 
90 static void test_client_memope_multi(void)
91 {
92  struct HIPC_client cl;
93  void *pary_mem[3];
94  struct HIPC_ptr_link_list const *pary_pllist[3];
95 
96  cl.pllist_p = NULL;
97  pary_mem[0] = hipcClientMalloc(&cl, 128);
98  assert(NULL != cl.pllist_p);
99  pary_pllist[0] = cl.pllist_p;
100  assert(pary_pllist[0]->ptr == pary_mem[0]);
101  assert(NULL == pary_pllist[0]->next);
102  print_pllist(stdout, cl.pllist_p);
103  fprintf(stdout, "\n");
104 
105  pary_mem[1] = hipcClientCalloc(&cl, 8, 64);
106  assert(NULL != cl.pllist_p);
107  pary_pllist[1] = cl.pllist_p;
108  assert(pary_pllist[1]->ptr == pary_mem[1]);
109  assert(pary_pllist[0] == pary_pllist[1]->next);
110  assert(pary_pllist[0]->ptr == pary_mem[0]);
111  assert(NULL == pary_pllist[0]->next);
112  print_pllist(stdout, cl.pllist_p);
113  fprintf(stdout, "\n");
114 
115  pary_mem[2] = hipcClientMalloc(&cl, 256);
116  assert(NULL != cl.pllist_p);
117  pary_pllist[2] = cl.pllist_p;
118  assert(pary_pllist[2]->ptr == pary_mem[2]);
119  assert(pary_pllist[1] == pary_pllist[2]->next);
120  assert(pary_pllist[1]->ptr == pary_mem[1]);
121  assert(pary_pllist[0] == pary_pllist[1]->next);
122  assert(pary_pllist[0]->ptr == pary_mem[0]);
123  assert(NULL == pary_pllist[0]->next);
124  print_pllist(stdout, cl.pllist_p);
125  fprintf(stdout, "\n");
126 
127  hipcClientTerminate(&cl);
128  assert(NULL == cl.pllist_p);
129  print_pllist(stdout, cl.pllist_p);
130 
131  return;
132 }
133 
134 int main(int argc, char *argv[])
135 {
136  if (2 != argc) {
137  fprintf(stderr, "Usage: %s test_id\n", argv[0]);
138  exit(EXIT_FAILURE);
139  }
140 
141  switch (argv[1][0]) {
142  case 'a':
144  break;
145  case 'b':
147  break;
148  case 'c':
150  break;
151  case 'd':
153  break;
154  default:
155  fprintf(stderr, "Invalid test_id: %s \n", argv[1]);
156  exit(EXIT_FAILURE);
157  }
158 
159  return 0;
160 }
static void test_client_memope_malloc(void)
Check normal termination when there is one malloc.
int main(int argc, char *argv[])
static void test_client_memope_multi(void)
Check normal termination when there are multiple memory allocations.
void hipcClientTerminate(struct HIPC_client *const cl_p)
Terminates a HIPC client with releasing the memory space that the client has.
Definition: client.c:438
static void print_pllist(FILE *fp, struct HIPC_ptr_link_list const *pllist_p)
static void * hipcClientCalloc(struct HIPC_client *const cl_p, size_t nmemb, size_t size)
Allocates memory by using calloc.
Definition: client.c:84
static void test_client_memope_no_allocation(void)
Check normal termination when there is no memory allocation.
static void * hipcClientMalloc(struct HIPC_client *const cl_p, size_t size)
Allocates memory by using malloc.
Definition: client.c:49
struct HIPC_ptr_link_list * pllist_p
Definition: client.h:87
static void test_client_memope_calloc(void)
Check normal termination when there is one calloc.