remove cx_for_n() macro - fixes #467

Thu, 31 Oct 2024 14:54:44 +0100

author
Mike Becker <universe@uap-core.de>
date
Thu, 31 Oct 2024 14:54:44 +0100
changeset 962
cd418898af5c
parent 961
bc8b7c5f55fb
child 963
2f601274bbac

remove cx_for_n() macro - fixes #467

docs/src/features.md file | annotate | diff | comparison | revisions
src/Makefile file | annotate | diff | comparison | revisions
src/cx/utils.h file | annotate | diff | comparison | revisions
src/hash_map.c file | annotate | diff | comparison | revisions
src/linked_list.c file | annotate | diff | comparison | revisions
src/mempool.c file | annotate | diff | comparison | revisions
src/string.c file | annotate | diff | comparison | revisions
tests/Makefile file | annotate | diff | comparison | revisions
tests/test_list.c file | annotate | diff | comparison | revisions
tests/test_utils.c file | annotate | diff | comparison | revisions
--- a/docs/src/features.md	Thu Oct 31 14:39:05 2024 +0100
+++ b/docs/src/features.md	Thu Oct 31 14:54:44 2024 +0100
@@ -353,11 +353,9 @@
 
 *Header file:* [utils.h](api/utils_8h.html)
 
-UCX provides some utilities for routine tasks. Most of them are simple macros, like e.g. the `cx_for_n()` macro,
-creating a `for` loop counting from zero to (n-1) which is extremely useful to traverse the indices of
-an array.
+UCX provides some utilities for routine tasks.
 
-But the most useful utilities are the *stream copy* functions, which provide a simple way to copy all - or a
+The most useful utilities are the *stream copy* functions, which provide a simple way to copy all - or a
 bounded amount of - data from one stream to another. Since the read/write functions of a UCX buffer are
 fully compatible with stream read/write functions, you can easily transfer data from file or network streams to
 a UCX buffer or vice-versa.
--- a/src/Makefile	Thu Oct 31 14:39:05 2024 +0100
+++ b/src/Makefile	Thu Oct 31 14:54:44 2024 +0100
@@ -91,7 +91,7 @@
 
 $(build_dir)/hash_map$(OBJ_EXT): hash_map.c cx/hash_map.h cx/map.h \
  cx/common.h cx/collection.h cx/allocator.h cx/iterator.h cx/compare.h \
- cx/string.h cx/hash_key.h cx/utils.h
+ cx/string.h cx/hash_key.h
 	@echo "Compiling $<"
 	$(CC) -o $@ $(CFLAGS) -c $<
 
@@ -107,7 +107,7 @@
 
 $(build_dir)/linked_list$(OBJ_EXT): linked_list.c cx/linked_list.h \
  cx/common.h cx/list.h cx/collection.h cx/allocator.h cx/iterator.h \
- cx/compare.h cx/utils.h cx/compare.h
+ cx/compare.h cx/compare.h
 	@echo "Compiling $<"
 	$(CC) -o $@ $(CFLAGS) -c $<
 
@@ -138,7 +138,7 @@
 	$(CC) -o $@ $(CFLAGS) -c $<
 
 $(build_dir)/string$(OBJ_EXT): string.c cx/string.h cx/common.h \
- cx/allocator.h cx/utils.h
+ cx/allocator.h
 	@echo "Compiling $<"
 	$(CC) -o $@ $(CFLAGS) -c $<
 
