tests/test_list.c

changeset 801
04aa3913c0e3
parent 800
1274e46b3013
child 803
0711d869ce4d
equal deleted inserted replaced
800:1274e46b3013 801:04aa3913c0e3
633 } 633 }
634 cxListDestroy(ll); 634 cxListDestroy(ll);
635 cxListDestroy(al); 635 cxListDestroy(al);
636 } 636 }
637 637
638 CX_TEST(test_list_ll_create) {
639 CxTestingAllocator talloc;
640 cx_testing_allocator_init(&talloc);
641 CxAllocator *alloc = &talloc.base;
642 CX_TEST_DO {
643 CxList *list = cxLinkedListCreate(alloc, cx_cmp_int, sizeof(int));
644 CX_TEST_ASSERT(list != NULL);
645 CX_TEST_ASSERT(list->item_size == sizeof(int));
646 CX_TEST_ASSERT(list->simple_destructor == NULL);
647 CX_TEST_ASSERT(list->advanced_destructor == NULL);
648 CX_TEST_ASSERT(list->destructor_data == NULL);
649 CX_TEST_ASSERT(cxListSize(list) == 0);
650 CX_TEST_ASSERT(list->allocator == alloc);
651 CX_TEST_ASSERT(list->cmpfunc == cx_cmp_int);
652 CX_TEST_ASSERT(!cxListIsStoringPointers(list));
653 cxListDestroy(list);
654 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
655 }
656 cx_testing_allocator_destroy(&talloc);
657 }
658
659 CX_TEST(test_list_ll_create_simple) {
660 CxList *list = cxLinkedListCreateSimple(sizeof(int));
661 CX_TEST_DO {
662 CX_TEST_ASSERT(list != NULL);
663 CX_TEST_ASSERT(list->item_size == sizeof(int));
664 CX_TEST_ASSERT(list->simple_destructor == NULL);
665 CX_TEST_ASSERT(list->advanced_destructor == NULL);
666 CX_TEST_ASSERT(list->destructor_data == NULL);
667 CX_TEST_ASSERT(cxListSize(list) == 0);
668 CX_TEST_ASSERT(list->allocator == cxDefaultAllocator);
669 CX_TEST_ASSERT(list->cmpfunc == NULL);
670 CX_TEST_ASSERT(!cxListIsStoringPointers(list));
671 }
672 cxListDestroy(list);
673 }
674
675 CX_TEST(test_list_ll_store_pointers) {
676 CxList *list = cxLinkedListCreateSimple(47);
677 CX_TEST_DO {
678 CX_TEST_ASSERT(!cxListIsStoringPointers(list));
679 cxListStorePointers(list);
680 CX_TEST_ASSERT(list->item_size == sizeof(void *));
681 CX_TEST_ASSERT(list->cl != NULL);
682 CX_TEST_ASSERT(list->climpl != NULL);
683 CX_TEST_ASSERT(cxListIsStoringPointers(list));
684 cxListStoreObjects(list);
685 CX_TEST_ASSERT(list->cl != NULL);
686 CX_TEST_ASSERT(list->climpl == NULL);
687 CX_TEST_ASSERT(!cxListIsStoringPointers(list));
688 }
689 cxListDestroy(list);
690 }
691
692 CX_TEST(test_list_ll_create_simple_for_pointers) {
693 CxList *list = cxLinkedListCreateSimple(CX_STORE_POINTERS);
694 CX_TEST_DO {
695 CX_TEST_ASSERT(list != NULL);
696 CX_TEST_ASSERT(list->item_size == sizeof(void*));
697 CX_TEST_ASSERT(list->simple_destructor == NULL);
698 CX_TEST_ASSERT(list->advanced_destructor == NULL);
699 CX_TEST_ASSERT(list->destructor_data == NULL);
700 CX_TEST_ASSERT(cxListSize(list) == 0);
701 CX_TEST_ASSERT(list->allocator == cxDefaultAllocator);
702 CX_TEST_ASSERT(list->cmpfunc == cx_cmp_ptr);
703 CX_TEST_ASSERT(cxListIsStoringPointers(list));
704 }
705 cxListDestroy(list);
706 }
707
708 CX_TEST(test_list_arl_create) {
709 CxTestingAllocator talloc;
710 cx_testing_allocator_init(&talloc);
711 CxAllocator *alloc = &talloc.base;
712 CX_TEST_DO {
713 CxList *list = cxArrayListCreate(alloc, cx_cmp_int, sizeof(int), 8);
714 CX_TEST_ASSERT(list != NULL);
715 CX_TEST_ASSERT(list->item_size == sizeof(int));
716 CX_TEST_ASSERT(list->simple_destructor == NULL);
717 CX_TEST_ASSERT(list->advanced_destructor == NULL);
718 CX_TEST_ASSERT(list->destructor_data == NULL);
719 CX_TEST_ASSERT(cxListSize(list) == 0);
720 CX_TEST_ASSERT(list->allocator == alloc);
721 CX_TEST_ASSERT(list->cmpfunc == cx_cmp_int);
722 CX_TEST_ASSERT(!cxListIsStoringPointers(list));
723 cxListDestroy(list);
724 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
725 }
726 cx_testing_allocator_destroy(&talloc);
727 }
728
729 CX_TEST(test_list_arl_create_simple) {
730 CxList *list = cxArrayListCreateSimple(sizeof(int), 8);
731 CX_TEST_DO {
732 CX_TEST_ASSERT(list != NULL);
733 CX_TEST_ASSERT(list->item_size == sizeof(int));
734 CX_TEST_ASSERT(list->simple_destructor == NULL);
735 CX_TEST_ASSERT(list->advanced_destructor == NULL);
736 CX_TEST_ASSERT(list->destructor_data == NULL);
737 CX_TEST_ASSERT(cxListSize(list) == 0);
738 CX_TEST_ASSERT(list->allocator == cxDefaultAllocator);
739 CX_TEST_ASSERT(list->cmpfunc == NULL);
740 CX_TEST_ASSERT(!cxListIsStoringPointers(list));
741 }
742 cxListDestroy(list);
743 }
744
745 CX_TEST(test_list_arl_create_simple_for_pointers) {
746 CxList *list = cxArrayListCreateSimple(CX_STORE_POINTERS, 8);
747 CX_TEST_DO {
748 CX_TEST_ASSERT(list != NULL);
749 CX_TEST_ASSERT(list->item_size == sizeof(void*));
750 CX_TEST_ASSERT(list->simple_destructor == NULL);
751 CX_TEST_ASSERT(list->advanced_destructor == NULL);
752 CX_TEST_ASSERT(list->destructor_data == NULL);
753 CX_TEST_ASSERT(cxListSize(list) == 0);
754 CX_TEST_ASSERT(list->allocator == cxDefaultAllocator);
755 CX_TEST_ASSERT(list->cmpfunc == cx_cmp_ptr);
756 CX_TEST_ASSERT(cxListIsStoringPointers(list));
757 }
758 cxListDestroy(list);
759 }
760
761 static void test_fake_simple_int_destr(void *elem) {
762 *(int *) elem = 42;
763 }
764
765 CX_TEST(test_list_pll_destroy_no_destr) {
766 CxTestingAllocator talloc;
767 cx_testing_allocator_init(&talloc);
768 CxAllocator *alloc = &talloc.base;
769 CX_TEST_DO {
770 void *item = cxMalloc(alloc, sizeof(int));
771 CxList *list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS);
772 cxListAdd(list, item);
773 CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc));
774 cxListDestroy(list);
775 // item is not yet freed
776 CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc));
777 cxFree(alloc, item);
778 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
779 }
780 cx_testing_allocator_destroy(&talloc);
781 }
782
783 CX_TEST(test_list_pll_destroy_simple_destr) {
784 CX_TEST_DO {
785 int item = 0;
786 CxList *list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS);
787 list->simple_destructor = test_fake_simple_int_destr;
788 cxListAdd(list, &item);
789 cxListDestroy(list);
790 CX_TEST_ASSERT(item == 42);
791 }
792 }
793
794 CX_TEST(test_list_pll_destroy_adv_destr) {
795 CxTestingAllocator talloc;
796 cx_testing_allocator_init(&talloc);
797 CxAllocator *alloc = &talloc.base;
798 CX_TEST_DO {
799 void *item = cxMalloc(alloc, sizeof(int));
800 CxList *list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS);
801 list->destructor_data = alloc;
802 list->advanced_destructor = (cx_destructor_func2) cxFree;
803 cxListAdd(list, item);
804 CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc));
805 cxListDestroy(list);
806 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
807 }
808 cx_testing_allocator_destroy(&talloc);
809 }
810
811 CX_TEST(test_list_parl_destroy_no_destr) {
812 CxTestingAllocator talloc;
813 cx_testing_allocator_init(&talloc);
814 CxAllocator *alloc = &talloc.base;
815 CX_TEST_DO {
816 void *item = cxMalloc(alloc, sizeof(int));
817 CxList *list = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS, 4);
818 cxListAdd(list, item);
819 CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc));
820 cxListDestroy(list);
821 // item is not yet freed
822 CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc));
823 cxFree(alloc, item);
824 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
825 }
826 cx_testing_allocator_destroy(&talloc);
827 }
828
829 CX_TEST(test_list_parl_destroy_simple_destr) {
830 CX_TEST_DO {
831 int item = 0;
832 CxList *list = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS, 4);
833 list->simple_destructor = test_fake_simple_int_destr;
834 cxListAdd(list, &item);
835 cxListDestroy(list);
836 CX_TEST_ASSERT(item == 42);
837 }
838 }
839
840 CX_TEST(test_list_parl_destroy_adv_destr) {
841 CxTestingAllocator talloc;
842 cx_testing_allocator_init(&talloc);
843 CxAllocator *alloc = &talloc.base;
844 CX_TEST_DO {
845 void *item = cxMalloc(alloc, sizeof(int));
846 CxList *list = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS, 4);
847 list->destructor_data = alloc;
848 list->advanced_destructor = (cx_destructor_func2) cxFree;
849 cxListAdd(list, item);
850 CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc));
851 cxListDestroy(list);
852 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
853 }
854 cx_testing_allocator_destroy(&talloc);
855 }
856
638 CxTestSuite *cx_test_suite_array_list(void) { 857 CxTestSuite *cx_test_suite_array_list(void) {
639 CxTestSuite *suite = cx_test_suite_new("array_list"); 858 CxTestSuite *suite = cx_test_suite_new("array_list");
859
860 cx_test_register(suite, test_list_arl_create);
861 cx_test_register(suite, test_list_arl_create_simple);
862 cx_test_register(suite, test_list_arl_create_simple_for_pointers);
863 cx_test_register(suite, test_list_parl_destroy_no_destr);
864 cx_test_register(suite, test_list_parl_destroy_simple_destr);
865 cx_test_register(suite, test_list_parl_destroy_adv_destr);
640 866
641 return suite; 867 return suite;
642 } 868 }
643 869
644 CxTestSuite *cx_test_suite_linked_list(void) { 870 CxTestSuite *cx_test_suite_linked_list(void) {
659 cx_test_register(suite, test_linked_list_size); 885 cx_test_register(suite, test_linked_list_size);
660 cx_test_register(suite, test_linked_list_sort_empty); 886 cx_test_register(suite, test_linked_list_sort_empty);
661 cx_test_register(suite, test_linked_list_sort); 887 cx_test_register(suite, test_linked_list_sort);
662 cx_test_register(suite, test_linked_list_reverse); 888 cx_test_register(suite, test_linked_list_reverse);
663 889
890 cx_test_register(suite, test_list_ll_create);
891 cx_test_register(suite, test_list_ll_create_simple);
892 cx_test_register(suite, test_list_ll_store_pointers);
893 cx_test_register(suite, test_list_ll_create_simple_for_pointers);
894 cx_test_register(suite, test_list_pll_destroy_no_destr);
895 cx_test_register(suite, test_list_pll_destroy_simple_destr);
896 cx_test_register(suite, test_list_pll_destroy_adv_destr);
897
664 return suite; 898 return suite;
665 } 899 }
666 900
667 CxTestSuite *cx_test_suite_empty_list(void) { 901 CxTestSuite *cx_test_suite_empty_list(void) {
668 CxTestSuite *suite = cx_test_suite_new("empty list dummy"); 902 CxTestSuite *suite = cx_test_suite_new("empty list dummy");

mercurial