Convert tests/unicode-encoding.c to glib test framework

This commit is contained in:
Emmanuel Fleury 2022-04-03 12:46:43 +02:00
parent 6c31ef6f18
commit 43759ca951

View File

@ -1,40 +1,9 @@
#undef G_DISABLE_ASSERT
#undef G_LOG_DOMAIN
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <glib.h> #include <glib.h>
static gint exit_status = 0;
G_GNUC_PRINTF (1, 2)
static void
croak (char *format, ...)
{
va_list va;
va_start (va, format);
vfprintf (stderr, format, va);
va_end (va);
exit (1);
}
G_GNUC_PRINTF (1, 2)
static void
fail (char *format, ...)
{
va_list va;
va_start (va, format);
vfprintf (stderr, format, va);
va_end (va);
exit_status |= 1;
}
typedef enum typedef enum
{ {
VALID, VALID,
@ -79,22 +48,12 @@ utf16_count (gunichar2 *a)
return result; return result;
} }
static void
print_ucs4 (const gchar *prefix, gunichar *ucs4, gint ucs4_len)
{
gint i;
g_print ("%s ", prefix);
for (i = 0; i < ucs4_len; i++)
g_print ("%x ", ucs4[i]);
g_print ("\n");
}
static void static void
process (gint line, process (gint line,
gchar *utf8, gchar *utf8,
Status status, Status status,
gunichar *ucs4, gunichar *ucs4,
gint ucs4_len) gint ucs4_len)
{ {
const gchar *end; const gchar *end;
gboolean is_valid = g_utf8_validate (utf8, -1, &end); gboolean is_valid = g_utf8_validate (utf8, -1, &end);
@ -104,21 +63,14 @@ process (gint line,
switch (status) switch (status)
{ {
case VALID: case VALID:
if (!is_valid) g_assert_true (is_valid);
{
fail ("line %d: valid but g_utf8_validate returned FALSE\n", line);
return;
}
break; break;
case NOTUNICODE: case NOTUNICODE:
case INCOMPLETE: case INCOMPLETE:
case OVERLONG: case OVERLONG:
case MALFORMED: case MALFORMED:
if (is_valid) g_assert_false (is_valid);
{
fail ("line %d: invalid but g_utf8_validate returned TRUE\n", line);
return;
}
break; break;
} }
@ -128,20 +80,14 @@ process (gint line,
ucs4_result = g_utf8_to_ucs4 (utf8, -1, NULL, NULL, &error); ucs4_result = g_utf8_to_ucs4 (utf8, -1, NULL, NULL, &error);
if (!error || !g_error_matches (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT)) g_assert_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT);
{
fail ("line %d: incomplete input not properly detected\n", line);
return;
}
g_clear_error (&error); g_clear_error (&error);
ucs4_result = g_utf8_to_ucs4 (utf8, -1, &items_read, NULL, &error); ucs4_result = g_utf8_to_ucs4 (utf8, -1, &items_read, NULL, &error);
g_assert_no_error (error);
if (!ucs4_result || items_read == (glong) strlen (utf8)) g_assert_nonnull (ucs4_result);
{ g_assert_cmpint (items_read, !=, strlen (utf8));
fail ("line %d: incomplete input not properly detected\n", line);
return;
}
g_free (ucs4_result); g_free (ucs4_result);
} }
@ -151,21 +97,12 @@ process (gint line,
gunichar *ucs4_result; gunichar *ucs4_result;
ucs4_result = g_utf8_to_ucs4 (utf8, -1, &items_read, &items_written, &error); ucs4_result = g_utf8_to_ucs4 (utf8, -1, &items_read, &items_written, &error);
if (!ucs4_result) g_assert_no_error (error);
{ g_assert_nonnull (ucs4_result);
fail ("line %d: conversion with status %d to ucs4 failed: %s\n", line, status, error->message);
return;
}
if (!ucs4_equal (ucs4_result, ucs4) || g_assert_true (ucs4_equal (ucs4_result, ucs4));
items_read != (glong) strlen (utf8) || g_assert_cmpint (items_read, ==, strlen (utf8));
items_written != ucs4_len) g_assert_cmpint (items_written, ==, ucs4_len);
{
fail ("line %d: results of conversion with status %d to ucs4 do not match expected.\n", line, status);
print_ucs4 ("expected: ", ucs4, ucs4_len);
print_ucs4 ("received: ", ucs4_result, items_written);
return;
}
g_free (ucs4_result); g_free (ucs4_result);
} }
@ -176,30 +113,18 @@ process (gint line,
gchar *utf8_result; gchar *utf8_result;
ucs4_result = g_utf8_to_ucs4_fast (utf8, -1, &items_written); ucs4_result = g_utf8_to_ucs4_fast (utf8, -1, &items_written);
g_assert_nonnull (ucs4_result);
if (!ucs4_equal (ucs4_result, ucs4) || g_assert_true (ucs4_equal (ucs4_result, ucs4));
items_written != ucs4_len) g_assert_cmpint (items_written, ==, ucs4_len);
{
fail ("line %d: results of fast conversion with status %d to ucs4 do not match expected.\n", line, status);
print_ucs4 ("expected: ", ucs4, ucs4_len);
print_ucs4 ("received: ", ucs4_result, items_written);
return;
}
utf8_result = g_ucs4_to_utf8 (ucs4_result, -1, &items_read, &items_written, &error); utf8_result = g_ucs4_to_utf8 (ucs4_result, -1, &items_read, &items_written, &error);
if (!utf8_result) g_assert_no_error (error);
{ g_assert_nonnull (utf8_result);
fail ("line %d: conversion back to utf8 failed: %s", line, error->message);
return;
}
if (strcmp (utf8_result, utf8) != 0 || g_assert_cmpstr ((char *) utf8_result, ==, (char *) utf8);
items_read != ucs4_len || g_assert_cmpint (items_read, ==, ucs4_len);
items_written != (glong) strlen (utf8)) g_assert_cmpint (items_written, ==, strlen (utf8));
{
fail ("line %d: conversion back to utf8 did not match original\n", line);
return;
}
g_free (utf8_result); g_free (utf8_result);
g_free (ucs4_result); g_free (ucs4_result);
@ -222,100 +147,68 @@ process (gint line,
#define TARGET "UTF-16" #define TARGET "UTF-16"
#endif #endif
if (!(utf16_expected_tmp = (gunichar2 *)g_convert (utf8, -1, TARGET, "UTF-8", utf16_expected_tmp =
NULL, &bytes_written, NULL))) (gunichar2 *) g_convert (utf8, -1, TARGET, "UTF-8", NULL, &bytes_written, NULL);
{ g_assert_nonnull (utf16_expected_tmp);
fail ("line %d: could not convert to UTF-16 via g_convert\n", line);
return;
}
/* zero-terminate and remove BOM /* zero-terminate and remove BOM */
*/
n_chars = bytes_written / 2; n_chars = bytes_written / 2;
if (utf16_expected_tmp[0] == 0xfeff) /* BOM */ if (utf16_expected_tmp[0] == 0xfeff) /* BOM */
{ {
n_chars--; n_chars--;
utf16_expected = g_new (gunichar2, n_chars + 1); utf16_expected = g_new (gunichar2, n_chars + 1);
memcpy (utf16_expected, utf16_expected_tmp + 1, sizeof(gunichar2) * n_chars); memcpy (utf16_expected, utf16_expected_tmp + 1, sizeof(gunichar2) * n_chars);
} }
else if (utf16_expected_tmp[0] == 0xfffe) /* ANTI-BOM */
{
fail ("line %d: conversion via iconv to \"UTF-16\" is not native-endian\n", line);
return;
}
else else
{ {
utf16_expected = g_new (gunichar2, n_chars + 1); /* We expect the result of the conversion
memcpy (utf16_expected, utf16_expected_tmp, sizeof(gunichar2) * n_chars); via iconv() to be native-endian. */
} g_assert_false (utf16_expected_tmp[0] == 0xfffe);
utf16_expected = g_new (gunichar2, n_chars + 1);
memcpy (utf16_expected, utf16_expected_tmp, sizeof(gunichar2) * n_chars);
}
utf16_expected[n_chars] = '\0'; utf16_expected[n_chars] = '\0';
if (!(utf16_from_utf8 = g_utf8_to_utf16 (utf8, -1, &items_read, &items_written, &error))) utf16_from_utf8 =
{ g_utf8_to_utf16 (utf8, -1, &items_read, &items_written, &error);
fail ("line %d: conversion to ucs16 failed: %s\n", line, error->message); g_assert_no_error (error);
return; g_assert_nonnull (utf16_from_utf8);
}
if (items_read != (glong) strlen (utf8) || g_assert_cmpint (items_read, ==, (glong) strlen (utf8));
utf16_count (utf16_from_utf8) != items_written) g_assert_cmpint (utf16_count (utf16_from_utf8), ==, items_written);
{
fail ("line %d: length error in conversion to ucs16\n", line);
return;
}
if (!(utf16_from_ucs4 = g_ucs4_to_utf16 (ucs4, -1, &items_read, &items_written, &error))) utf16_from_ucs4 =
{ g_ucs4_to_utf16 (ucs4, -1, &items_read, &items_written, &error);
fail ("line %d: conversion to ucs16 failed: %s\n", line, error->message); g_assert_no_error (error);
return; g_assert_nonnull (utf16_from_ucs4);
}
if (items_read != ucs4_len || g_assert_cmpint (items_read, ==, ucs4_len);
utf16_count (utf16_from_ucs4) != items_written) g_assert_cmpint (utf16_count (utf16_from_ucs4), ==, items_written);
{
fail ("line %d: length error in conversion to ucs16\n", line);
return;
}
if (!utf16_equal (utf16_from_utf8, utf16_expected) || g_assert_true (utf16_equal (utf16_from_utf8, utf16_expected));
!utf16_equal (utf16_from_ucs4, utf16_expected)) g_assert_true (utf16_equal (utf16_from_ucs4, utf16_expected));
{ g_assert_cmpstr ((char *) utf16_from_utf8, ==, (char *) utf16_expected);
fail ("line %d: results of conversion to ucs16 do not match\n", line); g_assert_cmpstr ((char *) utf16_from_ucs4, ==, (char *) utf16_expected);
return;
}
if (!(utf8_result = g_utf16_to_utf8 (utf16_from_utf8, -1, &items_read, &items_written, &error))) utf8_result =
{ g_utf16_to_utf8 (utf16_from_utf8, -1, &items_read, &items_written, &error);
fail ("line %d: conversion back to utf8 failed: %s\n", line, error->message); g_assert_no_error (error);
return; g_assert_nonnull (utf8_result);
}
if (items_read != utf16_count (utf16_from_utf8) || g_assert_cmpint (items_read, ==, utf16_count (utf16_from_utf8));
items_written != (glong) strlen (utf8)) g_assert_cmpint (items_written, ==, (glong) strlen (utf8));
{
fail ("line %d: length error in conversion from ucs16 to utf8\n", line);
return;
}
if (!(ucs4_result = g_utf16_to_ucs4 (utf16_from_ucs4, -1, &items_read, &items_written, &error))) ucs4_result =
{ g_utf16_to_ucs4 (utf16_from_ucs4, -1, &items_read, &items_written, &error);
fail ("line %d: conversion back to utf8/ucs4 failed\n", line); g_assert_no_error (error);
return; g_assert_nonnull (ucs4_result);
}
if (items_read != utf16_count (utf16_from_utf8) || g_assert_cmpint (items_read, ==, utf16_count (utf16_from_utf8));
items_written != ucs4_len) g_assert_cmpint (items_written, ==, ucs4_len);
{
fail ("line %d: length error in conversion from ucs16 to ucs4\n", line);
return;
}
if (strcmp (utf8, utf8_result) != 0 || g_assert_cmpstr (utf8, ==, utf8_result);
!ucs4_equal (ucs4, ucs4_result)) g_assert_cmpstr ((char *) ucs4, ==, (char *) ucs4_result);
{
fail ("line %d: conversion back to utf8/ucs4 did not match original\n", line);
return;
}
g_free (utf16_expected_tmp); g_free (utf16_expected_tmp);
g_free (utf16_expected); g_free (utf16_expected);
@ -326,8 +219,8 @@ process (gint line,
} }
} }
int static void
main (int argc, char **argv) test_unicode_encoding (void)
{ {
gchar *testfile; gchar *testfile;
gchar *contents; gchar *contents;
@ -336,18 +229,15 @@ main (int argc, char **argv)
char *tmp; char *tmp;
gint state = 0; gint state = 0;
gint line = 1; gint line = 1;
gint start_line = 0; /* Quiet GCC */ gint start_line = 0; /* Quiet GCC */
gchar *utf8 = NULL; /* Quiet GCC */ gchar *utf8 = NULL; /* Quiet GCC */
GArray *ucs4; GArray *ucs4;
Status status = VALID; /* Quiet GCC */ Status status = VALID; /* Quiet GCC */
g_test_init (&argc, &argv, NULL);
testfile = g_test_build_filename (G_TEST_DIST, "utf8.txt", NULL); testfile = g_test_build_filename (G_TEST_DIST, "utf8.txt", NULL);
g_file_get_contents (testfile, &contents, NULL, &error); g_file_get_contents (testfile, &contents, NULL, &error);
if (error) g_assert_no_error (error);
croak ("Cannot open utf8.txt: %s", error->message);
ucs4 = g_array_new (TRUE, FALSE, sizeof(gunichar)); ucs4 = g_array_new (TRUE, FALSE, sizeof(gunichar));
@ -357,88 +247,91 @@ main (int argc, char **argv)
while (*p) while (*p)
{ {
while (*p && (*p == ' ' || *p == '\t')) while (*p && (*p == ' ' || *p == '\t'))
p++; p++;
end = p; end = p;
while (*end && (*end != '\r' && *end != '\n')) while (*end && (*end != '\r' && *end != '\n'))
end++; end++;
if (!*p || *p == '#' || *p == '\r' || *p == '\n') if (!*p || *p == '#' || *p == '\r' || *p == '\n')
goto next_line; goto next_line;
tmp = g_strstrip (g_strndup (p, end - p)); tmp = g_strstrip (g_strndup (p, end - p));
switch (state) switch (state)
{ {
case 0: case 0:
/* UTF-8 string */ /* UTF-8 string */
start_line = line; start_line = line;
utf8 = tmp; utf8 = tmp;
tmp = NULL; tmp = NULL;
break; break;
case 1: case 1:
/* Status */ /* Status */
if (!strcmp (tmp, "VALID")) if (!strcmp (tmp, "VALID"))
status = VALID; status = VALID;
else if (!strcmp (tmp, "INCOMPLETE")) else if (!strcmp (tmp, "INCOMPLETE"))
status = INCOMPLETE; status = INCOMPLETE;
else if (!strcmp (tmp, "NOTUNICODE")) else if (!strcmp (tmp, "NOTUNICODE"))
status = NOTUNICODE; status = NOTUNICODE;
else if (!strcmp (tmp, "OVERLONG")) else if (!strcmp (tmp, "OVERLONG"))
status = OVERLONG; status = OVERLONG;
else if (!strcmp (tmp, "MALFORMED")) else if (!strcmp (tmp, "MALFORMED"))
status = MALFORMED; status = MALFORMED;
else else
croak ("Invalid status on line %d\n", line); g_assert_not_reached ();
if (status != VALID && status != NOTUNICODE) if (status != VALID && status != NOTUNICODE)
state++; /* No UCS-4 data */ state++; /* No UCS-4 data */
break;
break; case 2:
/* UCS-4 version */
p = strtok (tmp, " \t");
while (p)
{
gchar *endptr;
gunichar ch = strtoul (p, &endptr, 16);
g_assert_cmpint (*endptr, == ,'\0');
case 2: g_array_append_val (ucs4, ch);
/* UCS-4 version */
p = strtok (tmp, " \t"); p = strtok (NULL, " \t");
while (p) }
{ break;
gchar *endptr; }
gunichar ch = strtoul (p, &endptr, 16);
if (*endptr != '\0')
croak ("Invalid UCS-4 character on line %d\n", line);
g_array_append_val (ucs4, ch);
p = strtok (NULL, " \t");
}
break;
}
g_free (tmp); g_free (tmp);
state = (state + 1) % 3; state = (state + 1) % 3;
if (state == 0) if (state == 0)
{ {
process (start_line, utf8, status, (gunichar *)ucs4->data, ucs4->len); process (start_line, utf8, status, (gunichar *)ucs4->data, ucs4->len);
g_array_set_size (ucs4, 0); g_array_set_size (ucs4, 0);
g_free (utf8); g_free (utf8);
} }
next_line: next_line:
p = end; p = end;
if (*p && *p == '\r') if (*p && *p == '\r')
p++; p++;
if (*p && *p == '\n') if (*p && *p == '\n')
p++; p++;
line++; line++;
} }
g_free (testfile); g_free (testfile);
g_array_free (ucs4, TRUE); g_array_free (ucs4, TRUE);
g_free (contents); g_free (contents);
return exit_status; }
int
main (int argc, char **argv)
{
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/unicode/encoding", test_unicode_encoding);
return g_test_run ();
} }