From: Timo Sirainen Date: Tue, 18 May 2021 14:15:02 +0000 (+0300) Subject: lib: test-array - Add more unit tests for array_foreach*() X-Git-Tag: 2.3.16~104 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=3b3d90d2c922a33d6ba0b8d3d13900a1b18e372b;p=thirdparty%2Fdovecot%2Fcore.git lib: test-array - Add more unit tests for array_foreach*() Make sure that the elem value is as expected after the loop completes. --- diff --git a/src/lib/test-array.c b/src/lib/test-array.c index ccaf1c3e7a..7d2d9bb664 100644 --- a/src/lib/test-array.c +++ b/src/lib/test-array.c @@ -12,11 +12,18 @@ static void test_array_elem(void) { ARRAY(struct foo *) foos; struct foo *nfoo; + struct foo *foo; + struct foo local_foo; unsigned int i; test_begin("array elem"); t_array_init(&foos, 32); + foo = &local_foo; + array_foreach_elem(&foos, foo) + test_assert(FALSE); + test_assert(foo == &local_foo); + for (i = 1; i <= 3; i++) { nfoo = t_new(struct foo, 1); nfoo->a = i; @@ -25,7 +32,7 @@ static void test_array_elem(void) struct foo *const *foo_p = array_idx(&foos, 1); unsigned int idx = 1; - struct foo *foo = array_idx_elem(&foos, idx++); + foo = array_idx_elem(&foos, idx++); /* make sure idx isn't expanded multiple times in the macro */ test_assert(idx == 2); test_assert(*foo_p == foo); @@ -35,6 +42,7 @@ static void test_array_elem(void) test_assert(foo->a == i); i++; } + test_assert(foo->a == i-1); test_end(); } @@ -77,6 +85,8 @@ static void test_array_foreach(void) test_assert(foo->b == i); test_assert(foo->c == i); } + /* points past the last element */ + test_assert(foo == array_idx(&foos, i)+1); test_end(); } static void test_array_foreach_elem_string(void)