34 #endif |
34 #endif |
35 |
35 |
36 #include "cx/allocator.h" |
36 #include "cx/allocator.h" |
37 #include <errno.h> |
37 #include <errno.h> |
38 |
38 |
39 CX_TEST(test_allocator_default) { |
|
40 CX_TEST_DO { |
|
41 CX_TEST_ASSERT(cxDefaultAllocator->cl != NULL); |
|
42 } |
|
43 } |
|
44 |
|
45 CX_TEST(test_allocator_default_malloc) { |
39 CX_TEST(test_allocator_default_malloc) { |
46 void *test = cxMalloc(cxDefaultAllocator, 16); |
40 void *test = cxMalloc(cxDefaultAllocator, 16); |
47 CX_TEST_DO { |
41 CX_TEST_DO { |
48 CX_TEST_ASSERT(test != NULL); |
42 CX_TEST_ASSERT(test != NULL); |
49 // we cannot assert sth. but valgrind will detect an error |
43 // we cannot assert sth. but valgrind will detect an error |
50 memcpy(test, "0123456789ABCDEF", 16); |
44 memcpy(test, "0123456789ABCDEF", 16); |
51 } |
45 } |
52 free(test); |
46 cxFree(cxDefaultAllocator, test); |
53 } |
47 } |
54 |
48 |
55 CX_TEST(test_allocator_default_calloc) { |
49 CX_TEST(test_allocator_default_calloc) { |
56 char *test = cxCalloc(cxDefaultAllocator, 8, 2); |
50 char *test = cxCalloc(cxDefaultAllocator, 8, 2); |
57 CX_TEST_DO { |
51 CX_TEST_DO { |
58 CX_TEST_ASSERT(test != NULL); |
52 CX_TEST_ASSERT(test != NULL); |
59 for (int i = 0; i < 16; i++) { |
53 for (int i = 0; i < 16; i++) { |
60 CX_TEST_ASSERT(test[i] == 0); |
54 CX_TEST_ASSERT(test[i] == 0); |
61 } |
55 } |
62 } |
56 } |
63 free(test); |
57 cxFree(cxDefaultAllocator, test); |
64 } |
58 } |
65 |
59 |
66 CX_TEST(test_allocator_default_realloc) { |
60 CX_TEST(test_allocator_default_realloc) { |
67 char *test = calloc(8, 1); |
61 char *test = cxCalloc(cxDefaultAllocator, 8, 1); |
68 memcpy(test, "Test", 5); |
62 memcpy(test, "Test", 5); |
69 CX_TEST_DO { |
63 CX_TEST_DO { |
70 test = cxRealloc(cxDefaultAllocator, test, 16); |
64 test = cxRealloc(cxDefaultAllocator, test, 16); |
71 CX_TEST_ASSERT(test != NULL); |
65 CX_TEST_ASSERT(test != NULL); |
72 CX_TEST_ASSERT(0 == strcmp(test, "Test")); |
66 CX_TEST_ASSERT(0 == strcmp(test, "Test")); |
73 } |
67 } |
74 free(test); |
68 cxFree(cxDefaultAllocator, test); |
75 } |
69 } |
76 |
70 |
77 CX_TEST(test_allocator_default_reallocarray) { |
71 CX_TEST(test_allocator_default_reallocarray) { |
78 char *test = calloc(8, 1); |
72 char *test = cxCalloc(cxDefaultAllocator, 8, 1); |
79 memcpy(test, "Test", 5); |
73 memcpy(test, "Test", 5); |
80 CX_TEST_DO { |
74 CX_TEST_DO { |
81 test = cxReallocArray(cxDefaultAllocator, test, 16, 2); |
75 test = cxReallocArray(cxDefaultAllocator, test, 16, 2); |
82 CX_TEST_ASSERT(test != NULL); |
76 CX_TEST_ASSERT(test != NULL); |
83 CX_TEST_ASSERT(0 == strcmp(test, "Test")); |
77 CX_TEST_ASSERT(0 == strcmp(test, "Test")); |
84 } |
78 } |
85 free(test); |
79 cxFree(cxDefaultAllocator, test); |
86 } |
80 } |
87 |
81 |
88 CX_TEST(test_allocator_default_reallocarray_overflow) { |
82 CX_TEST(test_allocator_default_reallocarray_overflow) { |
89 char *test = calloc(8, 1); |
83 char *test = cxCalloc(cxDefaultAllocator, 8, 1); |
90 memcpy(test, "Test", 5); |
84 memcpy(test, "Test", 5); |
91 CX_TEST_DO { |
85 CX_TEST_DO { |
92 void *fail = cxReallocArray(cxDefaultAllocator, test, SIZE_MAX/2, 4); |
86 void *fail = cxReallocArray(cxDefaultAllocator, test, SIZE_MAX/2, 4); |
93 CX_TEST_ASSERT(errno == EOVERFLOW); |
87 CX_TEST_ASSERT(errno == EOVERFLOW); |
94 CX_TEST_ASSERT(fail == NULL); |
88 CX_TEST_ASSERT(fail == NULL); |
95 CX_TEST_ASSERT(0 == strcmp(test, "Test")); |
89 CX_TEST_ASSERT(0 == strcmp(test, "Test")); |
96 } |
90 } |
97 free(test); |
91 cxFree(cxDefaultAllocator, test); |
98 } |
92 } |
99 |
93 |
100 CX_TEST(test_allocator_default_free) { |
94 CX_TEST(test_allocator_default_free) { |
101 void *test = cxMalloc(cxDefaultAllocator, 16); |
95 void *test = cxMalloc(cxDefaultAllocator, 16); |
102 CX_TEST_DO { |
96 CX_TEST_DO { |
105 CX_TEST_ASSERT(true); |
99 CX_TEST_ASSERT(true); |
106 } |
100 } |
107 } |
101 } |
108 |
102 |
109 CX_TEST(test_allocator_reallocate) { |
103 CX_TEST(test_allocator_reallocate) { |
110 char *test = calloc(8, 1); |
104 char *test = cxCalloc(cxDefaultAllocator, 8, 1); |
111 memcpy(test, "Test", 5); |
105 memcpy(test, "Test", 5); |
112 CX_TEST_DO { |
106 CX_TEST_DO { |
113 int ret = cxReallocate(cxDefaultAllocator, &test, 16); |
107 int ret = cxReallocate(cxDefaultAllocator, &test, 16); |
114 CX_TEST_ASSERT(ret == 0); |
108 CX_TEST_ASSERT(ret == 0); |
115 CX_TEST_ASSERT(test != NULL); |
109 CX_TEST_ASSERT(test != NULL); |
116 CX_TEST_ASSERT(0 == strcmp(test, "Test")); |
110 CX_TEST_ASSERT(0 == strcmp(test, "Test")); |
117 } |
111 } |
118 free(test); |
112 cxFree(cxDefaultAllocator, test); |
119 } |
113 } |
120 |
114 |
121 CX_TEST(test_allocator_reallocate_low_level) { |
115 CX_TEST(test_allocator_reallocate_low_level) { |
122 void *test = calloc(8, 1); |
116 void *test = calloc(8, 1); |
123 memcpy(test, "Test", 5); |
117 memcpy(test, "Test", 5); |
129 } |
123 } |
130 free(test); |
124 free(test); |
131 } |
125 } |
132 |
126 |
133 CX_TEST(test_allocator_reallocatearray) { |
127 CX_TEST(test_allocator_reallocatearray) { |
134 char *test = calloc(8, 1); |
128 char *test = cxCalloc(cxDefaultAllocator, 8, 1); |
135 memcpy(test, "Test", 5); |
129 memcpy(test, "Test", 5); |
136 CX_TEST_DO { |
130 CX_TEST_DO { |
137 int ret = cxReallocateArray(cxDefaultAllocator, &test, 16, 2); |
131 int ret = cxReallocateArray(cxDefaultAllocator, &test, 16, 2); |
138 CX_TEST_ASSERT(ret == 0); |
132 CX_TEST_ASSERT(ret == 0); |
139 CX_TEST_ASSERT(test != NULL); |
133 CX_TEST_ASSERT(test != NULL); |
140 CX_TEST_ASSERT(0 == strcmp(test, "Test")); |
134 CX_TEST_ASSERT(0 == strcmp(test, "Test")); |
141 } |
135 } |
142 free(test); |
136 cxFree(cxDefaultAllocator, test); |
143 } |
137 } |
144 |
138 |
145 CX_TEST(test_allocator_reallocatearray_overflow) { |
139 CX_TEST(test_allocator_reallocatearray_overflow) { |
146 char *test = calloc(8, 1); |
140 char *test = cxCalloc(cxDefaultAllocator, 8, 1); |
147 memcpy(test, "Test", 5); |
141 memcpy(test, "Test", 5); |
148 CX_TEST_DO { |
142 CX_TEST_DO { |
149 int ret = cxReallocateArray(cxDefaultAllocator, &test, SIZE_MAX/2, 4); |
143 int ret = cxReallocateArray(cxDefaultAllocator, &test, SIZE_MAX/2, 4); |
150 CX_TEST_ASSERT(ret != 0); |
144 CX_TEST_ASSERT(ret != 0); |
151 CX_TEST_ASSERT(errno == EOVERFLOW); |
145 CX_TEST_ASSERT(errno == EOVERFLOW); |
152 CX_TEST_ASSERT(test != NULL); |
146 CX_TEST_ASSERT(test != NULL); |
153 CX_TEST_ASSERT(0 == strcmp(test, "Test")); |
147 CX_TEST_ASSERT(0 == strcmp(test, "Test")); |
154 } |
148 } |
155 free(test); |
149 cxFree(cxDefaultAllocator, test); |
156 } |
150 } |
157 |
151 |
158 CX_TEST(test_allocator_reallocatearray_low_level) { |
152 CX_TEST(test_allocator_reallocatearray_low_level) { |
159 char *test = calloc(8, 1); |
153 char *test = calloc(8, 1); |
160 memcpy(test, "Test", 5); |
154 memcpy(test, "Test", 5); |
209 } |
203 } |
210 |
204 |
211 CxTestSuite *cx_test_suite_allocator(void) { |
205 CxTestSuite *cx_test_suite_allocator(void) { |
212 CxTestSuite *suite = cx_test_suite_new("allocator"); |
206 CxTestSuite *suite = cx_test_suite_new("allocator"); |
213 |
207 |
214 cx_test_register(suite, test_allocator_default); |
|
215 cx_test_register(suite, test_allocator_default_malloc); |
208 cx_test_register(suite, test_allocator_default_malloc); |
216 cx_test_register(suite, test_allocator_default_calloc); |
209 cx_test_register(suite, test_allocator_default_calloc); |
217 cx_test_register(suite, test_allocator_default_realloc); |
210 cx_test_register(suite, test_allocator_default_realloc); |
218 cx_test_register(suite, test_allocator_default_reallocarray); |
211 cx_test_register(suite, test_allocator_default_reallocarray); |
219 cx_test_register(suite, test_allocator_default_reallocarray_overflow); |
212 cx_test_register(suite, test_allocator_default_reallocarray_overflow); |