dovecot-2.2: lib: test-array - test array_cmp()

dovecot at dovecot.org dovecot at dovecot.org
Tue Nov 25 01:53:53 UTC 2014


details:   http://hg.dovecot.org/dovecot-2.2/rev/a45ee7e221b0
changeset: 18110:a45ee7e221b0
user:      Phil Carmody <phil at dovecot.fi>
date:      Tue Nov 25 03:43:02 2014 +0200
description:
lib: test-array - test array_cmp()
Signed-off-by: Phil Carmody <phil at dovecot.fi>

diffstat:

 src/lib/test-array.c |  37 +++++++++++++++++++++++++++++++++++++
 1 files changed, 37 insertions(+), 0 deletions(-)

diffs (56 lines):

diff -r 88e985e7f2b6 -r a45ee7e221b0 src/lib/test-array.c
--- a/src/lib/test-array.c	Tue Nov 25 03:31:34 2014 +0200
+++ b/src/lib/test-array.c	Tue Nov 25 03:43:02 2014 +0200
@@ -3,6 +3,8 @@
 #include "test-lib.h"
 #include "array.h"
 
+#include <stdlib.h> /* rand() */
+
 struct foo {
 	unsigned int a, b, c;
 };
@@ -51,8 +53,43 @@
 	test_end();
 }
 
+static void test_array_cmp(void)
+{
+	static const unsigned short deltas[] = {
+		-32768, -16384, -512, -256, -128, -64, -2, -1,
+		0, 1, 2, 64, 128, 256, 512, 16384, 32768
+	};
+
+#define NELEMS 5u
+	ARRAY(unsigned short) arr1, arr2;
+	unsigned short elems[NELEMS];
+	unsigned int i;
+
+	test_begin("array compare (ushort)");
+	t_array_init(&arr1, NELEMS);
+	t_array_init(&arr2, NELEMS);
+	for (i = 0; i < NELEMS; i++) {
+		elems[i] = rand();
+		array_append(&arr2, &elems[i], 1);
+	}
+	array_append(&arr1, elems, NELEMS);
+	test_assert(array_cmp(&arr1, &arr2) == 1);
+	for (i = 0; i < 256; i++) {
+		unsigned int j = rand() % NELEMS;
+		unsigned short tmp = *array_idx(&arr2, j);
+		unsigned short repl = tmp + deltas[rand() % N_ELEMENTS(deltas)];
+
+		array_idx_set(&arr2, j, &repl);
+		test_assert_idx(array_cmp(&arr1, &arr2) == (tmp == repl), i);
+		array_idx_set(&arr2, j, &tmp);
+		test_assert_idx(array_cmp(&arr1, &arr2) == TRUE, i);
+	}
+	test_end();
+}
+
 void test_array(void)
 {
 	test_array_foreach();
 	test_array_reverse();
+	test_array_cmp();
 }


More information about the dovecot-cvs mailing list