We do handle > BMP now, so test it.
[platform/upstream/glib.git] / tests / unicode-normalize.c
1 #undef G_DISABLE_ASSERT
2 #undef G_LOG_DOMAIN
3
4 #include <glib.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8
9 gboolean success = TRUE;
10
11 static char *
12 decode (const gchar *input)
13 {
14   unsigned ch;
15   int offset = 0;
16   GString *result = g_string_new (NULL);
17   
18   do 
19     {
20       if (sscanf (input + offset, "%x", &ch) != 1)
21         {
22           fprintf (stderr, "Error parsing character string %s\n", input);
23           exit (1);
24         }
25
26       /* FIXME: We don't handle the Hangul syllables */
27       if (ch >= 0xac00 && ch <= 0xd7ff)  /* Hangul syllables */
28         {
29           g_string_free (result, TRUE);
30           return NULL;
31         }
32
33       g_string_append_unichar (result, ch);
34       
35       while (input[offset] && input[offset] != ' ')
36         offset++;
37       while (input[offset] && input[offset] == ' ')
38         offset++;
39     }
40   while (input[offset]);
41
42   return g_string_free (result, FALSE);
43 }
44
45 const char *names[4] = {
46   "NFD",
47   "NFC",
48   "NFKD",
49   "NFKC"
50 };
51
52 static void
53 test_form (int            line,
54            GNormalizeMode mode,
55            gboolean       do_compat,
56            int            expected,
57            char         **c,
58            char         **raw)
59 {
60   int i;
61   
62   gboolean mode_is_compat = (mode == G_NORMALIZE_NFKC ||
63                              mode == G_NORMALIZE_NFKD);
64
65   if (mode_is_compat || !do_compat)
66     {
67       for (i = 0; i < 3; i++)
68         {
69           char *result = g_utf8_normalize (c[i], -1, mode);
70           if (strcmp (result, c[expected]) != 0)
71             {
72               fprintf (stderr, "\nFailure: %d/%d: %s\n", line, i + 1, raw[5]);
73               fprintf (stderr, "  g_utf8_normalize (%s, %s) != %s\n",
74                    raw[i], names[mode], raw[expected]);
75               success = FALSE;
76             }
77           
78           g_free (result);
79         }
80     }
81   if (mode_is_compat || do_compat)
82     {
83       for (i = 3; i < 5; i++)
84         {
85           char *result = g_utf8_normalize (c[i], -1, mode);
86           if (strcmp (result, c[expected]) != 0)
87             {
88               fprintf (stderr, "\nFailure: %d/%d: %s\n", line, i, raw[5]);
89               fprintf (stderr, "  g_utf8_normalize (%s, %s) != %s\n",
90                    raw[i], names[mode], raw[expected]);
91               success = FALSE;
92             }
93           
94           g_free (result);
95         }
96     }
97 }
98
99 static gboolean
100 process_one (int line, gchar **columns)
101 {
102   char *c[5];
103   int i;
104   gboolean skip = FALSE;
105
106   for (i=0; i < 5; i++)
107     {
108       c[i] = decode(columns[i]);
109       if (!c[i])
110         skip = TRUE;
111     }
112
113   if (!skip)
114     {
115       test_form (line, G_NORMALIZE_NFD, FALSE, 2, c, columns);
116       test_form (line, G_NORMALIZE_NFD, TRUE, 4, c, columns);
117       test_form (line, G_NORMALIZE_NFC, FALSE, 1, c, columns);
118       test_form (line, G_NORMALIZE_NFC, TRUE, 3, c, columns);
119       test_form (line, G_NORMALIZE_NFKD, TRUE, 4, c, columns);
120       test_form (line, G_NORMALIZE_NFKC, TRUE, 3, c, columns);
121     }
122
123   for (i=0; i < 5; i++)
124     g_free (c[i]);
125   
126   return TRUE;
127 }
128
129 int main (int argc, char **argv)
130 {
131   GIOChannel *in;
132   GError *error = NULL;
133   GString *buffer = g_string_new (NULL);
134   int line_to_do = 0;
135   int line = 1;
136
137   if (argc != 2 && argc != 3)
138     {
139       fprintf (stderr, "Usage: unicode-normalize NormalizationTest.txt LINE\n");
140       return 1;
141     }
142
143   if (argc == 3)
144     line_to_do = atoi(argv[2]);
145
146   in = g_io_channel_new_file (argv[1], "r", &error);
147   if (!in)
148     {
149       fprintf (stderr, "Cannot open %s: %s\n", argv[1], error->message);
150       return 1;
151     }
152
153   while (TRUE)
154     {
155       gsize term_pos;
156       gchar **columns;
157
158       if (g_io_channel_read_line_string (in, buffer, &term_pos, &error) != G_IO_STATUS_NORMAL)
159         break;
160         
161       if (line_to_do && line != line_to_do)
162         goto next;
163       
164       buffer->str[term_pos] = '\0';
165       
166       if (buffer->str[0] == '#') /* Comment */
167         goto next;
168       if (buffer->str[0] == '@') /* Part */
169         {
170           fprintf (stderr, "\nProcessing %s\n", buffer->str + 1);
171           goto next;
172         }
173       
174       columns = g_strsplit (buffer->str, ";", -1);
175       if (!columns[0])
176         goto next;
177       
178       if (!process_one (line, columns))
179         return 1;
180       g_strfreev (columns);
181
182     next:
183       g_string_truncate (buffer, 0);
184       line++;
185     }
186
187   if (error)
188     {
189       fprintf (stderr, "Error reading test file, %s\n", error->message);
190       return 1;
191     }
192
193   g_io_channel_unref (in);
194   g_string_free (buffer, TRUE);
195
196   return !success;
197 }