use c99 bool + add test for low level find

Tue, 28 Dec 2021 14:25:05 +0100

author
Mike Becker <universe@uap-core.de>
date
Tue, 28 Dec 2021 14:25:05 +0100
changeset 487
4bd19279778c
parent 486
d7ca126eab7f
child 488
9138acaa494b

use c99 bool + add test for low level find

src/cx/common.h file | annotate | diff | comparison | revisions
src/cx/linked_list.h file | annotate | diff | comparison | revisions
src/linked_list.c file | annotate | diff | comparison | revisions
test/test_list.c file | annotate | diff | comparison | revisions
     1.1 --- a/src/cx/common.h	Tue Dec 28 14:16:04 2021 +0100
     1.2 +++ b/src/cx/common.h	Tue Dec 28 14:25:05 2021 +0100
     1.3 @@ -91,6 +91,7 @@
     1.4  
     1.5  #include <stdlib.h>
     1.6  #include <stddef.h>
     1.7 +#include <stdbool.h>
     1.8  
     1.9  #ifdef _WIN32
    1.10  #if !(defined __ssize_t_defined || defined _SSIZE_T_)
     2.1 --- a/src/cx/linked_list.h	Tue Dec 28 14:16:04 2021 +0100
     2.2 +++ b/src/cx/linked_list.h	Tue Dec 28 14:25:05 2021 +0100
     2.3 @@ -124,7 +124,7 @@
     2.4          void *start,
     2.5          ptrdiff_t loc_advance,
     2.6          ptrdiff_t loc_data,
     2.7 -        int follow_ptr,
     2.8 +        bool follow_ptr,
     2.9          CxListComparator cmp_func,
    2.10          void *elem
    2.11  ) __attribute__((__nonnull__));
    2.12 @@ -378,7 +378,7 @@
    2.13          ptrdiff_t loc_prev,
    2.14          ptrdiff_t loc_next,
    2.15          ptrdiff_t loc_data,
    2.16 -        int follow_ptr,
    2.17 +        bool follow_ptr,
    2.18          CxListComparator cmp_func
    2.19  ) __attribute__((__nonnull__(1, 7)));
    2.20  
    2.21 @@ -400,7 +400,7 @@
    2.22          void *begin_right,
    2.23          ptrdiff_t loc_advance,
    2.24          ptrdiff_t loc_data,
    2.25 -        int follow_ptr,
    2.26 +        bool follow_ptr,
    2.27          CxListComparator cmp_func
    2.28  ) __attribute__((__nonnull__(6)));
    2.29  
     3.1 --- a/src/linked_list.c	Tue Dec 28 14:16:04 2021 +0100
     3.2 +++ b/src/linked_list.c	Tue Dec 28 14:25:05 2021 +0100
     3.3 @@ -60,7 +60,7 @@
     3.4          void *start,
     3.5          ptrdiff_t loc_advance,
     3.6          ptrdiff_t loc_data,
     3.7 -        int follow_ptr,
     3.8 +        bool follow_ptr,
     3.9          CxListComparator cmp_func,
    3.10          void *elem
    3.11  ) {
    3.12 @@ -284,7 +284,7 @@
    3.13          ptrdiff_t loc_prev,
    3.14          ptrdiff_t loc_next,
    3.15          ptrdiff_t loc_data,
    3.16 -        int follow_ptr,
    3.17 +        bool follow_ptr,
    3.18          size_t length,
    3.19          void *ls,
    3.20          void *le,
    3.21 @@ -340,7 +340,7 @@
    3.22          ptrdiff_t loc_prev,
    3.23          ptrdiff_t loc_next,
    3.24          ptrdiff_t loc_data,
    3.25 -        int follow_ptr,
    3.26 +        bool follow_ptr,
    3.27          CxListComparator cmp_func
    3.28  ) {
    3.29      assert(begin != NULL);
    3.30 @@ -401,7 +401,7 @@
    3.31          void *begin_right,
    3.32          ptrdiff_t loc_advance,
    3.33          ptrdiff_t loc_data,
    3.34 -        int follow_ptr,
    3.35 +        bool follow_ptr,
    3.36          CxListComparator cmp_func
    3.37  ) {
    3.38      void *left = begin_left, *right = begin_right;
    3.39 @@ -587,7 +587,7 @@
    3.40  ) {
    3.41      return cx_linked_list_find(((cx_linked_list *) list)->begin,
    3.42                                 CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
    3.43 -                               0, list->cmpfunc, elem);
    3.44 +                               false, list->cmpfunc, elem);
    3.45  }
    3.46  
    3.47  static size_t cx_pll_find(
    3.48 @@ -596,21 +596,21 @@
    3.49  ) {
    3.50      return cx_linked_list_find(((cx_linked_list *) list)->begin,
    3.51                                 CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
    3.52 -                               1, list->cmpfunc, elem);
    3.53 +                               true, list->cmpfunc, elem);
    3.54  }
    3.55  
    3.56  static void cx_ll_sort(cx_list_s *list) {
    3.57      cx_linked_list *ll = (cx_linked_list *) list;
    3.58      cx_linked_list_sort((void **) &ll->begin, (void **) &ll->end,
    3.59                          CX_LL_LOC_PREV, CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
    3.60 -                        0, list->cmpfunc);
    3.61 +                        false, list->cmpfunc);
    3.62  }
    3.63  
    3.64  static void cx_pll_sort(cx_list_s *list) {
    3.65      cx_linked_list *ll = (cx_linked_list *) list;
    3.66      cx_linked_list_sort((void **) &ll->begin, (void **) &ll->end,
    3.67                          CX_LL_LOC_PREV, CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
    3.68 -                        1, list->cmpfunc);
    3.69 +                        true, list->cmpfunc);
    3.70  }
    3.71  
    3.72  static cx_list_class cx_linked_list_class = {
     4.1 --- a/test/test_list.c	Tue Dec 28 14:16:04 2021 +0100
     4.2 +++ b/test/test_list.c	Tue Dec 28 14:25:05 2021 +0100
     4.3 @@ -125,6 +125,31 @@
     4.4      CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&d, 3, loc_prev, 1), &b)
     4.5  }
     4.6  
     4.7 +void test_linked_list_find(void) {
     4.8 +    int data[] = {2, 4, 6, 8};
     4.9 +    void *list = create_test_data(4, data);
    4.10 +    int s;
    4.11 +
    4.12 +    s = 2;
    4.13 +    CU_ASSERT_EQUAL(cx_linked_list_find(list, loc_next, loc_data,
    4.14 +                                        false, (CxListComparator) cmp_int, &s), 0)
    4.15 +    s = 4;
    4.16 +    CU_ASSERT_EQUAL(cx_linked_list_find(list, loc_next, loc_data,
    4.17 +                                        false, (CxListComparator) cmp_int, &s), 1)
    4.18 +    s = 6;
    4.19 +    CU_ASSERT_EQUAL(cx_linked_list_find(list, loc_next, loc_data,
    4.20 +                                        false, (CxListComparator) cmp_int, &s), 2)
    4.21 +    s = 8;
    4.22 +    CU_ASSERT_EQUAL(cx_linked_list_find(list, loc_next, loc_data,
    4.23 +                                        false, (CxListComparator) cmp_int, &s), 3)
    4.24 +    s = 10;
    4.25 +    CU_ASSERT_EQUAL(cx_linked_list_find(list, loc_next, loc_data,
    4.26 +                                        false, (CxListComparator) cmp_int, &s), 4)
    4.27 +    s = -2;
    4.28 +    CU_ASSERT_EQUAL(cx_linked_list_find(list, loc_next, loc_data,
    4.29 +                                        false, (CxListComparator) cmp_int, &s), 4)
    4.30 +}
    4.31 +
    4.32  void test_linked_list_compare(void) {
    4.33      int a[] = {2, 4, 6, 8};
    4.34      int b[] = {2, 4, 6};
    4.35 @@ -135,19 +160,19 @@
    4.36      void *lc = create_test_data(4, c);
    4.37  
    4.38      CU_ASSERT_TRUE(0 < cx_linked_list_compare(la, lb, loc_next, loc_data,
    4.39 -                                              0, (CxListComparator) cmp_int)
    4.40 +                                              false, (CxListComparator) cmp_int)
    4.41      )
    4.42      CU_ASSERT_TRUE(0 > cx_linked_list_compare(lb, la, loc_next, loc_data,
    4.43 -                                              0, (CxListComparator) cmp_int)
    4.44 +                                              false, (CxListComparator) cmp_int)
    4.45      )
    4.46      CU_ASSERT_TRUE(0 < cx_linked_list_compare(lc, la, loc_next, loc_data,
    4.47 -                                              0, (CxListComparator) cmp_int)
    4.48 +                                              false, (CxListComparator) cmp_int)
    4.49      )
    4.50      CU_ASSERT_TRUE(0 > cx_linked_list_compare(la, lc, loc_next, loc_data,
    4.51 -                                              0, (CxListComparator) cmp_int)
    4.52 +                                              false, (CxListComparator) cmp_int)
    4.53      )
    4.54      CU_ASSERT_TRUE(0 == cx_linked_list_compare(la, la, loc_next, loc_data,
    4.55 -                                               0, (CxListComparator) cmp_int)
    4.56 +                                               false, (CxListComparator) cmp_int)
    4.57      )
    4.58  
    4.59      destroy_test_data(la);
    4.60 @@ -487,7 +512,7 @@
    4.61      void *end = cx_linked_list_last(begin, loc_next);
    4.62  
    4.63      cx_linked_list_sort(&begin, &end, loc_prev, loc_next, loc_data,
    4.64 -                        0, (CxListComparator) cmp_int);
    4.65 +                        false, (CxListComparator) cmp_int);
    4.66  
    4.67      struct node *check = begin;
    4.68      struct node *check_last = NULL;
    4.69 @@ -952,6 +977,7 @@
    4.70  
    4.71      cu_add_test(suite, test_linked_list_link_unlink);
    4.72      cu_add_test(suite, test_linked_list_at);
    4.73 +    cu_add_test(suite, test_linked_list_find);
    4.74      cu_add_test(suite, test_linked_list_compare);
    4.75      cu_add_test(suite, test_linked_list_prepend);
    4.76      cu_add_test(suite, test_linked_list_add);

mercurial