dovecot-2.0: liblib unit tests are now split to separate files.
dovecot at dovecot.org
dovecot at dovecot.org
Mon Jun 1 08:11:25 EEST 2009
details: http://hg.dovecot.org/dovecot-2.0/rev/810e36796e3d
changeset: 9425:810e36796e3d
user: Timo Sirainen <tss at iki.fi>
date: Mon Jun 01 01:11:17 2009 -0400
description:
liblib unit tests are now split to separate files.
diffstat:
17 files changed, 1000 insertions(+), 906 deletions(-)
src/lib/Makefile.am | 15
src/lib/test-aqueue.c | 71 ++
src/lib/test-array.c | 30 +
src/lib/test-base64.c | 110 ++++
src/lib/test-bsearch-insert-pos.c | 48 +
src/lib/test-buffer.c | 135 +++++
src/lib/test-istream.c | 2
src/lib/test-lib.c | 908 -------------------------------------
src/lib/test-lib.h | 20
src/lib/test-mempool-alloconly.c | 46 +
src/lib/test-network.c | 53 ++
src/lib/test-primes.c | 24
src/lib/test-priorityq.c | 101 ++++
src/lib/test-seq-range-array.c | 162 ++++++
src/lib/test-str-find.c | 85 +++
src/lib/test-str-sanitize.c | 40 +
src/lib/test-utc-mktime.c | 56 ++
diffs (truncated from 2009 to 300 lines):
diff -r 97f62a823666 -r 810e36796e3d src/lib/Makefile.am
--- a/src/lib/Makefile.am Mon Jun 01 00:41:23 2009 -0400
+++ b/src/lib/Makefile.am Mon Jun 01 01:11:17 2009 -0400
@@ -210,7 +210,20 @@ test_libs = \
test_lib_SOURCES = \
test-lib.c \
- test-istream.c
+ test-array.c \
+ test-aqueue.c \
+ test-base64.c \
+ test-bsearch-insert-pos.c \
+ test-buffer.c \
+ test-istream.c \
+ test-mempool-alloconly.c \
+ test-network.c \
+ test-primes.c \
+ test-priorityq.c \
+ test-seq-range-array.c \
+ test-str-find.c \
+ test-str-sanitize.c \
+ test-utc-mktime.c
test_headers = \
test-lib.h
diff -r 97f62a823666 -r 810e36796e3d src/lib/test-aqueue.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/test-aqueue.c Mon Jun 01 01:11:17 2009 -0400
@@ -0,0 +1,71 @@
+/* Copyright (c) 2007-2009 Dovecot authors, see the included COPYING file */
+
+#include "test-lib.h"
+#include "array.h"
+#include "aqueue.h"
+
+static bool aqueue_is_ok(struct aqueue *aqueue, unsigned int deleted_n)
+{
+ const unsigned int *p;
+ unsigned int n, i, count;
+
+ count = aqueue_count(aqueue);
+ for (i = 0, n = 1; i < count; i++, n++) {
+ p = array_idx_i(aqueue->arr, aqueue_idx(aqueue, i));
+ if (i == deleted_n)
+ n++;
+ if (*p != n)
+ return FALSE;
+ }
+ return TRUE;
+}
+
+static const unsigned int aqueue_input[] = { 1, 2, 3, 4, 5, 6 };
+static const char *test_aqueue2(unsigned int initial_size)
+{
+ ARRAY_DEFINE(aqueue_array, unsigned int);
+ unsigned int i, j, k;
+ struct aqueue *aqueue;
+
+ for (i = 0; i < N_ELEMENTS(aqueue_input); i++) {
+ for (k = 0; k < N_ELEMENTS(aqueue_input); k++) {
+ t_array_init(&aqueue_array, initial_size);
+ aqueue = aqueue_init(&aqueue_array.arr);
+ aqueue->head = aqueue->tail = initial_size - 1;
+ for (j = 0; j < k; j++) {
+ aqueue_append(aqueue, &aqueue_input[j]);
+ if (aqueue_count(aqueue) != j + 1) {
+ return t_strdup_printf("Wrong count after append %u vs %u)",
+ aqueue_count(aqueue), j + 1);
+ }
+ if (!aqueue_is_ok(aqueue, -1U))
+ return "Invalid data after append";
+ }
+
+ if (k != 0 && i < k) {
+ aqueue_delete(aqueue, i);
+ if (aqueue_count(aqueue) != k - 1)
+ return "Wrong count after delete";
+ if (!aqueue_is_ok(aqueue, i))
+ return "Invalid data after delete";
+ }
+ }
+ }
+ aqueue_clear(aqueue);
+ if (aqueue_count(aqueue) != 0)
+ return "aqueue_clear() broken";
+ return NULL;
+}
+
+void test_aqueue(void)
+{
+ unsigned int i;
+ const char *reason = NULL;
+
+ for (i = 1; i <= N_ELEMENTS(aqueue_input) + 1 && reason == NULL; i++) {
+ T_BEGIN {
+ reason = test_aqueue2(i);
+ } T_END;
+ }
+ test_out_reason("aqueue", reason == NULL, reason);
+}
diff -r 97f62a823666 -r 810e36796e3d src/lib/test-array.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/test-array.c Mon Jun 01 01:11:17 2009 -0400
@@ -0,0 +1,30 @@
+/* Copyright (c) 2007-2009 Dovecot authors, see the included COPYING file */
+
+#include "test-lib.h"
+#include "array.h"
+
+static void test_array_reverse(void)
+{
+ ARRAY_DEFINE(intarr, int);
+ int input[] = { -1234567890, -272585721, 2724859223U, 824725652 };
+ const int *output;
+ unsigned int i, j;
+
+ test_begin("array reverse");
+ t_array_init(&intarr, 5);
+ for (i = 0; i < N_ELEMENTS(input); i++) {
+ array_clear(&intarr);
+ array_append(&intarr, input, i);
+ array_reverse(&intarr);
+
+ output = i == 0 ? NULL : array_idx(&intarr, 0);
+ for (j = 0; j < i; j++)
+ test_assert(input[i-j-1] == output[j]);
+ }
+ test_end();
+}
+
+void test_array(void)
+{
+ test_array_reverse();
+}
diff -r 97f62a823666 -r 810e36796e3d src/lib/test-base64.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/test-base64.c Mon Jun 01 01:11:17 2009 -0400
@@ -0,0 +1,110 @@
+/* Copyright (c) 2007-2009 Dovecot authors, see the included COPYING file */
+
+#include "test-lib.h"
+#include "str.h"
+#include "base64.h"
+
+#include <stdlib.h>
+
+static void test_base64_encode(void)
+{
+ static const char *input[] = {
+ "hello world",
+ "foo barits",
+ "just niin"
+ };
+ static const char *output[] = {
+ "aGVsbG8gd29ybGQ=",
+ "Zm9vIGJhcml0cw==",
+ "anVzdCBuaWlu"
+ };
+ string_t *str;
+ unsigned int i;
+
+ test_begin("base64_encode()");
+ str = t_str_new(256);
+ for (i = 0; i < N_ELEMENTS(input); i++) {
+ str_truncate(str, 0);
+ base64_encode(input[i], strlen(input[i]), str);
+ test_assert(strcmp(output[i], str_c(str)) == 0);
+ }
+ test_end();
+}
+
+struct test_base64_decode_output {
+ const char *text;
+ int ret;
+ unsigned int src_pos;
+};
+
+static void test_base64_decode(void)
+{
+ static const char *input[] = {
+ "\taGVsbG8gd29ybGQ=",
+ "\nZm9v\n \tIGJh \t\ncml0cw==",
+ " anVzdCBuaWlu \n",
+ "aGVsb",
+ "aGVsb!!!!!",
+ "aGVs!!!!!"
+ };
+ static const struct test_base64_decode_output output[] = {
+ { "hello world", 0, -1 },
+ { "foo barits", 0, -1 },
+ { "just niin", 1, -1 },
+ { "hel", 1, 4 },
+ { "hel", -1, 4 },
+ { "hel", -1, 4 }
+ };
+ string_t *str;
+ unsigned int i;
+ size_t src_pos;
+ int ret;
+
+ test_begin("base64_decode()");
+ str = t_str_new(256);
+ for (i = 0; i < N_ELEMENTS(input); i++) {
+ str_truncate(str, 0);
+
+ src_pos = 0;
+ ret = base64_decode(input[i], strlen(input[i]), &src_pos, str);
+
+ test_assert(output[i].ret == ret &&
+ strcmp(output[i].text, str_c(str)) == 0 &&
+ (src_pos == output[i].src_pos ||
+ (output[i].src_pos == (unsigned int)-1 &&
+ src_pos == strlen(input[i]))));
+ }
+ test_end();
+}
+
+static void test_base64_random(void)
+{
+ string_t *str, *dest;
+ char buf[10];
+ unsigned int i, j, max;
+
+ str = t_str_new(256);
+ dest = t_str_new(256);
+
+ test_begin("base64 encode/decode with random input");
+ for (i = 0; i < 1000; i++) {
+ max = rand() % sizeof(buf);
+ for (j = 0; j < max; j++)
+ buf[j] = rand();
+
+ str_truncate(str, 0);
+ str_truncate(dest, 0);
+ base64_encode(buf, max, str);
+ base64_decode(str_data(str), str_len(str), NULL, dest);
+ test_assert(str_len(dest) == max &&
+ memcmp(buf, str_data(dest), max) == 0);
+ }
+ test_end();
+}
+
+void test_base64(void)
+{
+ test_base64_encode();
+ test_base64_decode();
+ test_base64_random();
+}
diff -r 97f62a823666 -r 810e36796e3d src/lib/test-bsearch-insert-pos.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/test-bsearch-insert-pos.c Mon Jun 01 01:11:17 2009 -0400
@@ -0,0 +1,48 @@
+/* Copyright (c) 2007-2009 Dovecot authors, see the included COPYING file */
+
+#include "test-lib.h"
+#include "bsearch-insert-pos.h"
+
+static int cmp_uint(const void *p1, const void *p2)
+{
+ const unsigned int *i1 = p1, *i2 = p2;
+
+ return *i1 - *i2;
+}
+
+void test_bsearch_insert_pos(void)
+{
+ static const unsigned int input[] = {
+ 1, 5, 9, 15, 16, -1,
+ 1, 5, 9, 15, 16, 17, -1,
+ -1
+ };
+ static const unsigned int max_key = 18;
+ const unsigned int *cur;
+ unsigned int key, len, i, idx;
+ bool success;
+
+ cur = input;
+ for (i = 0; cur[0] != -1U; i++) {
+ for (len = 0; cur[len] != -1U; len++) ;
+ for (key = 0; key < max_key; key++) {
+ if (bsearch_insert_pos(&key, cur, len, sizeof(*cur),
+ cmp_uint, &idx))
+ success = cur[idx] == key;
+ else if (idx == 0)
+ success = cur[0] > key;
+ else if (idx == len)
+ success = cur[len-1] < key;
+ else {
+ success = cur[idx-1] < key &&
+ cur[idx+1] > key;
+ }
+ if (!success)
+ break;
+ }
+ cur += len + 1;
+
+ test_out(t_strdup_printf("bsearch_insert_pos(%d,%d)", i, key),
+ success);
+ }
+}
More information about the dovecot-cvs
mailing list