|
| 1 | +#include "unit-test.h" |
| 2 | +#include "utf8.h" |
| 3 | +#include "strbuf.h" |
| 4 | + |
| 5 | +/* |
| 6 | + * Test utf8_strnwidth with various Chinese strings |
| 7 | + * Chinese characters typically have a width of 2 columns when displayed |
| 8 | + */ |
| 9 | +void test_utf8_width__strnwidth_chinese(void) |
| 10 | +{ |
| 11 | + const char *ansi_test; |
| 12 | + const char *str; |
| 13 | + |
| 14 | + /* Test basic ASCII - each character should have width 1 */ |
| 15 | + cl_assert_equal_i(5, utf8_strnwidth("hello", 5, 0)); |
| 16 | + cl_assert_equal_i(5, utf8_strnwidth("hello", 5, 1)); /* skip_ansi = 1 */ |
| 17 | + |
| 18 | + /* Test simple Chinese characters - each should have width 2 */ |
| 19 | + cl_assert_equal_i(4, utf8_strnwidth("你好", 6, 0)); /* "你好" is 6 bytes (3 bytes per char in UTF-8), 4 display columns */ |
| 20 | + |
| 21 | + /* Test mixed ASCII and Chinese - ASCII = 1 column, Chinese = 2 columns */ |
| 22 | + cl_assert_equal_i(6, utf8_strnwidth("hi你好", 8, 0)); /* "h"(1) + "i"(1) + "你"(2) + "好"(2) = 6 */ |
| 23 | + |
| 24 | + /* Test longer Chinese string */ |
| 25 | + cl_assert_equal_i(10, utf8_strnwidth("你好世界!", 15, 0)); /* 5 Chinese chars = 10 display columns */ |
| 26 | + |
| 27 | + /* Test with skip_ansi = 1 to make sure it works with escape sequences */ |
| 28 | + ansi_test = "\033[31m你好\033[0m"; |
| 29 | + cl_assert_equal_i(4, utf8_strnwidth(ansi_test, strlen(ansi_test), 1)); /* Skip escape sequences, just count "你好" which should be 4 columns */ |
| 30 | + |
| 31 | + /* Test individual Chinese character width */ |
| 32 | + cl_assert_equal_i(2, utf8_strnwidth("中", 3, 0)); /* Single Chinese char should be 2 columns */ |
| 33 | + |
| 34 | + /* Test empty string */ |
| 35 | + cl_assert_equal_i(0, utf8_strnwidth("", 0, 0)); |
| 36 | + |
| 37 | + /* Test length limiting */ |
| 38 | + str = "你好世界"; |
| 39 | + cl_assert_equal_i(2, utf8_strnwidth(str, 3, 0)); /* Only first char "你"(2 columns) within 3 bytes */ |
| 40 | + cl_assert_equal_i(4, utf8_strnwidth(str, 6, 0)); /* First two chars "你好"(4 columns) in 6 bytes */ |
| 41 | +} |
| 42 | + |
| 43 | +/* |
| 44 | + * Tests for utf8_strwidth (simpler version without length limit) |
| 45 | + */ |
| 46 | +void test_utf8_width__strwidth_chinese(void) |
| 47 | +{ |
| 48 | + /* Test basic ASCII */ |
| 49 | + cl_assert_equal_i(5, utf8_strwidth("hello")); |
| 50 | + |
| 51 | + /* Test Chinese characters */ |
| 52 | + cl_assert_equal_i(4, utf8_strwidth("你好")); /* 2 Chinese chars = 4 display columns */ |
| 53 | + |
| 54 | + /* Test mixed ASCII and Chinese */ |
| 55 | + cl_assert_equal_i(9, utf8_strwidth("hello世界")); /* 5 ASCII (5 cols) + 2 Chinese (4 cols) = 9 */ |
| 56 | + cl_assert_equal_i(7, utf8_strwidth("hi世界!")); /* 2 ASCII (2 cols) + 2 Chinese (4 cols) + 1 ASCII (1 col) = 7 */ |
| 57 | +} |
| 58 | + |
| 59 | +/* |
| 60 | + * Additional tests with other East Asian characters |
| 61 | + */ |
| 62 | +void test_utf8_width__strnwidth_japanese_korean(void) |
| 63 | +{ |
| 64 | + /* Japanese characters (should also be 2 columns each) */ |
| 65 | + cl_assert_equal_i(10, utf8_strnwidth("こんにちは", 15, 0)); /* 5 Japanese chars @ 2 cols each = 10 display columns */ |
| 66 | + |
| 67 | + /* Korean characters (should also be 2 columns each) */ |
| 68 | + cl_assert_equal_i(10, utf8_strnwidth("안녕하세요", 15, 0)); /* 5 Korean chars @ 2 cols each = 10 display columns */ |
| 69 | +} |
| 70 | + |
| 71 | +/* |
| 72 | + * Test edge cases with partial UTF-8 sequences |
| 73 | + */ |
| 74 | +void test_utf8_width__strnwidth_edge_cases(void) |
| 75 | +{ |
| 76 | + const char *invalid; |
| 77 | + unsigned char truncated_bytes[] = {0xe4, 0xbd, 0x00}; /* First 2 bytes of "中" + null */ |
| 78 | + |
| 79 | + /* Test invalid UTF-8 - should fall back to byte count */ |
| 80 | + invalid = "\xff\xfe"; /* Invalid UTF-8 sequence */ |
| 81 | + cl_assert_equal_i(2, utf8_strnwidth(invalid, 2, 0)); /* Should return length if invalid UTF-8 */ |
| 82 | + |
| 83 | + /* Test partial UTF-8 character (truncated) */ |
| 84 | + cl_assert_equal_i(2, utf8_strnwidth((const char*)truncated_bytes, 2, 0)); /* Invalid UTF-8, returns byte count */ |
| 85 | +} |
0 commit comments