diff options
author | Szabolcs Nagy <szabolcs.nagy@arm.com> | 2020-04-16 11:46:02 +0100 |
---|---|---|
committer | Szabolcs Nagy <szabolcs.nagy@arm.com> | 2020-04-24 13:28:47 +0100 |
commit | 22fd93176f9eca0ee38954e233826d05fdb31b0f (patch) | |
tree | f0e6908e1804a1943a56a445ebec861f31a967d6 /string/test/stringtest.h | |
parent | 563b710ddc13810bd89fc17f057f4f7f8479e744 (diff) | |
download | arm-optimized-routines-22fd93176f9eca0ee38954e233826d05fdb31b0f.tar.gz |
string: test cleanups
Tests printed too much output on broken string function
and the output was not entirely useful.
Added a new header file with some common logic for
printing buffers nicely.
In str* tests len now means string length (not buffer
size which was confusing).
Diffstat (limited to 'string/test/stringtest.h')
-rw-r--r-- | string/test/stringtest.h | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/string/test/stringtest.h b/string/test/stringtest.h new file mode 100644 index 0000000..b9c034a --- /dev/null +++ b/string/test/stringtest.h @@ -0,0 +1,50 @@ +/* + * Common string test code. + * + * Copyright (c) 2020, Arm Limited. + * SPDX-License-Identifier: MIT + */ + +#include <ctype.h> +#include <stdio.h> + +/* Accounting errors for a test case. */ +static int err_count; +#define ERR_LIMIT 10 +#define ERR(...) (err_count++, printf(__VA_ARGS__)) + +static inline void quotechar(unsigned char c) +{ + if (isprint(c)) + putchar(c); + else + printf("\\x%02x", c); +} + +/* quoted print around at or the entire string if at < 0. */ +static void quoteat(const char *prefix, const void *p, int len, int at) +{ + static const int CTXLEN = 15; + int i; + const char *pre="\""; + const char *post="\""; + const char *s = p; + if (at > CTXLEN) { + s += at - CTXLEN; + len -= at - CTXLEN; + pre = "...\""; + } + if (at >= 0 && len > 2*CTXLEN + 1) { + len = 2*CTXLEN + 1; + post = "\"..."; + } + printf("%4s: %s", prefix, pre); + for (i = 0; i < len; i++) + quotechar(s[i]); + printf("%s\n", post); +} + +static inline void quote(const char *prefix, const void *p, int len) +{ + quoteat(prefix, p, len, -1); +} |