tests/test_allocator.c

changeset 782
74d777455e96
equal deleted inserted replaced
781:a786b0a89b37 782:74d777455e96
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2023 Mike Becker, Olaf Wintermann All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "cx/test.h"
30
31 #include "cx/allocator.h"
32
33 CX_TEST(test_allocator_default) {
34 CX_TEST_DO {
35 CX_TEST_ASSERT(cxDefaultAllocator->cl != NULL);
36 }
37 }
38
39 CX_TEST(test_allocator_default_malloc) {
40 void *test = cxMalloc(cxDefaultAllocator, 16);
41 CX_TEST_DO {
42 CX_TEST_ASSERT(test != NULL);
43 // we cannot assert sth. but valgrind will detect an error
44 memcpy(test, "0123456789ABCDEF", 16);
45 }
46 free(test);
47 }
48
49 CX_TEST(test_allocator_default_calloc) {
50 char *test = cxCalloc(cxDefaultAllocator, 8, 2);
51 CX_TEST_DO {
52 CX_TEST_ASSERT(test != NULL);
53 for (int i = 0; i < 16; i++) {
54 CX_TEST_ASSERT(test[i] == 0);
55 }
56 }
57 free(test);
58 }
59
60 CX_TEST(test_allocator_default_realloc) {
61 char *test = calloc(8, 1);
62 memcpy(test, "Test", 5);
63 CX_TEST_DO {
64 test = cxRealloc(cxDefaultAllocator, test, 16);
65 CX_TEST_ASSERT(test != NULL);
66 CX_TEST_ASSERT(0 == strcmp(test, "Test"));
67 }
68 free(test);
69 }
70
71 CX_TEST(test_allocator_default_free) {
72 void *test = malloc(16);
73 CX_TEST_DO {
74 // we cannot assert sth. but valgrind will detect an error
75 cxFree(cxDefaultAllocator, test);
76 CX_TEST_ASSERT(true);
77 }
78 }
79
80 CX_TEST(test_allocator_reallocate) {
81 void *test = calloc(8, 1);
82 memcpy(test, "Test", 5);
83 CX_TEST_DO {
84 int ret = cxReallocate(cxDefaultAllocator, &test, 16);
85 CX_TEST_ASSERT(ret == 0);
86 CX_TEST_ASSERT(test != NULL);
87 CX_TEST_ASSERT(0 == strcmp(test, "Test"));
88 }
89 free(test);
90 }
91
92 CX_TEST(test_allocator_reallocate_low_level) {
93 void *test = calloc(8, 1);
94 memcpy(test, "Test", 5);
95 CX_TEST_DO {
96 int ret = cx_reallocate(&test, 16);
97 CX_TEST_ASSERT(ret == 0);
98 CX_TEST_ASSERT(test != NULL);
99 CX_TEST_ASSERT(0 == strcmp(test, "Test"));
100 }
101 free(test);
102 }
103
104 static void *test_allocator_mock_failing_realloc(
105 __attribute__((__unused__))void *p,
106 __attribute__((__unused__))void *d,
107 __attribute__((__unused__))size_t n
108 ) {
109 return NULL;
110 }
111
112 CX_TEST(test_allocator_reallocate_fails) {
113 // Mock an allocator that always returns NULL on realloc
114 cx_allocator_class mock_cl;
115 mock_cl.realloc = test_allocator_mock_failing_realloc;
116 CxAllocator mock = {&mock_cl, NULL};
117
118 void *test = calloc(8, 1);
119 memcpy(test, "Test", 5);
120 void *original = test;
121 CX_TEST_DO {
122 int ret = cxReallocate(&mock, &test, 16);
123 // non-zero return code because of the failure
124 CX_TEST_ASSERT(ret != 0);
125 // the test pointer was not changed and still points to the same memory
126 CX_TEST_ASSERT(test == original);
127 CX_TEST_ASSERT(0 == strcmp(test, "Test"));
128 }
129 free(test);
130 }
131
132 CxTestSuite *cx_test_suite_allocator(void) {
133 CxTestSuite *suite = cx_test_suite_new("allocator");
134
135 cx_test_register(suite, test_allocator_default);
136 cx_test_register(suite, test_allocator_default_malloc);
137 cx_test_register(suite, test_allocator_default_calloc);
138 cx_test_register(suite, test_allocator_default_realloc);
139 cx_test_register(suite, test_allocator_default_free);
140 cx_test_register(suite, test_allocator_reallocate);
141 cx_test_register(suite, test_allocator_reallocate_fails);
142 cx_test_register(suite, test_allocator_reallocate_low_level);
143
144 return suite;
145 }

mercurial