| 729 const node *left = l; |
729 const node *left = l; |
| 730 const node *right = r; |
730 const node *right = r; |
| 731 return left->data - right->data; |
731 return left->data - right->data; |
| 732 } |
732 } |
| 733 |
733 |
| |
734 static int test_ccmp_node(const void *l, const void *r, void *c) { |
| |
735 if (c == NULL || *(int*)c != 1337) return -1; |
| |
736 const node *left = l; |
| |
737 const node *right = r; |
| |
738 return left->data - right->data; |
| |
739 } |
| |
740 |
| 734 const ptrdiff_t loc_prev = offsetof(struct node, prev); |
741 const ptrdiff_t loc_prev = offsetof(struct node, prev); |
| 735 const ptrdiff_t loc_next = offsetof(struct node, next); |
742 const ptrdiff_t loc_next = offsetof(struct node, next); |
| 736 const ptrdiff_t loc_data = offsetof(struct node, data); |
743 const ptrdiff_t loc_data = offsetof(struct node, data); |
| 737 |
744 |
| 738 static node *create_nodes_test_data(size_t len) { |
745 static node *create_nodes_test_data(size_t len) { |
| 851 CX_TEST_ASSERT(cx_linked_list_find(list, loc_next, loc_data, &s, &i, cx_cmp_int) == NULL); |
858 CX_TEST_ASSERT(cx_linked_list_find(list, loc_next, loc_data, &s, &i, cx_cmp_int) == NULL); |
| 852 } |
859 } |
| 853 destroy_nodes_test_data(list); |
860 destroy_nodes_test_data(list); |
| 854 } |
861 } |
| 855 |
862 |
| |
863 CX_TEST(test_linked_list_find_c) { |
| |
864 node *list = create_nodes_test_data(4); |
| |
865 assign_nodes_test_data(list, 2, 4, 6, 8); |
| |
866 CX_TEST_DO { |
| |
867 int z = 1337; |
| |
868 size_t i = 10; |
| |
869 int s; |
| |
870 s = 2; |
| |
871 node *n = list; |
| |
872 CX_TEST_ASSERT(cx_linked_list_find_c(list, loc_next, loc_data, &s, &i, test_ccmp_int, &z) == n); |
| |
873 CX_TEST_ASSERT(i == 0); |
| |
874 n = n->next; |
| |
875 s = 4; |
| |
876 CX_TEST_ASSERT(cx_linked_list_find_c(list, loc_next, loc_data, &s, &i, test_ccmp_int, &z) == n); |
| |
877 CX_TEST_ASSERT(i == 1); |
| |
878 n = n->next; |
| |
879 s = 6; |
| |
880 CX_TEST_ASSERT(cx_linked_list_find_c(list, loc_next, loc_data, &s, &i, test_ccmp_int, &z) == n); |
| |
881 CX_TEST_ASSERT(i == 2); |
| |
882 n = n->next; |
| |
883 s = 8; |
| |
884 CX_TEST_ASSERT(cx_linked_list_find_c(list, loc_next, loc_data, &s, &i, test_ccmp_int, &z) == n); |
| |
885 CX_TEST_ASSERT(i == 3); |
| |
886 s = 10; |
| |
887 CX_TEST_ASSERT(cx_linked_list_find_c(list, loc_next, loc_data, &s, &i, test_ccmp_int, &z) == NULL); |
| |
888 s = -2; |
| |
889 CX_TEST_ASSERT(cx_linked_list_find_c(list, loc_next, loc_data, &s, &i, test_ccmp_int, &z) == NULL); |
| |
890 } |
| |
891 destroy_nodes_test_data(list); |
| |
892 } |
| |
893 |
| 856 CX_TEST(test_linked_list_compare) { |
894 CX_TEST(test_linked_list_compare) { |
| 857 void *la = create_nodes_test_data(4); |
895 void *la = create_nodes_test_data(4); |
| 858 void *lb = create_nodes_test_data(3); |
896 void *lb = create_nodes_test_data(3); |
| 859 void *lc = create_nodes_test_data(4); |
897 void *lc = create_nodes_test_data(4); |
| 860 assign_nodes_test_data(la, 2, 4, 6, 8); |
898 assign_nodes_test_data(la, 2, 4, 6, 8); |
| 864 CX_TEST_ASSERT(cx_linked_list_compare(la, lb, loc_next, loc_data, cx_cmp_int) > 0); |
902 CX_TEST_ASSERT(cx_linked_list_compare(la, lb, loc_next, loc_data, cx_cmp_int) > 0); |
| 865 CX_TEST_ASSERT(cx_linked_list_compare(lb, la, loc_next, loc_data, cx_cmp_int) < 0); |
903 CX_TEST_ASSERT(cx_linked_list_compare(lb, la, loc_next, loc_data, cx_cmp_int) < 0); |
| 866 CX_TEST_ASSERT(cx_linked_list_compare(lc, la, loc_next, loc_data, cx_cmp_int) > 0); |
904 CX_TEST_ASSERT(cx_linked_list_compare(lc, la, loc_next, loc_data, cx_cmp_int) > 0); |
| 867 CX_TEST_ASSERT(cx_linked_list_compare(la, lc, loc_next, loc_data, cx_cmp_int) < 0); |
905 CX_TEST_ASSERT(cx_linked_list_compare(la, lc, loc_next, loc_data, cx_cmp_int) < 0); |
| 868 CX_TEST_ASSERT(cx_linked_list_compare(la, la, loc_next, loc_data, cx_cmp_int) == 0); |
906 CX_TEST_ASSERT(cx_linked_list_compare(la, la, loc_next, loc_data, cx_cmp_int) == 0); |
| |
907 } |
| |
908 destroy_nodes_test_data(la); |
| |
909 destroy_nodes_test_data(lb); |
| |
910 destroy_nodes_test_data(lc); |
| |
911 } |
| |
912 |
| |
913 CX_TEST(test_linked_list_compare_c) { |
| |
914 void *la = create_nodes_test_data(4); |
| |
915 void *lb = create_nodes_test_data(3); |
| |
916 void *lc = create_nodes_test_data(4); |
| |
917 assign_nodes_test_data(la, 2, 4, 6, 8); |
| |
918 assign_nodes_test_data(lb, 2, 4, 6); |
| |
919 assign_nodes_test_data(lc, 2, 4, 6, 9); |
| |
920 CX_TEST_DO { |
| |
921 int z = 1337; |
| |
922 CX_TEST_ASSERT(cx_linked_list_compare_c(la, lb, loc_next, loc_data, test_ccmp_int, &z) > 0); |
| |
923 CX_TEST_ASSERT(cx_linked_list_compare_c(lb, la, loc_next, loc_data, test_ccmp_int, &z) < 0); |
| |
924 CX_TEST_ASSERT(cx_linked_list_compare_c(lc, la, loc_next, loc_data, test_ccmp_int, &z) > 0); |
| |
925 CX_TEST_ASSERT(cx_linked_list_compare_c(la, lc, loc_next, loc_data, test_ccmp_int, &z) < 0); |
| |
926 CX_TEST_ASSERT(cx_linked_list_compare_c(la, la, loc_next, loc_data, test_ccmp_int, &z) == 0); |
| 869 } |
927 } |
| 870 destroy_nodes_test_data(la); |
928 destroy_nodes_test_data(la); |
| 871 destroy_nodes_test_data(lb); |
929 destroy_nodes_test_data(lb); |
| 872 destroy_nodes_test_data(lc); |
930 destroy_nodes_test_data(lc); |
| 873 } |
931 } |
| 1141 CX_TEST_ASSERT(end == &nodes[4]); |
1201 CX_TEST_ASSERT(end == &nodes[4]); |
| 1142 |
1202 |
| 1143 // insert a new beginning node |
1203 // insert a new beginning node |
| 1144 node new_begin = {0}; |
1204 node new_begin = {0}; |
| 1145 new_begin.data = 1; |
1205 new_begin.data = 1; |
| 1146 cx_linked_list_insert_sorted( |
1206 cx_linked_list_insert_sorted_c( |
| 1147 &begin, &end, |
1207 &begin, &end, |
| 1148 loc_prev, loc_next, |
1208 loc_prev, loc_next, |
| 1149 &new_begin, test_cmp_node |
1209 &new_begin, test_ccmp_node, &z |
| 1150 ); |
1210 ); |
| 1151 CX_TEST_ASSERT(new_begin.prev == NULL); |
1211 CX_TEST_ASSERT(new_begin.prev == NULL); |
| 1152 CX_TEST_ASSERT(new_begin.next == &nodes[0]); |
1212 CX_TEST_ASSERT(new_begin.next == &nodes[0]); |
| 1153 CX_TEST_ASSERT(nodes[0].prev == &new_begin); |
1213 CX_TEST_ASSERT(nodes[0].prev == &new_begin); |
| 1154 CX_TEST_ASSERT(begin == &new_begin); |
1214 CX_TEST_ASSERT(begin == &new_begin); |
| 1224 CX_TEST_ASSERT(end == &nodes[4]); |
1285 CX_TEST_ASSERT(end == &nodes[4]); |
| 1225 |
1286 |
| 1226 // now as duplicate |
1287 // now as duplicate |
| 1227 node new_node_dup = {0}; |
1288 node new_node_dup = {0}; |
| 1228 new_node_dup.data = 5; |
1289 new_node_dup.data = 5; |
| 1229 CX_TEST_ASSERT(0 != cx_linked_list_insert_unique( |
1290 CX_TEST_ASSERT(0 != cx_linked_list_insert_unique_c( |
| 1230 &begin, &end, |
1291 &begin, &end, |
| 1231 loc_prev, loc_next, |
1292 loc_prev, loc_next, |
| 1232 &new_node_dup, test_cmp_node |
1293 &new_node_dup, test_ccmp_node, &z |
| 1233 )); |
1294 )); |
| 1234 CX_TEST_ASSERT(new_node_dup.prev == NULL); |
1295 CX_TEST_ASSERT(new_node_dup.prev == NULL); |
| 1235 CX_TEST_ASSERT(new_node_dup.next == NULL); |
1296 CX_TEST_ASSERT(new_node_dup.next == NULL); |
| 1236 CX_TEST_ASSERT(new_node.prev == &nodes[0]); |
1297 CX_TEST_ASSERT(new_node.prev == &nodes[0]); |
| 1237 CX_TEST_ASSERT(new_node.next == &nodes[1]); |
1298 CX_TEST_ASSERT(new_node.next == &nodes[1]); |
| 3799 CxTestSuite *suite = cx_test_suite_new("linked_list (low-level)"); |
3860 CxTestSuite *suite = cx_test_suite_new("linked_list (low-level)"); |
| 3800 |
3861 |
| 3801 cx_test_register(suite, test_linked_list_link_unlink); |
3862 cx_test_register(suite, test_linked_list_link_unlink); |
| 3802 cx_test_register(suite, test_linked_list_at); |
3863 cx_test_register(suite, test_linked_list_at); |
| 3803 cx_test_register(suite, test_linked_list_find); |
3864 cx_test_register(suite, test_linked_list_find); |
| |
3865 cx_test_register(suite, test_linked_list_find_c); |
| 3804 cx_test_register(suite, test_linked_list_compare); |
3866 cx_test_register(suite, test_linked_list_compare); |
| |
3867 cx_test_register(suite, test_linked_list_compare_c); |
| 3805 cx_test_register(suite, test_linked_list_add); |
3868 cx_test_register(suite, test_linked_list_add); |
| 3806 cx_test_register(suite, test_linked_list_prepend); |
3869 cx_test_register(suite, test_linked_list_prepend); |
| 3807 cx_test_register(suite, test_linked_list_insert); |
3870 cx_test_register(suite, test_linked_list_insert); |
| 3808 cx_test_register(suite, test_linked_list_insert_chain); |
3871 cx_test_register(suite, test_linked_list_insert_chain); |
| 3809 cx_test_register(suite, test_linked_list_insert_sorted); |
3872 cx_test_register(suite, test_linked_list_insert_sorted); |