Atrinik Server  4.0
memory.c
1 /*************************************************************************
2  * Atrinik, a Multiplayer Online Role Playing Game *
3  * *
4  * Copyright (C) 2009-2014 Alex Tokar and Atrinik Development Team *
5  * *
6  * Fork from Crossfire (Multiplayer game for X-windows). *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the Free Software *
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
21  * *
22  * The author can be reached at admin@atrinik.org *
23  ************************************************************************/
24 
25 #include <global.h>
26 #include <check.h>
27 #include <checkstd.h>
28 #include <check_proto.h>
29 #include <malloc.h>
30 
31 START_TEST(test_memory_emalloc)
32 {
33  char *ptr;
34  memory_status_t memory_status;
35  size_t size;
36 
37  ptr = emalloc(8);
38  ck_assert_ptr_ne(ptr, NULL);
39  snprintf(ptr, 8, "%s", "testing");
40  ck_assert_str_eq(ptr, "testing");
41 
42  if (memory_get_status(ptr, &memory_status)) {
43  ck_assert_uint_eq(memory_status, MEMORY_STATUS_OK);
44  }
45 
46  if (memory_get_size(ptr, &size)) {
47  ck_assert_uint_eq(size, 8);
48  }
49 
50  efree(ptr);
51 }
52 END_TEST
53 
54 START_TEST(test_memory_efree)
55 {
56  void *ptr;
57  memory_status_t memory_status;
58  size_t size;
59 
60  ptr = emalloc(42);
61  ck_assert_ptr_ne(ptr, NULL);
62 
63  if (memory_get_status(ptr, &memory_status)) {
64  ck_assert_uint_eq(memory_status, MEMORY_STATUS_OK);
65  }
66 
67  if (memory_get_size(ptr, &size)) {
68  ck_assert_uint_eq(size, 42);
69  }
70 
71  efree(ptr);
72 
73  if (memory_get_status(ptr, &memory_status)) {
74  ck_assert_uint_eq(memory_status, MEMORY_STATUS_FREE);
75  }
76 }
77 END_TEST
78 
79 START_TEST(test_memory_ecalloc)
80 {
81  int32_t *ptr;
82  size_t size;
83  memory_status_t memory_status;
84 
85  ptr = ecalloc(42, sizeof(*ptr));
86  ck_assert_ptr_ne(ptr, NULL);
87 
88  if (memory_get_status(ptr, &memory_status)) {
89  ck_assert_uint_eq(memory_status, MEMORY_STATUS_OK);
90  }
91 
92  if (memory_get_size(ptr, &size)) {
93  ck_assert_uint_eq(size, 42 * sizeof(*ptr));
94  }
95 
96  for (size_t i = 0; i < 42; i++) {
97  ck_assert_uint_eq(ptr[i], 0);
98  }
99 
100  efree(ptr);
101 
102  if (memory_get_status(ptr, &memory_status)) {
103  ck_assert_uint_eq(memory_status, MEMORY_STATUS_FREE);
104  }
105 }
106 END_TEST
107 
108 START_TEST(test_memory_erealloc)
109 {
110  char *ptr;
111  memory_status_t memory_status;
112  size_t size;
113 
114  ptr = emalloc(4);
115  ck_assert_ptr_ne(ptr, NULL);
116 
117  if (memory_get_status(ptr, &memory_status)) {
118  ck_assert_uint_eq(memory_status, MEMORY_STATUS_OK);
119  }
120 
121  if (memory_get_size(ptr, &size)) {
122  ck_assert_uint_eq(size, 4);
123  }
124 
125  snprintf(ptr, 4, "%s", "testing");
126  ck_assert_str_eq(ptr, "tes");
127 
128  ptr = erealloc(ptr, 8);
129 
130  if (memory_get_status(ptr, &memory_status)) {
131  ck_assert_uint_eq(memory_status, MEMORY_STATUS_OK);
132  }
133 
134  if (memory_get_size(ptr, &size)) {
135  ck_assert_uint_eq(size, 8);
136  }
137 
138  ck_assert_str_eq(ptr, "tes");
139  snprintf(ptr, 8, "%s", "testing");
140  ck_assert_str_eq(ptr, "testing");
141 
142  ptr = erealloc(ptr, 20);
143 
144  if (memory_get_status(ptr, &memory_status)) {
145  ck_assert_uint_eq(memory_status, MEMORY_STATUS_OK);
146  }
147 
148  if (memory_get_size(ptr, &size)) {
149  ck_assert_uint_eq(size, 20);
150  }
151 
152  ck_assert_str_eq(ptr, "testing");
153 
154  ptr = erealloc(ptr, 8);
155 
156  if (memory_get_status(ptr, &memory_status)) {
157  ck_assert_uint_eq(memory_status, MEMORY_STATUS_OK);
158  }
159 
160  if (memory_get_size(ptr, &size)) {
161  ck_assert_uint_eq(size, 8);
162  }
163 
164  ck_assert_str_eq(ptr, "testing");
165 
166  efree(ptr);
167 
168  if (memory_get_status(ptr, &memory_status)) {
169  ck_assert_uint_eq(memory_status, MEMORY_STATUS_FREE);
170  }
171 }
172 END_TEST
173 
174 START_TEST(test_memory_reallocz)
175 {
176  int32_t *ptr;
177  memory_status_t memory_status;
178  size_t size, i;
179 
180  ptr = emalloc(sizeof(*ptr) * 42);
181  ck_assert_ptr_ne(ptr, NULL);
182 
183  if (memory_get_status(ptr, &memory_status)) {
184  ck_assert_uint_eq(memory_status, MEMORY_STATUS_OK);
185  }
186 
187  if (memory_get_size(ptr, &size)) {
188  ck_assert_uint_eq(size, sizeof(*ptr) * 42);
189  }
190 
191  for (i = 0; i < 42; i++) {
192  ptr[i] = 1337;
193  }
194 
195  for (i = 0; i < 42; i++) {
196  ck_assert_uint_eq(ptr[i], 1337);
197  }
198 
199  ptr = ereallocz(ptr, sizeof(*ptr) * 42, sizeof(*ptr) * 42 * 2);
200 
201  if (memory_get_status(ptr, &memory_status)) {
202  ck_assert_uint_eq(memory_status, MEMORY_STATUS_OK);
203  }
204 
205  if (memory_get_size(ptr, &size)) {
206  ck_assert_uint_eq(size, sizeof(*ptr) * 42 * 2);
207  }
208 
209  for (i = 0; i < 42; i++) {
210  ck_assert_uint_eq(ptr[i], 1337);
211  }
212 
213  for (i = 42; i < 42 * 2; i++) {
214  ck_assert_uint_eq(ptr[i], 0);
215  }
216 
217  ptr = ereallocz(ptr, sizeof(*ptr) * 42 * 2, sizeof(*ptr) * 42);
218 
219  if (memory_get_status(ptr, &memory_status)) {
220  ck_assert_uint_eq(memory_status, MEMORY_STATUS_OK);
221  }
222 
223  if (memory_get_size(ptr, &size)) {
224  ck_assert_uint_eq(size, sizeof(*ptr) * 42);
225  }
226 
227  for (i = 0; i < 42; i++) {
228  ck_assert_uint_eq(ptr[i], 1337);
229  }
230 
231  efree(ptr);
232 
233  if (memory_get_status(ptr, &memory_status)) {
234  ck_assert_uint_eq(memory_status, MEMORY_STATUS_FREE);
235  }
236 }
237 END_TEST
238 
239 static Suite *suite(void)
240 {
241  Suite *s = suite_create("memory");
242  TCase *tc_core = tcase_create("Core");
243 
244  tcase_add_unchecked_fixture(tc_core, check_setup, check_teardown);
245  tcase_add_checked_fixture(tc_core, check_test_setup, check_test_teardown);
246 
247  suite_add_tcase(s, tc_core);
248  tcase_add_test(tc_core, test_memory_emalloc);
249  tcase_add_test(tc_core, test_memory_efree);
250  tcase_add_test(tc_core, test_memory_ecalloc);
251  tcase_add_test(tc_core, test_memory_erealloc);
252  tcase_add_test(tc_core, test_memory_reallocz);
253 
254  return s;
255 }
256 
257 void check_server_memory(void)
258 {
259  check_run_suite(suite(), __FILE__);
260 }