From deb495de42f077e1eadc9ff426769357d85ff59c Mon Sep 17 00:00:00 2001 From: Guillaume Desmottes Date: Tue, 7 Jul 2020 16:50:51 +0200 Subject: [PATCH] glib: unicode: add tests for g_utf8_normalize() Test corner cases and some examples from Unicode Standard Annex #15 http://unicode.org/reports/tr15/ --- glib/tests/unicode.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/glib/tests/unicode.c b/glib/tests/unicode.c index d4955c6d1..5bb33d91e 100644 --- a/glib/tests/unicode.c +++ b/glib/tests/unicode.c @@ -1889,6 +1889,45 @@ test_iso15924 (void) #undef PACK } +static void +test_normalize (void) +{ + guint i; + typedef struct + { + const gchar *str; + const gchar *nfd; + const gchar *nfc; + const gchar *nfkd; + const gchar *nfkc; + } Test; + Test tests[] = { + { "Äffin", "A\u0308ffin", "Äffin", "A\u0308ffin", "Äffin" }, + { "Ä\uFB03n", "A\u0308\uFB03n", "Ä\uFB03n", "A\u0308ffin", "Äffin" }, + { "Henry IV", "Henry IV", "Henry IV", "Henry IV", "Henry IV" }, + { "Henry \u2163", "Henry \u2163", "Henry \u2163", "Henry IV", "Henry IV" }, + { "non-utf\x88", NULL, NULL, NULL, NULL }, + { "", "", "", "", "" }, + }; + +#define TEST(str, mode, expected) \ + { \ + gchar *normalized = g_utf8_normalize (str, -1, mode); \ + g_assert_cmpstr (normalized, ==, expected); \ + g_free (normalized); \ + } + + for (i = 0; i < G_N_ELEMENTS (tests); i++) + { + TEST (tests[i].str, G_NORMALIZE_NFD, tests[i].nfd); + TEST (tests[i].str, G_NORMALIZE_NFC, tests[i].nfc); + TEST (tests[i].str, G_NORMALIZE_NFKD, tests[i].nfkd); + TEST (tests[i].str, G_NORMALIZE_NFKC, tests[i].nfkc); + } + +#undef TEST +} + int main (int argc, char *argv[]) @@ -1933,6 +1972,7 @@ main (int argc, g_test_add_func ("/unicode/xdigit", test_xdigit); g_test_add_func ("/unicode/xdigit-value", test_xdigit_value); g_test_add_func ("/unicode/zero-width", test_zerowidth); + g_test_add_func ("/unicode/normalize", test_normalize); return g_test_run(); }