tests/test_iterator.c

changeset 1070
0a5a356a4486
parent 951
8f6d6556380a
--- a/tests/test_iterator.c	Wed Jan 01 13:31:38 2025 +0100
+++ b/tests/test_iterator.c	Wed Jan 01 14:03:49 2025 +0100
@@ -65,13 +65,13 @@
 
     CxIterator iter = cxIterator(array, sizeof(unsigned), size);
     CX_TEST_DO {
+        CX_TEST_ASSERT(iter.elem_size == sizeof(unsigned));
+        CX_TEST_ASSERT(iter.elem_count == size);
+        CX_TEST_ASSERT(iter.src_handle.c == array);
         unsigned expected = 0;
         cx_foreach(unsigned *, e, iter) {
             CX_TEST_ASSERT(iter.index == expected);
             CX_TEST_ASSERT(*e == expected);
-            CX_TEST_ASSERT(iter.elem_size == sizeof(unsigned));
-            CX_TEST_ASSERT(iter.elem_count == size);
-            CX_TEST_ASSERT(iter.src_handle.c == array);
             CX_TEST_ASSERT(iter.elem_handle == &array[expected]);
             expected++;
         }
@@ -79,6 +79,31 @@
     }
 }
 
+CX_TEST(test_iterator_iterate_pointers) {
+    unsigned array[20];
+    unsigned* ptr_array[20];
+    size_t size = cx_nmemb(array);
+    for (unsigned i = 0 ; i < size ; i++) {
+        array[i] = 3*i;
+        ptr_array[i] = &array[i];
+    }
+
+    CxIterator iter = cxIteratorPtr(ptr_array, size);
+    CX_TEST_DO {
+        CX_TEST_ASSERT(iter.elem_size == sizeof(void*));
+        CX_TEST_ASSERT(iter.elem_count == size);
+        CX_TEST_ASSERT(iter.src_handle.c == ptr_array);
+        unsigned idx = 0;
+        cx_foreach(unsigned *, e, iter) {
+            CX_TEST_ASSERT(iter.index == idx);
+            CX_TEST_ASSERT(*e == array[idx]);
+            CX_TEST_ASSERT(iter.elem_handle == &ptr_array[idx]);
+            idx++;
+        }
+        CX_TEST_ASSERT(idx == size);
+    }
+}
+
 CX_TEST(test_iterator_with_slow_remove) {
     unsigned array[20];
     size_t size = cx_nmemb(array);
@@ -170,6 +195,7 @@
 
     cx_test_register(suite, test_iterator_create);
     cx_test_register(suite, test_iterator_iterate);
+    cx_test_register(suite, test_iterator_iterate_pointers);
     cx_test_register(suite, test_iterator_with_slow_remove);
     cx_test_register(suite, test_iterator_with_fast_remove);
 

mercurial