]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: test-array - Add more unit tests for array_foreach*()
authorTimo Sirainen <timo.sirainen@open-xchange.com>
Tue, 18 May 2021 14:15:02 +0000 (17:15 +0300)
committeraki.tuomi <aki.tuomi@open-xchange.com>
Tue, 25 May 2021 09:42:16 +0000 (09:42 +0000)
Make sure that the elem value is as expected after the loop completes.

src/lib/test-array.c

index ccaf1c3e7ab80092af439523f1d94b60a84b36fc..7d2d9bb6641596fb103fb8b8be2aea10b14258e5 100644 (file)
@@ -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)