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