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
--- a/src/cx/common.h	Tue Dec 28 14:16:04 2021 +0100
+++ b/src/cx/common.h	Tue Dec 28 14:25:05 2021 +0100
@@ -91,6 +91,7 @@
 
 #include <stdlib.h>
 #include <stddef.h>
+#include <stdbool.h>
 
 #ifdef _WIN32
 #if !(defined __ssize_t_defined || defined _SSIZE_T_)
--- a/src/cx/linked_list.h	Tue Dec 28 14:16:04 2021 +0100
+++ b/src/cx/linked_list.h	Tue Dec 28 14:25:05 2021 +0100
@@ -124,7 +124,7 @@
         void *start,
         ptrdiff_t loc_advance,
         ptrdiff_t loc_data,
-        int follow_ptr,
+        bool follow_ptr,
         CxListComparator cmp_func,
         void *elem
 ) __attribute__((__nonnull__));
@@ -378,7 +378,7 @@
         ptrdiff_t loc_prev,
         ptrdiff_t loc_next,
         ptrdiff_t loc_data,
-        int follow_ptr,
+        bool follow_ptr,
         CxListComparator cmp_func
 ) __attribute__((__nonnull__(1, 7)));
 
@@ -400,7 +400,7 @@
         void *begin_right,
         ptrdiff_t loc_advance,
         ptrdiff_t loc_data,
-        int follow_ptr,
+        bool follow_ptr,
         CxListComparator cmp_func
 ) __attribute__((__nonnull__(6)));
 
--- a/src/linked_list.c	Tue Dec 28 14:16:04 2021 +0100
+++ b/src/linked_list.c	Tue Dec 28 14:25:05 2021 +0100
@@ -60,7 +60,7 @@
         void *start,
         ptrdiff_t loc_advance,
         ptrdiff_t loc_data,
-        int follow_ptr,
+        bool follow_ptr,
         CxListComparator cmp_func,
         void *elem
 ) {
@@ -284,7 +284,7 @@
         ptrdiff_t loc_prev,
         ptrdiff_t loc_next,
         ptrdiff_t loc_data,
-        int follow_ptr,
+        bool follow_ptr,
         size_t length,
         void *ls,
         void *le,
@@ -340,7 +340,7 @@
         ptrdiff_t loc_prev,
         ptrdiff_t loc_next,
         ptrdiff_t loc_data,
-        int follow_ptr,
+        bool follow_ptr,
         CxListComparator cmp_func
 ) {
     assert(begin != NULL);
@@ -401,7 +401,7 @@
         void *begin_right,
         ptrdiff_t loc_advance,
         ptrdiff_t loc_data,
-        int follow_ptr,
+        bool follow_ptr,
         CxListComparator cmp_func
 ) {
     void *left = begin_left, *right = begin_right;
@@ -587,7 +587,7 @@
 ) {
     return cx_linked_list_find(((cx_linked_list *) list)->begin,
                                CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
-                               0, list->cmpfunc, elem);
+                               false, list->cmpfunc, elem);
 }
 
 static size_t cx_pll_find(
@@ -596,21 +596,21 @@
 ) {
     return cx_linked_list_find(((cx_linked_list *) list)->begin,
                                CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
-                               1, list->cmpfunc, elem);
+                               true, list->cmpfunc, elem);
 }
 
 static void cx_ll_sort(cx_list_s *list) {
     cx_linked_list *ll = (cx_linked_list *) list;
     cx_linked_list_sort((void **) &ll->begin, (void **) &ll->end,
                         CX_LL_LOC_PREV, CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
-                        0, list->cmpfunc);
+                        false, list->cmpfunc);
 }
 
 static void cx_pll_sort(cx_list_s *list) {
     cx_linked_list *ll = (cx_linked_list *) list;
     cx_linked_list_sort((void **) &ll->begin, (void **) &ll->end,
                         CX_LL_LOC_PREV, CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
-                        1, list->cmpfunc);
+                        true, list->cmpfunc);
 }
 
 static cx_list_class cx_linked_list_class = {
--- a/test/test_list.c	Tue Dec 28 14:16:04 2021 +0100
+++ b/test/test_list.c	Tue Dec 28 14:25:05 2021 +0100
@@ -125,6 +125,31 @@
     CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&d, 3, loc_prev, 1), &b)
 }
 
