From d3ec525b7d6ecd8de064d3f3a54d112769d1d3b3 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 11 Oct 2025 14:19:39 -0400 Subject: Add unicode support to justify --- tests/test_utf8.c | 118 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 tests/test_utf8.c (limited to 'tests/test_utf8.c') diff --git a/tests/test_utf8.c b/tests/test_utf8.c new file mode 100644 index 0000000..623b607 --- /dev/null +++ b/tests/test_utf8.c @@ -0,0 +1,118 @@ +#include "utf8.h" +// clang-format off +#include +#include +#include +#include +// clang-format on + +/** + * GIVEN an ASCII string + * WHEN we calculate the display width + * THEN we expect the width to equal the byte length + */ +void test_utf8_display_width_ascii(void **state) { + const char *input = "hello"; + size_t expected = 5; + + size_t result = utf8_display_width(input); + + assert_int_equal(result, expected); +} + +/** + * GIVEN a string with accented characters (combining marks) + * WHEN we calculate the display width + * THEN we expect the width to be less than the byte length + */ +void test_utf8_display_width_accented(void **state) { + const char *input = "café"; // é is 2 bytes in UTF-8 + size_t expected = 4; // 4 visible characters + + size_t result = utf8_display_width(input); + + assert_int_equal(result, expected); +} + +/** + * GIVEN a string with East Asian wide characters + * WHEN we calculate the display width + * THEN we expect the width to be double the character count + */ +void test_utf8_display_width_cjk(void **state) { + const char *input = "日本語"; // 3 Japanese characters + size_t expected = 6; // Each CJK character is 2 columns wide + + size_t result = utf8_display_width(input); + + assert_int_equal(result, expected); +} + +/** + * GIVEN a string with mixed ASCII and UTF-8 characters + * WHEN we calculate the display width + * THEN we expect the correct combined width + */ +void test_utf8_display_width_mixed(void **state) { + const char *input = "Hello 世界"; // "Hello " (6) + "世界" (4) + size_t expected = 10; // 6 ASCII + 2 CJK (2 columns each) + + size_t result = utf8_display_width(input); + + assert_int_equal(result, expected); +} + +/** + * GIVEN an empty string + * WHEN we calculate the display width + * THEN we expect width of 0 + */ +void test_utf8_display_width_empty(void **state) { + const char *input = ""; + size_t expected = 0; + + size_t result = utf8_display_width(input); + + assert_int_equal(result, expected); +} + +/** + * GIVEN a NULL pointer + * WHEN we calculate the display width + * THEN we expect width of 0 + */ +void test_utf8_display_width_null(void **state) { + size_t expected = 0; + + size_t result = utf8_display_width(NULL); + + assert_int_equal(result, expected); +} + +/** + * GIVEN a string with various Unicode characters + * WHEN we calculate the display width + * THEN we expect the correct width + */ +void test_utf8_display_width_various_unicode(void **state) { + const char *input = "Zürich"; // ü is 2 bytes in UTF-8 + size_t expected = 6; // 6 visible characters + + size_t result = utf8_display_width(input); + + assert_int_equal(result, expected); +} + +int main(void) { + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_utf8_display_width_ascii), + cmocka_unit_test(test_utf8_display_width_accented), + cmocka_unit_test(test_utf8_display_width_cjk), + cmocka_unit_test(test_utf8_display_width_mixed), + cmocka_unit_test(test_utf8_display_width_empty), + cmocka_unit_test(test_utf8_display_width_null), + cmocka_unit_test(test_utf8_display_width_various_unicode), + }; + + return cmocka_run_group_tests(tests, NULL, NULL); +} -- cgit v1.2.3