29 #include "cx/test.h" |
29 #include "cx/test.h" |
30 #include "util_allocator.h" |
30 #include "util_allocator.h" |
31 |
31 |
32 #include "cx/kv_list.h" |
32 #include "cx/kv_list.h" |
33 |
33 |
|
34 static int kv_list_test_destr_val; |
|
35 static void kv_list_test_destr(void *data) { |
|
36 kv_list_test_destr_val = *(int*)data; |
|
37 } |
|
38 |
|
39 static void *kv_list_test_destr2_extra; |
|
40 static int kv_list_test_destr2_val; |
|
41 static void kv_list_test_destr2(void* extra, void *data) { |
|
42 kv_list_test_destr2_extra = extra; |
|
43 kv_list_test_destr2_val = *(int*)data; |
|
44 } |
|
45 |
34 CX_TEST(test_kv_list_map_as_list) { |
46 CX_TEST(test_kv_list_map_as_list) { |
35 CxList *list = cxKvListCreateSimple(sizeof(int)); |
47 CxList *list = cxKvListCreateSimple(sizeof(int)); |
36 CX_TEST_DO { |
48 CX_TEST_DO { |
37 CxMap *map = cxKvListAsMap(list); |
49 CxMap *map = cxKvListAsMap(list); |
38 CX_TEST_ASSERT(map != NULL); |
50 CX_TEST_ASSERT(map != NULL); |
148 CX_TEST_ASSERT(cxMapSize(map) == 1); |
160 CX_TEST_ASSERT(cxMapSize(map) == 1); |
149 CX_TEST_ASSERT(cxMapGet(map, "hij") == NULL); |
161 CX_TEST_ASSERT(cxMapGet(map, "hij") == NULL); |
150 |
162 |
151 y = 666; |
163 y = 666; |
152 CX_TEST_ASSERT(cxListSize(list) == cxListFindRemove(list, &y)); |
164 CX_TEST_ASSERT(cxListSize(list) == cxListFindRemove(list, &y)); |
|
165 |
|
166 y = 37; |
|
167 CX_TEST_ASSERT(0 == cxListFindRemove(list, &y)); |
|
168 CX_TEST_ASSERT(cxListSize(list) == 0); |
|
169 CX_TEST_ASSERT(cxMapSize(map) == 0); |
|
170 |
|
171 CX_TEST_ASSERT(0 == cxListFindRemove(list, &y)); |
153 } |
172 } |
154 cxListFree(list); |
173 cxListFree(list); |
155 } |
174 } |
156 |
175 |
157 CX_TEST(test_kv_list_remove_and_get) { |
176 CX_TEST(test_kv_list_remove_and_get) { |
297 CX_TEST_ASSERT((int*)cxMapGet(map, "xyz") == &x); |
316 CX_TEST_ASSERT((int*)cxMapGet(map, "xyz") == &x); |
298 } |
317 } |
299 cxListFree(list); |
318 cxListFree(list); |
300 } |
319 } |
301 |
320 |
|
321 CX_TEST(test_kv_list_map_put_not_hashed) { |
|
322 CxList *list = cxKvListCreateSimple(sizeof(int)); |
|
323 int x; |
|
324 CX_TEST_DO { |
|
325 CxMap *map = cxKvListAsMap(list); |
|
326 x = 13; |
|
327 // test with a custom key that was not hashed, yet |
|
328 CxHashKey key = {}; |
|
329 key.data = "xyz"; |
|
330 key.len = 3; |
|
331 CX_TEST_ASSERT(0 == cxMapPut(map, key, &x)); |
|
332 CX_TEST_ASSERT(*(int*)cxListAt(list, 0) == 13); |
|
333 CX_TEST_ASSERT(*(int*)cxMapGet(map, "xyz") == 13); |
|
334 |
|
335 // remove and check if the copied key is correctly removed |
|
336 CX_TEST_ASSERT(0 == cxListRemove(list, 0)); |
|
337 CX_TEST_ASSERT(cxMapGet(map, "xyz") == NULL); |
|
338 } |
|
339 cxListFree(list); |
|
340 } |
|
341 |
302 CX_TEST(test_kv_list_map_remove) { |
342 CX_TEST(test_kv_list_map_remove) { |
303 CxList *list = cxKvListCreateSimple(sizeof(int)); |
343 CxList *list = cxKvListCreateSimple(sizeof(int)); |
304 int x; |
344 int x; |
305 CX_TEST_DO { |
345 CX_TEST_DO { |
306 CxMap *map = cxKvListAsMap(list); |
346 CxMap *map = cxKvListAsMap(list); |
324 CX_TEST_ASSERT(0 != cxMapRemove(map, "xyz")); |
364 CX_TEST_ASSERT(0 != cxMapRemove(map, "xyz")); |
325 } |
365 } |
326 cxListFree(list); |
366 cxListFree(list); |
327 } |
367 } |
328 |
368 |
|
369 CX_TEST(test_kv_list_map_remove_and_get) { |
|
370 CxList *list = cxKvListCreateSimple(sizeof(int)); |
|
371 int x, y; |
|
372 CX_TEST_DO { |
|
373 CxMap *map = cxKvListAsMap(list); |
|
374 |
|
375 x = 13; |
|
376 CX_TEST_ASSERT(0 == cxMapPut(map, "xyz", &x)); |
|
377 |
|
378 x = 37; |
|
379 CX_TEST_ASSERT(0 == cxMapPut(map, "abc", &x)); |
|
380 |
|
381 CX_TEST_ASSERT(cxMapSize(map) == 2); |
|
382 cxDefineDestructor(map, kv_list_test_destr); |
|
383 kv_list_test_destr_val = 0; |
|
384 y = 0; |
|
385 CX_TEST_ASSERT(0 == cxMapRemoveAndGet(map, "xyz", &y)); |
|
386 CX_TEST_ASSERT(y == 13); |
|
387 // destr not called! |
|
388 CX_TEST_ASSERT(kv_list_test_destr_val == 0); |
|
389 CX_TEST_ASSERT(cxMapSize(map) == 1); |
|
390 CX_TEST_ASSERT(cxMapGet(map, "abc") != NULL); |
|
391 CX_TEST_ASSERT(cxMapGet(map, "xyz") == NULL); |
|
392 |
|
393 CX_TEST_ASSERT(cxListSize(list) == 1); |
|
394 CX_TEST_ASSERT(*(int*)cxListAt(list, 0) == 37); |
|
395 |
|
396 CX_TEST_ASSERT(0 != cxMapRemove(map, "xyz")); |
|
397 } |
|
398 cxListFree(list); |
|
399 } |
|
400 |
329 CX_TEST(test_kv_list_set_key) { |
401 CX_TEST(test_kv_list_set_key) { |
330 CxList *list = cxKvListCreateSimple(sizeof(int)); |
402 CxList *list = cxKvListCreateSimple(sizeof(int)); |
331 int x; |
403 int x; |
332 CX_TEST_DO { |
404 CX_TEST_DO { |
333 x = 47; |
405 x = 47; |
334 cxListAdd(list, &x); |
406 cxListAdd(list, &x); |
335 CX_TEST_ASSERT(0 == cxKvListSetKey(list, 0, "xyz")); |
407 CX_TEST_ASSERT(0 == cxKvListSetKey(list, 0, "xyz")); |
|
408 // index out of bounds: |
|
409 CX_TEST_ASSERT(0 != cxKvListSetKey(list, 1, "abc")); |
336 |
410 |
337 CxMap *map = cxKvListAsMap(list); |
411 CxMap *map = cxKvListAsMap(list); |
338 |
412 |
339 CX_TEST_ASSERT(cxMapSize(map) == 1); |
413 CX_TEST_ASSERT(cxMapSize(map) == 1); |
340 |
414 |
366 |
440 |
367 int *y = cxMapGet(map, "abc"); |
441 int *y = cxMapGet(map, "abc"); |
368 CX_TEST_ASSERT(y != NULL); |
442 CX_TEST_ASSERT(y != NULL); |
369 CX_TEST_ASSERT(*y == 11); |
443 CX_TEST_ASSERT(*y == 11); |
370 |
444 |
371 cxKvListRemoveKey(list, 1); |
445 CX_TEST_ASSERT(0 == cxKvListRemoveKey(list, 1)); |
372 CX_TEST_ASSERT(cxMapGet(map, "abc") == NULL); |
446 CX_TEST_ASSERT(cxMapGet(map, "abc") == NULL); |
373 CX_TEST_ASSERT(cxMapSize(map) == 2); |
447 CX_TEST_ASSERT(cxMapSize(map) == 2); |
374 CX_TEST_ASSERT(cxListSize(list) == 3); |
448 CX_TEST_ASSERT(cxListSize(list) == 3); |
375 |
449 |
376 CX_TEST_ASSERT(*(int*)cxListAt(list, 1) == 11); |
450 CX_TEST_ASSERT(*(int*)cxListAt(list, 1) == 11); |
524 |
595 |
525 cxDefineDestructor(map, kv_list_test_destr); |
596 cxDefineDestructor(map, kv_list_test_destr); |
526 kv_list_test_destr_val = 0; |
597 kv_list_test_destr_val = 0; |
527 CX_TEST_ASSERT(0 == cxMapRemove(map, "xyz")); |
598 CX_TEST_ASSERT(0 == cxMapRemove(map, "xyz")); |
528 CX_TEST_ASSERT(kv_list_test_destr_val == 0xabcd); |
599 CX_TEST_ASSERT(kv_list_test_destr_val == 0xabcd); |
|
600 } |
|
601 cxMapFree(map); |
|
602 } |
|
603 |
|
604 CX_TEST(test_kv_list_list_remove_destr2_in_list) { |
|
605 CxList *list = cxKvListCreateSimple(sizeof(int)); |
|
606 int x; |
|
607 CX_TEST_DO { |
|
608 x = 0xabcd; |
|
609 CX_TEST_ASSERT(0 == cxListAdd(list, &x)); |
|
610 cxKvListSetKey(list, 0, "xyz"); |
|
611 |
|
612 cxDefineAdvancedDestructor(list, kv_list_test_destr2, (void*)0xef47); |
|
613 kv_list_test_destr2_val = 0; |
|
614 kv_list_test_destr2_extra = NULL; |
|
615 CX_TEST_ASSERT(0 == cxListRemove(list, 0)); |
|
616 CX_TEST_ASSERT(kv_list_test_destr2_val == 0xabcd); |
|
617 CX_TEST_ASSERT(kv_list_test_destr2_extra == (void*)0xef47); |
|
618 } |
|
619 cxListFree(list); |
|
620 } |
|
621 |
|
622 CX_TEST(test_kv_list_list_remove_destr2_in_map) { |
|
623 CxList *list = cxKvListCreateSimple(sizeof(int)); |
|
624 int x; |
|
625 CX_TEST_DO { |
|
626 x = 0xabcd; |
|
627 CX_TEST_ASSERT(0 == cxListAdd(list, &x)); |
|
628 cxKvListSetKey(list, 0, "xyz"); |
|
629 CxMap *map = cxKvListAsMap(list); |
|
630 cxDefineAdvancedDestructor(map, kv_list_test_destr2, (void*)0xef47); |
|
631 kv_list_test_destr2_val = 0; |
|
632 kv_list_test_destr2_extra = NULL; |
|
633 CX_TEST_ASSERT(0 == cxListRemove(list, 0)); |
|
634 CX_TEST_ASSERT(kv_list_test_destr2_val == 0xabcd); |
|
635 CX_TEST_ASSERT(kv_list_test_destr2_extra == (void*)0xef47); |
|
636 } |
|
637 cxListFree(list); |
|
638 } |
|
639 |
|
640 CX_TEST(test_kv_list_map_remove_destr2_in_list) { |
|
641 CxMap *map = cxKvListCreateAsMapSimple(sizeof(int)); |
|
642 int x; |
|
643 CX_TEST_DO { |
|
644 x = 0xabcd; |
|
645 CX_TEST_ASSERT(0 == cxMapPut(map, "xyz", &x)); |
|
646 CxList *list = cxKvListAsList(map); |
|
647 cxDefineAdvancedDestructor(list, kv_list_test_destr2, (void*)0xef47); |
|
648 kv_list_test_destr2_val = 0; |
|
649 kv_list_test_destr2_extra = NULL; |
|
650 CX_TEST_ASSERT(0 == cxMapRemove(map, "xyz")); |
|
651 CX_TEST_ASSERT(kv_list_test_destr2_val == 0xabcd); |
|
652 CX_TEST_ASSERT(kv_list_test_destr2_extra == (void*)0xef47); |
|
653 } |
|
654 cxMapFree(map); |
|
655 } |
|
656 |
|
657 CX_TEST(test_kv_list_map_remove_destr2_in_map) { |
|
658 CxMap *map = cxKvListCreateAsMapSimple(sizeof(int)); |
|
659 int x; |
|
660 CX_TEST_DO { |
|
661 x = 0xabcd; |
|
662 CX_TEST_ASSERT(0 == cxMapPut(map, "xyz", &x)); |
|
663 |
|
664 cxDefineAdvancedDestructor(map, kv_list_test_destr2, (void*)0xef47); |
|
665 kv_list_test_destr2_val = 0; |
|
666 kv_list_test_destr2_extra = NULL; |
|
667 CX_TEST_ASSERT(0 == cxMapRemove(map, "xyz")); |
|
668 CX_TEST_ASSERT(kv_list_test_destr2_val == 0xabcd); |
|
669 CX_TEST_ASSERT(kv_list_test_destr2_extra == (void*)0xef47); |
529 } |
670 } |
530 cxMapFree(map); |
671 cxMapFree(map); |
531 } |
672 } |
532 |
673 |
533 CX_TEST(test_kv_list_list_clear_destr_in_list) { |
674 CX_TEST(test_kv_list_list_clear_destr_in_list) { |
618 cx_test_register(suite, test_kv_list_remove_and_get); |
759 cx_test_register(suite, test_kv_list_remove_and_get); |
619 cx_test_register(suite, test_kv_list_remove_array); |
760 cx_test_register(suite, test_kv_list_remove_array); |
620 cx_test_register(suite, test_kv_list_remove_array_and_get); |
761 cx_test_register(suite, test_kv_list_remove_array_and_get); |
621 cx_test_register(suite, test_kv_list_map_put); |
762 cx_test_register(suite, test_kv_list_map_put); |
622 cx_test_register(suite, test_kv_list_map_put_ptr); |
763 cx_test_register(suite, test_kv_list_map_put_ptr); |
|
764 cx_test_register(suite, test_kv_list_map_put_not_hashed); |
623 cx_test_register(suite, test_kv_list_map_remove); |
765 cx_test_register(suite, test_kv_list_map_remove); |
|
766 cx_test_register(suite, test_kv_list_map_remove_and_get); |
624 cx_test_register(suite, test_kv_list_set_key); |
767 cx_test_register(suite, test_kv_list_set_key); |
625 cx_test_register(suite, test_kv_list_remove_key); |
768 cx_test_register(suite, test_kv_list_remove_key); |
626 cx_test_register(suite, test_kv_list_insert_with_key); |
769 cx_test_register(suite, test_kv_list_insert_with_key); |
627 cx_test_register(suite, test_kv_list_insert_ptr_with_key); |
770 cx_test_register(suite, test_kv_list_insert_ptr_with_key); |
628 cx_test_register(suite, test_kv_list_insert_array_and_set_keys); |
771 cx_test_register(suite, test_kv_list_insert_array_and_set_keys); |
629 cx_test_register(suite, test_kv_list_list_remove_destr_in_list); |
772 cx_test_register(suite, test_kv_list_list_remove_destr_in_list); |
630 cx_test_register(suite, test_kv_list_list_remove_destr_in_map); |
773 cx_test_register(suite, test_kv_list_list_remove_destr_in_map); |
631 cx_test_register(suite, test_kv_list_map_remove_destr_in_list); |
774 cx_test_register(suite, test_kv_list_map_remove_destr_in_list); |
632 cx_test_register(suite, test_kv_list_map_remove_destr_in_map); |
775 cx_test_register(suite, test_kv_list_map_remove_destr_in_map); |
|
776 cx_test_register(suite, test_kv_list_list_remove_destr2_in_list); |
|
777 cx_test_register(suite, test_kv_list_list_remove_destr2_in_map); |
|
778 cx_test_register(suite, test_kv_list_map_remove_destr2_in_list); |
|
779 cx_test_register(suite, test_kv_list_map_remove_destr2_in_map); |
633 cx_test_register(suite, test_kv_list_list_clear_destr_in_list); |
780 cx_test_register(suite, test_kv_list_list_clear_destr_in_list); |
634 cx_test_register(suite, test_kv_list_list_clear_destr_in_map); |
781 cx_test_register(suite, test_kv_list_list_clear_destr_in_map); |
635 cx_test_register(suite, test_kv_list_map_clear_destr_in_list); |
782 cx_test_register(suite, test_kv_list_map_clear_destr_in_list); |
636 cx_test_register(suite, test_kv_list_map_clear_destr_in_map); |
783 cx_test_register(suite, test_kv_list_map_clear_destr_in_map); |
637 cx_test_register(suite, test_kv_list_destr_ptr); |
784 cx_test_register(suite, test_kv_list_destr_ptr); |