test/test_list.c

changeset 499
3dc9075df822
parent 498
435c9965b2dd
child 500
eb9e7bd40a8e
     1.1 --- a/test/test_list.c	Sat Jan 29 12:46:07 2022 +0100
     1.2 +++ b/test/test_list.c	Sat Jan 29 14:32:04 2022 +0100
     1.3 @@ -1001,6 +1001,92 @@
     1.4      test_hl_linked_list_iterator_impl(list);
     1.5  }
     1.6  
     1.7 +void test_hl_linked_list_insert_via_iterator(void) {
     1.8 +    cxTestingAllocatorReset();
     1.9 +    CxList list = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int));
    1.10 +    for (int i = 0; i < 5; i++) {
    1.11 +        cxListAdd(list, &i);
    1.12 +    }
    1.13 +    CxIterator iter = cxListIterator(list, 2);
    1.14 +    CU_ASSERT_EQUAL(iter.index, 2)
    1.15 +    CU_ASSERT_EQUAL(*(int *) cxIteratorCurrent(&iter), 2)
    1.16 +
    1.17 +    int data = 10;
    1.18 +    cxListInsertAfter(&iter, &data);
    1.19 +    CU_ASSERT_EQUAL(iter.index, 2)
    1.20 +    CU_ASSERT_EQUAL(*(int *) cxIteratorCurrent(&iter), 2)
    1.21 +    data = 20;
    1.22 +    cxListInsertBefore(&iter, &data);
    1.23 +    CU_ASSERT_EQUAL(iter.index, 3)
    1.24 +    CU_ASSERT_EQUAL(*(int *) cxIteratorCurrent(&iter), 2)
    1.25 +
    1.26 +    data = 30;
    1.27 +    iter = cxListBegin(list);
    1.28 +    cxListInsertBefore(&iter, &data);
    1.29 +    CU_ASSERT_EQUAL(iter.index, 1)
    1.30 +    CU_ASSERT_EQUAL(*(int *) cxIteratorCurrent(&iter), 0)
    1.31 +    data = 40;
    1.32 +    iter = cxListIterator(list, list->size);
    1.33 +    cxListInsertBefore(&iter, &data);
    1.34 +    CU_ASSERT_EQUAL(iter.index, 9)
    1.35 +    CU_ASSERT_FALSE(cxIteratorValid(&iter))
    1.36 +    data = 50;
    1.37 +    iter = cxListIterator(list, list->size);
    1.38 +    cxListInsertAfter(&iter, &data);
    1.39 +    CU_ASSERT_EQUAL(iter.index, 10)
    1.40 +    CU_ASSERT_FALSE(cxIteratorValid(&iter))
    1.41 +
    1.42 +    int expdata[] = {30, 0, 1, 20, 2, 10, 3, 4, 40, 50};
    1.43 +    CxList expected = cxLinkedListFromArray(cxTestingAllocator,
    1.44 +                                            cmp_int, sizeof(int), 10, expdata);
    1.45 +
    1.46 +    CU_ASSERT_EQUAL(0, cxListCompare(list, expected))
    1.47 +    cxLinkedListDestroy(list);
    1.48 +    cxLinkedListDestroy(expected);
    1.49 +    CU_ASSERT_TRUE(cxTestingAllocatorVerify())
    1.50 +}
    1.51 +
    1.52 +void test_hl_ptr_linked_list_insert_via_iterator(void) {
    1.53 +    int testdata[] = {0, 1, 2, 3, 4, 10, 20, 30, 40, 50};
    1.54 +    cxTestingAllocatorReset();
    1.55 +    CxList list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int);
    1.56 +    int i;
    1.57 +    for (i = 0; i < 5; i++) {
    1.58 +        cxListAdd(list, &testdata[i]);
    1.59 +    }
    1.60 +    CxIterator iter = cxListIterator(list, 2);
    1.61 +    CU_ASSERT_EQUAL(iter.index, 2)
    1.62 +    CU_ASSERT_EQUAL(*(int *) cxIteratorCurrent(&iter), 2)
    1.63 +
    1.64 +    cxListInsertAfter(&iter, &testdata[i++]);
    1.65 +    CU_ASSERT_EQUAL(iter.index, 2)
    1.66 +    CU_ASSERT_EQUAL(*(int *) cxIteratorCurrent(&iter), 2)
    1.67 +    cxListInsertBefore(&iter, &testdata[i++]);
    1.68 +    CU_ASSERT_EQUAL(iter.index, 3)
    1.69 +    CU_ASSERT_EQUAL(*(int *) cxIteratorCurrent(&iter), 2)
    1.70 +
    1.71 +    iter = cxListBegin(list);
    1.72 +    cxListInsertBefore(&iter, &testdata[i++]);
    1.73 +    CU_ASSERT_EQUAL(iter.index, 1)
    1.74 +    CU_ASSERT_EQUAL(*(int *) cxIteratorCurrent(&iter), 0)
    1.75 +    iter = cxListIterator(list, list->size);
    1.76 +    cxListInsertBefore(&iter, &testdata[i++]);
    1.77 +    CU_ASSERT_EQUAL(iter.index, 9)
    1.78 +    CU_ASSERT_FALSE(cxIteratorValid(&iter))
    1.79 +    iter = cxListIterator(list, list->size);
    1.80 +    cxListInsertAfter(&iter, &testdata[i++]);
    1.81 +    CU_ASSERT_EQUAL(iter.index, 10)
    1.82 +    CU_ASSERT_FALSE(cxIteratorValid(&iter))
    1.83 +
    1.84 +    int expdata[] = {30, 0, 1, 20, 2, 10, 3, 4, 40, 50};
    1.85 +    for (i = 0; i < 10; i++) {
    1.86 +        CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), expdata[i])
    1.87 +    }
    1.88 +
    1.89 +    cxLinkedListDestroy(list);
    1.90 +    CU_ASSERT_TRUE(cxTestingAllocatorVerify())
    1.91 +}
    1.92 +
    1.93  int main() {
    1.94      CU_pSuite suite = NULL;
    1.95  
    1.96 @@ -1037,6 +1123,7 @@
    1.97      cu_add_test(suite, test_hl_linked_list_find);
    1.98      cu_add_test(suite, test_hl_linked_list_sort);
    1.99      cu_add_test(suite, test_hl_linked_list_iterator);
   1.100 +    cu_add_test(suite, test_hl_linked_list_insert_via_iterator);
   1.101  
   1.102      suite = CU_add_suite("high level pointer linked list", NULL, NULL);
   1.103  
   1.104 @@ -1048,6 +1135,7 @@
   1.105      cu_add_test(suite, test_hl_ptr_linked_list_find);
   1.106      cu_add_test(suite, test_hl_ptr_linked_list_sort);
   1.107      cu_add_test(suite, test_hl_ptr_linked_list_iterator);
   1.108 +    cu_add_test(suite, test_hl_ptr_linked_list_insert_via_iterator);
   1.109  
   1.110      CU_basic_set_mode(UCX_CU_BRM);
   1.111  

mercurial