1 /* Test the Unicode character name functions.
2 Copyright (C) 2000-2003, 2005, 2007, 2009 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27 /* The names according to the UnicodeData.txt file, modified to contain the
28 Hangul syllable names, as described in the Unicode 3.0 book. */
29 const char * unicode_names [0x110000];
31 /* Maximum length of a field in the UnicodeData.txt file. */
34 /* Reads the next field from STREAM. The buffer BUFFER has size FIELDLEN.
35 Reads up to (but excluding) DELIM.
36 Returns 1 when a field was successfully read, otherwise 0. */
38 getfield (FILE *stream, char *buffer, int delim)
43 for (; (c = getc (stream)), (c != EOF && c != delim); )
45 /* Put c into the buffer. */
46 if (++count >= FIELDLEN - 1)
48 fprintf (stderr, "field too long\n");
61 /* Stores in unicode_names[] the relevant contents of the UnicodeData.txt
64 fill_names (const char *unicodedata_filename)
68 char field0[FIELDLEN];
69 char field1[FIELDLEN];
72 for (i = 0; i < 0x110000; i++)
73 unicode_names[i] = NULL;
75 stream = fopen (unicodedata_filename, "r");
78 fprintf (stderr, "error during fopen of '%s'\n", unicodedata_filename);
88 n = getfield (stream, field0, ';');
89 n += getfield (stream, field1, ';');
94 fprintf (stderr, "short line in '%s':%d\n",
95 unicodedata_filename, lineno);
98 for (; (c = getc (stream)), (c != EOF && c != '\n'); )
100 i = strtoul (field0, NULL, 16);
103 fprintf (stderr, "index too large\n");
106 unicode_names[i] = xstrdup (field1);
108 if (ferror (stream) || fclose (stream))
110 fprintf (stderr, "error reading from '%s'\n", unicodedata_filename);
115 /* Perform an exhaustive test of the unicode_character_name function. */
121 char buf[UNINAME_MAX];
123 for (i = 0; i < 0x11000; i++)
125 char *result = unicode_character_name (i, buf);
127 if (unicode_names[i] != NULL)
131 fprintf (stderr, "\\u%04X name lookup failed!\n", i);
134 else if (strcmp (result, unicode_names[i]) != 0)
136 fprintf (stderr, "\\u%04X name lookup returned wrong name: %s\n",
145 fprintf (stderr, "\\u%04X name lookup returned wrong name: %s\n",
152 for (i = 0x110000; i < 0x1000000; i++)
154 char *result = unicode_character_name (i, buf);
158 fprintf (stderr, "\\u%04X name lookup returned wrong name: %s\n",
167 /* Perform a test of the unicode_name_character function. */
169 test_inverse_lookup ()
174 /* First, verify all valid character names are recognized. */
175 for (i = 0; i < 0x110000; i++)
176 if (unicode_names[i] != NULL)
178 unsigned int result = unicode_name_character (unicode_names[i]);
181 if (result == UNINAME_INVALID)
182 fprintf (stderr, "inverse name lookup of \"%s\" failed\n",
186 "inverse name lookup of \"%s\" returned 0x%04X\n",
187 unicode_names[i], result);
192 /* Second, generate random but likely names and verify they are not
193 recognized unless really valid. */
194 for (i = 0; i < 10000; i++)
199 unsigned int l1, l2, j1, j2;
200 char buf[2*UNINAME_MAX];
203 do i1 = ((rand () % 0x11) << 16)
204 + ((rand () & 0xff) << 8)
206 while (unicode_names[i1] == NULL);
208 do i2 = ((rand () % 0x11) << 16)
209 + ((rand () & 0xff) << 8)
211 while (unicode_names[i2] == NULL);
213 s1 = unicode_names[i1];
215 s2 = unicode_names[i2];
218 /* Concatenate a starting piece of s1 with an ending piece of s2. */
219 for (j1 = 1; j1 <= l1; j1++)
220 if (j1 == l1 || s1[j1] == ' ')
221 for (j2 = 0; j2 < l2; j2++)
222 if (j2 == 0 || s2[j2-1] == ' ')
224 memcpy (buf, s1, j1);
226 memcpy (buf + j1 + 1, s2 + j2, l2 - j2 + 1);
228 result = unicode_name_character (buf);
229 if (result != UNINAME_INVALID
230 && !(unicode_names[result] != NULL
231 && strcmp (unicode_names[result], buf) == 0))
234 "inverse name lookup of \"%s\" returned 0x%04X\n",
235 unicode_names[i], result);
241 /* Third, some extreme case that used to loop. */
242 if (unicode_name_character ("A A") != UNINAME_INVALID)
249 main (int argc, char *argv[])
253 set_program_name (argv[0]);
255 fill_names (argv[1]);
257 error |= test_name_lookup ();
258 error |= test_inverse_lookup ();