]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: Added more unit tests to str_*()
authorTimo Sirainen <timo.sirainen@dovecot.fi>
Tue, 3 May 2016 17:17:12 +0000 (20:17 +0300)
committerTimo Sirainen <timo.sirainen@dovecot.fi>
Tue, 3 May 2016 17:17:12 +0000 (20:17 +0300)
src/lib/test-str.c

index 35a5e30f27045e68cbc1a85fa661f8d63ff3d76b..77ca078fb984c0ffc7675f8ae258fce8423de9dc 100644 (file)
@@ -3,6 +3,26 @@
 #include "test-lib.h"
 #include "str.h"
 
+static void test_str_append(void)
+{
+       string_t *str = t_str_new(32);
+       string_t *str2 = t_str_new(32);
+
+       test_begin("str_append_*()");
+       str_append(str, "foo");
+       str_append_c(str, '|');
+       str_append_c(str, '\0');
+       test_assert(str->used == 5 && memcmp(str_data(str), "foo|\0", 5) == 0);
+
+       str_append(str2, "sec");
+       str_append_c(str2, '\0');
+       str_append(str2, "ond");
+       str_append_str(str, str2);
+       test_assert(str->used == 5+7 && memcmp(str_data(str), "foo|\0sec\0ond", 5+7) == 0);
+
+       test_end();
+}
+
 static void test_str_c(void)
 {
        string_t *str;
@@ -25,7 +45,75 @@ static void test_str_c(void)
        test_end();
 }
 
+static void test_str_insert(void)
+{
+       string_t *str = t_str_new(32);
+
+       test_begin("str_insert()");
+       str_insert(str, 0, "foo");
+       str_insert(str, 3, ">");
+       str_insert(str, 3, "bar");
+       str_insert(str, 0, "<");
+       test_assert(str->used == 8 && memcmp(str_data(str), "<foobar>", 8) == 0);
+
+       str_insert(str, 10, "!");
+       test_assert(str->used == 11 && memcmp(str_data(str), "<foobar>\0\0!", 11) == 0);
+
+       test_end();
+}
+
+static void test_str_delete(void)
+{
+       string_t *str = t_str_new(32);
+
+       test_begin("str_delete()");
+       str_delete(str, 0, 100);
+       str_append(str, "123456");
+       str_delete(str, 0, 1);
+       str_delete(str, 4, 1);
+       str_delete(str, 1, 1);
+       test_assert(str->used == 3 && memcmp(str_data(str), "245", 3) == 0);
+
+       str_delete(str, 1, 2);
+       test_assert(str->used == 1 && memcmp(str_data(str), "2", 1) == 0);
+
+       str_append(str, "bar");
+       str_delete(str, 1, 100);
+       test_assert(str->used == 1 && memcmp(str_data(str), "2", 1) == 0);
+
+       test_end();
+}
+
+static void test_str_append_n(void)
+{
+       string_t *str = t_str_new(32);
+
+       test_begin("str_append_n()");
+       str_append_n(str, "foo", 0);
+       test_assert(str->used == 0);
+
+       str_append_n(str, "\0foo", 4);
+       test_assert(str->used == 0);
+
+       str_append_n(str, "foo", 3);
+       test_assert(str->used == 3 && memcmp(str_data(str), "foo", 3) == 0);
+       str_truncate(str, 0);
+
+       str_append_n(str, "foo", 2);
+       test_assert(str->used == 2 && memcmp(str_data(str), "fo", 2) == 0);
+       str_truncate(str, 0);
+
+       str_append_n(str, "foo\0bar", 7);
+       test_assert(str->used == 3 && memcmp(str_data(str), "foo", 3) == 0);
+       str_truncate(str, 0);
+       test_end();
+}
+
 void test_str(void)
 {
+       test_str_append();
        test_str_c();
+       test_str_insert();
+       test_str_delete();
+       test_str_append_n();
 }