-
Notifications
You must be signed in to change notification settings - Fork 1
/
tests.c
182 lines (158 loc) · 3.55 KB
/
tests.c
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include "minunit.h"
#include "cansid.h"
#define feed(x) mu_assert_int_eq('\0', cansid_process(&state, x).ascii)
#define yields_self(x) mu_assert_int_eq(x, cansid_process(&state, x).ascii)
#define state(x) mu_assert_int_eq(CANSID_ ## x, state.state)
#define causes_reset(x) do { mu_assert_int_eq(x, cansid_process(&state, x).ascii); state(ESC); mu_assert_int_eq(state.style, state.next_style); } while (0)
struct cansid_state state;
void test_setup(void) {
state = cansid_init();
}
MU_TEST(init) {
mu_assert_int_eq(state.style, 0x0F);
mu_assert_int_eq(state.next_style, state.style);
state(ESC);
}
MU_TEST(state_change) {
feed('\x1B'); state(BRACKET);
feed('['); state(PARSE);
feed('3'); state(FGCOLOR);
feed('1'); state(ENDVAL);
feed(';'); state(PARSE);
feed('4'); state(BGCOLOR);
feed('2'); state(ENDVAL);
feed(';'); state(PARSE);
feed('='); state(EQUALS);
feed('1'); state(ENDVAL);
feed('m'); state(ESC);
}
MU_TEST(fail_parse) {
feed('\x1B');
causes_reset(']');
feed('\x1B');
feed('[');
causes_reset('m');
feed('\x1B');
feed('[');
causes_reset(';');
feed('\x1B');
feed('[');
causes_reset('2');
feed('\x1B');
feed('[');
causes_reset('a');
feed('\x1B');
feed('[');
feed('3');
feed('1');
causes_reset('1');
feed('\x1B');
feed('[');
feed('3');
feed('1');
causes_reset('[');
feed('\x1B');
feed('[');
feed('=');
causes_reset('=');
feed('\x1B');
feed('[');
feed('=');
causes_reset(';');
}
MU_TEST(succeed_parse) {
yields_self('m');
yields_self('[');
yields_self(';');
yields_self('a');
feed('\x1B');
feed('[');
feed('3');
feed('1');
feed('m');
yields_self('m');
yields_self('[');
yields_self(';');
yields_self('a');
}
MU_TEST(fg_colors) {
const unsigned char lookup_table[8] = { 0, 4, 2, 6, 1, 5, 3, 7 };
for (int i = 0; i < 8; i++) {
feed('\x1B');
feed('[');
feed('3');
feed(i + '0');
mu_assert_int_eq(lookup_table[i], state.next_style & 0x07);
unsigned char style_temp = state.style;
feed('m');
mu_assert_int_eq(lookup_table[i], state.style & 0x07);
mu_assert_int_eq(lookup_table[i], cansid_process(&state, 'm').style & 0x07);
mu_check(state.style != style_temp);
}
}
MU_TEST(bg_colors) {
const unsigned char lookup_table[8] = { 0, 4, 2, 6, 1, 5, 3, 7 };
state.style = 0x10; // Otherwise the mu_check fails on the first time
for (int i = 0; i < 8; i++) {
feed('\x1B');
feed('[');
feed('4');
feed(i + '0');
mu_assert_int_eq(lookup_table[i] << 4, state.next_style & 0xF0);
unsigned char style_temp = state.style;
feed('m');
mu_assert_int_eq(lookup_table[i] << 4, state.style & 0xF0);
mu_assert_int_eq(lookup_table[i] << 4, cansid_process(&state, 'm').style & 0xF0);
mu_check(state.style != style_temp);
}
}
MU_TEST(reset_state) {
feed('\x1B');
feed('[');
feed('3');
feed('1');
feed(';');
feed('4');
feed('1');
feed('m');
mu_assert_int_eq(4 | (1 << 3) | (4 << 4), state.style);
feed('\x1B');
feed('[');
feed('0');
feed('m');
mu_assert_int_eq(0x0F, state.style);
}
MU_TEST(bright_bit) {
state.style = 0x00;
feed('\x1B');
feed('[');
feed('3');
feed('1');
feed(';');
feed('1');
feed('m');
mu_assert_int_eq(4 | (1 << 3), state.style);
feed('\x1B');
feed('[');
feed('3');
feed('1');
feed(';');
feed('=');
feed('1');
feed('m');
}
MU_TEST_SUITE(test_suite) {
MU_RUN_TEST(init);
MU_RUN_TEST(state_change);
MU_RUN_TEST(fail_parse);
MU_RUN_TEST(succeed_parse);
MU_RUN_TEST(fg_colors);
MU_RUN_TEST(bg_colors);
MU_RUN_TEST(reset_state);
}
int main(int argc, char *argv[]) {
MU_SUITE_CONFIGURE(test_setup, NULL);
MU_RUN_SUITE(test_suite);
MU_REPORT();
return minunit_fail != 0;
}