xref: /freebsd/contrib/arm-optimized-routines/string/test/stringtest.h (revision d5b0e70f7e04d971691517ce1304d86a1e367e2e)
1 /*
2  * Common string test code.
3  *
4  * Copyright (c) 2020, Arm Limited.
5  * SPDX-License-Identifier: MIT
6  */
7 
8 #include <ctype.h>
9 #include <stdio.h>
10 
11 /* Accounting errors for a test case.  */
12 static int err_count;
13 #define ERR_LIMIT 10
14 #define ERR(...) (err_count++, printf (__VA_ARGS__))
15 
16 static inline void
17 quotechar (unsigned char c)
18 {
19   if (isprint (c))
20     putchar (c);
21   else
22     printf ("\\x%02x", c);
23 }
24 
25 /* quoted print around at or the entire string if at < 0.  */
26 static void
27 quoteat (const char *prefix, const void *p, int len, int at)
28 {
29   static const int CTXLEN = 15;
30   int i;
31   const char *pre = "\"";
32   const char *post = "\"";
33   const char *s = p;
34   if (at > CTXLEN)
35     {
36       s += at - CTXLEN;
37       len -= at - CTXLEN;
38       pre = "...\"";
39     }
40   if (at >= 0 && len > 2 * CTXLEN + 1)
41     {
42       len = 2 * CTXLEN + 1;
43       post = "\"...";
44     }
45   printf ("%4s: %s", prefix, pre);
46   for (i = 0; i < len; i++)
47     quotechar (s[i]);
48   printf ("%s\n", post);
49 }
50 
51 static inline void
52 quote (const char *prefix, const void *p, int len)
53 {
54   quoteat (prefix, p, len, -1);
55 }
56