-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcdc_test.c
135 lines (105 loc) · 3.29 KB
/
cdc_test.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
#include "cdc.h"
#include "keyboard.h"
#include "core/defs.h"
/*sublime-c-static-fn-hoist-start*/
static int setup(void **ts);
static void test_sets_leds(void **ts);
static void cdc_receive_bytes(uint8_t const *data, size_t len);
static void test_sets_full_keyboard_state(void **ts);
static void test_buffers_keyboard_commands(void **ts);
/*sublime-c-static-fn-hoist-end*/
static struct {
uint8_t led_mask; // active-low
} self;
void LEDs_TurnOnLEDs(const uint8_t mask) {
self.led_mask &= ~mask;
}
void LEDs_TurnOffLEDs(const uint8_t mask) {
self.led_mask |= mask;
}
static int setup(void **ts) {
keyboard_reset();
cdc_reset();
memset(&self, 0, sizeof(self));
self.led_mask = 0xFF; // all off
return 0;
}
static void test_sets_leds(void **ts) {
assert_int_equal(0xFF, self.led_mask);
cdc_receive_bytes((uint8_t []) { ACTION_led_state, 1, 1 }, 3);
assert_int_equal(0xDF, self.led_mask);
cdc_receive_bytes((uint8_t []) { ACTION_led_state, 2, 1 }, 3);
assert_int_equal(0x9F, self.led_mask);
cdc_receive_bytes((uint8_t []) { ACTION_led_state, 1, 0 }, 3);
assert_int_equal(0xBF, self.led_mask);
cdc_receive_bytes((uint8_t []) { ACTION_led_state, 2, 0 }, 3);
assert_int_equal(0xFF, self.led_mask);
}
static void cdc_receive_bytes(uint8_t const *data, size_t len) {
for (size_t i = 0; i < len; i++) {
cdc_receive_byte(data[i]);
}
}
static void test_sets_full_keyboard_state(void **ts) {
cdc_receive_bytes((uint8_t []) {
ACTION_keyboard_full_state,
1, // modifiers
0, 1, 2, 3, 4, 5 // keys, only 4 and 5 are valid
}, 8);
struct keyboard_state_s state;
keyboard_get_state(&state);
assert_int_equal(1, state.modifiers);
assert_int_equal(2, state.key_count);
assert_int_equal(4, state.keys[0]);
assert_int_equal(5, state.keys[1]);
}
static void test_buffers_keyboard_commands(void **ts) {
cdc_receive_bytes((uint8_t []) {
ACTION_keyboard_key_state, 8, 1
}, 3);
cdc_receive_bytes((uint8_t []) {
ACTION_keyboard_full_state,
1, // modifiers
4, 0, 0, 0, 0, 0,
}, 8);
cdc_receive_bytes((uint8_t []) {
ACTION_keyboard_key_state, 9, 1
}, 3);
cdc_receive_bytes((uint8_t []) {
ACTION_keyboard_full_state,
2, // modifiers
5, 0, 0, 0, 0, 0,
}, 8);
struct keyboard_state_s state;
keyboard_get_state(&state);
assert_int_equal(1, state.key_count);
assert_int_equal(0, state.modifiers);
assert_int_equal(8, state.keys[0]);
keyboard_state_unlock();
cdc_update();
keyboard_get_state(&state);
assert_int_equal(1, state.key_count);
assert_int_equal(1, state.modifiers);
assert_int_equal(4, state.keys[0]);
keyboard_state_unlock();
cdc_update();
keyboard_get_state(&state);
assert_int_equal(2, state.key_count);
assert_int_equal(1, state.modifiers);
assert_int_equal(4, state.keys[0]);
assert_int_equal(9, state.keys[1]);
keyboard_state_unlock();
cdc_update();
keyboard_get_state(&state);
assert_int_equal(1, state.key_count);
assert_int_equal(2, state.modifiers);
assert_int_equal(5, state.keys[0]);
}
int main(void) {
const struct CMUnitTest tests[] = {
cmocka_unit_test_setup(test_sets_leds, setup),
cmocka_unit_test_setup(test_sets_full_keyboard_state, setup),
cmocka_unit_test_setup(test_buffers_keyboard_commands, setup),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}