Add.
[libidn.git] / tst_nfkc.c
blob25625693178ecda5768e70fb4e1bf4333e32b27a
1 /* tst_nfkc.c Self tests for stringprep_utf8_nfkc_normalize().
2 * Copyright (C) 2002, 2003 Simon Josefsson
4 * This file is part of GNU Libidn.
6 * GNU Libidn 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 2 of the License, or
9 * (at your option) any later version.
11 * GNU Libidn 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 GNU Libidn; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "internal.h"
24 static int debug = 0;
25 static int error_count = 0;
26 static int break_on_error = 0;
28 static void
29 fail (const char *format, ...)
31 va_list arg_ptr;
33 va_start (arg_ptr, format);
34 vfprintf (stderr, format, arg_ptr);
35 va_end (arg_ptr);
36 error_count++;
37 if (break_on_error)
38 exit (1);
41 static void
42 escapeprint (char *str, int len)
44 int i;
46 printf ("\t ;; `");
47 for (i = 0; i < len; i++)
49 str[i] = str[i] & 0xFF;
50 if ((str[i] >= 'A' && str[i] <= 'Z') ||
51 (str[i] >= 'a' && str[i] <= 'z') ||
52 (str[i] >= '0' && str[i] <= '9') || str[i] == '.')
53 printf ("%c", str[i]);
54 else
55 printf ("\\x%02x", str[i]);
57 printf ("' (length %d bytes)\n", len);
60 static void
61 hexprint (char *str, int len)
63 int i;
65 printf ("\t ;; ");
66 for (i = 0; i < len; i++)
68 str[i] = str[i] & 0xFF;
69 printf ("%02x ", str[i]);
70 if ((i + 1) % 8 == 0)
71 printf (" ");
72 if ((i + 1) % 16 == 0 && i + 1 < len)
73 printf ("\n\t ;; ");
77 static void
78 binprint (char *str, int len)
80 int i;
82 printf ("\t ;; ");
83 for (i = 0; i < len; i++)
85 str[i] = str[i] & 0xFF;
86 printf ("%d%d%d%d%d%d%d%d ",
87 str[i] & 0x80 ? 1 : 0,
88 str[i] & 0x40 ? 1 : 0,
89 str[i] & 0x20 ? 1 : 0,
90 str[i] & 0x10 ? 1 : 0,
91 str[i] & 0x08 ? 1 : 0,
92 str[i] & 0x04 ? 1 : 0,
93 str[i] & 0x02 ? 1 : 0, str[i] & 0x01 ? 1 : 0);
94 if ((i + 1) % 3 == 0)
95 printf (" ");
96 if ((i + 1) % 6 == 0 && i + 1 < len)
97 printf ("\n\t ;; ");
101 struct nfkc
103 char *in;
104 char *out;
106 nfkc[] =
109 "\xC2\xB5", "\xCE\xBC"}
112 "\xC2\xAA", "\x61"}
116 main (int argc, char *argv[])
118 char *out;
119 size_t i;
122 if (strcmp (argv[argc - 1], "-v") == 0 ||
123 strcmp (argv[argc - 1], "--verbose") == 0)
124 debug = 1;
125 else if (strcmp (argv[argc - 1], "-b") == 0 ||
126 strcmp (argv[argc - 1], "--break-on-error") == 0)
127 break_on_error = 1;
128 else if (strcmp (argv[argc - 1], "-h") == 0 ||
129 strcmp (argv[argc - 1], "-?") == 0 ||
130 strcmp (argv[argc - 1], "--help") == 0)
132 printf ("Usage: %s [-vbh?] [--verbose] [--break-on-error] [--help]\n",
133 argv[0]);
134 return 1;
136 while (argc-- > 1);
138 for (i = 0; i < sizeof (nfkc) / sizeof (nfkc[0]); i++)
140 if (debug)
141 printf ("NFKC entry %d\n", i);
143 out = stringprep_utf8_nfkc_normalize (nfkc[i].in, strlen (nfkc[i].in));
144 if (out == NULL)
146 fail ("NFKC entry %d failed fatally\n", i);
147 continue;
150 if (debug)
152 printf ("in:\n");
153 escapeprint (nfkc[i].in, strlen (nfkc[i].in));
154 hexprint (nfkc[i].in, strlen (nfkc[i].in));
155 puts ("");
156 binprint (nfkc[i].in, strlen (nfkc[i].in));
157 puts ("");
159 printf ("out:\n");
160 escapeprint (out, strlen (out));
161 hexprint (out, strlen (out));
162 puts ("");
163 binprint (out, strlen (out));
164 puts ("");
166 printf ("expected out:\n");
167 escapeprint (nfkc[i].out, strlen (nfkc[i].out));
168 hexprint (nfkc[i].out, strlen (nfkc[i].out));
169 puts ("");
170 binprint (nfkc[i].out, strlen (nfkc[i].out));
171 puts ("");
174 if (strlen (nfkc[i].out) != strlen (out) ||
175 memcmp (nfkc[i].out, out, strlen (out)) != 0)
177 fail ("NFKC entry %d failed\n", i);
178 if (debug)
179 printf ("ERROR\n");
181 else if (debug)
182 printf ("OK\n");
184 free (out);
187 if (debug)
188 printf ("NFKC self tests done with %d errors\n", error_count);
190 return error_count ? 1 : 0;