Branch data Line data Source code
1 : : /* utils.c --- Self test utilities.
2 : : * Copyright (C) 2002-2012 Simon Josefsson
3 : : *
4 : : * This file is part of GNU Libidn.
5 : : *
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.
10 : : *
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.
15 : : *
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/>.
18 : : *
19 : : */
20 : :
21 : : #ifdef HAVE_CONFIG_H
22 : : # include "config.h"
23 : : #endif
24 : :
25 : : #include <stdio.h>
26 : : #include <stdlib.h>
27 : :
28 : : #include "utils.h"
29 : :
30 : : int debug = 0;
31 : : int error_count = 0;
32 : : int break_on_error = 0;
33 : :
34 : : void
35 : 0 : fail (const char *format, ...)
36 : : {
37 : : va_list arg_ptr;
38 : :
39 : 0 : va_start (arg_ptr, format);
40 : 0 : vfprintf (stderr, format, arg_ptr);
41 : 0 : va_end (arg_ptr);
42 : 0 : error_count++;
43 [ # # ]: 0 : if (break_on_error)
44 : 0 : exit (EXIT_FAILURE);
45 : 0 : }
46 : :
47 : : void
48 : 0 : escapeprint (const char *str, size_t len)
49 : : {
50 : : size_t i;
51 : :
52 : 0 : printf (" (length %ld bytes):\n\t", len);
53 [ # # ]: 0 : for (i = 0; i < len; i++)
54 : : {
55 [ # # ][ # # ]: 0 : if (((str[i] & 0xFF) >= 'A' && (str[i] & 0xFF) <= 'Z') ||
[ # # ]
56 [ # # ][ # # ]: 0 : ((str[i] & 0xFF) >= 'a' && (str[i] & 0xFF) <= 'z') ||
57 [ # # ]: 0 : ((str[i] & 0xFF) >= '0' && (str[i] & 0xFF) <= '9')
58 [ # # ][ # # ]: 0 : || (str[i] & 0xFF) == ' ' || (str[i] & 0xFF) == '.')
59 : 0 : printf ("%c", (str[i] & 0xFF));
60 : : else
61 : 0 : printf ("\\x%02X", (str[i] & 0xFF));
62 [ # # ][ # # ]: 0 : if ((i + 1) % 16 == 0 && (i + 1) < len)
63 : 0 : printf ("'\n\t'");
64 : : }
65 : 0 : printf ("\n");
66 : 0 : }
67 : :
68 : : void
69 : 0 : hexprint (const char *str, size_t len)
70 : : {
71 : : size_t i;
72 : :
73 : 0 : printf ("\t;; ");
74 [ # # ]: 0 : for (i = 0; i < len; i++)
75 : : {
76 : 0 : printf ("%02x ", (str[i] & 0xFF));
77 [ # # ]: 0 : if ((i + 1) % 8 == 0)
78 : 0 : printf (" ");
79 [ # # ][ # # ]: 0 : if ((i + 1) % 16 == 0 && i + 1 < len)
80 : 0 : printf ("\n\t;; ");
81 : : }
82 : 0 : printf ("\n");
83 : 0 : }
84 : :
85 : : void
86 : 0 : binprint (const char *str, size_t len)
87 : : {
88 : : size_t i;
89 : :
90 : 0 : printf ("\t;; ");
91 [ # # ]: 0 : for (i = 0; i < len; i++)
92 : : {
93 : 0 : printf ("%d%d%d%d%d%d%d%d ",
94 : 0 : (str[i] & 0xFF) & 0x80 ? 1 : 0,
95 : 0 : (str[i] & 0xFF) & 0x40 ? 1 : 0,
96 : 0 : (str[i] & 0xFF) & 0x20 ? 1 : 0,
97 : 0 : (str[i] & 0xFF) & 0x10 ? 1 : 0,
98 : 0 : (str[i] & 0xFF) & 0x08 ? 1 : 0,
99 : 0 : (str[i] & 0xFF) & 0x04 ? 1 : 0,
100 : 0 : (str[i] & 0xFF) & 0x02 ? 1 : 0, (str[i] & 0xFF) & 0x01 ? 1 : 0);
101 [ # # ]: 0 : if ((i + 1) % 3 == 0)
102 : 0 : printf (" ");
103 [ # # ][ # # ]: 0 : if ((i + 1) % 6 == 0 && i + 1 < len)
104 : 0 : printf ("\n\t;; ");
105 : : }
106 : 0 : printf ("\n");
107 : 0 : }
108 : :
109 : : void
110 : 0 : ucs4print (const uint32_t * str, size_t len)
111 : : {
112 : : size_t i;
113 : :
114 : 0 : printf ("\t;; ");
115 [ # # ][ # # ]: 0 : for (i = 0; (len == (size_t) -1) ? str[i] : i < len; i++)
116 : : {
117 : 0 : printf ("U+%04x ", str[i]);
118 [ # # ]: 0 : if ((i + 1) % 4 == 0)
119 : 0 : printf (" ");
120 [ # # ][ # # ]: 0 : if ((i + 1) % 8 == 0 && i + 1 < len)
121 : 0 : printf ("\n\t;; ");
122 : : }
123 : 0 : puts ("");
124 : 0 : }
125 : :
126 : : int
127 : 11 : main (int argc, char *argv[])
128 : : {
129 : : do
130 [ + - ][ - + ]: 11 : if (strcmp (argv[argc - 1], "-v") == 0 ||
131 : 11 : strcmp (argv[argc - 1], "--verbose") == 0)
132 : 0 : debug = 1;
133 [ + - ][ - + ]: 11 : else if (strcmp (argv[argc - 1], "-b") == 0 ||
134 : 11 : strcmp (argv[argc - 1], "--break-on-error") == 0)
135 : 0 : break_on_error = 1;
136 [ + - ][ + - ]: 11 : else if (strcmp (argv[argc - 1], "-h") == 0 ||
137 [ - + ]: 11 : strcmp (argv[argc - 1], "-?") == 0 ||
138 : 11 : strcmp (argv[argc - 1], "--help") == 0)
139 : : {
140 : 0 : printf ("Usage: %s [-vbh?] [--verbose] [--break-on-error] [--help]\n",
141 : : argv[0]);
142 : 0 : return 1;
143 : : }
144 [ - + ]: 11 : while (argc-- > 1);
145 : :
146 : 11 : doit ();
147 : :
148 [ - + ]: 11 : if (debug)
149 : 0 : printf ("Self tests done with %d errors\n", error_count);
150 : :
151 : 11 : return error_count ? 1 : 0;
152 : : }
|