diff -r 435c9965b2dd -r 3dc9075df822 test/test_list.c --- a/test/test_list.c Sat Jan 29 12:46:07 2022 +0100 +++ b/test/test_list.c Sat Jan 29 14:32:04 2022 +0100 @@ -1001,6 +1001,92 @@ test_hl_linked_list_iterator_impl(list); } +void test_hl_linked_list_insert_via_iterator(void) { + cxTestingAllocatorReset(); + CxList list = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int)); + for (int i = 0; i < 5; i++) { + cxListAdd(list, &i); + } + CxIterator iter = cxListIterator(list, 2); + CU_ASSERT_EQUAL(iter.index, 2) + CU_ASSERT_EQUAL(*(int *) cxIteratorCurrent(&iter), 2) + + int data = 10; + cxListInsertAfter(&iter, &data); + CU_ASSERT_EQUAL(iter.index, 2) + CU_ASSERT_EQUAL(*(int *) cxIteratorCurrent(&iter), 2) + data = 20; + cxListInsertBefore(&iter, &data); + CU_ASSERT_EQUAL(iter.index, 3) + CU_ASSERT_EQUAL(*(int *) cxIteratorCurrent(&iter), 2) + + data = 30; + iter = cxListBegin(list); + cxListInsertBefore(&iter, &data); + CU_ASSERT_EQUAL(iter.index, 1) + CU_ASSERT_EQUAL(*(int *) cxIteratorCurrent(&iter), 0) + data = 40; + iter = cxListIterator(list, list->size); + cxListInsertBefore(&iter, &data); + CU_ASSERT_EQUAL(iter.index, 9) + CU_ASSERT_FALSE(cxIteratorValid(&iter)) + data = 50; + iter = cxListIterator(list, list->size); + cxListInsertAfter(&iter, &data); + CU_ASSERT_EQUAL(iter.index, 10) + CU_ASSERT_FALSE(cxIteratorValid(&iter)) + + int expdata[] = {30, 0, 1, 20, 2, 10, 3, 4, 40, 50}; + CxList expected = cxLinkedListFromArray(cxTestingAllocator, + cmp_int, sizeof(int), 10, expdata); + + CU_ASSERT_EQUAL(0, cxListCompare(list, expected)) + cxLinkedListDestroy(list); + cxLinkedListDestroy(expected); + CU_ASSERT_TRUE(cxTestingAllocatorVerify()) +} + +void test_hl_ptr_linked_list_insert_via_iterator(void) { + int testdata[] = {0, 1, 2, 3, 4, 10, 20, 30, 40, 50}; + cxTestingAllocatorReset(); + CxList list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int); + int i; + for (i = 0; i < 5; i++) { + cxListAdd(list, &testdata[i]); + } + CxIterator iter = cxListIterator(list, 2); + CU_ASSERT_EQUAL(iter.index, 2) + CU_ASSERT_EQUAL(*(int *) cxIteratorCurrent(&iter), 2) + + cxListInsertAfter(&iter, &testdata[i++]); + CU_ASSERT_EQUAL(iter.index, 2) + CU_ASSERT_EQUAL(*(int *) cxIteratorCurrent(&iter), 2) + cxListInsertBefore(&iter, &testdata[i++]); + CU_ASSERT_EQUAL(iter.index, 3) + CU_ASSERT_EQUAL(*(int *) cxIteratorCurrent(&iter), 2) + + iter = cxListBegin(list); + cxListInsertBefore(&iter, &testdata[i++]); + CU_ASSERT_EQUAL(iter.index, 1) + CU_ASSERT_EQUAL(*(int *) cxIteratorCurrent(&iter), 0) + iter = cxListIterator(list, list->size); + cxListInsertBefore(&iter, &testdata[i++]); + CU_ASSERT_EQUAL(iter.index, 9) + CU_ASSERT_FALSE(cxIteratorValid(&iter)) + iter = cxListIterator(list, list->size); + cxListInsertAfter(&iter, &testdata[i++]); + CU_ASSERT_EQUAL(iter.index, 10) + CU_ASSERT_FALSE(cxIteratorValid(&iter)) + + int expdata[] = {30, 0, 1, 20, 2, 10, 3, 4, 40, 50}; + for (i = 0; i < 10; i++) { + CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), expdata[i]) + } + + cxLinkedListDestroy(list); + CU_ASSERT_TRUE(cxTestingAllocatorVerify()) +} + int main() { CU_pSuite suite = NULL; @@ -1037,6 +1123,7 @@ cu_add_test(suite, test_hl_linked_list_find); cu_add_test(suite, test_hl_linked_list_sort); cu_add_test(suite, test_hl_linked_list_iterator); + cu_add_test(suite, test_hl_linked_list_insert_via_iterator); suite = CU_add_suite("high level pointer linked list", NULL, NULL); @@ -1048,6 +1135,7 @@ cu_add_test(suite, test_hl_ptr_linked_list_find); cu_add_test(suite, test_hl_ptr_linked_list_sort); cu_add_test(suite, test_hl_ptr_linked_list_iterator); + cu_add_test(suite, test_hl_ptr_linked_list_insert_via_iterator); CU_basic_set_mode(UCX_CU_BRM);