Test string_equal() using the same test cases as for string_compare().

Improve test case descriptions.
This commit is contained in:
Dag-Erling Smørgrav 2014-12-29 13:36:06 +00:00 committed by des
parent e20e76a3f7
commit 0dae3b2417
3 changed files with 34 additions and 9 deletions

View file

@ -50,49 +50,49 @@ static struct t_compare_case {
int cmp; int cmp;
} t_compare_cases[] = { } t_compare_cases[] = {
{ {
"both empty", "empty with empty",
CS(""), CS(""),
CS(""), CS(""),
0, 0,
}, },
{ {
"empty with non-empty (left)", "empty with non-empty",
CS(""), CS(""),
CS("xyzzy"), CS("xyzzy"),
-1, -1,
}, },
{ {
"empty with non-empty (right)", "non-empty with empty",
CS("xyzzy"), CS("xyzzy"),
CS(""), CS(""),
1, 1,
}, },
{ {
"equal non-empty", "non-empty with same non-empty",
CS("xyzzy"), CS("xyzzy"),
CS("xyzzy"), CS("xyzzy"),
0, 0,
}, },
{ {
"unequal of equal length (left)", "non-empty with later non-empty",
CS("abba"), CS("abba"),
CS("baba"), CS("baba"),
-1, -1,
}, },
{ {
"unequal of equal length (right)", "non-empty with earlier non-empty",
CS("baba"), CS("baba"),
CS("abba"), CS("abba"),
1, 1,
}, },
{ {
"prefix (left)", "non-empty prefix with non-empty",
CS("baba"), CS("baba"),
CS("babaorum"), CS("babaorum"),
-1, -1,
}, },
{ {
"prefix (right)", "non-empty with non-empty prefix",
CS("babaorum"), CS("babaorum"),
CS("baba"), CS("baba"),
1, 1,
@ -100,12 +100,13 @@ static struct t_compare_case {
}; };
static int static int
t_compare_test(char **desc CRYB_UNUSED, void *arg) t_compare_test(char **desc, void *arg)
{ {
struct t_compare_case *t = arg; struct t_compare_case *t = arg;
string *s1, *s2; string *s1, *s2;
int ret; int ret;
asprintf(desc, "%s(%s)", "string_compare", *desc);
if ((s1 = string_new()) == NULL || if ((s1 = string_new()) == NULL ||
string_append_cs(s1, t->s1, SIZE_MAX) < 0 || string_append_cs(s1, t->s1, SIZE_MAX) < 0 ||
(s2 = string_new()) == NULL || (s2 = string_new()) == NULL ||
@ -117,6 +118,25 @@ t_compare_test(char **desc CRYB_UNUSED, void *arg)
return (ret); 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 * Boilerplate
@ -134,6 +154,9 @@ t_prepare(int argc, char *argv[])
for (i = 0; i < sizeof t_compare_cases / sizeof *t_compare_cases; ++i) for (i = 0; i < sizeof t_compare_cases / sizeof *t_compare_cases; ++i)
t_add_test(t_compare_test, &t_compare_cases[i], t_add_test(t_compare_test, &t_compare_cases[i],
t_compare_cases[i].desc); 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); return (0);
} }

View file

@ -32,6 +32,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h>
#include <string.h> #include <string.h>
#include <cryb/string.h> #include <cryb/string.h>

View file

@ -32,6 +32,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h>
#include <string.h> #include <string.h>
#include <wchar.h> #include <wchar.h>