| 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_stdlib_malloc) { |
| |
40 void *test = cxMalloc(cxStdlibAllocator, 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 cxFree(cxStdlibAllocator, test); |
| |
47 } |
| |
48 |
| |
49 CX_TEST(test_allocator_stdlib_calloc) { |
| |
50 char *test = cxCalloc(cxStdlibAllocator, 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 cxFree(cxStdlibAllocator, test); |
| |
58 } |
| |
59 |
| |
60 CX_TEST(test_allocator_stdlib_realloc) { |
| |
61 char *test = cxCalloc(cxStdlibAllocator, 8, 1); |
| |
62 memcpy(test, "Test", 5); |
| |
63 CX_TEST_DO { |
| |
64 test = cxRealloc(cxStdlibAllocator, test, 16); |
| |
65 CX_TEST_ASSERT(test != NULL); |
| |
66 CX_TEST_ASSERT(0 == strcmp(test, "Test")); |
| |
67 } |
| |
68 cxFree(cxStdlibAllocator, test); |
| |
69 } |
| |
70 |
| |
71 CX_TEST(test_allocator_stdlib_reallocarray) { |
| |
72 char *test = cxCalloc(cxStdlibAllocator, 8, 1); |
| |
73 memcpy(test, "Test", 5); |
| |
74 CX_TEST_DO { |
| |
75 test = cxReallocArray(cxStdlibAllocator, test, 16, 2); |
| |
76 CX_TEST_ASSERT(test != NULL); |
| |
77 CX_TEST_ASSERT(0 == strcmp(test, "Test")); |
| |
78 } |
| |
79 cxFree(cxStdlibAllocator, test); |
| |
80 } |
| |
81 |
| |
82 CX_TEST(test_allocator_stdlib_reallocarray_overflow) { |
| |
83 char *test = cxCalloc(cxStdlibAllocator, 8, 1); |
| |
84 memcpy(test, "Test", 5); |
| |
85 CX_TEST_DO { |
| |
86 void *fail = cxReallocArray(cxStdlibAllocator, test, SIZE_MAX/2, 4); |
| |
87 CX_TEST_ASSERT(errno == EOVERFLOW); |
| |
88 CX_TEST_ASSERT(fail == NULL); |
| |
89 CX_TEST_ASSERT(0 == strcmp(test, "Test")); |
| |
90 } |
| |
91 cxFree(cxStdlibAllocator, test); |
| |
92 } |
| |
93 |
| |
94 CX_TEST(test_allocator_stdlib_free) { |
| |
95 void *test = cxMalloc(cxStdlibAllocator, 16); |
| |
96 CX_TEST_DO { |
| |
97 // we cannot assert sth. but valgrind will detect an error |
| |
98 cxFree(cxStdlibAllocator, test); |
| |
99 CX_TEST_ASSERT(true); |
| |
100 } |
| |
101 } |
| |
102 |
| 39 CX_TEST(test_allocator_default_malloc) { |
103 CX_TEST(test_allocator_default_malloc) { |
| 40 void *test = cxMallocDefault(16); |
104 void *test = cxMallocDefault(16); |
| 41 CX_TEST_DO { |
105 CX_TEST_DO { |
| 42 CX_TEST_ASSERT(test != NULL); |
106 CX_TEST_ASSERT(test != NULL); |
| 43 // we cannot assert sth. but valgrind will detect an error |
107 // we cannot assert sth. but valgrind will detect an error |
| 203 } |
267 } |
| 204 |
268 |
| 205 CxTestSuite *cx_test_suite_allocator(void) { |
269 CxTestSuite *cx_test_suite_allocator(void) { |
| 206 CxTestSuite *suite = cx_test_suite_new("allocator"); |
270 CxTestSuite *suite = cx_test_suite_new("allocator"); |
| 207 |
271 |
| |
272 cx_test_register(suite, test_allocator_stdlib_malloc); |
| |
273 cx_test_register(suite, test_allocator_stdlib_calloc); |
| |
274 cx_test_register(suite, test_allocator_stdlib_realloc); |
| |
275 cx_test_register(suite, test_allocator_stdlib_reallocarray); |
| |
276 cx_test_register(suite, test_allocator_stdlib_reallocarray_overflow); |
| |
277 cx_test_register(suite, test_allocator_stdlib_free); |
| 208 cx_test_register(suite, test_allocator_default_malloc); |
278 cx_test_register(suite, test_allocator_default_malloc); |
| 209 cx_test_register(suite, test_allocator_default_calloc); |
279 cx_test_register(suite, test_allocator_default_calloc); |
| 210 cx_test_register(suite, test_allocator_default_realloc); |
280 cx_test_register(suite, test_allocator_default_realloc); |
| 211 cx_test_register(suite, test_allocator_default_reallocarray); |
281 cx_test_register(suite, test_allocator_default_reallocarray); |
| 212 cx_test_register(suite, test_allocator_default_reallocarray_overflow); |
282 cx_test_register(suite, test_allocator_default_reallocarray_overflow); |