Add new symbols.
[glib.git] / tests / unicode-normalize.c
blob9621bf4483d7508cc7ef9e597ba3e957a66a8e72
1 #undef G_DISABLE_ASSERT
2 #undef G_LOG_DOMAIN
4 #include <glib.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
9 gboolean success = TRUE;
11 static char *
12 decode (const gchar *input)
14 unsigned ch;
15 int offset = 0;
16 GString *result = g_string_new (NULL);
18 do
20 if (sscanf (input + offset, "%x", &ch) != 1)
22 fprintf (stderr, "Error parsing character string %s\n", input);
23 exit (1);
26 g_string_append_unichar (result, ch);
28 while (input[offset] && input[offset] != ' ')
29 offset++;
30 while (input[offset] && input[offset] == ' ')
31 offset++;
33 while (input[offset]);
35 return g_string_free (result, FALSE);
38 const char *names[4] = {
39 "NFD",
40 "NFC",
41 "NFKD",
42 "NFKC"
45 static void
46 test_form (int line,
47 GNormalizeMode mode,
48 gboolean do_compat,
49 int expected,
50 char **c,
51 char **raw)
53 int i;
55 gboolean mode_is_compat = (mode == G_NORMALIZE_NFKC ||
56 mode == G_NORMALIZE_NFKD);
58 if (mode_is_compat || !do_compat)
60 for (i = 0; i < 3; i++)
62 char *result = g_utf8_normalize (c[i], -1, mode);
63 if (strcmp (result, c[expected]) != 0)
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;
71 g_free (result);
74 if (mode_is_compat || do_compat)
76 for (i = 3; i < 5; i++)
78 char *result = g_utf8_normalize (c[i], -1, mode);
79 if (strcmp (result, c[expected]) != 0)
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;
87 g_free (result);
92 static gboolean
93 process_one (int line, gchar **columns)
95 char *c[5];
96 int i;
97 gboolean skip = FALSE;
99 for (i=0; i < 5; i++)
101 c[i] = decode(columns[i]);
102 if (!c[i])
103 skip = TRUE;
106 if (!skip)
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);
116 for (i=0; i < 5; i++)
117 g_free (c[i]);
119 return TRUE;
122 int main (int argc, char **argv)
124 GIOChannel *in;
125 GError *error = NULL;
126 GString *buffer = g_string_new (NULL);
127 int line_to_do = 0;
128 int line = 1;
130 if (argc != 2 && argc != 3)
132 fprintf (stderr, "Usage: unicode-normalize NormalizationTest.txt LINE\n");
133 return 1;
136 if (argc == 3)
137 line_to_do = atoi(argv[2]);
139 in = g_io_channel_new_file (argv[1], "r", &error);
140 if (!in)
142 fprintf (stderr, "Cannot open %s: %s\n", argv[1], error->message);
143 return 1;
146 while (TRUE)
148 gsize term_pos;
149 gchar **columns;
151 if (g_io_channel_read_line_string (in, buffer, &term_pos, &error) != G_IO_STATUS_NORMAL)
152 break;
154 if (line_to_do && line != line_to_do)
155 goto next;
157 buffer->str[term_pos] = '\0';
159 if (buffer->str[0] == '#') /* Comment */
160 goto next;
161 if (buffer->str[0] == '@') /* Part */
163 fprintf (stderr, "\nProcessing %s\n", buffer->str + 1);
164 goto next;
167 columns = g_strsplit (buffer->str, ";", -1);
168 if (!columns[0])
169 goto next;
171 if (!process_one (line, columns))
172 return 1;
173 g_strfreev (columns);
175 next:
176 g_string_truncate (buffer, 0);
177 line++;
180 if (error)
182 fprintf (stderr, "Error reading test file, %s\n", error->message);
183 return 1;
186 g_io_channel_unref (in);
187 g_string_free (buffer, TRUE);
189 return !success;