assert instead of assume sorted property in debug builds

Fri, 10 Oct 2025 12:26:43 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 10 Oct 2025 12:26:43 +0200
changeset 1418
5e1579713bcf
parent 1417
b97faf8b7ab7
child 1419
e46406fd1b3c

assert instead of assume sorted property in debug builds

src/cx/list.h file | annotate | diff | comparison | revisions
--- a/src/cx/list.h	Wed Oct 08 20:20:54 2025 +0200
+++ b/src/cx/list.h	Fri Oct 10 12:26:43 2025 +0200
@@ -39,6 +39,8 @@
 #include "common.h"
 #include "collection.h"
 
+#include <assert.h>
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -481,7 +483,8 @@
         CxList *list,
         const void *elem
 ) {
-    list->collection.sorted = true; // guaranteed by definition
+    assert(list->collection.sorted || list->collection.size == 0);
+    list->collection.sorted = true;
     const void *data = list->collection.store_pointer ? &elem : elem;
     return list->cl->insert_sorted(list, data, 1) == 0;
 }
@@ -541,7 +544,8 @@
         const void *array,
         size_t n
 ) {
-    list->collection.sorted = true; // guaranteed by definition
+    assert(list->collection.sorted || list->collection.size == 0);
+    list->collection.sorted = true;
     return list->cl->insert_sorted(list, array, n);
 }
 
@@ -779,8 +783,8 @@
  */
 cx_attr_nonnull
 static inline void cxListClear(CxList *list) {
+    list->cl->clear(list);
     list->collection.sorted = true; // empty lists are always sorted
-    list->cl->clear(list);
 }
 
 /**

mercurial