aboutsummaryrefslogtreecommitdiff
path: root/string/test/stringtest.h
blob: fe855fc217369099ab10af634f392517edf89f66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
 * 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);
}