-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_utils.h
120 lines (101 loc) · 3.35 KB
/
test_utils.h
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
#include "gtest/gtest.h"
#include "schoenberg.h"
#include <unistd.h>
#include <fstream>
using namespace schoenberg;
typedef vector<pair<__u16, string>> TYPE_EVENTS;
vector<string> effective_keystrokes(TYPE_EVENTS event) {
auto res = vector<string>();
for (auto e:event) {
if (e.first == 1) {
res.push_back(e.second);
}
}
return res;
}
pair<Config, State> setup_test() {
auto config = schoenberg::read_config("tst/test.yaml");
return {config, schoenberg::build_state(config)};
}
TYPE_EVENTS process(Config &config, State &state, TYPE_EVENTS events) {
TYPE_EVENTS res;
for (auto eventInput :events) {
input_event i = {
.type = EV_KEY,
.code = (__u16) schoenberg::parse_key(
eventInput.second),
eventInput.first
};
vector<input_event> imm = schoenberg::process_mapping(config, i, cout);
for (auto event: imm) {
auto item_res = schoenberg::process_for_layer(state, event, cout);
for (auto e:item_res) {
//auto entry = pair<__u16, string>{e.value,e.value};
res.push_back({(__u16) e.value, schoenberg::serialize_key(e.code)});
}
}
}
return res;
}
void print_strokes(vector<string> strokes) {
for (const auto s:strokes) {
cout << "=> " << s << endl;
}
}
void print_events(TYPE_EVENTS events) {
for (const auto s:events) {
cout << "key: " << s.second << " value: " << s.first << endl;
}
}
void events_equals(TYPE_EVENTS expected, TYPE_EVENTS actual) {
EXPECT_EQ(expected.size(), actual.size());
if (expected.size() == actual.size()) {
for (int i = 0; i < expected.size(); i++) {
auto expected_item = expected[i];
auto actual_item = actual[i];
EXPECT_EQ(expected_item.first, actual_item.first);
EXPECT_EQ(expected_item.second, actual_item.second);
}
} else {
cout << "expected: " << endl;
print_events(expected);
cout << "actual: " << endl;
print_events(actual);
}
}
pair<State, TYPE_EVENTS> test_run(TYPE_EVENTS input, TYPE_EVENTS output, bool assert = true) {
auto setup = setup_test();
auto res = process(setup.first, setup.second, input);
if (assert) {
events_equals(output, res);
}
return {setup.second, res};
}
void assert_strokes(vector<string> expected, vector<string> actual) {
EXPECT_EQ(expected.size(), actual.size());
if (expected.size() == actual.size()) {
for (auto i = 0; i < expected.size(); i++) {
auto expected_item = expected[i];
auto actual_item = actual[i];
EXPECT_EQ(expected_item, actual_item);
}
} else {
for (auto i = 0; i < min(actual.size(), expected.size()); i++) {
auto expected_item = expected[i];
auto actual_item = actual[i];
cout << expected_item << " <=> " << actual_item << endl;
}
}
}
void no_stroke_left_behind(TYPE_EVENTS events) {
auto key_state = std::map<string, int>();
for (auto e: events) {
key_state[e.second] = e.first;
}
for (auto entry: key_state) {
EXPECT_EQ(0, entry.second);
if (0 != entry.second) {
cout << "key: " << entry.first << " is left behind" << endl;
}
}
}