test/test_list.c

changeset 466
28bc3e10ac28
parent 460
e075009b33b7
child 468
75ae1dccd101
equal deleted inserted replaced
465:1e3cb39815f8 466:28bc3e10ac28
156 CU_ASSERT_EQUAL(list->capacity, (size_t) -1) 156 CU_ASSERT_EQUAL(list->capacity, (size_t) -1)
157 CU_ASSERT_PTR_EQUAL(list->allocator, cxTestingAllocator) 157 CU_ASSERT_PTR_EQUAL(list->allocator, cxTestingAllocator)
158 CU_ASSERT_EQUAL(list->itemsize, sizeof(int)) 158 CU_ASSERT_EQUAL(list->itemsize, sizeof(int))
159 CU_ASSERT_PTR_EQUAL(list->cmpfunc, cmp_int) 159 CU_ASSERT_PTR_EQUAL(list->cmpfunc, cmp_int)
160 160
161 // assume this structure for a linked list 161 cxLinkedListDestroy(list);
162 struct ll_check {
163 cx_list_s base;
164 void *begin;
165 void *end;
166 };
167
168 struct ll_check *actual = (struct ll_check *) list;
169 CU_ASSERT_PTR_NULL(actual->begin)
170 CU_ASSERT_PTR_NULL(actual->end)
171
172 cxLinkedListDestroy(list);
173
174 CU_ASSERT_TRUE(cxTestingAllocatorVerify()) 162 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
175 } 163 }
176 164
177 void test_hl_linked_list_add(void) { 165 void test_hl_linked_list_add(void) {
178 cxTestingAllocatorReset(); 166 cxTestingAllocatorReset();
188 CU_ASSERT_EQUAL(cxListAdd(list, &data), 0) 176 CU_ASSERT_EQUAL(cxListAdd(list, &data), 0)
189 177
190 CU_ASSERT_EQUAL(list->size, 3) 178 CU_ASSERT_EQUAL(list->size, 3)
191 CU_ASSERT_TRUE(list->capacity >= list->size) 179 CU_ASSERT_TRUE(list->capacity >= list->size)
192 180
193 CU_ASSERT_EQUAL(*(int*)cxListAt(list, 0), 5) 181 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5)
194 CU_ASSERT_EQUAL(*(int*)cxListAt(list, 1), 47) 182 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47)
195 CU_ASSERT_EQUAL(*(int*)cxListAt(list, 2), 13) 183 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13)
196 184
197 cxLinkedListDestroy(list); 185 cxLinkedListDestroy(list);
198 CU_ASSERT_TRUE(cxTestingAllocatorVerify()) 186 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
199 } 187 }
200 188
206 194
207 CU_ASSERT_PTR_NULL(cxListLast(list)) 195 CU_ASSERT_PTR_NULL(cxListLast(list))
208 196
209 data = 5; 197 data = 5;
210 CU_ASSERT_EQUAL(cxListAdd(list, &data), 0) 198 CU_ASSERT_EQUAL(cxListAdd(list, &data), 0)
211 CU_ASSERT_EQUAL(*(int*)cxListLast(list), 5) 199 CU_ASSERT_EQUAL(*(int *) cxListLast(list), 5)
212 200
213 data = 47; 201 data = 47;
214 CU_ASSERT_EQUAL(cxListAdd(list, &data), 0) 202 CU_ASSERT_EQUAL(cxListAdd(list, &data), 0)
215 CU_ASSERT_EQUAL(*(int*)cxListLast(list), 47) 203 CU_ASSERT_EQUAL(*(int *) cxListLast(list), 47)
216 204
217 cxLinkedListDestroy(list); 205 cxLinkedListDestroy(list);
218 CU_ASSERT_TRUE(cxTestingAllocatorVerify()) 206 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
219 } 207 }
220 208
239 CU_ASSERT_EQUAL(cxListInsert(list, 3, &data), 0) 227 CU_ASSERT_EQUAL(cxListInsert(list, 3, &data), 0)
240 228
241 CU_ASSERT_EQUAL(list->size, 4) 229 CU_ASSERT_EQUAL(list->size, 4)
242 CU_ASSERT_TRUE(list->capacity >= list->size) 230 CU_ASSERT_TRUE(list->capacity >= list->size)
243 231
244 CU_ASSERT_EQUAL(*(int*)cxListAt(list, 0), 47) 232 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47)
245 CU_ASSERT_EQUAL(*(int*)cxListAt(list, 1), 13) 233 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 13)
246 CU_ASSERT_EQUAL(*(int*)cxListAt(list, 2), 5) 234 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 5)
247 CU_ASSERT_EQUAL(*(int*)cxListAt(list, 3), 42) 235 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 3), 42)
248 236
249 cxLinkedListDestroy(list); 237 cxLinkedListDestroy(list);
250 CU_ASSERT_TRUE(cxTestingAllocatorVerify()) 238 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
251 } 239 }
252 240
271 CU_ASSERT_NOT_EQUAL(cxListRemove(list, 4), 0) 259 CU_ASSERT_NOT_EQUAL(cxListRemove(list, 4), 0)
272 260
273 CU_ASSERT_EQUAL(cxListRemove(list, 2), 0) 261 CU_ASSERT_EQUAL(cxListRemove(list, 2), 0)
274 CU_ASSERT_EQUAL(list->size, 3) 262 CU_ASSERT_EQUAL(list->size, 3)
275 CU_ASSERT_TRUE(list->capacity >= list->size) 263 CU_ASSERT_TRUE(list->capacity >= list->size)
276 CU_ASSERT_EQUAL(*(int*)cxListAt(list, 0), 5) 264 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5)
277 CU_ASSERT_EQUAL(*(int*)cxListAt(list, 1), 47) 265 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47)
278 CU_ASSERT_EQUAL(*(int*)cxListAt(list, 2), 13) 266 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13)
279 267
280 CU_ASSERT_EQUAL(cxListRemove(list, 0), 0) 268 CU_ASSERT_EQUAL(cxListRemove(list, 0), 0)
281 CU_ASSERT_EQUAL(list->size, 2) 269 CU_ASSERT_EQUAL(list->size, 2)
282 CU_ASSERT_TRUE(list->capacity >= list->size) 270 CU_ASSERT_TRUE(list->capacity >= list->size)
283 CU_ASSERT_EQUAL(*(int*)cxListAt(list, 0), 47) 271 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47)
284 CU_ASSERT_EQUAL(*(int*)cxListAt(list, 1), 13) 272 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 13)
285 273
286 CU_ASSERT_EQUAL(cxListRemove(list, 1), 0) 274 CU_ASSERT_EQUAL(cxListRemove(list, 1), 0)
287 CU_ASSERT_EQUAL(list->size, 1) 275 CU_ASSERT_EQUAL(list->size, 1)
288 CU_ASSERT_TRUE(list->capacity >= list->size) 276 CU_ASSERT_TRUE(list->capacity >= list->size)
289 CU_ASSERT_EQUAL(*(int*)cxListAt(list, 0), 47) 277 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47)
290 278
291 CU_ASSERT_EQUAL(cxListRemove(list, 0), 0) 279 CU_ASSERT_EQUAL(cxListRemove(list, 0), 0)
292 CU_ASSERT_EQUAL(list->size, 0) 280 CU_ASSERT_EQUAL(list->size, 0)
293 CU_ASSERT_TRUE(list->capacity >= list->size) 281 CU_ASSERT_TRUE(list->capacity >= list->size)
294 282
327 315
328 cxLinkedListDestroy(list); 316 cxLinkedListDestroy(list);
329 CU_ASSERT_TRUE(cxTestingAllocatorVerify()) 317 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
330 } 318 }
331 319
320 void test_hl_ptr_linked_list_create(void) {
321 cxTestingAllocatorReset();
322
323 CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
324
325 CU_ASSERT_EQUAL(list->size, 0)
326 CU_ASSERT_EQUAL(list->capacity, (size_t) -1)
327 CU_ASSERT_PTR_EQUAL(list->allocator, cxTestingAllocator)
328 CU_ASSERT_EQUAL(list->itemsize, sizeof(void *))
329 CU_ASSERT_PTR_EQUAL(list->cmpfunc, cmp_int)
330
331 cxLinkedListDestroy(list);
332 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
333 }
334
335 void test_hl_ptr_linked_list_add(void) {
336 cxTestingAllocatorReset();
337
338 CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
339
340 int a = 5, b = 47, c = 13;
341
342 CU_ASSERT_EQUAL(cxListAdd(list, &a), 0)
343 CU_ASSERT_EQUAL(cxListAdd(list, &b), 0)
344 CU_ASSERT_EQUAL(cxListAdd(list, &c), 0)
345
346 CU_ASSERT_EQUAL(list->size, 3)
347 CU_ASSERT_TRUE(list->capacity >= list->size)
348
349 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5)
350 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47)
351 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13)
352
353 a = 9;
354 b = 10;
355 c = 11;
356
357 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 9)
358 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 10)
359 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 11)
360
361 cxLinkedListDestroy(list);
362 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
363 }
364
365 void test_hl_ptr_linked_list_last(void) {
366 cxTestingAllocatorReset();
367
368 CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
369 CU_ASSERT_PTR_NULL(cxListLast(list))
370
371 int a = 5, b = 47;
372
373 CU_ASSERT_EQUAL(cxListAdd(list, &a), 0)
374 CU_ASSERT_EQUAL(*(int *) cxListLast(list), 5)
375 CU_ASSERT_EQUAL(cxListAdd(list, &b), 0)
376 CU_ASSERT_EQUAL(*(int *) cxListLast(list), 47)
377
378 cxLinkedListDestroy(list);
379 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
380 }
381
382 void test_hl_ptr_linked_list_insert(void) {
383 cxTestingAllocatorReset();
384
385 CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
386
387 int a = 5, b = 47, c = 13, d = 42;
388
389 CU_ASSERT_NOT_EQUAL(cxListInsert(list, 1, &a), 0)
390 CU_ASSERT_EQUAL(list->size, 0)
391 CU_ASSERT_EQUAL(cxListInsert(list, 0, &a), 0)
392 CU_ASSERT_EQUAL(list->size, 1)
393 CU_ASSERT_EQUAL(cxListInsert(list, 0, &b), 0)
394 CU_ASSERT_EQUAL(list->size, 2)
395 CU_ASSERT_EQUAL(cxListInsert(list, 1, &c), 0)
396 CU_ASSERT_EQUAL(list->size, 3)
397 CU_ASSERT_EQUAL(cxListInsert(list, 3, &d), 0)
398
399 CU_ASSERT_EQUAL(list->size, 4)
400 CU_ASSERT_TRUE(list->capacity >= list->size)
401
402 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47)
403 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 13)
404 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 5)
405 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 3), 42)
406
407 cxLinkedListDestroy(list);
408 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
409 }
410
411 void test_hl_ptr_linked_list_remove(void) {
412 cxTestingAllocatorReset();
413
414 int a = 5, b = 47, c = 42, d = 13;
415 CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
416
417 cxListAdd(list, &a);
418 cxListAdd(list, &b);
419 cxListAdd(list, &c);
420 cxListAdd(list, &d);
421
422 CU_ASSERT_EQUAL(list->size, 4)
423 CU_ASSERT_TRUE(list->capacity >= list->size)
424
425 CU_ASSERT_NOT_EQUAL(cxListRemove(list, 4), 0)
426
427 CU_ASSERT_EQUAL(cxListRemove(list, 2), 0)
428 CU_ASSERT_EQUAL(list->size, 3)
429 CU_ASSERT_TRUE(list->capacity >= list->size)
430 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5)
431 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47)
432 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13)
433
434 CU_ASSERT_EQUAL(cxListRemove(list, 0), 0)
435 CU_ASSERT_EQUAL(list->size, 2)
436 CU_ASSERT_TRUE(list->capacity >= list->size)
437 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47)
438 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 13)
439
440 CU_ASSERT_EQUAL(cxListRemove(list, 1), 0)
441 CU_ASSERT_EQUAL(list->size, 1)
442 CU_ASSERT_TRUE(list->capacity >= list->size)
443 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47)
444
445 CU_ASSERT_EQUAL(cxListRemove(list, 0), 0)
446 CU_ASSERT_EQUAL(list->size, 0)
447 CU_ASSERT_TRUE(list->capacity >= list->size)
448
449 CU_ASSERT_NOT_EQUAL(cxListRemove(list, 0), 0)
450
451 cxLinkedListDestroy(list);
452 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
453 }
454
455 void test_hl_ptr_linked_list_find(void) {
456 cxTestingAllocatorReset();
457
458 int a = 5, b = 47, c = 13, criteria;
459 CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
460
461 cxListAdd(list, &a);
462 cxListAdd(list, &b);
463 cxListAdd(list, &c);
464
465 CU_ASSERT_EQUAL(list->size, 3)
466 CU_ASSERT_TRUE(list->capacity >= list->size)
467
468 criteria = 5;
469 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 0)
470 criteria = 47;
471 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 1)
472 criteria = 13;
473 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 2)
474 criteria = 9000;
475 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3)
476 criteria = -5;
477 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3)
478 b = -5;
479 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 1)
480
481 cxLinkedListDestroy(list);
482 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
483 }
484
332 int main() { 485 int main() {
333 CU_pSuite suite = NULL; 486 CU_pSuite suite = NULL;
334 487
335 if (CUE_SUCCESS != CU_initialize_registry()) { 488 if (CUE_SUCCESS != CU_initialize_registry()) {
336 return CU_get_error(); 489 return CU_get_error();
348 cu_add_test(suite, test_hl_linked_list_add); 501 cu_add_test(suite, test_hl_linked_list_add);
349 cu_add_test(suite, test_hl_linked_list_last); 502 cu_add_test(suite, test_hl_linked_list_last);
350 cu_add_test(suite, test_hl_linked_list_insert); 503 cu_add_test(suite, test_hl_linked_list_insert);
351 cu_add_test(suite, test_hl_linked_list_remove); 504 cu_add_test(suite, test_hl_linked_list_remove);
352 cu_add_test(suite, test_hl_linked_list_find); 505 cu_add_test(suite, test_hl_linked_list_find);
506
507 suite = CU_add_suite("high level pointer linked list", NULL, NULL);
508
509 cu_add_test(suite, test_hl_ptr_linked_list_create);
510 cu_add_test(suite, test_hl_ptr_linked_list_add);
511 cu_add_test(suite, test_hl_ptr_linked_list_last);
512 cu_add_test(suite, test_hl_ptr_linked_list_insert);
513 cu_add_test(suite, test_hl_ptr_linked_list_remove);
514 cu_add_test(suite, test_hl_ptr_linked_list_find);
353 515
354 CU_basic_set_mode(UCX_CU_BRM); 516 CU_basic_set_mode(UCX_CU_BRM);
355 517
356 int exitcode; 518 int exitcode;
357 if (CU_basic_run_tests()) { 519 if (CU_basic_run_tests()) {

mercurial