999 cxListAdd(list, &data[i]); |
999 cxListAdd(list, &data[i]); |
1000 } |
1000 } |
1001 test_hl_linked_list_iterator_impl(list); |
1001 test_hl_linked_list_iterator_impl(list); |
1002 } |
1002 } |
1003 |
1003 |
|
1004 void test_hl_linked_list_insert_via_iterator(void) { |
|
1005 cxTestingAllocatorReset(); |
|
1006 CxList list = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int)); |
|
1007 for (int i = 0; i < 5; i++) { |
|
1008 cxListAdd(list, &i); |
|
1009 } |
|
1010 CxIterator iter = cxListIterator(list, 2); |
|
1011 CU_ASSERT_EQUAL(iter.index, 2) |
|
1012 CU_ASSERT_EQUAL(*(int *) cxIteratorCurrent(&iter), 2) |
|
1013 |
|
1014 int data = 10; |
|
1015 cxListInsertAfter(&iter, &data); |
|
1016 CU_ASSERT_EQUAL(iter.index, 2) |
|
1017 CU_ASSERT_EQUAL(*(int *) cxIteratorCurrent(&iter), 2) |
|
1018 data = 20; |
|
1019 cxListInsertBefore(&iter, &data); |
|
1020 CU_ASSERT_EQUAL(iter.index, 3) |
|
1021 CU_ASSERT_EQUAL(*(int *) cxIteratorCurrent(&iter), 2) |
|
1022 |
|
1023 data = 30; |
|
1024 iter = cxListBegin(list); |
|
1025 cxListInsertBefore(&iter, &data); |
|
1026 CU_ASSERT_EQUAL(iter.index, 1) |
|
1027 CU_ASSERT_EQUAL(*(int *) cxIteratorCurrent(&iter), 0) |
|
1028 data = 40; |
|
1029 iter = cxListIterator(list, list->size); |
|
1030 cxListInsertBefore(&iter, &data); |
|
1031 CU_ASSERT_EQUAL(iter.index, 9) |
|
1032 CU_ASSERT_FALSE(cxIteratorValid(&iter)) |
|
1033 data = 50; |
|
1034 iter = cxListIterator(list, list->size); |
|
1035 cxListInsertAfter(&iter, &data); |
|
1036 CU_ASSERT_EQUAL(iter.index, 10) |
|
1037 CU_ASSERT_FALSE(cxIteratorValid(&iter)) |
|
1038 |
|
1039 int expdata[] = {30, 0, 1, 20, 2, 10, 3, 4, 40, 50}; |
|
1040 CxList expected = cxLinkedListFromArray(cxTestingAllocator, |
|
1041 cmp_int, sizeof(int), 10, expdata); |
|
1042 |
|
1043 CU_ASSERT_EQUAL(0, cxListCompare(list, expected)) |
|
1044 cxLinkedListDestroy(list); |
|
1045 cxLinkedListDestroy(expected); |
|
1046 CU_ASSERT_TRUE(cxTestingAllocatorVerify()) |
|
1047 } |
|
1048 |
|
1049 void test_hl_ptr_linked_list_insert_via_iterator(void) { |
|
1050 int testdata[] = {0, 1, 2, 3, 4, 10, 20, 30, 40, 50}; |
|
1051 cxTestingAllocatorReset(); |
|
1052 CxList list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int); |
|
1053 int i; |
|
1054 for (i = 0; i < 5; i++) { |
|
1055 cxListAdd(list, &testdata[i]); |
|
1056 } |
|
1057 CxIterator iter = cxListIterator(list, 2); |
|
1058 CU_ASSERT_EQUAL(iter.index, 2) |
|
1059 CU_ASSERT_EQUAL(*(int *) cxIteratorCurrent(&iter), 2) |
|
1060 |
|
1061 cxListInsertAfter(&iter, &testdata[i++]); |
|
1062 CU_ASSERT_EQUAL(iter.index, 2) |
|
1063 CU_ASSERT_EQUAL(*(int *) cxIteratorCurrent(&iter), 2) |
|
1064 cxListInsertBefore(&iter, &testdata[i++]); |
|
1065 CU_ASSERT_EQUAL(iter.index, 3) |
|
1066 CU_ASSERT_EQUAL(*(int *) cxIteratorCurrent(&iter), 2) |
|
1067 |
|
1068 iter = cxListBegin(list); |
|
1069 cxListInsertBefore(&iter, &testdata[i++]); |
|
1070 CU_ASSERT_EQUAL(iter.index, 1) |
|
1071 CU_ASSERT_EQUAL(*(int *) cxIteratorCurrent(&iter), 0) |
|
1072 iter = cxListIterator(list, list->size); |
|
1073 cxListInsertBefore(&iter, &testdata[i++]); |
|
1074 CU_ASSERT_EQUAL(iter.index, 9) |
|
1075 CU_ASSERT_FALSE(cxIteratorValid(&iter)) |
|
1076 iter = cxListIterator(list, list->size); |
|
1077 cxListInsertAfter(&iter, &testdata[i++]); |
|
1078 CU_ASSERT_EQUAL(iter.index, 10) |
|
1079 CU_ASSERT_FALSE(cxIteratorValid(&iter)) |
|
1080 |
|
1081 int expdata[] = {30, 0, 1, 20, 2, 10, 3, 4, 40, 50}; |
|
1082 for (i = 0; i < 10; i++) { |
|
1083 CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), expdata[i]) |
|
1084 } |
|
1085 |
|
1086 cxLinkedListDestroy(list); |
|
1087 CU_ASSERT_TRUE(cxTestingAllocatorVerify()) |
|
1088 } |
|
1089 |
1004 int main() { |
1090 int main() { |
1005 CU_pSuite suite = NULL; |
1091 CU_pSuite suite = NULL; |
1006 |
1092 |
1007 if (CUE_SUCCESS != CU_initialize_registry()) { |
1093 if (CUE_SUCCESS != CU_initialize_registry()) { |
1008 return CU_get_error(); |
1094 return CU_get_error(); |
1035 cu_add_test(suite, test_hl_linked_list_remove); |
1121 cu_add_test(suite, test_hl_linked_list_remove); |
1036 cu_add_test(suite, test_hl_linked_list_at); |
1122 cu_add_test(suite, test_hl_linked_list_at); |
1037 cu_add_test(suite, test_hl_linked_list_find); |
1123 cu_add_test(suite, test_hl_linked_list_find); |
1038 cu_add_test(suite, test_hl_linked_list_sort); |
1124 cu_add_test(suite, test_hl_linked_list_sort); |
1039 cu_add_test(suite, test_hl_linked_list_iterator); |
1125 cu_add_test(suite, test_hl_linked_list_iterator); |
|
1126 cu_add_test(suite, test_hl_linked_list_insert_via_iterator); |
1040 |
1127 |
1041 suite = CU_add_suite("high level pointer linked list", NULL, NULL); |
1128 suite = CU_add_suite("high level pointer linked list", NULL, NULL); |
1042 |
1129 |
1043 cu_add_test(suite, test_hl_ptr_linked_list_create); |
1130 cu_add_test(suite, test_hl_ptr_linked_list_create); |
1044 cu_add_test(suite, test_hl_ptr_linked_list_add); |
1131 cu_add_test(suite, test_hl_ptr_linked_list_add); |