aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_utf8.c
blob: 623b6071473e49d8ccda3c52525d2e87c1c2382b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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);
}