]> git.ipfire.org Git - thirdparty/git.git/commitdiff
t/unit-tests: add UTF-8 width tests for CJK chars
authorJiang Xin <worldhello.net@gmail.com>
Sat, 15 Nov 2025 13:36:10 +0000 (08:36 -0500)
committerJunio C Hamano <gitster@pobox.com>
Mon, 17 Nov 2025 00:04:24 +0000 (16:04 -0800)
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 <worldhello.net@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Makefile
t/meson.build
t/unit-tests/u-utf8-width.c [new file with mode: 0644]

index 7e0f77e2988e3b101dbf008a11b35cd0780d1aef..2a675461540c2642df3fd5112a0c486fbb153ace 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1525,6 +1525,7 @@ CLAR_TEST_SUITES += u-string-list
 CLAR_TEST_SUITES += u-strvec
 CLAR_TEST_SUITES += u-trailer
 CLAR_TEST_SUITES += u-urlmatch-normalization
+CLAR_TEST_SUITES += u-utf8-width
 CLAR_TEST_PROG = $(UNIT_TEST_BIN)/unit-tests$(X)
 CLAR_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(CLAR_TEST_SUITES))
 CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/clar/clar.o
index a5531df415ffe2cda8ff81fd688e9cb1e6717ab4..dc43d69636d15cdbb990bfa38a9289526ef2c818 100644 (file)
@@ -24,6 +24,7 @@ clar_test_suites = [
   'unit-tests/u-strvec.c',
   'unit-tests/u-trailer.c',
   'unit-tests/u-urlmatch-normalization.c',
+  'unit-tests/u-utf8-width.c',
 ]
 
 clar_sources = [
diff --git a/t/unit-tests/u-utf8-width.c b/t/unit-tests/u-utf8-width.c
new file mode 100644 (file)
index 0000000..3766f19
--- /dev/null
@@ -0,0 +1,97 @@
+#include "unit-test.h"
+#include "utf8.h"
+#include "strbuf.h"
+
+/*
+ * Test utf8_strnwidth with various Chinese strings
+ * Chinese characters typically have a width of 2 columns when displayed
+ */
+void test_utf8_width__strnwidth_chinese(void)
+{
+       const char *str;
+
+       /* Test basic ASCII - each character should have width 1 */
+       cl_assert_equal_i(5, utf8_strnwidth("Hello", 5, 0));
+       /* skip_ansi = 1 */
+       cl_assert_equal_i(5, utf8_strnwidth("Hello", 5, 1));
+
+       /* Test simple Chinese characters - each should have width 2 */
+       /* "你好" is 6 bytes (3 bytes per char in UTF-8), 4 display columns */
+       cl_assert_equal_i(4, utf8_strnwidth("你好", 6, 0));
+
+       /* Test mixed ASCII and Chinese - ASCII = 1 column, Chinese = 2 columns */
+       /* "h"(1) + "i"(1) + "你"(2) + "好"(2) = 6 */
+       cl_assert_equal_i(6, utf8_strnwidth("Hi你好", 8, 0));
+
+       /* Test longer Chinese string */
+       /* 5 Chinese chars = 10 display columns */
+       cl_assert_equal_i(10, utf8_strnwidth("你好世界!", 15, 0));
+
+       /* Test individual Chinese character width */
+       cl_assert_equal_i(2, utf8_strnwidth("中", 3, 0));
+
+       /* Test empty string */
+       cl_assert_equal_i(0, utf8_strnwidth("", 0, 0));
+
+       /* Test length limiting */
+       str = "你好世界";
+       /* Only first char "你"(2 columns) within 3 bytes */
+       cl_assert_equal_i(2, utf8_strnwidth(str, 3, 0));
+       /* First two chars "你好"(4 columns) in 6 bytes */
+       cl_assert_equal_i(4, utf8_strnwidth(str, 6, 0));
+}
+
+/*
+ * Tests for utf8_strwidth (simpler version without length limit)
+ */
+void test_utf8_width__strwidth_chinese(void)
+{
+       /* Test basic ASCII */
+       cl_assert_equal_i(5, utf8_strwidth("Hello"));
+
+       /* Test Chinese characters */
+       /* 2 Chinese chars = 4 display columns */
+       cl_assert_equal_i(4, utf8_strwidth("你好"));
+
+       /* Test longer Chinese string */
+       /* 5 Chinese chars = 10 display columns */
+       cl_assert_equal_i(10, utf8_strwidth("你好世界!"));
+
+       /* Test mixed ASCII and Chinese */
+       /* 5 ASCII (5 cols) + 2 Chinese (4 cols) = 9 */
+       cl_assert_equal_i(9, utf8_strwidth("Hello世界"));
+       /* 2 ASCII (2 cols) + 2 Chinese (4 cols) + 1 ASCII (1 col) = 7 */
+       cl_assert_equal_i(7, utf8_strwidth("Hi世界!"));
+}
+
+/*
+ * Additional tests with other East Asian characters
+ */
+void test_utf8_width__strnwidth_japanese_korean(void)
+{
+       /* Japanese characters (should also be 2 columns each) */
+       /* 5 Japanese chars x 2 cols each = 10 display columns */
+       cl_assert_equal_i(10, utf8_strnwidth("こんにちは", 15, 0));
+
+       /* Korean characters (should also be 2 columns each) */
+       /* 5 Korean chars x 2 cols each = 10 display columns */
+       cl_assert_equal_i(10, utf8_strnwidth("안녕하세요", 15, 0));
+}
+
+/*
+ * Test utf8_strnwidth with CJK strings and ANSI sequences
+ */
+void test_utf8_width__strnwidth_cjk_with_ansi(void)
+{
+       /* Test CJK with ANSI sequences */
+       const char *ansi_test = "\033[1m你好\033[0m";
+       int width = utf8_strnwidth(ansi_test, strlen(ansi_test), 1);
+       /* Should skip ANSI sequences and count "你好" as 4 columns */
+       cl_assert_equal_i(4, width);
+
+       /* Test mixed ASCII, CJK, and ANSI */
+       ansi_test = "Hello\033[32m世界\033[0m!";
+       width = utf8_strnwidth(ansi_test, strlen(ansi_test), 1);
+       /* "Hello"(5) + "世界"(4) + "!"(1) = 10 */
+       cl_assert_equal_i(10, width);
+}