]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
Added unit test for istream-tee.
authorTimo Sirainen <tss@iki.fi>
Fri, 14 Aug 2009 00:03:52 +0000 (20:03 -0400)
committerTimo Sirainen <tss@iki.fi>
Fri, 14 Aug 2009 00:03:52 +0000 (20:03 -0400)
--HG--
branch : HEAD

src/lib/Makefile.am
src/lib/test-istream-tee.c [new file with mode: 0644]
src/lib/test-lib.c
src/lib/test-lib.h

index 3fb4ee717456e225c17fecfcc7312425931f4d77..4068bb9481ff5e1379a82f4b4795538ea9387262 100644 (file)
@@ -220,6 +220,7 @@ test_lib_SOURCES = \
        test-buffer.c \
        test-hex-binary.c \
        test-istream-crlf.c \
+       test-istream-tee.c \
        test-mempool-alloconly.c \
        test-network.c \
        test-primes.c \
diff --git a/src/lib/test-istream-tee.c b/src/lib/test-istream-tee.c
new file mode 100644 (file)
index 0000000..dbf185f
--- /dev/null
@@ -0,0 +1,117 @@
+/* Copyright (c) 2009 Dovecot authors, see the included COPYING file */
+
+#include "test-lib.h"
+#include "str.h"
+#include "istream-internal.h"
+#include "istream-tee.h"
+
+#include <stdlib.h>
+
+#define TEST_BUF_SIZE I_STREAM_MIN_SIZE
+#define TEST_STR_LEN (TEST_BUF_SIZE*3)
+#define CHILD_COUNT 5
+
+static void test_istream_tee_tailing(const char *str)
+{
+       struct istream *test_input, *child_input[CHILD_COUNT];
+       struct tee_istream *tee;
+       unsigned int i, len;
+
+       test_input = test_istream_create(str);
+       test_istream_set_max_buffer_size(test_input, TEST_BUF_SIZE);
+
+       test_begin("istream tee tailing");
+       tee = tee_i_stream_create(test_input);
+       for (i = 0; i < CHILD_COUNT; i++)
+               child_input[i] = tee_i_stream_create_child(tee);
+
+       test_istream_set_allow_eof(test_input, FALSE);
+       for (len = 1; len < TEST_BUF_SIZE; len++) {
+               test_istream_set_size(test_input, len);
+               for (i = 0; i < CHILD_COUNT; i++) {
+                       test_assert(i_stream_read(child_input[i]) == 1);
+                       test_assert(i_stream_read(child_input[i]) == 0);
+               }
+       }
+
+       test_istream_set_size(test_input, len);
+       for (i = 0; i < CHILD_COUNT; i++) {
+               test_assert(i_stream_read(child_input[i]) == 1);
+               test_assert(i_stream_read(child_input[i]) == -2);
+       }
+
+       for (len++; len <= TEST_STR_LEN; len++) {
+               test_istream_set_size(test_input, len);
+               for (i = 0; i < CHILD_COUNT; i++)
+                       test_assert(i_stream_read(child_input[i]) == -2);
+               for (i = 0; i < CHILD_COUNT-1; i++) {
+                       i_stream_skip(child_input[i], 1);
+                       test_assert(i_stream_read(child_input[i]) == 0);
+               }
+               i_stream_skip(child_input[i], 1);
+               for (i = 0; i < CHILD_COUNT; i++) {
+                       test_assert(i_stream_read(child_input[i]) == 1);
+                       test_assert(i_stream_read(child_input[i]) == -2);
+               }
+       }
+
+       for (i = 0; i < CHILD_COUNT; i++) {
+               i_stream_skip(child_input[i], 1);
+               test_assert(i_stream_read(child_input[i]) == 0);
+       }
+       test_istream_set_allow_eof(test_input, TRUE);
+       for (i = 0; i < CHILD_COUNT; i++) {
+               test_assert(i_stream_read(child_input[i]) == -1);
+               i_stream_unref(&child_input[i]);
+       }
+       i_stream_unref(&test_input);
+
+       test_end();
+}
+
+static void test_istream_tee_blocks(const char *str)
+{
+       struct istream *test_input, *child_input[CHILD_COUNT];
+       struct tee_istream *tee;
+       unsigned int i, j;
+
+       test_input = test_istream_create(str);
+       test_istream_set_max_buffer_size(test_input, TEST_BUF_SIZE);
+
+       test_begin("istream tee blocks");
+       tee = tee_i_stream_create(test_input);
+       for (i = 0; i < CHILD_COUNT; i++)
+               child_input[i] = tee_i_stream_create_child(tee);
+
+       test_istream_set_allow_eof(test_input, FALSE);
+       for (j = 1; j <= 3; j++) {
+               test_istream_set_size(test_input, TEST_BUF_SIZE*j);
+               for (i = 0; i < CHILD_COUNT; i++) {
+                       test_assert(i_stream_read(child_input[i]) == TEST_BUF_SIZE);
+                       i_stream_skip(child_input[i], TEST_BUF_SIZE);
+               }
+       }
+       test_istream_set_allow_eof(test_input, TRUE);
+       for (i = 0; i < CHILD_COUNT; i++) {
+               test_assert(i_stream_read(child_input[i]) == -1);
+               i_stream_unref(&child_input[i]);
+       }
+       i_stream_unref(&test_input);
+
+       test_end();
+}
+
+void test_istream_tee(void)
+{
+       string_t *str;
+       unsigned int i;
+
+       str = str_new(default_pool, TEST_STR_LEN);
+       for (i = 0; i < TEST_STR_LEN; i++)
+               str_append_c(str, 'a' + i%26);
+
+       test_istream_tee_tailing(str_c(str));
+       test_istream_tee_blocks(str_c(str));
+
+       str_free(&str);
+}
index 620ecd6170b1d727858d3df1376d1654613266fc..a8c2f03c12011b2d16d4330613649c0c14be610e 100644 (file)
@@ -12,6 +12,7 @@ int main(void)
                test_buffer,
                test_hex_binary,
                test_istream_crlf,
+               test_istream_tee,
                test_mempool_alloconly,
                test_network,
                test_primes,
index ef3c02e6989fc0ca69010feec3a079829ee7f5e3..6536dd58296be612cb398dcd66801972870ba9ee 100644 (file)
@@ -11,6 +11,7 @@ void test_bsearch_insert_pos(void);
 void test_buffer(void);
 void test_hex_binary(void);
 void test_istream_crlf(void);
+void test_istream_tee(void);
 void test_mempool_alloconly(void);
 void test_network(void);
 void test_primes(void);