#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); }