1 #undef G_DISABLE_ASSERT
9 gboolean success
= TRUE
;
12 decode (const gchar
*input
)
16 GString
*result
= g_string_new (NULL
);
20 if (sscanf (input
+ offset
, "%x", &ch
) != 1)
22 fprintf (stderr
, "Error parsing character string %s\n", input
);
26 g_string_append_unichar (result
, ch
);
28 while (input
[offset
] && input
[offset
] != ' ')
30 while (input
[offset
] && input
[offset
] == ' ')
33 while (input
[offset
]);
35 return g_string_free (result
, FALSE
);
38 const char *names
[4] = {
46 encode (const gchar
*input
)
48 GString
*result
= g_string_new(NULL
);
50 const gchar
*p
= input
;
53 gunichar c
= g_utf8_get_char (p
);
54 g_string_append_printf (result
, "%04X ", c
);
55 p
= g_utf8_next_char(p
);
58 return g_string_free (result
, FALSE
);
71 gboolean mode_is_compat
= (mode
== G_NORMALIZE_NFKC
||
72 mode
== G_NORMALIZE_NFKD
);
74 if (mode_is_compat
|| !do_compat
)
76 for (i
= 0; i
< 3; i
++)
78 char *result
= g_utf8_normalize (c
[i
], -1, mode
);
79 if (strcmp (result
, c
[expected
]) != 0)
81 char *result_raw
= encode(result
);
82 fprintf (stderr
, "\nFailure: %d/%d: %s\n", line
, i
+ 1, raw
[5]);
83 fprintf (stderr
, " g_utf8_normalize (%s, %s) != %s but %s\n",
84 raw
[i
], names
[mode
], raw
[expected
], result_raw
);
92 if (mode_is_compat
|| do_compat
)
94 for (i
= 3; i
< 5; i
++)
96 char *result
= g_utf8_normalize (c
[i
], -1, mode
);
97 if (strcmp (result
, c
[expected
]) != 0)
99 char *result_raw
= encode(result
);
100 fprintf (stderr
, "\nFailure: %d/%d: %s\n", line
, i
, raw
[5]);
101 fprintf (stderr
, " g_utf8_normalize (%s, %s) != %s but %s\n",
102 raw
[i
], names
[mode
], raw
[expected
], result_raw
);
113 process_one (int line
, gchar
**columns
)
117 gboolean skip
= FALSE
;
119 for (i
=0; i
< 5; i
++)
121 c
[i
] = decode(columns
[i
]);
128 test_form (line
, G_NORMALIZE_NFD
, FALSE
, 2, c
, columns
);
129 test_form (line
, G_NORMALIZE_NFD
, TRUE
, 4, c
, columns
);
130 test_form (line
, G_NORMALIZE_NFC
, FALSE
, 1, c
, columns
);
131 test_form (line
, G_NORMALIZE_NFC
, TRUE
, 3, c
, columns
);
132 test_form (line
, G_NORMALIZE_NFKD
, TRUE
, 4, c
, columns
);
133 test_form (line
, G_NORMALIZE_NFKC
, TRUE
, 3, c
, columns
);
136 for (i
=0; i
< 5; i
++)
142 int main (int argc
, char **argv
)
145 GError
*error
= NULL
;
146 GString
*buffer
= g_string_new (NULL
);
150 if (argc
!= 2 && argc
!= 3)
152 fprintf (stderr
, "Usage: unicode-normalize NormalizationTest.txt LINE\n");
157 line_to_do
= atoi(argv
[2]);
159 in
= g_io_channel_new_file (argv
[1], "r", &error
);
162 fprintf (stderr
, "Cannot open %s: %s\n", argv
[1], error
->message
);
171 if (g_io_channel_read_line_string (in
, buffer
, &term_pos
, &error
) != G_IO_STATUS_NORMAL
)
174 if (line_to_do
&& line
!= line_to_do
)
177 buffer
->str
[term_pos
] = '\0';
179 if (buffer
->str
[0] == '#') /* Comment */
181 if (buffer
->str
[0] == '@') /* Part */
183 fprintf (stderr
, "\nProcessing %s\n", buffer
->str
+ 1);
187 columns
= g_strsplit (buffer
->str
, ";", -1);
191 if (!process_one (line
, columns
))
193 g_strfreev (columns
);
196 g_string_truncate (buffer
, 0);
202 fprintf (stderr
, "Error reading test file, %s\n", error
->message
);
206 g_io_channel_unref (in
);
207 g_string_free (buffer
, TRUE
);