tests/test_allocator.c

changeset 1319
aa1f580f8f59
parent 1318
12fa1d37fe48
equal deleted inserted replaced
1318:12fa1d37fe48 1319:aa1f580f8f59
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_malloc) { 39 CX_TEST(test_allocator_default_malloc) {
40 void *test = cxMalloc(cxDefaultAllocator, 16); 40 void *test = cxMallocDefault(16);
41 CX_TEST_DO { 41 CX_TEST_DO {
42 CX_TEST_ASSERT(test != NULL); 42 CX_TEST_ASSERT(test != NULL);
43 // we cannot assert sth. but valgrind will detect an error 43 // we cannot assert sth. but valgrind will detect an error
44 memcpy(test, "0123456789ABCDEF", 16); 44 memcpy(test, "0123456789ABCDEF", 16);
45 } 45 }
46 cxFree(cxDefaultAllocator, test); 46 cxFreeDefault(test);
47 } 47 }
48 48
49 CX_TEST(test_allocator_default_calloc) { 49 CX_TEST(test_allocator_default_calloc) {
50 char *test = cxCalloc(cxDefaultAllocator, 8, 2); 50 char *test = cxCallocDefault(8, 2);
51 CX_TEST_DO { 51 CX_TEST_DO {
52 CX_TEST_ASSERT(test != NULL); 52 CX_TEST_ASSERT(test != NULL);
53 for (int i = 0; i < 16; i++) { 53 for (int i = 0; i < 16; i++) {
54 CX_TEST_ASSERT(test[i] == 0); 54 CX_TEST_ASSERT(test[i] == 0);
55 } 55 }
56 } 56 }
57 cxFree(cxDefaultAllocator, test); 57 cxFreeDefault(test);
58 } 58 }
59 59
60 CX_TEST(test_allocator_default_realloc) { 60 CX_TEST(test_allocator_default_realloc) {
61 char *test = cxCalloc(cxDefaultAllocator, 8, 1); 61 char *test = cxCallocDefault(8, 1);
62 memcpy(test, "Test", 5); 62 memcpy(test, "Test", 5);
63 CX_TEST_DO { 63 CX_TEST_DO {
64 test = cxRealloc(cxDefaultAllocator, test, 16); 64 test = cxReallocDefault(test, 16);
65 CX_TEST_ASSERT(test != NULL); 65 CX_TEST_ASSERT(test != NULL);
66 CX_TEST_ASSERT(0 == strcmp(test, "Test")); 66 CX_TEST_ASSERT(0 == strcmp(test, "Test"));
67 } 67 }
68 cxFree(cxDefaultAllocator, test); 68 cxFreeDefault(test);
69 } 69 }
70 70
71 CX_TEST(test_allocator_default_reallocarray) { 71 CX_TEST(test_allocator_default_reallocarray) {
72 char *test = cxCalloc(cxDefaultAllocator, 8, 1); 72 char *test = cxCallocDefault(8, 1);
73 memcpy(test, "Test", 5); 73 memcpy(test, "Test", 5);
74 CX_TEST_DO { 74 CX_TEST_DO {
75 test = cxReallocArray(cxDefaultAllocator, test, 16, 2); 75 test = cxReallocArrayDefault(test, 16, 2);
76 CX_TEST_ASSERT(test != NULL); 76 CX_TEST_ASSERT(test != NULL);
77 CX_TEST_ASSERT(0 == strcmp(test, "Test")); 77 CX_TEST_ASSERT(0 == strcmp(test, "Test"));
78 } 78 }
79 cxFree(cxDefaultAllocator, test); 79 cxFreeDefault(test);
80 } 80 }
81 81
82 CX_TEST(test_allocator_default_reallocarray_overflow) { 82 CX_TEST(test_allocator_default_reallocarray_overflow) {
83 char *test = cxCalloc(cxDefaultAllocator, 8, 1); 83 char *test = cxCallocDefault(8, 1);
84 memcpy(test, "Test", 5); 84 memcpy(test, "Test", 5);
85 CX_TEST_DO { 85 CX_TEST_DO {
86 void *fail = cxReallocArray(cxDefaultAllocator, test, SIZE_MAX/2, 4); 86 void *fail = cxReallocArrayDefault(test, SIZE_MAX/2, 4);
87 CX_TEST_ASSERT(errno == EOVERFLOW); 87 CX_TEST_ASSERT(errno == EOVERFLOW);
88 CX_TEST_ASSERT(fail == NULL); 88 CX_TEST_ASSERT(fail == NULL);
89 CX_TEST_ASSERT(0 == strcmp(test, "Test")); 89 CX_TEST_ASSERT(0 == strcmp(test, "Test"));
90 } 90 }
91 cxFree(cxDefaultAllocator, test); 91 cxFreeDefault(test);
92 } 92 }
93 93
94 CX_TEST(test_allocator_default_free) { 94 CX_TEST(test_allocator_default_free) {
95 void *test = cxMalloc(cxDefaultAllocator, 16); 95 void *test = cxMallocDefault(16);
96 CX_TEST_DO { 96 CX_TEST_DO {
97 // we cannot assert sth. but valgrind will detect an error 97 // we cannot assert sth. but valgrind will detect an error
98 cxFree(cxDefaultAllocator, test); 98 cxFreeDefault(test);
99 CX_TEST_ASSERT(true); 99 CX_TEST_ASSERT(true);
100 } 100 }
101 } 101 }
102 102
103 CX_TEST(test_allocator_reallocate) { 103 CX_TEST(test_allocator_reallocate) {
104 char *test = cxCalloc(cxDefaultAllocator, 8, 1); 104 char *test = cxCallocDefault(8, 1);
105 memcpy(test, "Test", 5); 105 memcpy(test, "Test", 5);
106 CX_TEST_DO { 106 CX_TEST_DO {
107 int ret = cxReallocate(cxDefaultAllocator, &test, 16); 107 int ret = cxReallocateDefault(&test, 16);
108 CX_TEST_ASSERT(ret == 0); 108 CX_TEST_ASSERT(ret == 0);
109 CX_TEST_ASSERT(test != NULL); 109 CX_TEST_ASSERT(test != NULL);
110 CX_TEST_ASSERT(0 == strcmp(test, "Test")); 110 CX_TEST_ASSERT(0 == strcmp(test, "Test"));
111 } 111 }
112 cxFree(cxDefaultAllocator, test); 112 cxFreeDefault(test);
113 } 113 }
114 114
115 CX_TEST(test_allocator_reallocate_low_level) { 115 CX_TEST(test_allocator_reallocate_low_level) {
116 void *test = calloc(8, 1); 116 void *test = calloc(8, 1);
117 memcpy(test, "Test", 5); 117 memcpy(test, "Test", 5);
123 } 123 }
124 free(test); 124 free(test);
125 } 125 }
126 126
127 CX_TEST(test_allocator_reallocatearray) { 127 CX_TEST(test_allocator_reallocatearray) {
128 char *test = cxCalloc(cxDefaultAllocator, 8, 1); 128 char *test = cxCallocDefault(8, 1);
129 memcpy(test, "Test", 5); 129 memcpy(test, "Test", 5);
130 CX_TEST_DO { 130 CX_TEST_DO {
131 int ret = cxReallocateArray(cxDefaultAllocator, &test, 16, 2); 131 int ret = cxReallocateArrayDefault(&test, 16, 2);
132 CX_TEST_ASSERT(ret == 0); 132 CX_TEST_ASSERT(ret == 0);
133 CX_TEST_ASSERT(test != NULL); 133 CX_TEST_ASSERT(test != NULL);
134 CX_TEST_ASSERT(0 == strcmp(test, "Test")); 134 CX_TEST_ASSERT(0 == strcmp(test, "Test"));
135 } 135 }
136 cxFree(cxDefaultAllocator, test); 136 cxFreeDefault(test);
137 } 137 }
138 138
139 CX_TEST(test_allocator_reallocatearray_overflow) { 139 CX_TEST(test_allocator_reallocatearray_overflow) {
140 char *test = cxCalloc(cxDefaultAllocator, 8, 1); 140 char *test = cxCallocDefault(8, 1);
141 memcpy(test, "Test", 5); 141 memcpy(test, "Test", 5);
142 CX_TEST_DO { 142 CX_TEST_DO {
143 int ret = cxReallocateArray(cxDefaultAllocator, &test, SIZE_MAX/2, 4); 143 int ret = cxReallocateArrayDefault(&test, SIZE_MAX/2, 4);
144 CX_TEST_ASSERT(ret != 0); 144 CX_TEST_ASSERT(ret != 0);
145 CX_TEST_ASSERT(errno == EOVERFLOW); 145 CX_TEST_ASSERT(errno == EOVERFLOW);
146 CX_TEST_ASSERT(test != NULL); 146 CX_TEST_ASSERT(test != NULL);
147 CX_TEST_ASSERT(0 == strcmp(test, "Test")); 147 CX_TEST_ASSERT(0 == strcmp(test, "Test"));
148 } 148 }
149 cxFree(cxDefaultAllocator, test); 149 cxFreeDefault(test);
150 } 150 }
151 151
152 CX_TEST(test_allocator_reallocatearray_low_level) { 152 CX_TEST(test_allocator_reallocatearray_low_level) {
153 char *test = calloc(8, 1); 153 char *test = calloc(8, 1);
154 memcpy(test, "Test", 5); 154 memcpy(test, "Test", 5);

mercurial