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