From 0dae3b2417fcb7349ce921a8c28bf1e463e0a9d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dag-Erling=20Sm=C3=B8rgrav?= Date: Mon, 29 Dec 2014 13:36:06 +0000 Subject: [PATCH] Test string_equal() using the same test cases as for string_compare(). Improve test case descriptions. --- t/t__string.c | 41 ++++++++++++++++++++++++++++++++--------- t/t_string.c | 1 + t/t_wstring.c | 1 + 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/t/t__string.c b/t/t__string.c index 3ecb73a..57e6c1b 100644 --- a/t/t__string.c +++ b/t/t__string.c @@ -50,49 +50,49 @@ static struct t_compare_case { int cmp; } t_compare_cases[] = { { - "both empty", + "empty with empty", CS(""), CS(""), 0, }, { - "empty with non-empty (left)", + "empty with non-empty", CS(""), CS("xyzzy"), -1, }, { - "empty with non-empty (right)", + "non-empty with empty", CS("xyzzy"), CS(""), 1, }, { - "equal non-empty", + "non-empty with same non-empty", CS("xyzzy"), CS("xyzzy"), 0, }, { - "unequal of equal length (left)", + "non-empty with later non-empty", CS("abba"), CS("baba"), -1, }, { - "unequal of equal length (right)", + "non-empty with earlier non-empty", CS("baba"), CS("abba"), 1, }, { - "prefix (left)", + "non-empty prefix with non-empty", CS("baba"), CS("babaorum"), -1, }, { - "prefix (right)", + "non-empty with non-empty prefix", CS("babaorum"), CS("baba"), 1, @@ -100,12 +100,13 @@ static struct t_compare_case { }; static int -t_compare_test(char **desc CRYB_UNUSED, void *arg) +t_compare_test(char **desc, void *arg) { struct t_compare_case *t = arg; string *s1, *s2; int ret; + asprintf(desc, "%s(%s)", "string_compare", *desc); if ((s1 = string_new()) == NULL || string_append_cs(s1, t->s1, SIZE_MAX) < 0 || (s2 = string_new()) == NULL || @@ -117,6 +118,25 @@ t_compare_test(char **desc CRYB_UNUSED, void *arg) return (ret); } +static int +t_equal_test(char **desc, void *arg) +{ + struct t_compare_case *t = arg; + string *s1, *s2; + int ret; + + asprintf(desc, "%s(%s)", "string_equal", *desc); + if ((s1 = string_new()) == NULL || + string_append_cs(s1, t->s1, SIZE_MAX) < 0 || + (s2 = string_new()) == NULL || + string_append_cs(s2, t->s2, SIZE_MAX) < 0) + return (0); + ret = (string_equal(s1, s2) == 0) == (t->cmp != 0); + string_delete(s2); + string_delete(s1); + return (ret); +} + /*************************************************************************** * Boilerplate @@ -134,6 +154,9 @@ t_prepare(int argc, char *argv[]) for (i = 0; i < sizeof t_compare_cases / sizeof *t_compare_cases; ++i) t_add_test(t_compare_test, &t_compare_cases[i], t_compare_cases[i].desc); + for (i = 0; i < sizeof t_compare_cases / sizeof *t_compare_cases; ++i) + t_add_test(t_equal_test, &t_compare_cases[i], + t_compare_cases[i].desc); return (0); } diff --git a/t/t_string.c b/t/t_string.c index 8336912..2565081 100644 --- a/t/t_string.c +++ b/t/t_string.c @@ -32,6 +32,7 @@ #include #include +#include #include #include diff --git a/t/t_wstring.c b/t/t_wstring.c index c51f5ba..56c5aad 100644 --- a/t/t_wstring.c +++ b/t/t_wstring.c @@ -32,6 +32,7 @@ #include #include +#include #include #include