1 /* Test the Unicode character name functions.
2 Copyright (C) 2000-2003, 2005, 2007 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/>. */
26 /* The names according to the UnicodeData.txt file, modified to contain the
27 Hangul syllable names, as described in the Unicode 3.0 book. */
28 const char * unicode_names [0x110000];
30 /* Maximum length of a field in the UnicodeData.txt file. */
33 /* Reads the next field from STREAM. The buffer BUFFER has size FIELDLEN.
34 Reads up to (but excluding) DELIM.
35 Returns 1 when a field was successfully read, otherwise 0. */
37 getfield (FILE *stream, char *buffer, int delim)
42 for (; (c = getc (stream)), (c != EOF && c != delim); )
44 /* Put c into the buffer. */
45 if (++count >= FIELDLEN - 1)
47 fprintf (stderr, "field too long\n");
60 /* Stores in unicode_names[] the relevant contents of the UnicodeData.txt
63 fill_names (const char *unicodedata_filename)
67 char field0[FIELDLEN];
68 char field1[FIELDLEN];
71 for (i = 0; i < 0x110000; i++)
72 unicode_names[i] = NULL;
74 stream = fopen (unicodedata_filename, "r");
77 fprintf (stderr, "error during fopen of '%s'\n", unicodedata_filename);
87 n = getfield (stream, field0, ';');
88 n += getfield (stream, field1, ';');
93 fprintf (stderr, "short line in '%s':%d\n",
94 unicodedata_filename, lineno);
97 for (; (c = getc (stream)), (c != EOF && c != '\n'); )
99 i = strtoul (field0, NULL, 16);
102 fprintf (stderr, "index too large\n");
105 unicode_names[i] = xstrdup (field1);
107 if (ferror (stream) || fclose (stream))
109 fprintf (stderr, "error reading from '%s'\n", unicodedata_filename);
114 /* Perform an exhaustive test of the unicode_character_name function. */
120 char buf[UNINAME_MAX];
122 for (i = 0; i < 0x11000; i++)
124 char *result = unicode_character_name (i, buf);
126 if (unicode_names[i] != NULL)
130 fprintf (stderr, "\\u%04X name lookup failed!\n", i);
133 else if (strcmp (result, unicode_names[i]) != 0)
135 fprintf (stderr, "\\u%04X name lookup returned wrong name: %s\n",
144 fprintf (stderr, "\\u%04X name lookup returned wrong name: %s\n",
151 for (i = 0x110000; i < 0x1000000; i++)
153 char *result = unicode_character_name (i, buf);
157 fprintf (stderr, "\\u%04X name lookup returned wrong name: %s\n",
166 /* Perform a test of the unicode_name_character function. */
168 test_inverse_lookup ()
173 /* First, verify all valid character names are recognized. */
174 for (i = 0; i < 0x110000; i++)
175 if (unicode_names[i] != NULL)
177 unsigned int result = unicode_name_character (unicode_names[i]);
180 if (result == UNINAME_INVALID)
181 fprintf (stderr, "inverse name lookup of \"%s\" failed\n",
185 "inverse name lookup of \"%s\" returned 0x%04X\n",
186 unicode_names[i], result);
191 /* Second, generate random but likely names and verify they are not
192 recognized unless really valid. */
193 for (i = 0; i < 10000; i++)
198 unsigned int l1, l2, j1, j2;
199 char buf[2*UNINAME_MAX];
202 do i1 = ((rand () % 0x11) << 16)
203 + ((rand () & 0xff) << 8)
205 while (unicode_names[i1] == NULL);
207 do i2 = ((rand () % 0x11) << 16)
208 + ((rand () & 0xff) << 8)
210 while (unicode_names[i2] == NULL);
212 s1 = unicode_names[i1];
214 s2 = unicode_names[i2];
217 /* Concatenate a starting piece of s1 with an ending piece of s2. */
218 for (j1 = 1; j1 <= l1; j1++)
219 if (j1 == l1 || s1[j1] == ' ')
220 for (j2 = 0; j2 < l2; j2++)
221 if (j2 == 0 || s2[j2-1] == ' ')
223 memcpy (buf, s1, j1);
225 memcpy (buf + j1 + 1, s2 + j2, l2 - j2 + 1);
227 result = unicode_name_character (buf);
228 if (result != UNINAME_INVALID
229 && !(unicode_names[result] != NULL
230 && strcmp (unicode_names[result], buf) == 0))
233 "inverse name lookup of \"%s\" returned 0x%04X\n",
234 unicode_names[i], result);
240 /* Third, some extreme case that used to loop. */
241 if (unicode_name_character ("A A") != UNINAME_INVALID)
248 main (int argc, char *argv[])
252 fill_names (argv[1]);
254 error |= test_name_lookup ();
255 error |= test_inverse_lookup ();