aboutsummaryrefslogtreecommitdiff
path: root/src/test/test_util.c
diff options
context:
space:
mode:
authorEsteban Manchado Velázquez <emanchado@demiurgo.org>2012-02-20 13:20:32 +0100
committerNick Mathewson <nickm@torproject.org>2012-03-08 21:16:45 -0500
commit1f6e3ec5006076d5ee7254489795bebbf8dab36a (patch)
tree908e600002561c845676dfc2ed56c3a5caaf9d4b /src/test/test_util.c
parent03258529d8216e7454227b166be159e70f993e90 (diff)
downloadtor-1f6e3ec5006076d5ee7254489795bebbf8dab36a.tar
tor-1f6e3ec5006076d5ee7254489795bebbf8dab36a.tar.gz
Improve coverage in string-related unit tests
Diffstat (limited to 'src/test/test_util.c')
-rw-r--r--src/test/test_util.c49
1 files changed, 48 insertions, 1 deletions
diff --git a/src/test/test_util.c b/src/test/test_util.c
index 1305e2f08..4fb565d6e 100644
--- a/src/test/test_util.c
+++ b/src/test/test_util.c
@@ -789,6 +789,27 @@ test_util_strmisc(void)
test_streq(cp, "### Sm\n#### a\n#### l\n#### l\n#### t\n#### e\n#### s\n#### t\n");
tor_free(cp);
SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
+ smartlist_clear(sl);
+
+ wrap_string(sl, "First null", 6, NULL, "> ");
+ cp = smartlist_join_strings(sl, "", 0, NULL);
+ test_streq(cp, "First\n> null\n");
+ tor_free(cp);
+ SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
+ smartlist_clear(sl);
+
+ wrap_string(sl, "Second null", 6, "> ", NULL);
+ cp = smartlist_join_strings(sl, "", 0, NULL);
+ test_streq(cp, "> Seco\nnd\nnull\n");
+ tor_free(cp);
+ SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
+ smartlist_clear(sl);
+
+ wrap_string(sl, "Both null", 6, NULL, NULL);
+ cp = smartlist_join_strings(sl, "", 0, NULL);
+ test_streq(cp, "Both\nnull\n");
+ tor_free(cp);
+ SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
smartlist_free(sl);
/* Can't test prefixes that have the same length as the line width, because
@@ -797,7 +818,7 @@ test_util_strmisc(void)
/* Test hex_str */
{
- char binary_data[64];
+ char binary_data[68];
size_t i;
for (i = 0; i < sizeof(binary_data); ++i)
binary_data[i] = i;
@@ -807,11 +828,36 @@ test_util_strmisc(void)
test_streq(hex_str(binary_data, 32),
"000102030405060708090A0B0C0D0E0F"
"101112131415161718191A1B1C1D1E1F");
+ test_streq(hex_str(binary_data, 34),
+ "000102030405060708090A0B0C0D0E0F"
+ "101112131415161718191A1B1C1D1E1F");
/* Repeat these tests for shorter strings after longer strings
have been tried, to make sure we're correctly terminating strings */
test_streq(hex_str(binary_data, 1), "00");
test_streq(hex_str(binary_data, 0), "");
}
+
+ /* Test strcmp_opt */
+ test_eq(-1, strcmp_opt("", "foo"));
+ test_eq(0, strcmp_opt("", ""));
+ test_eq(1, strcmp_opt("foo", ""));
+
+ test_eq(-1, strcmp_opt(NULL, ""));
+ test_eq(0, strcmp_opt(NULL, NULL));
+ test_eq(1, strcmp_opt("", NULL));
+
+ test_eq(-1, strcmp_opt(NULL, "foo"));
+ test_eq(1, strcmp_opt("foo", NULL));
+
+ /* Test strcmp_len */
+ test_eq(1, strcmp_len("foo", "bar", 3));
+ test_eq(-1, strcmp_len("foo", "bar", 2)); /* First len, then lexical */
+ test_eq(1, strcmp_len("foo2", "foo1", 4));
+ test_eq(-1, strcmp_len("foo2", "foo1", 3)); /* Really stop at len */
+ test_eq(0, strcmp_len("foo2", "foo", 3)); /* Really stop at len */
+ test_eq(1, strcmp_len("blah", "", 4));
+ test_eq(0, strcmp_len("blah", "", 0));
+
done:
;
}
@@ -1565,6 +1611,7 @@ test_util_find_str_at_start_of_line(void *ptr)
(void)ptr;
test_eq_ptr(long_string, find_str_at_start_of_line(long_string, ""));
+ test_eq_ptr(NULL, find_str_at_start_of_line(short_string, "nonsense"));
test_eq_ptr(NULL, find_str_at_start_of_line(long_string, "nonsense"));
test_eq_ptr(NULL, find_str_at_start_of_line(long_string, "\n"));
test_eq_ptr(NULL, find_str_at_start_of_line(long_string, "how "));