backup: Wire up qemu full pull backup commands over QMP
[libvirt/ericb.git] / tests / viralloctest.c
blob27cbdcbbe46fa8164a2fe6e1e877946a8894a4c2
1 /*
2 * viralloctest.c: Test memory allocation APIs
4 * Copyright (C) 2014 Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library. If not, see
18 * <http://www.gnu.org/licenses/>.
22 #include <config.h>
24 #include <viralloc.h>
26 #include "testutils.h"
28 #define VIR_FROM_THIS VIR_FROM_NONE
30 typedef struct testDummyStruct {
31 int a;
32 int b;
33 } testDummyStruct;
35 static int
36 testCheckNonNull(void *t)
38 if (t == NULL) {
39 fprintf(stderr, "Allocation succeeded but pointer is NULL\n");
40 return -1;
43 return 0;
46 static int
47 testAllocScalar(const void *opaque ATTRIBUTE_UNUSED)
49 testDummyStruct *t;
50 int ret = -1;
52 if (VIR_ALLOC(t) < 0)
53 return -1;
55 if (testCheckNonNull(t) < 0)
56 goto cleanup;
58 if (t->a != 0 ||
59 t->b != 0) {
60 fprintf(stderr, "Allocated ram was not zerod\n");
61 goto cleanup;
64 VIR_FREE(t);
66 if (t != NULL) {
67 fprintf(stderr, "Pointer is still set after free\n");
68 goto cleanup;
71 ret = 0;
72 cleanup:
73 VIR_FREE(t);
74 return ret;
78 static int
79 testAllocArray(const void *opaque ATTRIBUTE_UNUSED)
81 testDummyStruct *t;
82 size_t nt = 10, i;
83 int ret = -1;
85 if (VIR_ALLOC_N(t, nt) < 0)
86 return -1;
88 if (testCheckNonNull(t) < 0)
89 goto cleanup;
91 for (i = 0; i < nt; i++) {
92 if (t[i].a != 0 ||
93 t[i].b != 0) {
94 fprintf(stderr, "Allocated ram block %zu was not zerod\n", i);
95 goto cleanup;
99 VIR_FREE(t);
101 if (t != NULL) {
102 fprintf(stderr, "Pointer is still set after free\n");
103 goto cleanup;
106 ret = 0;
107 cleanup:
108 VIR_FREE(t);
109 return ret;
113 static int
114 testReallocArray(const void *opaque ATTRIBUTE_UNUSED)
116 testDummyStruct *t;
117 size_t nt = 10, i;
118 int ret = -1;
120 if (VIR_ALLOC_N(t, nt) < 0)
121 return -1;
123 if (testCheckNonNull(t) < 0)
124 goto cleanup;
126 for (i = 0; i < nt; i++) {
127 t[i].a = 10;
128 t[i].b = 20;
131 if (VIR_REALLOC_N(t, nt + 5) < 0)
132 goto cleanup;
134 if (testCheckNonNull(t) < 0)
135 goto cleanup;
137 for (i = 0; i < nt; i++) {
138 if (t[i].a != 10 ||
139 t[i].b != 20) {
140 fprintf(stderr, "Reallocated ram block %zu lost data\n", i);
141 goto cleanup;
145 if (VIR_REALLOC_N(t, nt) < 0)
146 goto cleanup;
148 if (testCheckNonNull(t) < 0)
149 goto cleanup;
151 for (i = 0; i < nt; i++) {
152 if (t[i].a != 10 ||
153 t[i].b != 20) {
154 fprintf(stderr, "Reallocated ram block %zu lost data\n", i);
155 goto cleanup;
159 if (VIR_REALLOC_N(t, nt - 5) < 0)
160 goto cleanup;
162 if (testCheckNonNull(t) < 0)
163 goto cleanup;
165 for (i = 0; i < (nt - 5); i++) {
166 if (t[i].a != 10 ||
167 t[i].b != 20) {
168 fprintf(stderr, "Reallocated ram block %zu lost data\n", i);
169 goto cleanup;
173 VIR_FREE(t);
175 if (t != NULL) {
176 fprintf(stderr, "Pointer is still set after free\n");
177 goto cleanup;
180 ret = 0;
181 cleanup:
182 VIR_FREE(t);
183 return ret;
187 static int
188 testExpandArray(const void *opaque ATTRIBUTE_UNUSED)
190 testDummyStruct *t;
191 size_t nt = 10, i;
192 int ret = -1;
194 if (VIR_ALLOC_N(t, nt) < 0)
195 return -1;
197 if (testCheckNonNull(t) < 0)
198 goto cleanup;
200 for (i = 0; i < nt; i++) {
201 t[i].a = 10;
202 t[i].b = 20;
205 if (VIR_EXPAND_N(t, nt, 5) < 0)
206 goto cleanup;
208 if (testCheckNonNull(t) < 0)
209 goto cleanup;
211 for (i = 0; i < (nt - 5); i++) {
212 if (t[i].a != 10 ||
213 t[i].b != 20) {
214 fprintf(stderr, "Reallocated ram block %zu lost data\n", i);
215 goto cleanup;
219 for (i = (nt - 5); i < nt; i++) {
220 if (t[i].a != 0 ||
221 t[i].b != 0) {
222 fprintf(stderr, "New ram block %zu was not zerod\n", i);
223 goto cleanup;
227 VIR_SHRINK_N(t, nt, 5);
229 if (testCheckNonNull(t) < 0)
230 goto cleanup;
232 for (i = 0; i < nt; i++) {
233 if (t[i].a != 10 ||
234 t[i].b != 20) {
235 fprintf(stderr, "Reallocated ram block %zu lost data\n", i);
236 goto cleanup;
240 VIR_SHRINK_N(t, nt, 5);
242 if (testCheckNonNull(t) < 0)
243 goto cleanup;
245 for (i = 0; i < nt; i++) {
246 if (t[i].a != 10 ||
247 t[i].b != 20) {
248 fprintf(stderr, "Reallocated ram block %zu lost data\n", i);
249 goto cleanup;
253 VIR_FREE(t);
255 if (t != NULL) {
256 fprintf(stderr, "Pointer is still set after free\n");
257 goto cleanup;
260 ret = 0;
261 cleanup:
262 VIR_FREE(t);
263 return ret;
267 static int
268 testResizeArray(const void *opaque ATTRIBUTE_UNUSED)
270 testDummyStruct *t;
271 size_t nt = 10, at, i;
272 int ret = -1;
274 if (VIR_ALLOC_N(t, nt) < 0)
275 return -1;
277 at = nt;
279 if (testCheckNonNull(t) < 0)
280 goto cleanup;
282 for (i = 0; i < nt; i++) {
283 t[i].a = 10;
284 t[i].b = 20;
287 if (VIR_RESIZE_N(t, at, nt, 8) < 0)
288 goto cleanup;
290 if (testCheckNonNull(t) < 0)
291 goto cleanup;
293 if (at != 18) {
294 fprintf(stderr, "Expected allocation of 16 not %zu\n", at);
295 goto cleanup;
298 for (i = 0; i < at; i++) {
299 if (i >= nt) {
300 if (t[i].a != 0 ||
301 t[i].b != 0) {
302 fprintf(stderr, "New ram block %zu was not zerod\n", i);
303 goto cleanup;
305 } else {
306 if (t[i].a != 10 ||
307 t[i].b != 20) {
308 fprintf(stderr, "Reallocated ram block %zu lost data\n", i);
309 goto cleanup;
314 VIR_FREE(t);
316 if (t != NULL) {
317 fprintf(stderr, "Pointer is still set after free\n");
318 goto cleanup;
321 ret = 0;
322 cleanup:
323 VIR_FREE(t);
324 return ret;
328 static int
329 testInsertArray(const void *opaque ATTRIBUTE_UNUSED)
331 testDummyStruct **t;
332 size_t nt = 10, i;
333 int ret = -1;
334 testDummyStruct *n = (void *)0xff;
336 if (VIR_ALLOC_N(t, nt) < 0)
337 return -1;
339 if (testCheckNonNull(t) < 0)
340 goto cleanup;
342 for (i = 0; i < nt; i++)
343 t[i] = (void*)0x50;
345 if (VIR_INSERT_ELEMENT(t, 3, nt, n) < 0) {
346 if (nt != 10) {
347 fprintf(stderr, "Expecting array size 10 after OOM not %zu\n", nt);
348 goto cleanup;
350 goto cleanup;
353 if (nt != 11) {
354 fprintf(stderr, "Expecting array size 11 not %zu\n", nt);
355 goto cleanup;
358 if (n != NULL) {
359 fprintf(stderr, "Expecting element to be set to NULL\n");
360 goto cleanup;
363 for (i = 0; i < nt; i++) {
364 void *expect = i == 3 ? (void *)0xff : (void*)0x50;
365 if (t[i] != expect) {
366 fprintf(stderr, "Expecting %p at offset %zu not %p\n",
367 expect, i, t[i]);
368 goto cleanup;
372 VIR_FREE(t);
374 if (t != NULL) {
375 fprintf(stderr, "Pointer is still set after free\n");
376 goto cleanup;
379 ret = 0;
380 cleanup:
381 VIR_FREE(t);
382 return ret;
386 static int
387 testDispose(const void *opaque ATTRIBUTE_UNUSED)
389 int *num = NULL;
390 int *nums = NULL;
391 size_t nnums = 0;
392 char *str = NULL;
394 VIR_DISPOSE(num);
395 VIR_DISPOSE_N(nums, nnums);
396 VIR_DISPOSE_STRING(str);
398 nnums = 10;
399 VIR_DISPOSE_N(nums, nnums);
401 if (VIR_ALLOC(num) < 0)
402 return -1;
404 VIR_DISPOSE(num);
406 nnums = 10;
407 if (VIR_ALLOC_N(nums, nnums) < 0)
408 return -1;
410 VIR_DISPOSE_N(nums, nnums);
412 if (VIR_STRDUP(str, "test") < 0)
413 return -1;
415 VIR_DISPOSE_STRING(str);
417 return 0;
421 static int
422 mymain(void)
424 int ret = 0;
426 if (virTestRun("alloc scalar", testAllocScalar, NULL) < 0)
427 ret = -1;
428 if (virTestRun("alloc array", testAllocArray, NULL) < 0)
429 ret = -1;
430 if (virTestRun("realloc array", testReallocArray, NULL) < 0)
431 ret = -1;
432 if (virTestRun("expand array", testExpandArray, NULL) < 0)
433 ret = -1;
434 if (virTestRun("resize array", testResizeArray, NULL) < 0)
435 ret = -1;
436 if (virTestRun("insert array", testInsertArray, NULL) < 0)
437 ret = -1;
438 if (virTestRun("dispose tests", testDispose, NULL) < 0)
439 ret = -1;
441 return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
444 VIR_TEST_MAIN(mymain)