dovecot-2.0: Added array_foreach_idx()

dovecot at dovecot.org dovecot at dovecot.org
Wed Dec 2 23:35:58 EET 2009


details:   http://hg.dovecot.org/dovecot-2.0/rev/cc4e9d1fef7e
changeset: 10405:cc4e9d1fef7e
user:      Timo Sirainen <tss at iki.fi>
date:      Wed Dec 02 15:35:32 2009 -0600
description:
Added array_foreach_idx()

diffstat:

2 files changed, 31 insertions(+)
src/lib/array.h      |    3 +++
src/lib/test-array.c |   28 ++++++++++++++++++++++++++++

diffs (57 lines):

diff -r f8151445662e -r cc4e9d1fef7e src/lib/array.h
--- a/src/lib/array.h	Wed Nov 25 13:19:42 2009 -0500
+++ b/src/lib/array.h	Wed Dec 02 15:35:32 2009 -0600
@@ -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)
diff -r f8151445662e -r cc4e9d1fef7e src/lib/test-array.c
--- a/src/lib/test-array.c	Wed Nov 25 13:19:42 2009 -0500
+++ b/src/lib/test-array.c	Wed Dec 02 15:35:32 2009 -0600
@@ -2,6 +2,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)
 {
@@ -26,5 +53,6 @@ static void test_array_reverse(void)
 
 void test_array(void)
 {
+	test_array_foreach();
 	test_array_reverse();
 }


More information about the dovecot-cvs mailing list