Skip to content

Commit 625c781

Browse files
committed
t/unit-tests: Add UTF-8 width tests for CJK chars
This commit adds a new test suite (u-utf8-width.c) to test the UTF-8 width functions in Git, particularly focusing on multi-byte characters from East Asian languages like Chinese, Japanese, and Korean that typically require 2 display columns per character. The test suite includes: - Tests for utf8_strnwidth with Chinese strings - Tests for utf8_strwidth with Chinese strings - Tests for Japanese and Korean characters - Edge case tests with invalid UTF-8 sequences - Proper test function naming following the Clar framework convention Also updated the build configuration in Makefile and meson.build to include the new test suite in the build process. Change-Id: I5a3e2f0a12eed9b8c5dae855a78ddc30b3549336 Co-developed-by: Claude <noreply@anthropic.com> Signed-off-by: Jiang Xin <worldhello.net@gmail.com>
1 parent 56f7db5 commit 625c781

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1525,6 +1525,7 @@ CLAR_TEST_SUITES += u-string-list
15251525
CLAR_TEST_SUITES += u-strvec
15261526
CLAR_TEST_SUITES += u-trailer
15271527
CLAR_TEST_SUITES += u-urlmatch-normalization
1528+
CLAR_TEST_SUITES += u-utf8-width
15281529
CLAR_TEST_PROG = $(UNIT_TEST_BIN)/unit-tests$(X)
15291530
CLAR_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(CLAR_TEST_SUITES))
15301531
CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/clar/clar.o

t/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ clar_test_suites = [
2424
'unit-tests/u-strvec.c',
2525
'unit-tests/u-trailer.c',
2626
'unit-tests/u-urlmatch-normalization.c',
27+
'unit-tests/u-utf8-width.c',
2728
]
2829

2930
clar_sources = [

t/unit-tests/u-utf8-width.c

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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

Comments
 (0)