File size: 5,681 Bytes
78d2150
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include "../../unity/unity.h"
#include <stdint.h>
#include <stdbool.h>
#include <string.h>

/* Use the in-translation-unit symbols:
   - static enum { COUNT_COLUMNS, COUNT_BYTES, COUNT_CHARACTERS } counting_mode;
   - static int last_character_width;
   - static size_t adjust_column (size_t column, mcel_t g);
   - #define TAB_WIDTH 8
   These are defined earlier in fold.c and are visible here because
   this file is included at the end of that translation unit. */

static mcel_t make_mcel(uint32_t ch, int len)
{
    mcel_t g;
    /* Zero-initialize in case mcel_t has other fields. */
    memset(&g, 0, sizeof(g));
    g.ch = ch;
    g.len = len;
    return g;
}

void setUp(void) {
    /* Reset to predictable defaults before each test. */
    counting_mode = COUNT_COLUMNS;
    last_character_width = 0;
}

void tearDown(void) {
    /* Nothing to cleanup. */
}

/* COUNT_COLUMNS: ordinary printable character should add its display width (1). */
void test_adjust_column_columns_regular_char_increments_and_sets_last_width(void)
{
    size_t start = 0;
    mcel_t g = make_mcel('a', 1);
    size_t col = adjust_column(start, g);
    TEST_ASSERT_EQUAL_UINT64(1, col);
    TEST_ASSERT_EQUAL_INT(1, last_character_width);
}

/* COUNT_COLUMNS: backspace subtracts last_character_width if column > 0. */
void test_adjust_column_backspace_decrements_by_last_width_when_column_gt0(void)
{
    size_t start = 5;
    last_character_width = 3; /* Simulate previous wide character */
    mcel_t g = make_mcel('\b', 1);
    size_t col = adjust_column(start, g);
    TEST_ASSERT_EQUAL_UINT64(2, col);
    /* last_character_width unchanged in this branch */
    TEST_ASSERT_EQUAL_INT(3, last_character_width);
}

/* COUNT_COLUMNS: backspace at column 0 must not underflow. */
void test_adjust_column_backspace_no_effect_when_column_zero(void)
{
    size_t start = 0;
    last_character_width = 4;
    mcel_t g = make_mcel('\b', 1);
    size_t col = adjust_column(start, g);
    TEST_ASSERT_EQUAL_UINT64(0, col);
    TEST_ASSERT_EQUAL_INT(4, last_character_width);
}

/* COUNT_COLUMNS: carriage return resets column to 0. */
void test_adjust_column_carriage_return_resets_column(void)
{
    size_t start = 10;
    mcel_t g = make_mcel('\r', 1);
    size_t col = adjust_column(start, g);
    TEST_ASSERT_EQUAL_UINT64(0, col);
}

/* COUNT_COLUMNS: tab advances to next tab stop from 0 (-> 8). */
void test_adjust_column_tab_from_zero_advances_to_8(void)
{
    size_t start = 0;
    mcel_t g = make_mcel('\t', 1);
    size_t col = adjust_column(start, g);
    TEST_ASSERT_EQUAL_UINT64(TAB_WIDTH, col);
}

/* COUNT_COLUMNS: tab advances to next tab stop from 7 (-> 8). */
void test_adjust_column_tab_from_7_advances_to_8(void)
{
    size_t start = 7;
    mcel_t g = make_mcel('\t', 1);
    size_t col = adjust_column(start, g);
    TEST_ASSERT_EQUAL_UINT64(8, col);
}

/* COUNT_COLUMNS: tab advances to next tab stop from 8 (-> 16). */
void test_adjust_column_tab_from_8_advances_to_16(void)
{
    size_t start = 8;
    mcel_t g = make_mcel('\t', 1);
    size_t col = adjust_column(start, g);
    TEST_ASSERT_EQUAL_UINT64(16, col);
}

/* COUNT_CHARACTERS: regardless of the character, counts as 1 and sets last_character_width=1. */
void test_adjust_column_characters_mode_counts_one_and_sets_last_width(void)
{
    counting_mode = COUNT_CHARACTERS;
    size_t start = 3;
    /* Use any code point; here 'X'. */
    mcel_t g = make_mcel('X', 1);
    size_t col = adjust_column(start, g);
    TEST_ASSERT_EQUAL_UINT64(4, col);
    TEST_ASSERT_EQUAL_INT(1, last_character_width);
}

/* COUNT_BYTES: counts len, ignores control semantics; last_character_width preserved. */
void test_adjust_column_bytes_mode_counts_len_and_preserves_last_width(void)
{
    counting_mode = COUNT_BYTES;
    last_character_width = 4; /* Should remain unchanged by COUNT_BYTES path */
    size_t start = 5;
    /* '\t' with len=1 should just add 1 in byte mode. */
    mcel_t g = make_mcel('\t', 1);
    size_t col = adjust_column(start, g);
    TEST_ASSERT_EQUAL_UINT64(6, col);
    TEST_ASSERT_EQUAL_INT(4, last_character_width);
}

/* COUNT_BYTES: multi-byte len added to column. */
void test_adjust_column_bytes_mode_multibyte_len(void)
{
    counting_mode = COUNT_BYTES;
    size_t start = 2;
    mcel_t g = make_mcel('x', 4);
    size_t col = adjust_column(start, g);
    TEST_ASSERT_EQUAL_UINT64(6, col);
}

/* COUNT_COLUMNS: invalid code point (surrogate) should fall back to width 1. */
void test_adjust_column_invalid_codepoint_defaults_to_one(void)
{
    counting_mode = COUNT_COLUMNS;
    size_t start = 10;
    /* Surrogate range; invalid Unicode scalar -> c32width typically returns <0 */
    mcel_t g = make_mcel(0xD800u, 3);
    size_t col = adjust_column(start, g);
    TEST_ASSERT_EQUAL_UINT64(11, col);
    TEST_ASSERT_EQUAL_INT(1, last_character_width);
}

int main(void)
{
    UNITY_BEGIN();
    RUN_TEST(test_adjust_column_columns_regular_char_increments_and_sets_last_width);
    RUN_TEST(test_adjust_column_backspace_decrements_by_last_width_when_column_gt0);
    RUN_TEST(test_adjust_column_backspace_no_effect_when_column_zero);
    RUN_TEST(test_adjust_column_carriage_return_resets_column);
    RUN_TEST(test_adjust_column_tab_from_zero_advances_to_8);
    RUN_TEST(test_adjust_column_tab_from_7_advances_to_8);
    RUN_TEST(test_adjust_column_tab_from_8_advances_to_16);
    RUN_TEST(test_adjust_column_characters_mode_counts_one_and_sets_last_width);
    RUN_TEST(test_adjust_column_bytes_mode_counts_len_and_preserves_last_width);
    RUN_TEST(test_adjust_column_bytes_mode_multibyte_len);
    RUN_TEST(test_adjust_column_invalid_codepoint_defaults_to_one);
    return UNITY_END();
}