1 /* utils.c --- Self test utilities.
2 * Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Simon Josefsson
4 * This file is part of GNU Libidn.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 int break_on_error
= 0;
35 fail (const char *format
, ...)
39 va_start (arg_ptr
, format
);
40 vfprintf (stderr
, format
, arg_ptr
);
48 escapeprint (const char *str
, size_t len
)
52 printf (" (length %d bytes):\n\t", len
);
53 for (i
= 0; i
< len
; i
++)
55 if (((str
[i
] & 0xFF) >= 'A' && (str
[i
] & 0xFF) <= 'Z') ||
56 ((str
[i
] & 0xFF) >= 'a' && (str
[i
] & 0xFF) <= 'z') ||
57 ((str
[i
] & 0xFF) >= '0' && (str
[i
] & 0xFF) <= '9')
58 || (str
[i
] & 0xFF) == ' ' || (str
[i
] & 0xFF) == '.')
59 printf ("%c", (str
[i
] & 0xFF));
61 printf ("\\x%02X", (str
[i
] & 0xFF));
62 if ((i
+ 1) % 16 == 0 && (i
+ 1) < len
)
69 hexprint (const char *str
, size_t len
)
74 for (i
= 0; i
< len
; i
++)
76 printf ("%02x ", (str
[i
] & 0xFF));
79 if ((i
+ 1) % 16 == 0 && i
+ 1 < len
)
86 binprint (const char *str
, size_t len
)
91 for (i
= 0; i
< len
; i
++)
93 printf ("%d%d%d%d%d%d%d%d ",
94 (str
[i
] & 0xFF) & 0x80 ? 1 : 0,
95 (str
[i
] & 0xFF) & 0x40 ? 1 : 0,
96 (str
[i
] & 0xFF) & 0x20 ? 1 : 0,
97 (str
[i
] & 0xFF) & 0x10 ? 1 : 0,
98 (str
[i
] & 0xFF) & 0x08 ? 1 : 0,
99 (str
[i
] & 0xFF) & 0x04 ? 1 : 0,
100 (str
[i
] & 0xFF) & 0x02 ? 1 : 0, (str
[i
] & 0xFF) & 0x01 ? 1 : 0);
101 if ((i
+ 1) % 3 == 0)
103 if ((i
+ 1) % 6 == 0 && i
+ 1 < len
)
110 ucs4print (const uint32_t * str
, size_t len
)
115 for (i
= 0; i
< len
; i
++)
117 printf ("U+%04x ", str
[i
]);
118 if ((i
+ 1) % 4 == 0)
120 if ((i
+ 1) % 8 == 0 && i
+ 1 < len
)
127 main (int argc
, char *argv
[])
130 if (strcmp (argv
[argc
- 1], "-v") == 0 ||
131 strcmp (argv
[argc
- 1], "--verbose") == 0)
133 else if (strcmp (argv
[argc
- 1], "-b") == 0 ||
134 strcmp (argv
[argc
- 1], "--break-on-error") == 0)
136 else if (strcmp (argv
[argc
- 1], "-h") == 0 ||
137 strcmp (argv
[argc
- 1], "-?") == 0 ||
138 strcmp (argv
[argc
- 1], "--help") == 0)
140 printf ("Usage: %s [-vbh?] [--verbose] [--break-on-error] [--help]\n",
149 printf ("Self tests done with %d errors\n", error_count
);
151 return error_count
? 1 : 0;