Add a t_compare_strn() function which is to t_compare_str() what strncmp() is to

strcmp().
This commit is contained in:
Dag-Erling Smørgrav 2016-01-10 18:49:19 +01:00
parent d2a98cd870
commit 6eaaac308f
2 changed files with 17 additions and 0 deletions

View file

@ -86,6 +86,7 @@ int t_is_null(const void *);
int t_is_not_null(const void *); int t_is_not_null(const void *);
int t_compare_mem(const void *, const void *, size_t); int t_compare_mem(const void *, const void *, size_t);
int t_compare_str(const char *, const char *); int t_compare_str(const char *, const char *);
int t_compare_strn(const char *, const char *, size_t);
#define t_compare_num(n, t) int t_compare_##n(t, t); #define t_compare_num(n, t) int t_compare_##n(t, t);
t_compare_num(i, int); t_compare_num(i, int);
t_compare_num(u, unsigned int); t_compare_num(u, unsigned int);

View file

@ -102,6 +102,22 @@ t_compare_str(const char *expected, const char *received)
return (1); return (1);
} }
/*
* Compare two strings up to a specific length, and print a verbose
* message if they differ.
*/
int
t_compare_strn(const char *expected, const char *received, size_t len)
{
if (strncmp(expected, received, len) != 0) {
t_verbose("expected %.*s\n", (int)len, expected);
t_verbose("received %.*s\n", (int)len, received);
return (0);
}
return (1);
}
/* /*
* Compare two numbers, and print a verbose message if they differ. * Compare two numbers, and print a verbose message if they differ.
*/ */