Enable and convert tests/unicode-normalize.c to glib test framework

This commit is contained in:
Emmanuel Fleury 2022-04-08 11:24:43 +02:00
parent a7aa029a11
commit 2c37b92dfa
3 changed files with 19092 additions and 100 deletions

19047
tests/NormalizationTest.txt Normal file

File diff suppressed because it is too large Load Diff

View File

@ -38,6 +38,7 @@ tests = {
'source': 'module-test.c', 'source': 'module-test.c',
'c_args': ['-DMODULE_TYPE="plugin"'], 'c_args': ['-DMODULE_TYPE="plugin"'],
}, },
'unicode-normalize': {},
} }
test_extra_programs = { test_extra_programs = {

View File

@ -6,25 +6,18 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
gboolean success = TRUE;
static char * static char *
decode (const gchar *input) decode (const gchar *input)
{ {
unsigned ch; unsigned ch;
int offset = 0; int offset = 0;
GString *result = g_string_new (NULL); GString *result = g_string_new (NULL);
do
{
if (sscanf (input + offset, "%x", &ch) != 1)
{
fprintf (stderr, "Error parsing character string %s\n", input);
exit (1);
}
do
{
g_assert_cmpint (sscanf (input + offset, "%x", &ch), ==, 1);
g_string_append_unichar (result, ch); g_string_append_unichar (result, ch);
while (input[offset] && input[offset] != ' ') while (input[offset] && input[offset] != ' ')
offset++; offset++;
while (input[offset] && input[offset] == ' ') while (input[offset] && input[offset] == ' ')
@ -42,22 +35,6 @@ const char *names[4] = {
"NFKC" "NFKC"
}; };
static char *
encode (const gchar *input)
{
GString *result = g_string_new(NULL);
const gchar *p = input;
while (*p)
{
gunichar c = g_utf8_get_char (p);
g_string_append_printf (result, "%04X ", c);
p = g_utf8_next_char(p);
}
return g_string_free (result, FALSE);
}
static void static void
test_form (int line, test_form (int line,
GNormalizeMode mode, GNormalizeMode mode,
@ -67,7 +44,6 @@ test_form (int line,
char **raw) char **raw)
{ {
int i; int i;
gboolean mode_is_compat = (mode == G_NORMALIZE_NFKC || gboolean mode_is_compat = (mode == G_NORMALIZE_NFKC ||
mode == G_NORMALIZE_NFKD); mode == G_NORMALIZE_NFKD);
@ -76,17 +52,8 @@ test_form (int line,
for (i = 0; i < 3; i++) for (i = 0; i < 3; i++)
{ {
char *result = g_utf8_normalize (c[i], -1, mode); char *result = g_utf8_normalize (c[i], -1, mode);
if (strcmp (result, c[expected]) != 0) g_assert_cmpstr (result, ==, c[expected]);
{ g_free (result);
char *result_raw = encode(result);
fprintf (stderr, "\nFailure: %d/%d: %s\n", line, i + 1, raw[5]);
fprintf (stderr, " g_utf8_normalize (%s, %s) != %s but %s\n",
raw[i], names[mode], raw[expected], result_raw);
g_free (result_raw);
success = FALSE;
}
g_free (result);
} }
} }
if (mode_is_compat || do_compat) if (mode_is_compat || do_compat)
@ -94,74 +61,51 @@ test_form (int line,
for (i = 3; i < 5; i++) for (i = 3; i < 5; i++)
{ {
char *result = g_utf8_normalize (c[i], -1, mode); char *result = g_utf8_normalize (c[i], -1, mode);
if (strcmp (result, c[expected]) != 0) g_assert_cmpstr (result, ==, c[expected]);
{ g_free (result);
char *result_raw = encode(result);
fprintf (stderr, "\nFailure: %d/%d: %s\n", line, i, raw[5]);
fprintf (stderr, " g_utf8_normalize (%s, %s) != %s but %s\n",
raw[i], names[mode], raw[expected], result_raw);
g_free (result_raw);
success = FALSE;
}
g_free (result);
} }
} }
} }
static gboolean static void
process_one (int line, gchar **columns) process_one (int line, gchar **columns)
{ {
char *c[5]; char *c[5];
int i; int i;
gboolean skip = FALSE;
for (i=0; i < 5; i++) for (i = 0; i < 5; i++)
{ {
c[i] = decode(columns[i]); c[i] = decode (columns[i]);
if (!c[i]) g_assert_nonnull (c[i]);
skip = TRUE;
} }
if (!skip) test_form (line, G_NORMALIZE_NFD, FALSE, 2, c, columns);
{ test_form (line, G_NORMALIZE_NFD, TRUE, 4, c, columns);
test_form (line, G_NORMALIZE_NFD, FALSE, 2, c, columns); test_form (line, G_NORMALIZE_NFC, FALSE, 1, c, columns);
test_form (line, G_NORMALIZE_NFD, TRUE, 4, c, columns); test_form (line, G_NORMALIZE_NFC, TRUE, 3, c, columns);
test_form (line, G_NORMALIZE_NFC, FALSE, 1, c, columns); test_form (line, G_NORMALIZE_NFKD, TRUE, 4, c, columns);
test_form (line, G_NORMALIZE_NFC, TRUE, 3, c, columns); test_form (line, G_NORMALIZE_NFKC, TRUE, 3, c, columns);
test_form (line, G_NORMALIZE_NFKD, TRUE, 4, c, columns);
test_form (line, G_NORMALIZE_NFKC, TRUE, 3, c, columns);
}
for (i=0; i < 5; i++) for (i = 0; i < 5; i++)
g_free (c[i]); g_free (c[i]);
return TRUE;
} }
int main (int argc, char **argv) static void
test_unicode_normalize (void)
{ {
GIOChannel *in; GIOChannel *in;
GError *error = NULL; GError *error = NULL;
gchar *filename = NULL;
GString *buffer = g_string_new (NULL); GString *buffer = g_string_new (NULL);
int line_to_do = 0;
int line = 1; int line = 1;
if (argc != 2 && argc != 3) filename = g_test_build_filename (G_TEST_DIST, "NormalizationTest.txt", NULL);
{ g_assert_nonnull (filename);
fprintf (stderr, "Usage: unicode-normalize NormalizationTest.txt LINE\n");
return 1;
}
if (argc == 3) in = g_io_channel_new_file (filename, "r", &error);
line_to_do = atoi(argv[2]); g_assert_no_error (error);
g_assert_nonnull (in);
in = g_io_channel_new_file (argv[1], "r", &error); g_free (filename);
if (!in)
{
fprintf (stderr, "Cannot open %s: %s\n", argv[1], error->message);
return 1;
}
while (TRUE) while (TRUE)
{ {
@ -170,12 +114,9 @@ int main (int argc, char **argv)
if (g_io_channel_read_line_string (in, buffer, &term_pos, &error) != G_IO_STATUS_NORMAL) if (g_io_channel_read_line_string (in, buffer, &term_pos, &error) != G_IO_STATUS_NORMAL)
break; break;
if (line_to_do && line != line_to_do)
goto next;
buffer->str[term_pos] = '\0'; buffer->str[term_pos] = '\0';
if (buffer->str[0] == '#') /* Comment */ if (buffer->str[0] == '#') /* Comment */
goto next; goto next;
if (buffer->str[0] == '@') /* Part */ if (buffer->str[0] == '@') /* Part */
@ -183,13 +124,12 @@ int main (int argc, char **argv)
fprintf (stderr, "\nProcessing %s\n", buffer->str + 1); fprintf (stderr, "\nProcessing %s\n", buffer->str + 1);
goto next; goto next;
} }
columns = g_strsplit (buffer->str, ";", -1); columns = g_strsplit (buffer->str, ";", -1);
if (!columns[0]) if (!columns[0])
goto next; goto next;
if (!process_one (line, columns)) process_one (line, columns);
return 1;
g_strfreev (columns); g_strfreev (columns);
next: next:
@ -197,14 +137,18 @@ int main (int argc, char **argv)
line++; line++;
} }
if (error) g_assert_no_error (error);
{
fprintf (stderr, "Error reading test file, %s\n", error->message);
return 1;
}
g_io_channel_unref (in); g_io_channel_unref (in);
g_string_free (buffer, TRUE); g_string_free (buffer, TRUE);
}
return !success;
int
main (int argc, char **argv)
{
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/unicode/normalize", test_unicode_normalize);
return g_test_run ();
} }