tests/test_list.c

changeset 993
b642eca4b956
parent 986
38fa7e41194c
child 998
bb196054f3fd
equal deleted inserted replaced
992:14ca894190fd 993:b642eca4b956
838 CX_TEST(test_empty_list_noops) { 838 CX_TEST(test_empty_list_noops) {
839 CX_TEST_DO { 839 CX_TEST_DO {
840 CxList copy = *cxEmptyList; 840 CxList copy = *cxEmptyList;
841 cxListSort(cxEmptyList); 841 cxListSort(cxEmptyList);
842 cxListClear(cxEmptyList); 842 cxListClear(cxEmptyList);
843 cxListDestroy(cxEmptyList); 843 cxListFree(cxEmptyList);
844 CX_TEST_ASSERT(0 == memcmp(&copy, cxEmptyList, sizeof(CxList))); // NOLINT(*-suspicious-memory-comparison) 844 CX_TEST_ASSERT(0 == memcmp(&copy, cxEmptyList, sizeof(CxList))); // NOLINT(*-suspicious-memory-comparison)
845 } 845 }
846 } 846 }
847 847
848 CX_TEST(test_empty_list_at) { 848 CX_TEST(test_empty_list_at) {
878 CX_TEST_ASSERT(0 < cxListCompare(ll, cxEmptyList)); 878 CX_TEST_ASSERT(0 < cxListCompare(ll, cxEmptyList));
879 CX_TEST_ASSERT(0 < cxListCompare(al, cxEmptyList)); 879 CX_TEST_ASSERT(0 < cxListCompare(al, cxEmptyList));
880 CX_TEST_ASSERT(0 > cxListCompare(cxEmptyList, ll)); 880 CX_TEST_ASSERT(0 > cxListCompare(cxEmptyList, ll));
881 CX_TEST_ASSERT(0 > cxListCompare(cxEmptyList, al)); 881 CX_TEST_ASSERT(0 > cxListCompare(cxEmptyList, al));
882 } 882 }
883 cxListDestroy(ll); 883 cxListFree(ll);
884 cxListDestroy(al); 884 cxListFree(al);
885 } 885 }
886 886
887 CX_TEST(test_list_ll_create) { 887 CX_TEST(test_list_ll_create) {
888 CxTestingAllocator talloc; 888 CxTestingAllocator talloc;
889 cx_testing_allocator_init(&talloc); 889 cx_testing_allocator_init(&talloc);
897 CX_TEST_ASSERT(list->collection.destructor_data == NULL); 897 CX_TEST_ASSERT(list->collection.destructor_data == NULL);
898 CX_TEST_ASSERT(cxListSize(list) == 0); 898 CX_TEST_ASSERT(cxListSize(list) == 0);
899 CX_TEST_ASSERT(list->collection.allocator == alloc); 899 CX_TEST_ASSERT(list->collection.allocator == alloc);
900 CX_TEST_ASSERT(list->collection.cmpfunc == cx_cmp_int); 900 CX_TEST_ASSERT(list->collection.cmpfunc == cx_cmp_int);
901 CX_TEST_ASSERT(!cxListIsStoringPointers(list)); 901 CX_TEST_ASSERT(!cxListIsStoringPointers(list));
902 cxListDestroy(list); 902 cxListFree(list);
903 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 903 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
904 } 904 }
905 cx_testing_allocator_destroy(&talloc); 905 cx_testing_allocator_destroy(&talloc);
906 } 906 }
907 907
916 CX_TEST_ASSERT(cxListSize(list) == 0); 916 CX_TEST_ASSERT(cxListSize(list) == 0);
917 CX_TEST_ASSERT(list->collection.allocator == cxDefaultAllocator); 917 CX_TEST_ASSERT(list->collection.allocator == cxDefaultAllocator);
918 CX_TEST_ASSERT(list->collection.cmpfunc == NULL); 918 CX_TEST_ASSERT(list->collection.cmpfunc == NULL);
919 CX_TEST_ASSERT(!cxListIsStoringPointers(list)); 919 CX_TEST_ASSERT(!cxListIsStoringPointers(list));
920 } 920 }
921 cxListDestroy(list); 921 cxListFree(list);
922 } 922 }
923 923
924 CX_TEST(test_list_ll_store_pointers) { 924 CX_TEST(test_list_ll_store_pointers) {
925 CxList *list = cxLinkedListCreateSimple(47); 925 CxList *list = cxLinkedListCreateSimple(47);
926 CX_TEST_DO { 926 CX_TEST_DO {
933 cxListStoreObjects(list); 933 cxListStoreObjects(list);
934 CX_TEST_ASSERT(list->cl != NULL); 934 CX_TEST_ASSERT(list->cl != NULL);
935 CX_TEST_ASSERT(list->climpl == NULL); 935 CX_TEST_ASSERT(list->climpl == NULL);
936 CX_TEST_ASSERT(!cxListIsStoringPointers(list)); 936 CX_TEST_ASSERT(!cxListIsStoringPointers(list));
937 } 937 }
938 cxListDestroy(list); 938 cxListFree(list);
939 } 939 }
940 940
941 CX_TEST(test_list_ll_create_simple_for_pointers) { 941 CX_TEST(test_list_ll_create_simple_for_pointers) {
942 CxList *list = cxLinkedListCreateSimple(CX_STORE_POINTERS); 942 CxList *list = cxLinkedListCreateSimple(CX_STORE_POINTERS);
943 CX_TEST_DO { 943 CX_TEST_DO {
949 CX_TEST_ASSERT(cxListSize(list) == 0); 949 CX_TEST_ASSERT(cxListSize(list) == 0);
950 CX_TEST_ASSERT(list->collection.allocator == cxDefaultAllocator); 950 CX_TEST_ASSERT(list->collection.allocator == cxDefaultAllocator);
951 CX_TEST_ASSERT(list->collection.cmpfunc == cx_cmp_ptr); 951 CX_TEST_ASSERT(list->collection.cmpfunc == cx_cmp_ptr);
952 CX_TEST_ASSERT(cxListIsStoringPointers(list)); 952 CX_TEST_ASSERT(cxListIsStoringPointers(list));
953 } 953 }
954 cxListDestroy(list); 954 cxListFree(list);
955 } 955 }
956 956
957 CX_TEST(test_list_arl_create) { 957 CX_TEST(test_list_arl_create) {
958 CxTestingAllocator talloc; 958 CxTestingAllocator talloc;
959 cx_testing_allocator_init(&talloc); 959 cx_testing_allocator_init(&talloc);
967 CX_TEST_ASSERT(list->collection.destructor_data == NULL); 967 CX_TEST_ASSERT(list->collection.destructor_data == NULL);
968 CX_TEST_ASSERT(cxListSize(list) == 0); 968 CX_TEST_ASSERT(cxListSize(list) == 0);
969 CX_TEST_ASSERT(list->collection.allocator == alloc); 969 CX_TEST_ASSERT(list->collection.allocator == alloc);
970 CX_TEST_ASSERT(list->collection.cmpfunc == cx_cmp_int); 970 CX_TEST_ASSERT(list->collection.cmpfunc == cx_cmp_int);
971 CX_TEST_ASSERT(!cxListIsStoringPointers(list)); 971 CX_TEST_ASSERT(!cxListIsStoringPointers(list));
972 cxListDestroy(list); 972 cxListFree(list);
973 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 973 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
974 } 974 }
975 cx_testing_allocator_destroy(&talloc); 975 cx_testing_allocator_destroy(&talloc);
976 } 976 }
977 977
986 CX_TEST_ASSERT(cxListSize(list) == 0); 986 CX_TEST_ASSERT(cxListSize(list) == 0);
987 CX_TEST_ASSERT(list->collection.allocator == cxDefaultAllocator); 987 CX_TEST_ASSERT(list->collection.allocator == cxDefaultAllocator);
988 CX_TEST_ASSERT(list->collection.cmpfunc == NULL); 988 CX_TEST_ASSERT(list->collection.cmpfunc == NULL);
989 CX_TEST_ASSERT(!cxListIsStoringPointers(list)); 989 CX_TEST_ASSERT(!cxListIsStoringPointers(list));
990 } 990 }
991 cxListDestroy(list); 991 cxListFree(list);
992 } 992 }
993 993
994 CX_TEST(test_list_arl_create_simple_for_pointers) { 994 CX_TEST(test_list_arl_create_simple_for_pointers) {
995 CxList *list = cxArrayListCreateSimple(CX_STORE_POINTERS, 8); 995 CxList *list = cxArrayListCreateSimple(CX_STORE_POINTERS, 8);
996 CX_TEST_DO { 996 CX_TEST_DO {
1002 CX_TEST_ASSERT(cxListSize(list) == 0); 1002 CX_TEST_ASSERT(cxListSize(list) == 0);
1003 CX_TEST_ASSERT(list->collection.allocator == cxDefaultAllocator); 1003 CX_TEST_ASSERT(list->collection.allocator == cxDefaultAllocator);
1004 CX_TEST_ASSERT(list->collection.cmpfunc == cx_cmp_ptr); 1004 CX_TEST_ASSERT(list->collection.cmpfunc == cx_cmp_ptr);
1005 CX_TEST_ASSERT(cxListIsStoringPointers(list)); 1005 CX_TEST_ASSERT(cxListIsStoringPointers(list));
1006 } 1006 }
1007 cxListDestroy(list); 1007 cxListFree(list);
1008 } 1008 }
1009 1009
1010 static void test_fake_simple_int_destr(void *elem) { 1010 static void test_fake_simple_int_destr(void *elem) {
1011 *(int *) elem = 42; 1011 *(int *) elem = 42;
1012 } 1012 }
1018 CX_TEST_DO { 1018 CX_TEST_DO {
1019 void *item = cxMalloc(alloc, sizeof(int)); 1019 void *item = cxMalloc(alloc, sizeof(int));
1020 CxList *list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS); 1020 CxList *list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS);
1021 cxListAdd(list, item); 1021 cxListAdd(list, item);
1022 CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc)); 1022 CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc));
1023 cxListDestroy(list); 1023 cxListFree(list);
1024 // item is not yet freed 1024 // item is not yet freed
1025 CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc)); 1025 CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc));
1026 cxFree(alloc, item); 1026 cxFree(alloc, item);
1027 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 1027 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
1028 } 1028 }
1033 CX_TEST_DO { 1033 CX_TEST_DO {
1034 int item = 0; 1034 int item = 0;
1035 CxList *list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS); 1035 CxList *list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS);
1036 list->collection.simple_destructor = test_fake_simple_int_destr; 1036 list->collection.simple_destructor = test_fake_simple_int_destr;
1037 cxListAdd(list, &item); 1037 cxListAdd(list, &item);
1038 cxListDestroy(list); 1038 cxListFree(list);
1039 CX_TEST_ASSERT(item == 42); 1039 CX_TEST_ASSERT(item == 42);
1040 } 1040 }
1041 } 1041 }
1042 1042
1043 CX_TEST(test_list_pll_destroy_adv_destr) { 1043 CX_TEST(test_list_pll_destroy_adv_destr) {
1049 CxList *list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS); 1049 CxList *list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS);
1050 list->collection.destructor_data = alloc; 1050 list->collection.destructor_data = alloc;
1051 list->collection.advanced_destructor = (cx_destructor_func2) cxFree; 1051 list->collection.advanced_destructor = (cx_destructor_func2) cxFree;
1052 cxListAdd(list, item); 1052 cxListAdd(list, item);
1053 CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc)); 1053 CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc));
1054 cxListDestroy(list); 1054 cxListFree(list);
1055 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 1055 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
1056 } 1056 }
1057 cx_testing_allocator_destroy(&talloc); 1057 cx_testing_allocator_destroy(&talloc);
1058 } 1058 }
1059 1059
1064 CX_TEST_DO { 1064 CX_TEST_DO {
1065 void *item = cxMalloc(alloc, sizeof(int)); 1065 void *item = cxMalloc(alloc, sizeof(int));
1066 CxList *list = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS, 4); 1066 CxList *list = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS, 4);
1067 cxListAdd(list, item); 1067 cxListAdd(list, item);
1068 CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc)); 1068 CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc));
1069 cxListDestroy(list); 1069 cxListFree(list);
1070 // item is not yet freed 1070 // item is not yet freed
1071 CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc)); 1071 CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc));
1072 cxFree(alloc, item); 1072 cxFree(alloc, item);
1073 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 1073 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
1074 } 1074 }
1079 CX_TEST_DO { 1079 CX_TEST_DO {
1080 int item = 0; 1080 int item = 0;
1081 CxList *list = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS, 4); 1081 CxList *list = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS, 4);
1082 list->collection.simple_destructor = test_fake_simple_int_destr; 1082 list->collection.simple_destructor = test_fake_simple_int_destr;
1083 cxListAdd(list, &item); 1083 cxListAdd(list, &item);
1084 cxListDestroy(list); 1084 cxListFree(list);
1085 CX_TEST_ASSERT(item == 42); 1085 CX_TEST_ASSERT(item == 42);
1086 } 1086 }
1087 } 1087 }
1088 1088
1089 CX_TEST(test_list_parl_destroy_adv_destr) { 1089 CX_TEST(test_list_parl_destroy_adv_destr) {
1095 CxList *list = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS, 4); 1095 CxList *list = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS, 4);
1096 list->collection.destructor_data = alloc; 1096 list->collection.destructor_data = alloc;
1097 list->collection.advanced_destructor = (cx_destructor_func2) cxFree; 1097 list->collection.advanced_destructor = (cx_destructor_func2) cxFree;
1098 cxListAdd(list, item); 1098 cxListAdd(list, item);
1099 CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc)); 1099 CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc));
1100 cxListDestroy(list); 1100 cxListFree(list);
1101 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 1101 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
1102 } 1102 }
1103 cx_testing_allocator_destroy(&talloc); 1103 cx_testing_allocator_destroy(&talloc);
1104 } 1104 }
1105 1105
1107 CxTestingAllocator talloc; \ 1107 CxTestingAllocator talloc; \
1108 cx_testing_allocator_init(&talloc); \ 1108 cx_testing_allocator_init(&talloc); \
1109 CxAllocator *alloc = &talloc.base; \ 1109 CxAllocator *alloc = &talloc.base; \
1110 CX_TEST_DO { 1110 CX_TEST_DO {
1111 #define tear_down_combo \ 1111 #define tear_down_combo \
1112 cxListDestroy(list); \ 1112 cxListFree(list); \
1113 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));\ 1113 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));\
1114 } \ 1114 } \
1115 cx_testing_allocator_destroy(&talloc); 1115 cx_testing_allocator_destroy(&talloc);
1116 #define roll_out_test_invokers(name) \ 1116 #define roll_out_test_invokers(name) \
1117 CX_TEST(test_list_ll_##name) { \ 1117 CX_TEST(test_list_ll_##name) { \
1699 const size_t len = 47; \ 1699 const size_t len = 47; \
1700 int *testdata = int_test_data_added_to_list(list, isptrlist, len); \ 1700 int *testdata = int_test_data_added_to_list(list, isptrlist, len); \
1701 CxList *other = otherctr; \ 1701 CxList *other = otherctr; \
1702 for (size_t i = 0; i < len; i++) cxListAdd(other, &testdata[i]); \ 1702 for (size_t i = 0; i < len; i++) cxListAdd(other, &testdata[i]); \
1703 CX_TEST_CALL_SUBROUTINE(test_list_verify_compare, list, other); \ 1703 CX_TEST_CALL_SUBROUTINE(test_list_verify_compare, list, other); \
1704 cxListDestroy(other); \ 1704 cxListFree(other); \
1705 free(testdata); \ 1705 free(testdata); \
1706 }) 1706 })
1707 1707
1708 roll_out_compare_tests( 1708 roll_out_compare_tests(
1709 ll, cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int)) 1709 ll, cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int))
1726 int *testdata = int_test_data_added_to_list(list, isptrlist, len); 1726 int *testdata = int_test_data_added_to_list(list, isptrlist, len);
1727 CxList *other = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int), 50); 1727 CxList *other = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int), 50);
1728 do_set_default_class_funcs(other); 1728 do_set_default_class_funcs(other);
1729 for (size_t i = 0; i < len; i++) cxListAdd(other, &testdata[i]); 1729 for (size_t i = 0; i < len; i++) cxListAdd(other, &testdata[i]);
1730 CX_TEST_CALL_SUBROUTINE(test_list_verify_compare, list, other); 1730 CX_TEST_CALL_SUBROUTINE(test_list_verify_compare, list, other);
1731 cxListDestroy(other); 1731 cxListFree(other);
1732 free(testdata); 1732 free(testdata);
1733 }) 1733 })
1734 1734
1735 static unsigned destr_test_ctr; 1735 static unsigned destr_test_ctr;
1736 static int destr_last_value; 1736 static int destr_last_value;

mercurial