-
Notifications
You must be signed in to change notification settings - Fork 0
/
unit.cpp
323 lines (242 loc) · 9.09 KB
/
unit.cpp
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file
#include "catch.hpp"
#include "keycodes.h"
#include "layouts.h"
#include "bounded_array.h"
#include "keyboard_functional.h"
using namespace Z;
TEST_CASE( "Simple bounded array", "[bounded_array]" ) {
BoundedArray<int, 5> array;
REQUIRE_FALSE(array.contains(1));
REQUIRE(array.size() == 0);
array.push(1);
array.push(2);
array.push(3);
REQUIRE_THROWS(array[4]);
REQUIRE(array.size() == 3);
array.push(4);
array.push(5);
REQUIRE(array.contains(1) == true);
REQUIRE(array.contains(8) == false);
for(std::size_t i = 0; i < 5; i++)
{
REQUIRE(i+1 == array[i]);
}
REQUIRE_THROWS(array.push(6));
REQUIRE_THROWS(array[500]);
array.reset();
REQUIRE(array.size() == 0);
CHECK_NOTHROW(array.push(1));
REQUIRE_THROWS(array[2]);
}
TEST_CASE("Bounded array initializer", "[bounded_array]")
{
uint8_t raw_data[] = {1,2,3,4,5};
auto array = BoundedArray<uint8_t, 5>(raw_data);
REQUIRE(array.contains(1));
REQUIRE(array.contains(2));
REQUIRE(array.contains(3));
REQUIRE(array.contains(4));
REQUIRE(array.contains(5));
}
TEST_CASE("Keyboard coordinate equality", "[keyboard_coordinate]")
{
auto coordinate1 = KeyCoordinate(0,0);
auto coordinate2 = KeyCoordinate(0,0);
auto coordinate3 = KeyCoordinate(0,1);
auto coordinate4 = KeyCoordinate(1,1);
auto coordinate5 = KeyCoordinate(1,0);
REQUIRE(coordinate1 == coordinate2);
REQUIRE(coordinate2 != coordinate3);
REQUIRE(coordinate2 != coordinate4);
REQUIRE(coordinate2 != coordinate5);
}
TEST_CASE("Keyboard combination function", "[keyboard]")
{
auto coordinates1 = BoundedArray<KeyCoordinate, 10>();
coordinates1.push(KeyCoordinate(0, 0));
coordinates1.push(KeyCoordinate(1, 0));
coordinates1.push(KeyCoordinate(2, 0));
coordinates1.push(KeyCoordinate(3, 0));
auto coordinates2 = BoundedArray<KeyCoordinate, 10>();
coordinates2.push(KeyCoordinate(0, 0));
coordinates2.push(KeyCoordinate(1, 0));
coordinates2.push(KeyCoordinate(2, 0));
coordinates2.push(KeyCoordinate(5, 0));
auto combine_function = [](KeyCoordinate coord)
{
coord.y += 1;
return coord;
};
auto combined = merge_coordinates(coordinates1, coordinates2, combine_function);
//Contains a coordinate shared by both
REQUIRE(combined.contains(KeyCoordinate(0,0)));
//Contains a coordinate shared by both and translated
REQUIRE(combined.contains(KeyCoordinate(0,1)));
//Contains a coordinate not in the first array
REQUIRE(combined.contains(KeyCoordinate(5,1)));
//Does not contain a coordinate only contained in the second array
REQUIRE_FALSE(combined.contains(KeyCoordinate(5,0)));
//Does not contain a coordinate that shouldn't be in any of the parts
REQUIRE_FALSE(combined.contains(KeyCoordinate(6,1)));
}
TEST_CASE("Keyboard modifier offsets", "[keyboard]")
{
FunctionKey mod = FN_LOWER;
Keycode code = function_keycode(mod);
FunctionKey new_mod = function_from_keycode(code);
REQUIRE(mod == new_mod);
REQUIRE_THROWS(function_from_keycode(0x1000)); //Wrong keymask
REQUIRE_THROWS(function_from_keycode(FN_AMOUNT)); //Modifier index too large
}
TEST_CASE("Simple coverate tests", "[keyboard]")
{
init_function_key_list();
}
TEST_CASE("Keymap lookup", "[keyboard]")
{
//Ensuring that the keymap works as expected
const Keycode raw_keymap[3][4] = {
{1,4,7,10},
{2,5,8,11},
{3,6,9,12}
};
auto keymap = init_keymap<4,3>(raw_keymap);
REQUIRE(keymap[0][0] == 1);
REQUIRE(keymap[2][2] == 9);
REQUIRE(keymap[2][1] == 8);
REQUIRE_THROWS(keymap[0][3]);
REQUIRE_THROWS(keymap[4][0]);
//Testing the keymap lookup function
auto pressed_coords = BoundedArray<KeyCoordinate, 4*3>();
pressed_coords.push(KeyCoordinate(1,0));
pressed_coords.push(KeyCoordinate(3,2));
auto pressed_keys = translate_coordinates<4, 3>(pressed_coords, keymap);
//Ensure the specified keys are in the list
REQUIRE(pressed_keys.contains(4));
REQUIRE(pressed_keys.contains(12));
REQUIRE(pressed_keys.size() == pressed_coords.size());
}
TEST_CASE("Key type test", "[keyboard]")
{
CHECK(get_key_type(0xf000) == KeyType::STANDARD);
CHECK(get_key_type(MODIFIERKEY_RIGHT_ALT) == KeyType::MODIFIER);
CHECK(get_key_type(KEY_A) == KeyType::STANDARD);
CHECK(get_key_type(KEY_SYSTEM_POWER_DOWN) == KeyType::SYSTEM);
CHECK(get_key_type(KEY_MEDIA_MUTE) == KeyType::MEDIA);
CHECK(get_key_type(0x0f00) == KeyType::UNRECOGNISED);
CHECK(get_key_type(FunctionKey::FN_RAISE) == KeyType::FN);
}
TEST_CASE("Keytype translation", "[keyboard]")
{
auto keys = BoundedArray<Keycode, 10>();
keys.push(KEY_A);
keys.push(KEY_B);
keys.push(MODIFIERKEY_RIGHT_ALT);
keys.push(FunctionKey::FN_RAISE);
auto keytypes = keycodes_to_keytypes(keys);
REQUIRE(keytypes.standard_keys.contains(KEY_A));
REQUIRE(keytypes.standard_keys.contains(KEY_B));
REQUIRE(keytypes.standard_keys.size() == 2);
REQUIRE(keytypes.contains_functionkey(FunctionKey::FN_WM) == false);
REQUIRE(keytypes.contains_functionkey(FunctionKey::FN_RAISE) == true);
REQUIRE(keytypes.modifiers.contains(MODIFIERKEY_RIGHT_ALT));
REQUIRE(keytypes.modifiers.size() == 1);
}
TEST_CASE("Test planck layout", "[keyboard]")
{
KeyboardState current_state = KeyboardState::NORMAL;
auto keys = BoundedArray<Keycode, FULL_WIDTH*HEIGHT>();
//Make sure we are in the default layout
REQUIRE(get_current_keymap(current_state)[0][0] == KEY_TAB);
keys.push(FunctionKey::FN_LOWER);
auto keytypes = keycodes_to_keytypes(keys);
current_state = get_new_state(current_state, keytypes);
REQUIRE(get_current_keymap(current_state)[0][0] == KEY_TILDE);
}
TEST_CASE("Generating keyboard packets", "[keyboard packet generation]")
{
//No previous keys and not too many keys
auto keys = BoundedArray<Keycode, 10>();
keys.push(KEY_A);
keys.push(KEY_B);
keys.push(KEY_C);
auto old_packet = KeyPacket();
auto keytypes = keycodes_to_keytypes(keys);
auto packet = keytypes_to_packet(keytypes, old_packet);
CHECK(packet.keys.contains(KEY_A));
CHECK(packet.keys.contains(KEY_B));
CHECK(packet.keys.contains(KEY_C));
CHECK(packet.keys.size() == 3);
CHECK(packet.modifiers == 0);
//Too many keys for one packet, some modifiers, some old, some old removed
keys.reset();
keys.push(KEY_A);
keys.push(KEY_C);
keys.push(KEY_D);
keys.push(KEY_E);
keys.push(KEY_F);
keys.push(KEY_G);
keys.push(KEY_H);
keys.push(MODIFIERKEY_SHIFT);
keys.push(MODIFIERKEY_CTRL);
keytypes = keycodes_to_keytypes(keys);
packet = keytypes_to_packet(keytypes, packet);
CHECK(packet.keys.contains(KEY_A));
CHECK(packet.keys.contains(KEY_C));
CHECK_FALSE(packet.keys.contains(KEY_B));
CHECK(packet.keys.size() == 6);
CHECK(packet.modifiers == (MODIFIERKEY_SHIFT | MODIFIERKEY_CTRL));
}
TEST_CASE("Key message decode", "[key transmission]")
{
uint8_t checksum = 2 + 2 + 1 + 1 + 1 + 2;
uint8_t bytes_raw[11] = {0,0, 2,0, 0,2, 1,1, 1,2, checksum};
auto bytes = BoundedArray<uint8_t, 11>(bytes_raw);
auto decoded = decode_coordinates_from_bytes<5>(bytes);
REQUIRE(decoded.error == CoordsFromBytesError::SUCCESS);
REQUIRE(decoded.keys.contains(KeyCoordinate(0,0)));
REQUIRE(decoded.keys.contains(KeyCoordinate(2,0)));
REQUIRE(decoded.keys.contains(KeyCoordinate(0,2)));
REQUIRE(decoded.keys.contains(KeyCoordinate(1,1)));
REQUIRE(decoded.keys.contains(KeyCoordinate(1,2)));
//Fewer coordinates than the maximum allowed amount
bytes.reset();
checksum = 2;
bytes.push(1);
bytes.push(1);
bytes.push(checksum);
decoded = decode_coordinates_from_bytes<5>(bytes);
REQUIRE(decoded.error == CoordsFromBytesError::SUCCESS);
REQUIRE(decoded.keys.contains(KeyCoordinate(1,1)));
}
TEST_CASE("Malformed message decode", "[key transmission]")
{
{
//Invalid checksum
uint8_t checksum = 1; //should be 2
uint8_t bytes_raw[3] = {1,1, checksum};
auto bytes = BoundedArray<uint8_t, 3>(bytes_raw);
auto decoded = decode_coordinates_from_bytes<1>(bytes);
REQUIRE(decoded.error == CoordsFromBytesError::INVALID_CHECKSUM);
//Missing checksum
bytes.reset();
bytes.push(1);
bytes.push(1);
decoded = decode_coordinates_from_bytes<1>(bytes);
REQUIRE(decoded.error == CoordsFromBytesError::INVALID_AMOUNT_OF_BYTES);
}
}
TEST_CASE("Key message encode", "[key transmission]")
{
auto original_coordinates = BoundedArray<KeyCoordinate, 3>();
original_coordinates.push(KeyCoordinate(3,2));
original_coordinates.push(KeyCoordinate(3,2));
//Leaving one empty to test that
auto decoded = decode_coordinates_from_bytes<3>(coords_to_bytes(original_coordinates));
REQUIRE(decoded.error == CoordsFromBytesError::SUCCESS);
for(int i = 0; i < 2; ++i)
{
REQUIRE(original_coordinates[i] == decoded.keys[i]);
}
}