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/>.
26 #include "testutils.h"
28 #define VIR_FROM_THIS VIR_FROM_NONE
30 typedef struct testDummyStruct
{
36 testCheckNonNull(void *t
)
39 fprintf(stderr
, "Allocation succeeded but pointer is NULL\n");
47 testAllocScalar(const void *opaque ATTRIBUTE_UNUSED
)
55 if (testCheckNonNull(t
) < 0)
60 fprintf(stderr
, "Allocated ram was not zerod\n");
67 fprintf(stderr
, "Pointer is still set after free\n");
79 testAllocArray(const void *opaque ATTRIBUTE_UNUSED
)
85 if (VIR_ALLOC_N(t
, nt
) < 0)
88 if (testCheckNonNull(t
) < 0)
91 for (i
= 0; i
< nt
; i
++) {
94 fprintf(stderr
, "Allocated ram block %zu was not zerod\n", i
);
102 fprintf(stderr
, "Pointer is still set after free\n");
114 testReallocArray(const void *opaque ATTRIBUTE_UNUSED
)
120 if (VIR_ALLOC_N(t
, nt
) < 0)
123 if (testCheckNonNull(t
) < 0)
126 for (i
= 0; i
< nt
; i
++) {
131 if (VIR_REALLOC_N(t
, nt
+ 5) < 0)
134 if (testCheckNonNull(t
) < 0)
137 for (i
= 0; i
< nt
; i
++) {
140 fprintf(stderr
, "Reallocated ram block %zu lost data\n", i
);
145 if (VIR_REALLOC_N(t
, nt
) < 0)
148 if (testCheckNonNull(t
) < 0)
151 for (i
= 0; i
< nt
; i
++) {
154 fprintf(stderr
, "Reallocated ram block %zu lost data\n", i
);
159 if (VIR_REALLOC_N(t
, nt
- 5) < 0)
162 if (testCheckNonNull(t
) < 0)
165 for (i
= 0; i
< (nt
- 5); i
++) {
168 fprintf(stderr
, "Reallocated ram block %zu lost data\n", i
);
176 fprintf(stderr
, "Pointer is still set after free\n");
188 testExpandArray(const void *opaque ATTRIBUTE_UNUSED
)
194 if (VIR_ALLOC_N(t
, nt
) < 0)
197 if (testCheckNonNull(t
) < 0)
200 for (i
= 0; i
< nt
; i
++) {
205 if (VIR_EXPAND_N(t
, nt
, 5) < 0)
208 if (testCheckNonNull(t
) < 0)
211 for (i
= 0; i
< (nt
- 5); i
++) {
214 fprintf(stderr
, "Reallocated ram block %zu lost data\n", i
);
219 for (i
= (nt
- 5); i
< nt
; i
++) {
222 fprintf(stderr
, "New ram block %zu was not zerod\n", i
);
227 VIR_SHRINK_N(t
, nt
, 5);
229 if (testCheckNonNull(t
) < 0)
232 for (i
= 0; i
< nt
; i
++) {
235 fprintf(stderr
, "Reallocated ram block %zu lost data\n", i
);
240 VIR_SHRINK_N(t
, nt
, 5);
242 if (testCheckNonNull(t
) < 0)
245 for (i
= 0; i
< nt
; i
++) {
248 fprintf(stderr
, "Reallocated ram block %zu lost data\n", i
);
256 fprintf(stderr
, "Pointer is still set after free\n");
268 testResizeArray(const void *opaque ATTRIBUTE_UNUSED
)
271 size_t nt
= 10, at
, i
;
274 if (VIR_ALLOC_N(t
, nt
) < 0)
279 if (testCheckNonNull(t
) < 0)
282 for (i
= 0; i
< nt
; i
++) {
287 if (VIR_RESIZE_N(t
, at
, nt
, 8) < 0)
290 if (testCheckNonNull(t
) < 0)
294 fprintf(stderr
, "Expected allocation of 16 not %zu\n", at
);
298 for (i
= 0; i
< at
; i
++) {
302 fprintf(stderr
, "New ram block %zu was not zerod\n", i
);
308 fprintf(stderr
, "Reallocated ram block %zu lost data\n", i
);
317 fprintf(stderr
, "Pointer is still set after free\n");
329 testInsertArray(const void *opaque ATTRIBUTE_UNUSED
)
334 testDummyStruct
*n
= (void *)0xff;
336 if (VIR_ALLOC_N(t
, nt
) < 0)
339 if (testCheckNonNull(t
) < 0)
342 for (i
= 0; i
< nt
; i
++)
345 if (VIR_INSERT_ELEMENT(t
, 3, nt
, n
) < 0) {
347 fprintf(stderr
, "Expecting array size 10 after OOM not %zu\n", nt
);
354 fprintf(stderr
, "Expecting array size 11 not %zu\n", nt
);
359 fprintf(stderr
, "Expecting element to be set to NULL\n");
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",
375 fprintf(stderr
, "Pointer is still set after free\n");
387 testDispose(const void *opaque ATTRIBUTE_UNUSED
)
395 VIR_DISPOSE_N(nums
, nnums
);
396 VIR_DISPOSE_STRING(str
);
399 VIR_DISPOSE_N(nums
, nnums
);
401 if (VIR_ALLOC(num
) < 0)
407 if (VIR_ALLOC_N(nums
, nnums
) < 0)
410 VIR_DISPOSE_N(nums
, nnums
);
412 if (VIR_STRDUP(str
, "test") < 0)
415 VIR_DISPOSE_STRING(str
);
426 if (virTestRun("alloc scalar", testAllocScalar
, NULL
) < 0)
428 if (virTestRun("alloc array", testAllocArray
, NULL
) < 0)
430 if (virTestRun("realloc array", testReallocArray
, NULL
) < 0)
432 if (virTestRun("expand array", testExpandArray
, NULL
) < 0)
434 if (virTestRun("resize array", testResizeArray
, NULL
) < 0)
436 if (virTestRun("insert array", testInsertArray
, NULL
) < 0)
438 if (virTestRun("dispose tests", testDispose
, NULL
) < 0)
441 return ret
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;
444 VIR_TEST_MAIN(mymain
)