27 */ |
27 */ |
28 |
28 |
29 #include "array_tests.h" |
29 #include "array_tests.h" |
30 #include <ucx/utils.h> |
30 #include <ucx/utils.h> |
31 |
31 |
32 UCX_TEST(test_ucx_array_free) { |
32 UCX_TEST(test_ucx_array_destroy) { |
33 UcxArray array = ucx_array_new(16, sizeof(int)); |
33 UcxArray array; |
|
34 ucx_array_init(&array, 16, sizeof(int)); |
34 |
35 |
35 UCX_TEST_BEGIN |
36 UCX_TEST_BEGIN |
36 ucx_array_destroy(&array); |
37 ucx_array_destroy(&array); |
37 UCX_TEST_ASSERT(array.data == NULL, "data pointer not NULL after free"); |
38 UCX_TEST_ASSERT(array.data == NULL, "data pointer not NULL after destroy"); |
38 UCX_TEST_ASSERT(array.size == 0, "size not zero after free"); |
39 UCX_TEST_ASSERT(array.size == 0, "size not zero after destroy"); |
39 UCX_TEST_ASSERT(array.capacity == 0, "capacity not zero after free"); |
40 UCX_TEST_ASSERT(array.capacity == 0, "capacity not zero after destroy"); |
40 UCX_TEST_ASSERT(array.allocator == ucx_default_allocator(), |
41 UCX_TEST_ASSERT(array.allocator == ucx_default_allocator(), |
41 "allocator corrupted during free"); |
42 "allocator corrupted during destroy"); |
42 UCX_TEST_END |
43 UCX_TEST_END |
43 } |
44 } |
44 |
45 |
45 UCX_TEST(test_ucx_array_new) { |
46 UCX_TEST(test_ucx_array_new) { |
46 UcxArray array = ucx_array_new(16, 47); |
47 UcxArray* array = ucx_array_new(16, 47); |
47 |
48 |
48 UCX_TEST_BEGIN |
49 UCX_TEST_BEGIN |
49 UCX_TEST_ASSERT(array.data, "no memory allocated"); |
50 UCX_TEST_ASSERT(array->data, "no memory allocated"); |
50 UCX_TEST_ASSERT(array.size == 0, "size not initially zero"); |
51 UCX_TEST_ASSERT(array->size == 0, "size not initially zero"); |
51 UCX_TEST_ASSERT(array.capacity == 16, "capacity not as requested"); |
52 UCX_TEST_ASSERT(array->capacity == 16, "capacity not as requested"); |
52 UCX_TEST_ASSERT(array.elemsize == 47, "element size not as requested"); |
53 UCX_TEST_ASSERT(array->elemsize == 47, "element size not as requested"); |
53 UCX_TEST_ASSERT(array.allocator == ucx_default_allocator(), |
54 UCX_TEST_ASSERT(array->allocator == ucx_default_allocator(), |
54 "array not using the default allocator"); |
55 "array not using the default allocator"); |
55 UCX_TEST_END |
56 UCX_TEST_END |
56 ucx_array_destroy(&array); |
57 ucx_array_free(array); |
57 } |
58 } |
58 |
59 |
59 UCX_TEST(test_ucx_array_append_from) { |
60 UCX_TEST(test_ucx_array_append_from) { |
60 UcxArray array = ucx_array_new(16, sizeof(int)); |
61 UcxArray *array = ucx_array_new(16, sizeof(int)); |
61 int *elements; |
62 int *elements; |
62 |
63 |
63 int x = 42; |
64 int x = 42; |
64 ucx_array_append_from(&array, &x, 1); |
65 ucx_array_append_from(array, &x, 1); |
65 UCX_TEST_BEGIN |
66 UCX_TEST_BEGIN |
66 |
67 |
67 elements = array.data; |
68 elements = array->data; |
68 UCX_TEST_ASSERT(elements[0] == 42, "failed"); |
69 UCX_TEST_ASSERT(elements[0] == 42, "failed"); |
69 |
70 |
70 int y[2] = {13, 37}; |
71 int y[2] = {13, 37}; |
71 ucx_array_append_from(&array, y, 2); |
72 ucx_array_append_from(array, y, 2); |
72 |
73 |
73 elements = array.data; |
74 elements = array->data; |
74 UCX_TEST_ASSERT(array.size == 3, "incorrect size after append"); |
75 UCX_TEST_ASSERT(array->size == 3, "incorrect size after append"); |
75 UCX_TEST_ASSERT(elements[1] == 13, "failed"); |
76 UCX_TEST_ASSERT(elements[1] == 13, "failed"); |
76 UCX_TEST_ASSERT(elements[2] == 37, "failed"); |
77 UCX_TEST_ASSERT(elements[2] == 37, "failed"); |
77 UCX_TEST_ASSERT(elements[0] == 42, |
78 UCX_TEST_ASSERT(elements[0] == 42, |
78 "append corrupted previously inserted data"); |
79 "append corrupted previously inserted data"); |
79 |
80 |
80 ucx_array_append_from(&array, NULL, 2); |
81 ucx_array_append_from(array, NULL, 2); |
81 |
82 |
82 elements = array.data; |
83 elements = array->data; |
83 UCX_TEST_ASSERT(array.size == 5, "incorrect size after NULL append"); |
84 UCX_TEST_ASSERT(array->size == 5, "incorrect size after NULL append"); |
84 UCX_TEST_ASSERT(elements[3] == 0, "element is not zeroed"); |
85 UCX_TEST_ASSERT(elements[3] == 0, "element is not zeroed"); |
85 UCX_TEST_ASSERT(elements[4] == 0, "element is not zeroed"); |
86 UCX_TEST_ASSERT(elements[4] == 0, "element is not zeroed"); |
86 UCX_TEST_ASSERT(elements[0] == 42, |
87 UCX_TEST_ASSERT(elements[0] == 42, |
87 "NULL append corrupted previously inserted data"); |
88 "NULL append corrupted previously inserted data"); |
88 UCX_TEST_ASSERT(elements[1] == 13, |
89 UCX_TEST_ASSERT(elements[1] == 13, |
90 UCX_TEST_ASSERT(elements[2] == 37, |
91 UCX_TEST_ASSERT(elements[2] == 37, |
91 "NULL append corrupted previously inserted data"); |
92 "NULL append corrupted previously inserted data"); |
92 |
93 |
93 UCX_TEST_END |
94 UCX_TEST_END |
94 |
95 |
95 ucx_array_destroy(&array); |
96 ucx_array_free(array); |
96 } |
97 } |
97 |
98 |
98 UCX_TEST(test_ucx_array_prepend_from) { |
99 UCX_TEST(test_ucx_array_prepend_from) { |
99 int *elems; |
100 int *elems; |
100 UcxArray array = ucx_array_new(16, sizeof(int)); |
101 UcxArray *array = ucx_array_new(16, sizeof(int)); |
101 |
102 |
102 int x = 42; |
103 int x = 42; |
103 ucx_array_prepend_from(&array, &x, 1); |
104 ucx_array_prepend_from(array, &x, 1); |
104 UCX_TEST_BEGIN |
105 UCX_TEST_BEGIN |
105 |
106 |
106 elems = array.data; |
107 elems = array->data; |
107 UCX_TEST_ASSERT(elems[0] == 42, "failed"); |
108 UCX_TEST_ASSERT(elems[0] == 42, "failed"); |
108 |
109 |
109 int y[2] = {13, 37}; |
110 int y[2] = {13, 37}; |
110 ucx_array_prepend_from(&array, y, 2); |
111 ucx_array_prepend_from(array, y, 2); |
111 |
112 |
112 elems = array.data; |
113 elems = array->data; |
113 UCX_TEST_ASSERT(array.size == 3, "incorrect size after prepend"); |
114 UCX_TEST_ASSERT(array->size == 3, "incorrect size after prepend"); |
114 UCX_TEST_ASSERT(elems[0] == 13, "failed"); |
115 UCX_TEST_ASSERT(elems[0] == 13, "failed"); |
115 UCX_TEST_ASSERT(elems[1] == 37, "failed"); |
116 UCX_TEST_ASSERT(elems[1] == 37, "failed"); |
116 UCX_TEST_ASSERT(elems[2] == 42, |
117 UCX_TEST_ASSERT(elems[2] == 42, |
117 "prepend corrupted previously inserted data"); |
118 "prepend corrupted previously inserted data"); |
118 |
119 |
119 ucx_array_prepend_from(&array, NULL, 2); |
120 ucx_array_prepend_from(array, NULL, 2); |
120 |
121 |
121 elems = array.data; |
122 elems = array->data; |
122 UCX_TEST_ASSERT(array.size == 5, "incorrect size after NULL prepend"); |
123 UCX_TEST_ASSERT(array->size == 5, "incorrect size after NULL prepend"); |
123 UCX_TEST_ASSERT(elems[0] == 0, "element is not zeroed"); |
124 UCX_TEST_ASSERT(elems[0] == 0, "element is not zeroed"); |
124 UCX_TEST_ASSERT(elems[1] == 0, "element is not zeroed"); |
125 UCX_TEST_ASSERT(elems[1] == 0, "element is not zeroed"); |
125 UCX_TEST_ASSERT(elems[2] == 13, |
126 UCX_TEST_ASSERT(elems[2] == 13, |
126 "NULL prepend corrupted previously inserted data"); |
127 "NULL prepend corrupted previously inserted data"); |
127 UCX_TEST_ASSERT(elems[3] == 37, |
128 UCX_TEST_ASSERT(elems[3] == 37, |
129 UCX_TEST_ASSERT(elems[4] == 42, |
130 UCX_TEST_ASSERT(elems[4] == 42, |
130 "NULL prepend corrupted previously inserted data"); |
131 "NULL prepend corrupted previously inserted data"); |
131 |
132 |
132 UCX_TEST_END |
133 UCX_TEST_END |
133 |
134 |
134 ucx_array_destroy(&array); |
135 ucx_array_free(array); |
135 } |
136 } |
136 |
137 |
137 UCX_TEST(test_ucx_array_set_from) { |
138 UCX_TEST(test_ucx_array_set_from) { |
138 int *elems; |
139 int *elems; |
139 UcxArray array = ucx_array_new(16, sizeof(int)); |
140 UcxArray *array = ucx_array_new(16, sizeof(int)); |
140 |
141 |
141 int x = 42; |
142 int x = 42; |
142 |
143 |
143 UCX_TEST_BEGIN |
144 UCX_TEST_BEGIN |
144 |
145 |
145 ucx_array_set_from(&array, 7, &x, 1); |
146 ucx_array_set_from(array, 7, &x, 1); |
146 |
147 |
147 elems = array.data; |
148 elems = array->data; |
148 UCX_TEST_ASSERT(elems[7] == 42, "failed"); |
149 UCX_TEST_ASSERT(elems[7] == 42, "failed"); |
149 UCX_TEST_ASSERT(array.size >= 8, "array not resized on set"); |
150 UCX_TEST_ASSERT(array->size >= 8, "array not resized on set"); |
150 UCX_TEST_ASSERT(array.capacity == 16, "capacity changed unnecessarily"); |
151 UCX_TEST_ASSERT(array->capacity == 16, "capacity changed unnecessarily"); |
151 |
152 |
152 int y[2] = {13, 37}; |
153 int y[2] = {13, 37}; |
153 ucx_array_set_from(&array, 27, y, 2); |
154 ucx_array_set_from(array, 27, y, 2); |
154 |
155 |
155 elems = array.data; |
156 elems = array->data; |
156 UCX_TEST_ASSERT(elems[27] == 13, "failed"); |
157 UCX_TEST_ASSERT(elems[27] == 13, "failed"); |
157 UCX_TEST_ASSERT(elems[28] == 37, "failed"); |
158 UCX_TEST_ASSERT(elems[28] == 37, "failed"); |
158 UCX_TEST_ASSERT(array.size == 29, "array not resized on set"); |
159 UCX_TEST_ASSERT(array->size == 29, "array not resized on set"); |
159 UCX_TEST_ASSERT(array.capacity == 32, "capacity not grown"); |
160 UCX_TEST_ASSERT(array->capacity == 32, "capacity not grown"); |
160 |
161 |
161 ucx_array_set_from(&array, 7, NULL, 2); |
162 ucx_array_set_from(array, 7, NULL, 2); |
162 |
163 |
163 elems = array.data; |
164 elems = array->data; |
164 UCX_TEST_ASSERT(elems[7] == 0, "not zeroed on NULL set"); |
165 UCX_TEST_ASSERT(elems[7] == 0, "not zeroed on NULL set"); |
165 UCX_TEST_ASSERT(elems[8] == 0, "not zeroed on NULL set"); |
166 UCX_TEST_ASSERT(elems[8] == 0, "not zeroed on NULL set"); |
166 |
167 |
167 UCX_TEST_END |
168 UCX_TEST_END |
168 |
169 |
169 ucx_array_destroy(&array); |
170 ucx_array_free(array); |
170 } |
171 } |
171 |
172 |
172 UCX_TEST(test_ucx_array_append) { |
173 UCX_TEST(test_ucx_array_append) { |
173 UcxArray array = ucx_array_new(16, sizeof(int)); |
174 UcxArray *array = ucx_array_new(16, sizeof(int)); |
174 int *elements; |
175 int *elements; |
175 |
176 |
176 ucx_array_append(&array, 42); |
177 ucx_array_append(array, 42); |
177 UCX_TEST_BEGIN |
178 UCX_TEST_BEGIN |
178 |
179 |
179 elements = array.data; |
180 elements = array->data; |
180 UCX_TEST_ASSERT(elements[0] == 42, "failed"); |
181 UCX_TEST_ASSERT(elements[0] == 42, "failed"); |
181 |
182 |
182 ucx_array_append(&array, 13); |
183 ucx_array_append(array, 13); |
183 ucx_array_append(&array, 37); |
184 ucx_array_append(array, 37); |
184 |
185 |
185 elements = array.data; |
186 elements = array->data; |
186 UCX_TEST_ASSERT(array.size == 3, "incorrect size after append"); |
187 UCX_TEST_ASSERT(array->size == 3, "incorrect size after append"); |
187 UCX_TEST_ASSERT(elements[1] == 13, "failed"); |
188 UCX_TEST_ASSERT(elements[1] == 13, "failed"); |
188 UCX_TEST_ASSERT(elements[2] == 37, "failed"); |
189 UCX_TEST_ASSERT(elements[2] == 37, "failed"); |
189 UCX_TEST_ASSERT(elements[0] == 42, |
190 UCX_TEST_ASSERT(elements[0] == 42, |
190 "append corrupted previously inserted data"); |
191 "append corrupted previously inserted data"); |
191 |
192 |
192 UCX_TEST_END |
193 UCX_TEST_END |
193 |
194 |
194 ucx_array_destroy(&array); |
195 ucx_array_destroy(array); |
195 } |
196 } |
196 |
197 |
197 UCX_TEST(test_ucx_array_prepend) { |
198 UCX_TEST(test_ucx_array_prepend) { |
198 int *elems; |
199 int *elems; |
199 UcxArray array = ucx_array_new(16, sizeof(int)); |
200 UcxArray *array = ucx_array_new(16, sizeof(int)); |
200 |
201 |
201 ucx_array_prepend(&array, 42); |
202 ucx_array_prepend(array, 42); |
202 UCX_TEST_BEGIN |
203 UCX_TEST_BEGIN |
203 |
204 |
204 elems = array.data; |
205 elems = array->data; |
205 UCX_TEST_ASSERT(elems[0] == 42, "failed"); |
206 UCX_TEST_ASSERT(elems[0] == 42, "failed"); |
206 |
207 |
207 ucx_array_prepend(&array, 37); |
208 ucx_array_prepend(array, 37); |
208 ucx_array_prepend(&array, 13); |
209 ucx_array_prepend(array, 13); |
209 |
210 |
210 elems = array.data; |
211 elems = array->data; |
211 UCX_TEST_ASSERT(array.size == 3, "incorrect size after prepend"); |
212 UCX_TEST_ASSERT(array->size == 3, "incorrect size after prepend"); |
212 UCX_TEST_ASSERT(elems[0] == 13, "failed"); |
213 UCX_TEST_ASSERT(elems[0] == 13, "failed"); |
213 UCX_TEST_ASSERT(elems[1] == 37, "failed"); |
214 UCX_TEST_ASSERT(elems[1] == 37, "failed"); |
214 UCX_TEST_ASSERT(elems[2] == 42, |
215 UCX_TEST_ASSERT(elems[2] == 42, |
215 "prepend corrupted previously inserted data"); |
216 "prepend corrupted previously inserted data"); |
216 |
217 |
217 UCX_TEST_END |
218 UCX_TEST_END |
218 |
219 |
219 ucx_array_destroy(&array); |
220 ucx_array_free(array); |
220 } |
221 } |
221 |
222 |
222 UCX_TEST(test_ucx_array_set) { |
223 UCX_TEST(test_ucx_array_set) { |
223 int *elems; |
224 int *elems; |
224 UcxArray array = ucx_array_new(16, sizeof(int)); |
225 UcxArray *array = ucx_array_new(16, sizeof(int)); |
225 |
226 |
226 UCX_TEST_BEGIN |
227 UCX_TEST_BEGIN |
227 |
228 |
228 ucx_array_set(&array, 7, 42); |
229 ucx_array_set(array, 7, 42); |
229 |
230 |
230 elems = array.data; |
231 elems = array->data; |
231 UCX_TEST_ASSERT(elems[7] == 42, "failed"); |
232 UCX_TEST_ASSERT(elems[7] == 42, "failed"); |
232 UCX_TEST_ASSERT(array.size == 8, "array not resized on set"); |
233 UCX_TEST_ASSERT(array->size == 8, "array not resized on set"); |
233 UCX_TEST_ASSERT(array.capacity == 16, "capacity changed unnecessarily"); |
234 UCX_TEST_ASSERT(array->capacity == 16, "capacity changed unnecessarily"); |
234 |
235 |
235 ucx_array_set(&array, 27, 13); |
236 ucx_array_set(array, 27, 13); |
236 ucx_array_set(&array, 28, 37); |
237 ucx_array_set(array, 28, 37); |
237 |
238 |
238 elems = array.data; |
239 elems = array->data; |
239 UCX_TEST_ASSERT(elems[27] == 13, "failed"); |
240 UCX_TEST_ASSERT(elems[27] == 13, "failed"); |
240 UCX_TEST_ASSERT(elems[28] == 37, "failed"); |
241 UCX_TEST_ASSERT(elems[28] == 37, "failed"); |
241 UCX_TEST_ASSERT(array.size == 29, "array not resized on set"); |
242 UCX_TEST_ASSERT(array->size == 29, "array not resized on set"); |
242 UCX_TEST_ASSERT(array.capacity == 32, "capacity not grown"); |
243 UCX_TEST_ASSERT(array->capacity == 32, "capacity not grown"); |
243 |
244 |
244 UCX_TEST_END |
245 UCX_TEST_END |
245 |
246 |
246 ucx_array_destroy(&array); |
247 ucx_array_free(array); |
247 } |
248 } |
248 |
249 |
249 UCX_TEST(test_ucx_array_equals) { |
250 UCX_TEST(test_ucx_array_equals) { |
250 UcxArray a1 = ucx_array_new(16, sizeof(int32_t)); |
251 UcxArray *a1 = ucx_array_new(16, sizeof(int32_t)); |
251 UcxArray a2 = ucx_array_new(16, sizeof(int32_t)); |
252 UcxArray *a2 = ucx_array_new(16, sizeof(int32_t)); |
252 UcxArray a3 = ucx_array_new(16, sizeof(int64_t)); |
253 UcxArray *a3 = ucx_array_new(16, sizeof(int64_t)); |
253 UcxArray a4 = ucx_array_new(16, sizeof(int32_t)); |
254 UcxArray *a4 = ucx_array_new(16, sizeof(int32_t)); |
254 |
255 |
255 int32_t *intelems; |
256 int32_t *intelems; |
256 int64_t *longintelems; |
257 int64_t *longintelems; |
257 |
258 |
258 a1.size = 5; |
259 a1->size = 5; |
259 intelems = a1.data; |
260 intelems = a1->data; |
260 intelems[0] = 47; |
261 intelems[0] = 47; |
261 intelems[1] = 11; |
262 intelems[1] = 11; |
262 intelems[2] = 0; |
263 intelems[2] = 0; |
263 intelems[3] = 8; |
264 intelems[3] = 8; |
264 intelems[4] = 15; |
265 intelems[4] = 15; |
265 a2.size = 5; |
266 a2->size = 5; |
266 intelems = a2.data; |
267 intelems = a2->data; |
267 intelems[0] = 47; |
268 intelems[0] = 47; |
268 intelems[1] = 11; |
269 intelems[1] = 11; |
269 intelems[2] = 0; |
270 intelems[2] = 0; |
270 intelems[3] = 8; |
271 intelems[3] = 8; |
271 intelems[4] = 15; |
272 intelems[4] = 15; |
272 a3.size = 5; |
273 a3->size = 5; |
273 longintelems = a3.data; |
274 longintelems = a3->data; |
274 longintelems[0] = 47; |
275 longintelems[0] = 47; |
275 longintelems[1] = 11; |
276 longintelems[1] = 11; |
276 longintelems[2] = 0; |
277 longintelems[2] = 0; |
277 longintelems[3] = 8; |
278 longintelems[3] = 8; |
278 longintelems[4] = 15; |
279 longintelems[4] = 15; |
279 a4.size = 5; |
280 a4->size = 5; |
280 intelems = a4.data; |
281 intelems = a4->data; |
281 intelems[0] = 47; |
282 intelems[0] = 47; |
282 intelems[1] = 11; |
283 intelems[1] = 11; |
283 intelems[2] = -6; |
284 intelems[2] = -6; |
284 intelems[3] = 8; |
285 intelems[3] = 8; |
285 intelems[4] = 15; |
286 intelems[4] = 15; |
298 "compare using memcmp() failed"); |
299 "compare using memcmp() failed"); |
299 UCX_TEST_ASSERT(!ucx_array_equals(a1, a4, NULL, NULL), |
300 UCX_TEST_ASSERT(!ucx_array_equals(a1, a4, NULL, NULL), |
300 "compare using memcmp() failed"); |
301 "compare using memcmp() failed"); |
301 |
302 |
302 UCX_TEST_END |
303 UCX_TEST_END |
303 ucx_array_destroy(&a1); |
304 ucx_array_free(a1); |
304 ucx_array_destroy(&a2); |
305 ucx_array_free(a2); |
305 ucx_array_destroy(&a3); |
306 ucx_array_free(a3); |
306 ucx_array_destroy(&a4); |
307 ucx_array_free(a4); |
307 } |
308 } |
308 |
309 |
309 UCX_TEST(test_ucx_array_concat) { |
310 UCX_TEST(test_ucx_array_concat) { |
310 UcxArray a1 = ucx_array_new(16, sizeof(int)); |
311 UcxArray *a1 = ucx_array_new(16, sizeof(int)); |
311 UcxArray a2 = ucx_array_new(16, sizeof(int)); |
312 UcxArray *a2 = ucx_array_new(16, sizeof(int)); |
312 int *elems; |
313 int *elems; |
313 |
314 |
314 a1.size = 2; |
315 a1->size = 2; |
315 elems = a1.data; |
316 elems = a1->data; |
316 elems[0] = 47; |
317 elems[0] = 47; |
317 elems[1] = 11; |
318 elems[1] = 11; |
318 a2.size = 3; |
319 a2->size = 3; |
319 elems = a2.data; |
320 elems = a2->data; |
320 elems[0] = 0; |
321 elems[0] = 0; |
321 elems[1] = 8; |
322 elems[1] = 8; |
322 elems[2] = 15; |
323 elems[2] = 15; |
323 |
324 |
324 UCX_TEST_BEGIN |
325 UCX_TEST_BEGIN |
325 |
326 |
326 UCX_TEST_ASSERT(!ucx_array_concat(&a1, &a2), "failed"); |
327 UCX_TEST_ASSERT(!ucx_array_concat(a1, a2), "failed"); |
327 UCX_TEST_ASSERT(a1.size == 5, "failed"); |
328 UCX_TEST_ASSERT(a1->size == 5, "failed"); |
328 elems = a1.data; |
329 elems = a1->data; |
329 UCX_TEST_ASSERT(elems[0] == 47, "failed"); |
330 UCX_TEST_ASSERT(elems[0] == 47, "failed"); |
330 UCX_TEST_ASSERT(elems[1] == 11, "failed"); |
331 UCX_TEST_ASSERT(elems[1] == 11, "failed"); |
331 UCX_TEST_ASSERT(elems[2] == 0, "failed"); |
332 UCX_TEST_ASSERT(elems[2] == 0, "failed"); |
332 UCX_TEST_ASSERT(elems[3] == 8, "failed"); |
333 UCX_TEST_ASSERT(elems[3] == 8, "failed"); |
333 UCX_TEST_ASSERT(elems[4] == 15, "failed"); |
334 UCX_TEST_ASSERT(elems[4] == 15, "failed"); |
334 |
335 |
335 a1.elemsize *= 2; |
336 a1->elemsize *= 2; |
336 UCX_TEST_ASSERT(ucx_array_concat(&a1, &a2), |
337 UCX_TEST_ASSERT(ucx_array_concat(a1, a2), |
337 "arrays of different element size must not be concatenated"); |
338 "arrays of different element size must not be concatenated"); |
338 UCX_TEST_ASSERT(a1.size == 5, |
339 UCX_TEST_ASSERT(a1->size == 5, |
339 "arrays of different element size must not be concatenated"); |
340 "arrays of different element size must not be concatenated"); |
340 |
341 |
341 UCX_TEST_END |
342 UCX_TEST_END |
342 ucx_array_destroy(&a1); |
343 ucx_array_free(a1); |
343 ucx_array_destroy(&a2); |
344 ucx_array_free(a2); |
344 } |
345 } |
345 |
346 |
346 UCX_TEST(test_ucx_array_at) { |
347 UCX_TEST(test_ucx_array_at) { |
347 UcxArray array = ucx_array_new(16, sizeof(int)); |
348 UcxArray *array = ucx_array_new(16, sizeof(int)); |
348 |
349 |
349 int x[3] = {42, 13, 5}; |
350 int x[3] = {42, 13, 5}; |
350 ucx_array_append_from(&array, x, 3); |
351 ucx_array_append_from(array, x, 3); |
351 |
352 |
352 UCX_TEST_BEGIN |
353 UCX_TEST_BEGIN |
353 |
354 |
354 UCX_TEST_ASSERT(*(int*)ucx_array_at(array, 1) == 13, "failed"); |
355 UCX_TEST_ASSERT(*(int*)ucx_array_at(array, 1) == 13, "failed"); |
355 *(int*)ucx_array_at(array, 1) = 80; |
356 *(int*)ucx_array_at(array, 1) = 80; |
420 "false negative using memcmp()"); |
421 "false negative using memcmp()"); |
421 UCX_TEST_ASSERT(!ucx_array_contains(array,(void*)&y,NULL,NULL), |
422 UCX_TEST_ASSERT(!ucx_array_contains(array,(void*)&y,NULL,NULL), |
422 "false positive using memcmp()"); |
423 "false positive using memcmp()"); |
423 |
424 |
424 UCX_TEST_END |
425 UCX_TEST_END |
425 ucx_array_destroy(&array); |
426 ucx_array_free(array); |
426 } |
427 } |
427 |
428 |
428 UCX_TEST(test_ucx_array_remove) { |
429 UCX_TEST(test_ucx_array_remove) { |
429 UcxArray array = ucx_array_new(16, sizeof(int)); |
430 UcxArray *array = ucx_array_new(16, sizeof(int)); |
430 int *elems; |
431 int *elems; |
431 |
432 |
432 array.size = 5; |
433 array->size = 5; |
433 elems = array.data; |
434 elems = array->data; |
434 elems[0] = 47; |
435 elems[0] = 47; |
435 elems[1] = 11; |
436 elems[1] = 11; |
436 elems[2] = 0; |
437 elems[2] = 0; |
437 elems[3] = 8; |
438 elems[3] = 8; |
438 elems[4] = 15; |
439 elems[4] = 15; |
439 |
440 |
440 UCX_TEST_BEGIN |
441 UCX_TEST_BEGIN |
441 |
442 |
442 ucx_array_remove(&array, 2); |
443 ucx_array_remove(array, 2); |
443 elems = array.data; |
444 elems = array->data; |
444 UCX_TEST_ASSERT( |
445 UCX_TEST_ASSERT( |
445 elems[0] == 47 && |
446 elems[0] == 47 && |
446 elems[1] == 11 && |
447 elems[1] == 11 && |
447 elems[2] == 8 && |
448 elems[2] == 8 && |
448 elems[3] == 15, |
449 elems[3] == 15, |
449 "wrong contents after remove"); |
450 "wrong contents after remove"); |
450 UCX_TEST_ASSERT(array.size == 4, "wrong size after remove"); |
451 UCX_TEST_ASSERT(array->size == 4, "wrong size after remove"); |
451 |
452 |
452 ucx_array_remove_fast(&array, 1); |
453 ucx_array_remove_fast(array, 1); |
453 elems = array.data; |
454 elems = array->data; |
454 UCX_TEST_ASSERT( |
455 UCX_TEST_ASSERT( |
455 elems[0] == 47 && |
456 elems[0] == 47 && |
456 elems[1] == 15 && |
457 elems[1] == 15 && |
457 elems[2] == 8, |
458 elems[2] == 8, |
458 "wrong contents after fast remove"); |
459 "wrong contents after fast remove"); |
459 UCX_TEST_ASSERT(array.size == 3, "wrong size after fast remove"); |
460 UCX_TEST_ASSERT(array->size == 3, "wrong size after fast remove"); |
460 |
461 |
461 UCX_TEST_END |
462 UCX_TEST_END |
462 ucx_array_destroy(&array); |
463 ucx_array_free(array); |
463 } |
464 } |
464 |
465 |
465 UCX_TEST(test_ucx_array_clone) { |
466 UCX_TEST(test_ucx_array_clone) { |
466 UcxArray array = ucx_array_new(16, sizeof(int)); |
467 UcxArray array; |
|
468 UcxArray copy; |
|
469 ucx_array_init(&array, 16, sizeof(int)); |
467 int *elems; |
470 int *elems; |
468 |
471 |
469 array.size = 5; |
472 array.size = 5; |
470 elems = array.data; |
473 elems = array.data; |
471 elems[0] = 47; |
474 elems[0] = 47; |
472 elems[1] = 11; |
475 elems[1] = 11; |
473 elems[2] = 0; |
476 elems[2] = 0; |
474 elems[3] = 8; |
477 elems[3] = 8; |
475 elems[4] = 15; |
478 elems[4] = 15; |
476 |
479 |
477 UcxArray copy = ucx_array_clone(array); |
480 ucx_array_clone(©, &array); |
478 UCX_TEST_BEGIN |
481 UCX_TEST_BEGIN |
479 |
482 |
480 UCX_TEST_ASSERT(array.data != copy.data, "no true copy"); |
483 UCX_TEST_ASSERT(array.data != copy.data, "no true copy"); |
481 UCX_TEST_ASSERT(array.size == copy.size, "size mismatch"); |
484 UCX_TEST_ASSERT(array.size == copy.size, "size mismatch"); |
482 UCX_TEST_ASSERT(array.capacity == copy.capacity, "capacity mismatch"); |
485 UCX_TEST_ASSERT(array.capacity == copy.capacity, "capacity mismatch"); |
483 UCX_TEST_ASSERT(array.elemsize == copy.elemsize, "element size mismatch"); |
486 UCX_TEST_ASSERT(array.elemsize == copy.elemsize, "element size mismatch"); |
484 UCX_TEST_ASSERT(array.allocator == copy.allocator, "allocator mismatch"); |
487 UCX_TEST_ASSERT(array.allocator == copy.allocator, "allocator mismatch"); |
485 UCX_TEST_ASSERT(ucx_array_equals(array, copy, ucx_cmp_int, NULL), "failed"); |
488 UCX_TEST_ASSERT(ucx_array_equals(&array, ©, ucx_cmp_int, NULL), |
|
489 "contents do not match after clone"); |
486 |
490 |
487 UCX_TEST_END |
491 UCX_TEST_END |
488 |
492 |
489 ucx_array_destroy(&array); |
493 ucx_array_destroy(&array); |
490 ucx_array_destroy(©); |
494 ucx_array_destroy(©); |
495 } |
499 } |
496 |
500 |
497 UCX_TEST(test_ucx_array_sort) { |
501 UCX_TEST(test_ucx_array_sort) { |
498 int *elems; |
502 int *elems; |
499 |
503 |
500 UcxArray array = ucx_array_new(16, sizeof(int)); |
504 UcxArray *array = ucx_array_new(16, sizeof(int)); |
501 array.size = 5; |
505 array->size = 5; |
502 elems = array.data; |
506 elems = array->data; |
503 elems[0] = 47; |
507 elems[0] = 47; |
504 elems[1] = 11; |
508 elems[1] = 11; |
505 elems[2] = 0; |
509 elems[2] = 0; |
506 elems[3] = 8; |
510 elems[3] = 8; |
507 elems[4] = 15; |
511 elems[4] = 15; |
508 |
512 |
509 UcxArray expected = ucx_array_new(16, sizeof(int)); |
513 UcxArray *expected = ucx_array_new(16, sizeof(int)); |
510 expected.size = 5; |
514 expected->size = 5; |
511 elems = expected.data; |
515 elems = expected->data; |
512 elems[0] = 0; |
516 elems[0] = 0; |
513 elems[1] = 8; |
517 elems[1] = 8; |
514 elems[2] = 11; |
518 elems[2] = 11; |
515 elems[3] = 15; |
519 elems[3] = 15; |
516 elems[4] = 47; |
520 elems[4] = 47; |
517 |
521 |
518 UcxArray expectedrev = ucx_array_new(16, sizeof(int)); |
522 UcxArray *expectedrev = ucx_array_new(16, sizeof(int)); |
519 expectedrev.size = 5; |
523 expectedrev->size = 5; |
520 elems = expectedrev.data; |
524 elems = expectedrev->data; |
521 elems[0] = 47; |
525 elems[0] = 47; |
522 elems[1] = 15; |
526 elems[1] = 15; |
523 elems[2] = 11; |
527 elems[2] = 11; |
524 elems[3] = 8; |
528 elems[3] = 8; |
525 elems[4] = 0; |
529 elems[4] = 0; |
526 |
530 |
527 |
531 |
528 UCX_TEST_BEGIN |
532 UCX_TEST_BEGIN |
529 void* original_ptr = array.data; |
533 void* original_ptr = array->data; |
530 ucx_array_sort(array, ucx_cmp_int, NULL); |
534 ucx_array_sort(array, ucx_cmp_int, NULL); |
531 UCX_TEST_ASSERT(ucx_array_equals(array, expected, NULL, NULL), "failed"); |
535 UCX_TEST_ASSERT(ucx_array_equals(array, expected, NULL, NULL), "failed"); |
532 UCX_TEST_ASSERT(array.size == 5, "size corrupted"); |
536 UCX_TEST_ASSERT(array->size == 5, "size corrupted"); |
533 UCX_TEST_ASSERT(array.data == original_ptr, "shall not reallocate"); |
537 UCX_TEST_ASSERT(array->data == original_ptr, "shall not reallocate"); |
534 |
538 |
535 ucx_array_sort(array, ucx_cmp_int_reverse, NULL); |
539 ucx_array_sort(array, ucx_cmp_int_reverse, NULL); |
536 UCX_TEST_ASSERT(ucx_array_equals(array, expectedrev, NULL, NULL), "failed"); |
540 UCX_TEST_ASSERT(ucx_array_equals(array, expectedrev, NULL, NULL), "failed"); |
537 |
541 |
538 ucx_array_reserve(&array, 32); |
542 ucx_array_reserve(array, 32); |
539 ucx_array_reserve(&expected, 32); |
543 ucx_array_reserve(expected, 32); |
540 array.size = expected.size = 32; |
544 array->size = expected->size = 32; |
541 for (size_t i = 0 ; i < 32 ; i++) { |
545 for (size_t i = 0 ; i < 32 ; i++) { |
542 ((int*)array.data)[i]= ((i%2==0)?-1:1) * ((int) i); |
546 ((int*)array->data)[i]= ((i%2==0)?-1:1) * ((int) i); |
543 ((int*)expected.data)[i] = (-30+2*i) - (i > 15 ? 1 : 0); |
547 ((int*)expected->data)[i] = (-30+2*i) - (i > 15 ? 1 : 0); |
544 } |
548 } |
545 |
549 |
546 /* dummy third argument to trigger a possible fallback for qsort_s */ |
550 /* dummy third argument to trigger a possible fallback for qsort_s */ |
547 ucx_array_sort(array, ucx_cmp_int, array.data); |
551 ucx_array_sort(array, ucx_cmp_int, array->data); |
548 UCX_TEST_ASSERT(ucx_array_equals(array, expected, NULL, NULL), |
552 UCX_TEST_ASSERT(ucx_array_equals(array, expected, NULL, NULL), |
549 "failed for bigger arrays"); |
553 "failed for bigger arrays"); |
550 UCX_TEST_END |
554 UCX_TEST_END |
551 |
555 |
552 ucx_array_destroy(&expected); |
556 ucx_array_free(expectedrev); |
553 ucx_array_destroy(&array); |
557 ucx_array_free(expected); |
|
558 ucx_array_free(array); |
554 } |
559 } |
555 |
560 |
556 UCX_TEST(test_ucx_array_autogrow) { |
561 UCX_TEST(test_ucx_array_autogrow) { |
557 int *elems; |
562 int *elems; |
558 UcxArray array = ucx_array_new(4, sizeof(int)); |
563 UcxArray *array = ucx_array_new(4, sizeof(int)); |
559 array.size = 3; |
564 array->size = 3; |
560 elems = array.data; |
565 elems = array->data; |
561 elems[0] = 47; |
566 elems[0] = 47; |
562 elems[1] = 11; |
567 elems[1] = 11; |
563 int x = 5; |
568 int x = 5; |
564 |
569 |
565 UCX_TEST_BEGIN |
570 UCX_TEST_BEGIN |
566 |
571 |
567 void* oldptr = array.data; |
572 void* oldptr = array->data; |
568 |
573 |
569 ucx_array_append(&array, 5); |
574 ucx_array_append(array, 5); |
570 UCX_TEST_ASSERT(array.capacity == 4 && array.data == oldptr, |
575 UCX_TEST_ASSERT(array->capacity == 4 && array->data == oldptr, |
571 "array should not grow too early"); |
576 "array should not grow too early"); |
572 ucx_array_append(&array, 5); |
577 ucx_array_append(array, 5); |
573 elems = array.data; |
578 elems = array->data; |
574 UCX_TEST_ASSERT(array.capacity == 8, "array did not grow"); |
579 UCX_TEST_ASSERT(array->capacity == 8, "array did not grow"); |
575 UCX_TEST_ASSERT(array.size == 5, "incorrect size after grow"); |
580 UCX_TEST_ASSERT(array->size == 5, "incorrect size after grow"); |
576 UCX_TEST_ASSERT(elems[3] == 5 && elems[4] == 5, "corrupt data"); |
581 UCX_TEST_ASSERT(elems[3] == 5 && elems[4] == 5, "corrupt data"); |
577 |
582 |
578 UCX_TEST_END |
583 UCX_TEST_END |
579 ucx_array_destroy(&array); |
584 ucx_array_free(array); |
580 } |
585 } |
581 |
586 |
582 UCX_TEST(test_ucx_array_shrink) { |
587 UCX_TEST(test_ucx_array_shrink) { |
583 UcxArray array = ucx_array_new(16, sizeof(int)); |
588 UcxArray *array = ucx_array_new(16, sizeof(int)); |
584 array.size = 4; |
589 array->size = 4; |
585 |
590 |
586 UCX_TEST_BEGIN |
591 UCX_TEST_BEGIN |
587 UCX_TEST_ASSERT(!ucx_array_shrink(&array), "failed"); |
592 UCX_TEST_ASSERT(!ucx_array_shrink(array), "failed"); |
588 UCX_TEST_ASSERT(array.capacity == 4, "incorrect capacity after shrink"); |
593 UCX_TEST_ASSERT(array->capacity == 4, "incorrect capacity after shrink"); |
589 UCX_TEST_END |
594 UCX_TEST_END |
590 ucx_array_destroy(&array); |
595 ucx_array_free(array); |
591 } |
596 } |
592 |
597 |
593 UCX_TEST(test_ucx_array_resize) { |
598 UCX_TEST(test_ucx_array_resize) { |
594 UcxArray array = ucx_array_new(16, sizeof(int)); |
599 UcxArray *array = ucx_array_new(16, sizeof(int)); |
595 array.size = 8; |
600 array->size = 8; |
596 |
601 |
597 UCX_TEST_BEGIN |
602 UCX_TEST_BEGIN |
598 |
603 |
599 UCX_TEST_ASSERT(!ucx_array_resize(&array, 32), "failed"); |
604 UCX_TEST_ASSERT(!ucx_array_resize(array, 32), "failed"); |
600 UCX_TEST_ASSERT(array.capacity == 32, "incorrect capacity after resize"); |
605 UCX_TEST_ASSERT(array->capacity == 32, "incorrect capacity after resize"); |
601 UCX_TEST_ASSERT(array.size == 8, "incorrect size after resize"); |
606 UCX_TEST_ASSERT(array->size == 8, "incorrect size after resize"); |
602 |
607 |
603 UCX_TEST_ASSERT(!ucx_array_resize(&array, 4), "failed"); |
608 UCX_TEST_ASSERT(!ucx_array_resize(array, 4), "failed"); |
604 UCX_TEST_ASSERT(array.capacity == 4, "incorrect capacity after resize"); |
609 UCX_TEST_ASSERT(array->capacity == 4, "incorrect capacity after resize"); |
605 UCX_TEST_ASSERT(array.size == 4, "incorrect size after resize"); |
610 UCX_TEST_ASSERT(array->size == 4, "incorrect size after resize"); |
606 |
611 |
607 UCX_TEST_END |
612 UCX_TEST_END |
608 ucx_array_destroy(&array); |
613 ucx_array_free(array); |
609 } |
614 } |
610 |
615 |
611 UCX_TEST(test_ucx_array_reserve) { |
616 UCX_TEST(test_ucx_array_reserve) { |
612 UcxArray array = ucx_array_new(16, sizeof(int)); |
617 UcxArray *array = ucx_array_new(16, sizeof(int)); |
613 |
618 |
614 UCX_TEST_BEGIN |
619 UCX_TEST_BEGIN |
615 |
620 |
616 UCX_TEST_ASSERT(!ucx_array_reserve(&array, 4), "failed"); |
621 UCX_TEST_ASSERT(!ucx_array_reserve(array, 4), "failed"); |
617 UCX_TEST_ASSERT(array.capacity == 16, "reserve shall not shrink"); |
622 UCX_TEST_ASSERT(array->capacity == 16, "reserve shall not shrink"); |
618 |
623 |
619 UCX_TEST_ASSERT(!ucx_array_resize(&array, 32), "failed"); |
624 UCX_TEST_ASSERT(!ucx_array_resize(array, 32), "failed"); |
620 UCX_TEST_ASSERT(array.capacity == 32, "incorrect capacity after reserve"); |
625 UCX_TEST_ASSERT(array->capacity == 32, "incorrect capacity after reserve"); |
621 |
626 |
622 UCX_TEST_END |
627 UCX_TEST_END |
623 ucx_array_destroy(&array); |
628 ucx_array_free(array); |
624 } |
629 } |
625 |
630 |
626 UCX_TEST(test_ucx_array_util_set) { |
631 UCX_TEST(test_ucx_array_util_set) { |
627 size_t capacity = 16; |
632 size_t capacity = 16; |
628 int* array = malloc(sizeof(int)*capacity); |
633 int* array = malloc(sizeof(int)*capacity); |