--- a/src/cx/utils.h	Thu Oct 31 14:39:05 2024 +0100
+++ b/src/cx/utils.h	Thu Oct 31 14:54:44 2024 +0100
@@ -45,11 +45,6 @@
 extern "C" {
 #endif
 
-/**
- * Convenience macro for a for loop that counts from zero to n-1.
- */
-#define cx_for_n(varname, n) for (size_t varname = 0 ; (varname) < (n) ; (varname)++)
-
 // cx_szmul() definition
 
 #if (__GNUC__ >= 5 || defined(__clang__)) && !defined(CX_NO_SZMUL_BUILTIN)
--- a/src/hash_map.c	Thu Oct 31 14:39:05 2024 +0100
+++ b/src/hash_map.c	Thu Oct 31 14:54:44 2024 +0100
@@ -27,7 +27,6 @@
  */
 
 #include "cx/hash_map.h"
-#include "cx/utils.h"
 
 #include <string.h>
 #include <assert.h>
@@ -45,7 +44,7 @@
 
 static void cx_hash_map_clear(struct cx_map_s *map) {
     struct cx_hash_map_s *hash_map = (struct cx_hash_map_s *) map;
-    cx_for_n(i, hash_map->bucket_count) {
+    for (size_t i = 0; i < hash_map->bucket_count; i++) {
         struct cx_hash_map_element_s *elem = hash_map->buckets[i];
         if (elem != NULL) {
             do {
@@ -441,7 +440,7 @@
         }
 
         // iterate through the elements and assign them to their new slots
-        cx_for_n(slot, hash_map->bucket_count) {
+        for (size_t slot = 0; slot < hash_map->bucket_count; slot++) {
             struct cx_hash_map_element_s *elm = hash_map->buckets[slot];
             while (elm != NULL) {
                 struct cx_hash_map_element_s *next = elm->next;
--- a/src/linked_list.c	Thu Oct 31 14:39:05 2024 +0100
+++ b/src/linked_list.c	Thu Oct 31 14:54:44 2024 +0100
@@ -27,7 +27,6 @@
  */
 
 #include "cx/linked_list.h"
-#include "cx/utils.h"
 #include "cx/compare.h"
 #include <string.h>
 #include <assert.h>
@@ -447,7 +446,7 @@
 
     // Update pointer
     if (loc_prev >= 0) ll_prev(sorted[0]) = NULL;
-    cx_for_n (i, length - 1) {
+    for (size_t i = 0 ; i < length - 1; i++) {
         cx_linked_list_link(sorted[i], sorted[i + 1], loc_prev, loc_next);
     }
     ll_next(sorted[length - 1]) = NULL;
@@ -772,7 +771,7 @@
     // copy or destroy the removed chain
     if (targetbuf == NULL) {
         cx_linked_list_node *n = node;
-        cx_for_n(i, removed) {
+        for (size_t i = 0; i < removed; i++) {
             // element destruction
             cx_invoke_destructor(list, n->payload);
 
@@ -784,7 +783,7 @@
     } else {
         char *dest = targetbuf;
         cx_linked_list_node *n = node;
-        cx_for_n(i, removed) {
+        for (size_t i = 0; i < removed; i++) {
             // copy payload
             memcpy(dest, n->payload, list->collection.elem_size);
 
--- a/src/mempool.c	Thu Oct 31 14:39:05 2024 +0100
+++ b/src/mempool.c	Thu Oct 31 14:54:44 2024 +0100
@@ -97,7 +97,7 @@
         return NULL;
     }
     if (mem != newm) {
-        cx_for_n(i, pool->size) {
+        for (size_t i = 0; i < pool->size; i++) {
             if (pool->data[i] == mem) {
                 pool->data[i] = newm;
                 return ((char*)newm) + sizeof(cx_destructor_func);
@@ -119,7 +119,7 @@
     struct cx_mempool_memory_s *mem = (struct cx_mempool_memory_s *)
             ((char *) ptr - sizeof(cx_destructor_func));
 
-    cx_for_n(i, pool->size) {
+    for (size_t i = 0; i < pool->size; i++) {
         if (mem == pool->data[i]) {
             if (mem->destructor) {
                 mem->destructor(mem->c);
@@ -139,7 +139,7 @@
 
 void cxMempoolDestroy(CxMempool *pool) {
     struct cx_mempool_memory_s *mem;
-    cx_for_n(i, pool->size) {
+    for (size_t i = 0; i < pool->size; i++) {
         mem = pool->data[i];
         if (mem->destructor) {
             mem->destructor(mem->c);
--- a/src/string.c	Thu Oct 31 14:39:05 2024 +0100
+++ b/src/string.c	Thu Oct 31 14:54:44 2024 +0100
@@ -27,7 +27,6 @@
  */
 
 #include "cx/string.h"
-#include "cx/utils.h"
 
 #include <string.h>
 #include <stdarg.h>
@@ -89,7 +88,7 @@
     va_list ap;
     va_start(ap, count);
     size_t size = 0;
-    cx_for_n(i, count) {
+    for (size_t i = 0; i < count; i++) {
         cxstring str = va_arg(ap, cxstring);
         size += str.length;
     }
@@ -114,7 +113,7 @@
 
     // get all args and overall length
     size_t slen = str.length;
-    cx_for_n(i, count) {
+    for (size_t i = 0; i < count; i++) {
         cxstring s = va_arg (ap, cxstring);
         strings[i] = s;
         slen += s.length;
@@ -132,7 +131,7 @@
     // concatenate strings
     size_t pos = str.length;
     str.length = slen;
-    cx_for_n(i, count) {
+    for (size_t i = 0; i < count; i++) {
         cxstring s = strings[i];
         memcpy(str.ptr + pos, s.ptr, s.length);
         pos += s.length;
@@ -193,7 +192,7 @@
 ) {
     chr = 0xFF & chr;
     // TODO: improve by comparing multiple bytes at once
-    cx_for_n(i, string.length) {
+    for (size_t i = 0; i < string.length; i++) {
         if (string.ptr[i] == chr) {
             return cx_strsubs(string, i);
         }
@@ -556,13 +555,13 @@
 }
 
 void cx_strlower(cxmutstr string) {
-    cx_for_n(i, string.length) {
+    for (size_t i = 0; i < string.length; i++) {
         string.ptr[i] = (char) tolower(string.ptr[i]);
     }
 }
 
 void cx_strupper(cxmutstr string) {
-    cx_for_n(i, string.length) {
+    for (size_t i = 0; i < string.length; i++) {
         string.ptr[i] = (char) toupper(string.ptr[i]);
     }
 }
@@ -748,7 +747,7 @@
 
     // if more delimiters are specified, check them now
     if (ctx->delim_more_count > 0) {
-        cx_for_n(i, ctx->delim_more_count) {
+        for (size_t i = 0; i < ctx->delim_more_count; i++) {
             cxstring d = cx_strstr(haystack, ctx->delim_more[i]);
             if (d.length > 0 && (delim.length == 0 || d.ptr < delim.ptr)) {
                 delim.ptr = d.ptr;
--- a/tests/Makefile	Thu Oct 31 14:39:05 2024 +0100
+++ b/tests/Makefile	Thu Oct 31 14:54:44 2024 +0100
@@ -91,9 +91,9 @@
 
 $(TEST_DIR)/test_list$(OBJ_EXT): test_list.c ../src/cx/test.h \
  ../src/cx/common.h util_allocator.h ../src/cx/allocator.h \
- ../src/cx/compare.h ../src/cx/utils.h ../src/cx/array_list.h \
- ../src/cx/list.h ../src/cx/collection.h ../src/cx/allocator.h \
- ../src/cx/iterator.h ../src/cx/compare.h ../src/cx/linked_list.h
+ ../src/cx/compare.h ../src/cx/array_list.h ../src/cx/list.h \
+ ../src/cx/collection.h ../src/cx/allocator.h ../src/cx/iterator.h \
+ ../src/cx/compare.h ../src/cx/linked_list.h
 	@echo "Compiling $<"
 	$(CC) -o $@ $(CFLAGS) -c $<
 
--- a/tests/test_list.c	Thu Oct 31 14:39:05 2024 +0100
+++ b/tests/test_list.c	Thu Oct 31 14:54:44 2024 +0100
@@ -29,7 +29,6 @@
 #include "cx/test.h"
 #include "util_allocator.h"
 #include "cx/compare.h"
-#include "cx/utils.h"
 
 #include "cx/array_list.h"
 #include "cx/linked_list.h"
@@ -154,7 +153,7 @@
     };
 
     CX_TEST_DO {
-        cx_for_n(i, 18) {
+        for (size_t i = 0; i < 18; i++) {
             CX_TEST_ASSERT(i == cx_array_binary_search(array, 18, sizeof(int), &array[i], cx_cmp_int));
         }
 
@@ -1225,7 +1224,7 @@
 static inline int *int_test_data_added_to_list(CxList *list, bool isptrlist, size_t len) {
     int *testdata = int_test_data(len);
     if (isptrlist) {
-        cx_for_n(i, len) {
+        for (size_t i = 0; i < len; i++) {
             cxListAdd(list, &testdata[i]);
         }
     } else {
@@ -1237,14 +1236,24 @@
 roll_out_test_combos(add, {
     const size_t len = 250;
     int *testdata = int_test_data(len);
-    cx_for_n (i, len) CX_TEST_ASSERT(cxListAdd(list, &testdata[i]) == 0);
+    for (size_t i = 0; i < len; i++) {
+        CX_TEST_ASSERT(cxListAdd(list, &testdata[i]) == 0);
+    }
     CX_TEST_ASSERT(cxListSize(list) == len);
-    cx_for_n (i, len) CX_TEST_ASSERT(*(int *) cxListAt(list, i) == testdata[i]);
-    cx_for_n (i, len) ++testdata[i];
+    for (size_t i = 0; i < len; i++) {
+        CX_TEST_ASSERT(*(int *) cxListAt(list, i) == testdata[i]);
+    }
+    for (size_t i = 0; i < len; i++) {
+        ++testdata[i];
+    }
     if (isptrlist) {
-        cx_for_n (i, len) CX_TEST_ASSERT(*(int *) cxListAt(list, i) == testdata[i]);
+        for (size_t i = 0; i < len; i++) {
+            CX_TEST_ASSERT(*(int *) cxListAt(list, i) == testdata[i]);
+        }
     } else {
-        cx_for_n (i, len) CX_TEST_ASSERT(*(int *) cxListAt(list, i) == testdata[i] - 1);
+        for (size_t i = 0; i < len; i++) {
+            CX_TEST_ASSERT(*(int *) cxListAt(list, i) == testdata[i] - 1);
+        }
     }
     free(testdata);
 })
@@ -1275,7 +1284,7 @@
     int b[5] = array_init(9, 18, 72, 50, 7);
     int *aptr[5];
     int *bptr[5];
-    cx_for_n(i, 5) {
+    for (size_t i = 0; i < 5; i++) {
         aptr[i] = &a[i];
         bptr[i] = &b[i];
     }
@@ -1322,8 +1331,7 @@
     int d8 = 90;
     int *d6ptr[6];
     int *d7ptr[6];
-    cx_for_n(i, 6)
-    {
+    for (size_t i = 0; i < 6; i++) {
         d6ptr[i] = &d6a[i];
         d7ptr[i] = &d7a[i];
     }
@@ -1353,8 +1361,7 @@
     CX_TEST_ASSERT(0 == cxListInsertSorted(list, &d8));
 
     CX_TEST_ASSERT(cxListSize(list) == 18);
-    cx_for_n(i, 18)
-    {
+    for (size_t i = 0; i < 18; i++) {
         CX_TEST_ASSERT(*(int *) cxListAt(list, i) == expected[i]);
     }
 })
@@ -1493,7 +1500,7 @@
     size_t len = 128;
     int *testdata = int_test_data_added_to_list(list, isptrlist, 128);
     CX_TEST_ASSERT(cxListSize(list) == len);
-    cx_for_n (i, len) {
+    for (size_t i = 0; i < len; i++) {
         CX_TEST_ASSERT(*(int *) cxListAt(list, i) == testdata[i]);
     }
     CX_TEST_ASSERT(cxListAt(list, cxListSize(list)) == NULL);
@@ -1504,7 +1511,7 @@
     int original[16] = array_init(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
     int swapped[16] = array_init(8, 4, 14, 3, 1, 5, 9, 12, 0, 6, 11, 10, 7, 15, 2, 13);
 
-    cx_for_n(i, 16) {
+    for (size_t i = 0; i < 16; i++) {
         cxListAdd(list, &original[i]);
     }
 
@@ -1548,7 +1555,7 @@
     const size_t testdata_len = 500;
     int *testdata = int_test_data_added_to_list(list, isptrlist, testdata_len);
 
-    cx_for_n (attempt, 25) {
+    for (size_t attempt = 0; attempt < 25; attempt++) {
         int exp = rand() % testdata_len; // NOLINT(cert-msc50-cpp)
         int val = testdata[exp];
         // randomly picked number could occur earlier in list - find first position
@@ -1575,7 +1582,9 @@
     qsort(expected, testdata_len, sizeof(int), cx_cmp_int);
 
     cxListSort(list);
-    cx_for_n (i, testdata_len) CX_TEST_ASSERT(*(int *) cxListAt(list, i) == expected[i]);
+    for (size_t i = 0; i < testdata_len; i++) {
+        CX_TEST_ASSERT(*(int *) cxListAt(list, i) == expected[i]);
+    }
 
     free(expected);
     free(testdata);
@@ -1585,7 +1594,7 @@
     const size_t testdata_len = 50;
     int *testdata = int_test_data_added_to_list(list, isptrlist, testdata_len);
     cxListReverse(list);
-    cx_for_n(i, testdata_len) {
+    for (size_t i = 0; i < testdata_len; i++) {
         CX_TEST_ASSERT(*(int *) cxListAt(list, i) == testdata[testdata_len - 1 - i]);
     }
     free(testdata);
@@ -1637,14 +1646,18 @@
     }
     CX_TEST_ASSERT(i == 0);
     CX_TEST_ASSERT(cxListSize(list) == len / 2);
-    cx_for_n(k, len / 2) CX_TEST_ASSERT(*(int *) cxListAt(list, k) == testdata[k * 2]);
+    for (size_t k = 0; k < len / 2; k++) {
+        CX_TEST_ASSERT(*(int *) cxListAt(list, k) == testdata[k * 2]);
+    }
 
     free(testdata);
 })
 
 roll_out_test_combos(insert_with_iterator, {
     int fivenums[] = array_init(0, 1, 2, 3, 4);
-    cx_for_n(i, 5) cxListAdd(list, &fivenums[i]);
+    for (size_t i = 0; i < 5; i++) {
+        cxListAdd(list, &fivenums[i]);
+    }
     int newdata[] = array_init(10, 20, 30, 40, 50);
 
     CxIterator iter = cxListMutIteratorAt(list, 2);
@@ -1681,7 +1694,9 @@
     CX_TEST_ASSERT(iter.elem_count == 10);
 
     int expdata[] = array_init(30, 0, 1, 20, 2, 10, 3, 4, 40, 50);
-    cx_for_n (j, 10) CX_TEST_ASSERT(*(int *) cxListAt(list, j) == expdata[j]);
+    for (size_t j = 0; j < 10; j++) {
+        CX_TEST_ASSERT(*(int *) cxListAt(list, j) == expdata[j]);
+    }
 })
 
 static CX_TEST_SUBROUTINE(test_list_verify_compare, CxList *left, CxList *right) {
@@ -1709,7 +1724,7 @@
     const size_t len = 47; \
     int *testdata = int_test_data_added_to_list(list, isptrlist, len); \
     CxList *other = otherctr; \
-    cx_for_n(i, len) cxListAdd(other, &testdata[i]); \
+    for (size_t i = 0; i < len; i++) cxListAdd(other, &testdata[i]); \
     CX_TEST_CALL_SUBROUTINE(test_list_verify_compare, list, other); \
     cxListDestroy(other); \
     free(testdata); \
@@ -1732,17 +1747,14 @@
 )
 
 roll_out_test_combos_with_defaulted_funcs(compare_unoptimized, {
-    \
-    const size_t len = 33; \
-    int *testdata = int_test_data_added_to_list(list, isptrlist, len); \
-    CxList *other = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int), 50); \
-    do_set_default_class_funcs(other); \
-    cx_for_n(i, len)
-    cxListAdd(other, &testdata[i]); \
-    CX_TEST_CALL_SUBROUTINE(test_list_verify_compare, list, other); \
-    cxListDestroy(other); \
-    free(testdata); \
-
+    const size_t len = 33;
+    int *testdata = int_test_data_added_to_list(list, isptrlist, len);
+    CxList *other = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int), 50);
+    do_set_default_class_funcs(other);
+    for (size_t i = 0; i < len; i++) cxListAdd(other, &testdata[i]);
+    CX_TEST_CALL_SUBROUTINE(test_list_verify_compare, list, other);
+    cxListDestroy(other);
+    free(testdata);
 })
 
 static unsigned destr_test_ctr;
--- a/tests/test_utils.c	Thu Oct 31 14:39:05 2024 +0100
+++ b/tests/test_utils.c	Thu Oct 31 14:54:44 2024 +0100
@@ -95,17 +95,6 @@
     cxBufferDestroy(&target);
 }
 
-CX_TEST(test_forn) {
-    unsigned j;
-    j = 0;
-    CX_TEST_DO {
-        cx_for_n(i, 50) {
-            CX_TEST_ASSERT(i == j);
-            j++;
-        }
-    }
-}
-
 CX_TEST(test_szmul) {
     size_t r;
     int e;
@@ -233,7 +222,6 @@
 
     cx_test_register(suite, test_stream_bncopy);
     cx_test_register(suite, test_stream_ncopy);
-    cx_test_register(suite, test_forn);
     cx_test_register(suite, test_szmul);
 #ifdef CX_SZMUL_BUILTIN
     cx_test_register(suite, test_szmul_impl);

mercurial