]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
Added array_foreach_idx()
authorTimo Sirainen <tss@iki.fi>
Wed, 2 Dec 2009 21:35:32 +0000 (15:35 -0600)
committerTimo Sirainen <tss@iki.fi>
Wed, 2 Dec 2009 21:35:32 +0000 (15:35 -0600)
--HG--
branch : HEAD

src/lib/array.h
src/lib/test-array.c

index ebfa3d487f6350ea2860090a08540960ead7aaa8..71ac7279c59f2bd6a5babf2bf34d63644d0734f6 100644 (file)
@@ -77,6 +77,9 @@
             (elem)++)
 #endif
 
+#define array_foreach_idx(array, elem) \
+       ((elem) - (array)->v[0])
+
 static inline void
 array_create_from_buffer_i(struct array *array, buffer_t *buffer,
                           size_t element_size)
index 419e35dc28f9f312541a9ac27674556bbdc2a18d..f63a4a232e166e330e4416030f36b81b1b995bf6 100644 (file)
@@ -3,6 +3,33 @@
 #include "test-lib.h"
 #include "array.h"
 
+struct foo {
+       unsigned int a, b, c;
+};
+
+static void test_array_foreach(void)
+{
+       ARRAY_DEFINE(foos, struct foo);
+       const struct foo *foo;
+       struct foo nfoo;
+       unsigned int i;
+
+       test_begin("array foreach");
+       t_array_init(&foos, 32);
+       for (i = 0; i < 10; i++) {
+               nfoo.a = nfoo.b = nfoo.c = i;
+               array_append(&foos, &nfoo, 1);
+       }
+
+       array_foreach(&foos, foo) {
+               i = array_foreach_idx(&foos, foo);
+               test_assert(foo->a == i);
+               test_assert(foo->b == i);
+               test_assert(foo->c == i);
+       }
+       test_end();
+}
+
 static void test_array_reverse(void)
 {
        ARRAY_DEFINE(intarr, int);
@@ -26,5 +53,6 @@ static void test_array_reverse(void)
 
 void test_array(void)
 {
+       test_array_foreach();
        test_array_reverse();
 }