Skip to content

Commit 9a1381f

Browse files
committed
t/unit-tests: add UTF-8 width tests for CJK chars
The file "builtin/repo.c" uses utf8_strwidth() to calculate the display width of UTF-8 characters in a table, but the resulting output is still misaligned. Add test cases for both utf8_strwidth and utf8_strnwidth to verify that they correctly compute the display width for UTF-8 characters. Also updated the build configuration in Makefile and meson.build to include the new test suite in the build process. Signed-off-by: Jiang Xin <[email protected]>
1 parent 56f7db5 commit 9a1381f

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-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: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
/* skip_ansi = 1 */
17+
cl_assert_equal_i(5, utf8_strnwidth("Hello", 5, 1));
18+
19+
/* Test simple Chinese characters - each should have width 2 */
20+
/* "你好" is 6 bytes (3 bytes per char in UTF-8), 4 display columns */
21+
cl_assert_equal_i(4, utf8_strnwidth("你好", 6, 0));
22+
23+
/* Test mixed ASCII and Chinese - ASCII = 1 column, Chinese = 2 columns */
24+
/* "h"(1) + "i"(1) + "你"(2) + "好"(2) = 6 */
25+
cl_assert_equal_i(6, utf8_strnwidth("Hi你好", 8, 0));
26+
27+
/* Test longer Chinese string */
28+
/* 5 Chinese chars = 10 display columns */
29+
cl_assert_equal_i(10, utf8_strnwidth("你好世界!", 15, 0));
30+
31+
/* Test with skip_ansi = 1 to make sure it works with escape sequences */
32+
ansi_test = "\033[31m你好\033[0m";
33+
/* Skip escape sequences, just count "你好" which should be 4 columns */
34+
cl_assert_equal_i(4, utf8_strnwidth(ansi_test, strlen(ansi_test), 1));
35+
36+
/* Test individual Chinese character width */
37+
cl_assert_equal_i(2, utf8_strnwidth("中", 3, 0));
38+
39+
/* Test empty string */
40+
cl_assert_equal_i(0, utf8_strnwidth("", 0, 0));
41+
42+
/* Test length limiting */
43+
str = "你好世界";
44+
/* Only first char "你"(2 columns) within 3 bytes */
45+
cl_assert_equal_i(2, utf8_strnwidth(str, 3, 0));
46+
/* First two chars "你好"(4 columns) in 6 bytes */
47+
cl_assert_equal_i(4, utf8_strnwidth(str, 6, 0));
48+
}
49+
50+
/*
51+
* Tests for utf8_strwidth (simpler version without length limit)
52+
*/
53+
void test_utf8_width__strwidth_chinese(void)
54+
{
55+
/* Test basic ASCII */
56+
cl_assert_equal_i(5, utf8_strwidth("Hello"));
57+
58+
/* Test Chinese characters */
59+
/* 2 Chinese chars = 4 display columns */
60+
cl_assert_equal_i(4, utf8_strwidth("你好"));
61+
62+
/* Test mixed ASCII and Chinese */
63+
/* 5 ASCII (5 cols) + 2 Chinese (4 cols) = 9 */
64+
cl_assert_equal_i(9, utf8_strwidth("Hello世界"));
65+
/* 2 ASCII (2 cols) + 2 Chinese (4 cols) + 1 ASCII (1 col) = 7 */
66+
cl_assert_equal_i(7, utf8_strwidth("Hi世界!"));
67+
}
68+
69+
/*
70+
* Additional tests with other East Asian characters
71+
*/
72+
void test_utf8_width__strnwidth_japanese_korean(void)
73+
{
74+
/* Japanese characters (should also be 2 columns each) */
75+
/* 5 Japanese chars x 2 cols each = 10 display columns */
76+
cl_assert_equal_i(10, utf8_strnwidth("こんにちは", 15, 0));
77+
78+
/* Korean characters (should also be 2 columns each) */
79+
/* 5 Korean chars x 2 cols each = 10 display columns */
80+
cl_assert_equal_i(10, utf8_strnwidth("안녕하세요", 15, 0));
81+
}
82+
83+
/*
84+
* Test utf8_strnwidth with CJK strings and ANSI sequences
85+
*/
86+
void test_utf8_width__strnwidth_cjk_with_ansi(void)
87+
{
88+
/* Test CJK with ANSI sequences */
89+
const char *test_str = "\033[1m你好\033[0m";
90+
int width = utf8_strnwidth(test_str, strlen(test_str), 1);
91+
/* Should skip ANSI sequences and count "你好" as 4 columns */
92+
cl_assert_equal_i(4, width);
93+
94+
/* Test mixed ASCII, CJK, and ANSI */
95+
test_str = "Hello\033[32m世界\033[0m!";
96+
width = utf8_strnwidth(test_str, strlen(test_str), 1);
97+
/* "Hello"(5) + "世界"(4) + "!"(1) = 10 */
98+
cl_assert_equal_i(10, width);
99+
}

0 commit comments

Comments
 (0)