+void test_linked_list_find(void) {
+    int data[] = {2, 4, 6, 8};
+    void *list = create_test_data(4, data);
+    int s;
+
+    s = 2;
+    CU_ASSERT_EQUAL(cx_linked_list_find(list, loc_next, loc_data,
+                                        false, (CxListComparator) cmp_int, &s), 0)
+    s = 4;
+    CU_ASSERT_EQUAL(cx_linked_list_find(list, loc_next, loc_data,
+                                        false, (CxListComparator) cmp_int, &s), 1)
+    s = 6;
+    CU_ASSERT_EQUAL(cx_linked_list_find(list, loc_next, loc_data,
+                                        false, (CxListComparator) cmp_int, &s), 2)
+    s = 8;
+    CU_ASSERT_EQUAL(cx_linked_list_find(list, loc_next, loc_data,
+                                        false, (CxListComparator) cmp_int, &s), 3)
+    s = 10;
+    CU_ASSERT_EQUAL(cx_linked_list_find(list, loc_next, loc_data,
+                                        false, (CxListComparator) cmp_int, &s), 4)
+    s = -2;
+    CU_ASSERT_EQUAL(cx_linked_list_find(list, loc_next, loc_data,
+                                        false, (CxListComparator) cmp_int, &s), 4)
+}
+
 void test_linked_list_compare(void) {
     int a[] = {2, 4, 6, 8};
     int b[] = {2, 4, 6};
@@ -135,19 +160,19 @@
     void *lc = create_test_data(4, c);
 
     CU_ASSERT_TRUE(0 < cx_linked_list_compare(la, lb, loc_next, loc_data,
-                                              0, (CxListComparator) cmp_int)
+                                              false, (CxListComparator) cmp_int)
     )
     CU_ASSERT_TRUE(0 > cx_linked_list_compare(lb, la, loc_next, loc_data,
-                                              0, (CxListComparator) cmp_int)
+                                              false, (CxListComparator) cmp_int)
     )
     CU_ASSERT_TRUE(0 < cx_linked_list_compare(lc, la, loc_next, loc_data,
-                                              0, (CxListComparator) cmp_int)
+                                              false, (CxListComparator) cmp_int)
     )
     CU_ASSERT_TRUE(0 > cx_linked_list_compare(la, lc, loc_next, loc_data,
-                                              0, (CxListComparator) cmp_int)
+                                              false, (CxListComparator) cmp_int)
     )
     CU_ASSERT_TRUE(0 == cx_linked_list_compare(la, la, loc_next, loc_data,
-                                               0, (CxListComparator) cmp_int)
+                                               false, (CxListComparator) cmp_int)
     )
 
     destroy_test_data(la);
@@ -487,7 +512,7 @@
     void *end = cx_linked_list_last(begin, loc_next);
 
     cx_linked_list_sort(&begin, &end, loc_prev, loc_next, loc_data,
-                        0, (CxListComparator) cmp_int);
+                        false, (CxListComparator) cmp_int);
 
     struct node *check = begin;
     struct node *check_last = NULL;
@@ -952,6 +977,7 @@
 
     cu_add_test(suite, test_linked_list_link_unlink);
     cu_add_test(suite, test_linked_list_at);
+    cu_add_test(suite, test_linked_list_find);
     cu_add_test(suite, test_linked_list_compare);
     cu_add_test(suite, test_linked_list_prepend);
     cu_add_test(suite, test_linked_list_add);

mercurial