tests: Refactor g_unichar_fully_decompose() test to make it extensible

This introduces no functional changes, but allows the test to be easily
extended, in the following commit, to test restricted `result_len`
sizes.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>
This commit is contained in:
Philip Withnall 2024-10-21 18:45:58 +01:00
parent 6eb648de1c
commit 84e3a9cde9
No known key found for this signature in database
GPG Key ID: C5C42CFB268637CA

View File

@ -1549,50 +1549,65 @@ test_decompose (void)
static void static void
test_fully_decompose_canonical (void) test_fully_decompose_canonical (void)
{ {
gunichar decomp[5]; const struct
gsize len; {
gunichar input;
size_t expected_len;
gunichar expected_decomposition[4];
}
vectors[] =
{
#define TEST0(ch) { ch, 1, { ch, 0, 0, 0 }}
#define TEST1(ch, a) { ch, 1, { a, 0, 0, 0 }}
#define TEST2(ch, a, b) { ch, 2, { a, b, 0, 0 }}
#define TEST3(ch, a, b, c) { ch, 3, { a, b, c, 0 }}
#define TEST4(ch, a, b, c, d) { ch, 4, { a, b, c, d }}
#define TEST_DECOMP(ch, expected_len, a, b, c, d) \ /* Not decomposable */
len = g_unichar_fully_decompose (ch, FALSE, decomp, G_N_ELEMENTS (decomp)); \ TEST0 (0x0041),
g_assert_cmpint (expected_len, ==, len); \ TEST0 (0xFB01),
if (expected_len >= 1) g_assert_cmphex (decomp[0], ==, a); \
if (expected_len >= 2) g_assert_cmphex (decomp[1], ==, b); \
if (expected_len >= 3) g_assert_cmphex (decomp[2], ==, c); \
if (expected_len >= 4) g_assert_cmphex (decomp[3], ==, d); \
#define TEST0(ch) TEST_DECOMP (ch, 1, ch, 0, 0, 0) /* Singletons */
#define TEST1(ch, a) TEST_DECOMP (ch, 1, a, 0, 0, 0) TEST2 (0x212B, 0x0041, 0x030A),
#define TEST2(ch, a, b) TEST_DECOMP (ch, 2, a, b, 0, 0) TEST1 (0x2126, 0x03A9),
#define TEST3(ch, a, b, c) TEST_DECOMP (ch, 3, a, b, c, 0)
#define TEST4(ch, a, b, c, d) TEST_DECOMP (ch, 4, a, b, c, d)
/* Not decomposable */ /* Tricky pairs */
TEST0 (0x0041); TEST2 (0x0344, 0x0308, 0x0301),
TEST0 (0xFB01); TEST2 (0x0F73, 0x0F71, 0x0F72),
/* Singletons */ /* General */
TEST2 (0x212B, 0x0041, 0x030A); TEST2 (0x00C5, 0x0041, 0x030A),
TEST1 (0x2126, 0x03A9); TEST2 (0x00F4, 0x006F, 0x0302),
TEST3 (0x1E69, 0x0073, 0x0323, 0x0307),
TEST2 (0x1E63, 0x0073, 0x0323),
TEST2 (0x1E0B, 0x0064, 0x0307),
TEST2 (0x1E0D, 0x0064, 0x0323),
/* Tricky pairs */ /* Hangul */
TEST2 (0x0344, 0x0308, 0x0301); TEST3 (0xD4DB, 0x1111, 0x1171, 0x11B6),
TEST2 (0x0F73, 0x0F71, 0x0F72); TEST2 (0xD4CC, 0x1111, 0x1171),
TEST3 (0xCE31, 0x110E, 0x1173, 0x11B8),
TEST2 (0xCE20, 0x110E, 0x1173),
/* General */ #undef TEST4
TEST2 (0x00C5, 0x0041, 0x030A); #undef TEST3
TEST2 (0x00F4, 0x006F, 0x0302); #undef TEST2
TEST3 (0x1E69, 0x0073, 0x0323, 0x0307); #undef TEST1
TEST2 (0x1E63, 0x0073, 0x0323); #undef TEST0
TEST2 (0x1E0B, 0x0064, 0x0307); };
TEST2 (0x1E0D, 0x0064, 0x0323);
/* Hangul */ for (size_t i = 0; i < G_N_ELEMENTS (vectors); i++)
TEST3 (0xD4DB, 0x1111, 0x1171, 0x11B6); {
TEST2 (0xD4CC, 0x1111, 0x1171); gunichar decomp[5];
TEST3 (0xCE31, 0x110E, 0x1173, 0x11B8); size_t len;
TEST2 (0xCE20, 0x110E, 0x1173);
#undef TEST_DECOMP g_test_message ("Fully decomposing U+%06x; expecting %" G_GSIZE_FORMAT " codepoints",
vectors[i].input, vectors[i].expected_len);
len = g_unichar_fully_decompose (vectors[i].input, FALSE, decomp, G_N_ELEMENTS (decomp));
g_assert_cmpmem (decomp, len * sizeof (*decomp),
vectors[i].expected_decomposition, vectors[i].expected_len * sizeof (*vectors[i].expected_decomposition));
}
} }
/* Test that g_unicode_canonical_decomposition() returns the correct /* Test that g_unicode_canonical_decomposition() returns the correct