fix cx_linked_list_sort() not working for empty lists

Fri, 05 May 2023 19:07:56 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 05 May 2023 19:07:56 +0200
changeset 702
3390b58ad15a
parent 701
72a440c437e9
child 703
425d4279856f

fix cx_linked_list_sort() not working for empty lists

src/linked_list.c file | annotate | diff | comparison | revisions
tests/test_list.cpp file | annotate | diff | comparison | revisions
--- a/src/linked_list.c	Mon Apr 24 19:08:56 2023 +0200
+++ b/src/linked_list.c	Fri May 05 19:07:56 2023 +0200
@@ -355,6 +355,9 @@
     // set start node
     ls = *begin;
 
+    // early exit when this list is empty
+    if (ls == NULL) return;
+
     // check how many elements are already sorted
     lc = ls;
     size_t ln = 1;
--- a/tests/test_list.cpp	Mon Apr 24 19:08:56 2023 +0200
+++ b/tests/test_list.cpp	Fri May 05 19:07:56 2023 +0200
@@ -509,6 +509,13 @@
     }
 }
 
+TEST(LinkedList_LowLevel, cx_linked_list_sort_empty) {
+    void *begin = nullptr;
+    EXPECT_NO_FATAL_FAILURE(
+        cx_linked_list_sort(&begin, nullptr, loc_prev, loc_next, loc_data, cx_cmp_int);
+    );
+}
+
 TEST(LinkedList_LowLevel, cx_linked_list_sort) {
     int_test_data<1500> testdata;
     std::array<int, 1500> sorted{};

mercurial