diff options
| author | Eugene Triguba <hello@eugenetriguba.com> | 2025-10-11 14:19:39 -0400 |
|---|---|---|
| committer | Eugene Triguba <hello@eugenetriguba.com> | 2025-10-11 14:31:37 -0400 |
| commit | d3ec525b7d6ecd8de064d3f3a54d112769d1d3b3 (patch) | |
| tree | 935b51f95afb513604c82c9e1efca223af60c109 /tests/test_utf8.c | |
| parent | Fix git clone link in readme (diff) | |
| download | cli-d3ec525b7d6ecd8de064d3f3a54d112769d1d3b3.zip | |
Add unicode support to justify
Diffstat (limited to 'tests/test_utf8.c')
| -rw-r--r-- | tests/test_utf8.c | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/tests/test_utf8.c b/tests/test_utf8.c new file mode 100644 index 0000000..623b607 --- /dev/null +++ b/tests/test_utf8.c @@ -0,0 +1,118 @@ +#include "utf8.h" +// clang-format off +#include <stdarg.h> +#include <stddef.h> +#include <setjmp.h> +#include <cmocka.h> +// 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); +} |