Merge branch 'nfc-nfd-test' into 'main'

tests: Add a test for Unicode normalization

See merge request GNOME/glib!2204
This commit is contained in:
Philip Withnall 2021-08-02 13:17:23 +00:00
commit d6576e9781

View File

@ -1426,8 +1426,54 @@ test_fully_decompose_len (void)
}
}
/* Test that g_unichar_decompose() returns the correct value for various
* ASCII and Unicode alphabetic, numeric, and other, codepoints. */
/* Check various examples from Unicode Annex #15 for NFD and NFC
* normalization.
*/
static void
test_normalization (void)
{
const struct {
const char *source;
const char *nfd;
const char *nfc;
} tests[] = {
// Singletons
{ "\xe2\x84\xab", "A\xcc\x8a", "Å" }, // U+212B ANGSTROM SIGN
{ "\xe2\x84\xa6", "Ω", "Ω" }, // U+2126 OHM SIGN
// Canonical Composites
{ "Å", "A\xcc\x8a", "Å" }, // U+00C5 LATIN CAPITAL LETTER A WITH RING ABOVE
{ "ô", "o\xcc\x82", "ô" }, // U+00F4 LATIN SMALL LETTER O WITH CIRCUMFLEX
// Multiple Combining Marks
{ "\xe1\xb9\xa9", "s\xcc\xa3\xcc\x87", "" }, // U+1E69 LATIN SMALL LETTER S WITH DOT BELOW AND DOT ABOVE
{ "\xe1\xb8\x8b\xcc\xa3", "d\xcc\xa3\xcc\x87", "ḍ̇" },
{ "q\xcc\x87\xcc\xa3", "q\xcc\xa3\xcc\x87", "q̣̇" },
// Compatibility Composites
{ "", "", "" }, // U+FB01 LATIN SMALL LIGATURE FI
{ "2\xe2\x81\xb5", "2\xe2\x81\xb5", "2⁵" },
{ "\xe1\xba\x9b\xcc\xa3", "\xc5\xbf\xcc\xa3\xcc\x87", "ẛ̣" },
// Tests for behavior with reordered marks
{ "s\xcc\x87\xcc\xa3", "s\xcc\xa3\xcc\x87", "" },
{ "α\xcc\x94\xcd\x82", "α\xcc\x94\xcd\x82", "" },
{ "α\xcd\x82\xcc\x94", "α\xcd\x82\xcc\x94", "\xcc\x94" },
};
gsize i;
for (i = 0; i < G_N_ELEMENTS (tests); i++)
{
char *nfd, *nfc;
nfd = g_utf8_normalize (tests[i].source, -1, G_NORMALIZE_NFD);
g_assert_cmpstr (nfd, ==, tests[i].nfd);
nfc = g_utf8_normalize (tests[i].nfd, -1, G_NORMALIZE_NFC);
g_assert_cmpstr (nfc, ==, tests[i].nfc);
g_free (nfd);
g_free (nfc);
}
}
static void
test_iso15924 (void)
{
@ -1674,6 +1720,7 @@ main (int argc,
g_test_add_func ("/unicode/digit-value", test_digit_value);
g_test_add_func ("/unicode/fully-decompose-canonical", test_fully_decompose_canonical);
g_test_add_func ("/unicode/fully-decompose-len", test_fully_decompose_len);
g_test_add_func ("/unicode/normalization", test_normalization);
g_test_add_func ("/unicode/graph", test_graph);
g_test_add_func ("/unicode/iso15924", test_iso15924);
g_test_add_func ("/unicode/lower", test_lower);