From 6eaaac308fe968e5e159d73bd9a41b49cbf40806 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dag-Erling=20Sm=C3=B8rgrav?= Date: Sun, 10 Jan 2016 18:49:19 +0100 Subject: [PATCH] Add a t_compare_strn() function which is to t_compare_str() what strncmp() is to strcmp(). --- include/cryb/test.h | 1 + lib/test/t_util.c | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/include/cryb/test.h b/include/cryb/test.h index 1138941..77f02e7 100644 --- a/include/cryb/test.h +++ b/include/cryb/test.h @@ -86,6 +86,7 @@ int t_is_null(const void *); int t_is_not_null(const void *); int t_compare_mem(const void *, const void *, size_t); 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); t_compare_num(i, int); t_compare_num(u, unsigned int); diff --git a/lib/test/t_util.c b/lib/test/t_util.c index 025cdeb..8da5102 100644 --- a/lib/test/t_util.c +++ b/lib/test/t_util.c @@ -102,6 +102,22 @@ t_compare_str(const char *expected, const char *received) 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. */