diff options
Diffstat (limited to 'quantum/debounce/tests/debounce_test_common.cpp')
-rw-r--r-- | quantum/debounce/tests/debounce_test_common.cpp | 229 |
1 files changed, 229 insertions, 0 deletions
diff --git a/quantum/debounce/tests/debounce_test_common.cpp b/quantum/debounce/tests/debounce_test_common.cpp new file mode 100644 index 000000000..1c5e7c9f4 --- /dev/null +++ b/quantum/debounce/tests/debounce_test_common.cpp | |||
@@ -0,0 +1,229 @@ | |||
1 | /* Copyright 2021 Simon Arlott | ||
2 | * | ||
3 | * This program is free software: you can redistribute it and/or modify | ||
4 | * it under the terms of the GNU General Public License as published by | ||
5 | * the Free Software Foundation, either version 2 of the License, or | ||
6 | * (at your option) any later version. | ||
7 | * | ||
8 | * This program is distributed in the hope that it will be useful, | ||
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
11 | * GNU General Public License for more details. | ||
12 | * | ||
13 | * You should have received a copy of the GNU General Public License | ||
14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
15 | */ | ||
16 | |||
17 | #include "gtest/gtest.h" | ||
18 | |||
19 | #include "debounce_test_common.h" | ||
20 | |||
21 | #include <algorithm> | ||
22 | #include <iomanip> | ||
23 | #include <sstream> | ||
24 | |||
25 | extern "C" { | ||
26 | #include "quantum.h" | ||
27 | #include "timer.h" | ||
28 | #include "debounce.h" | ||
29 | |||
30 | void set_time(uint32_t t); | ||
31 | void advance_time(uint32_t ms); | ||
32 | } | ||
33 | |||
34 | void DebounceTest::addEvents(std::initializer_list<DebounceTestEvent> events) { | ||
35 | events_.insert(events_.end(), events.begin(), events.end()); | ||
36 | } | ||
37 | |||
38 | void DebounceTest::runEvents() { | ||
39 | /* Run the test multiple times, from 1kHz to 10kHz scan rate */ | ||
40 | for (extra_iterations_ = 0; extra_iterations_ < 10; extra_iterations_++) { | ||
41 | if (time_jumps_) { | ||
42 | /* Don't advance time smoothly, jump to the next event (some tests require this) */ | ||
43 | auto_advance_time_ = false; | ||
44 | runEventsInternal(); | ||
45 | } else { | ||
46 | /* Run the test with both smooth and irregular time; it must produce the same result */ | ||
47 | auto_advance_time_ = true; | ||
48 | runEventsInternal(); | ||
49 | auto_advance_time_ = false; | ||
50 | runEventsInternal(); | ||
51 | } | ||
52 | } | ||
53 | } | ||
54 | |||
55 | void DebounceTest::runEventsInternal() { | ||
56 | fast_timer_t previous = 0; | ||
57 | bool first = true; | ||
58 | |||
59 | /* Initialise keyboard with start time (offset to avoid testing at 0) and all keys UP */ | ||
60 | debounce_init(MATRIX_ROWS); | ||
61 | set_time(time_offset_); | ||
62 | std::fill(std::begin(input_matrix_), std::end(input_matrix_), 0); | ||
63 | std::fill(std::begin(output_matrix_), std::end(output_matrix_), 0); | ||
64 | |||
65 | for (auto &event : events_) { | ||
66 | if (!auto_advance_time_) { | ||
67 | /* Jump to the next event */ | ||
68 | set_time(time_offset_ + event.time_); | ||
69 | } else if (!first && event.time_ == previous + 1) { | ||
70 | /* This event immediately follows the previous one, don't make extra debounce() calls */ | ||
71 | advance_time(1); | ||
72 | } else { | ||
73 | /* Fast forward to the time for this event, calling debounce() with no changes */ | ||
74 | ASSERT_LT((time_offset_ + event.time_) - timer_read_fast(), 60000) << "Test tries to advance more than 1 minute of time"; | ||
75 | |||
76 | while (timer_read_fast() != time_offset_ + event.time_) { | ||
77 | runDebounce(false); | ||
78 | checkCookedMatrix(false, "debounce() modified cooked matrix"); | ||
79 | advance_time(1); | ||
80 | } | ||
81 | } | ||
82 | |||
83 | first = false; | ||
84 | previous = event.time_; | ||
85 | |||
86 | /* Prepare input matrix */ | ||
87 | for (auto &input : event.inputs_) { | ||
88 | matrixUpdate(input_matrix_, "input", input); | ||
89 | } | ||
90 | |||
91 | /* Call debounce */ | ||
92 | runDebounce(!event.inputs_.empty()); | ||
93 | |||
94 | /* Prepare output matrix */ | ||
95 | for (auto &output : event.outputs_) { | ||
96 | matrixUpdate(output_matrix_, "output", output); | ||
97 | } | ||
98 | |||
99 | /* Check output matrix has expected change events */ | ||
100 | for (auto &output : event.outputs_) { | ||
101 | EXPECT_EQ(!!(cooked_matrix_[output.row_] & (1U << output.col_)), directionValue(output.direction_)) | ||
102 | << "Missing event at " << strTime() | ||
103 | << " expected key " << output.row_ << "," << output.col_ << " " << directionLabel(output.direction_) | ||
104 | << "\ninput_matrix: changed=" << !event.inputs_.empty() << "\n" << strMatrix(input_matrix_) | ||
105 | << "\nexpected_matrix:\n" << strMatrix(output_matrix_) | ||
106 | << "\nactual_matrix:\n" << strMatrix(cooked_matrix_); | ||
107 | } | ||
108 | |||
109 | /* Check output matrix has no other changes */ | ||
110 | checkCookedMatrix(!event.inputs_.empty(), "debounce() cooked matrix does not match expected output matrix"); | ||
111 | |||
112 | /* Perform some extra iterations of the matrix scan with no changes */ | ||
113 | for (int i = 0; i < extra_iterations_; i++) { | ||
114 | runDebounce(false); | ||
115 | checkCookedMatrix(false, "debounce() modified cooked matrix"); | ||
116 | } | ||
117 | } | ||
118 | |||
119 | /* Check that no further changes happen for 1 minute */ | ||
120 | for (int i = 0; i < 60000; i++) { | ||
121 | runDebounce(false); | ||
122 | checkCookedMatrix(false, "debounce() modified cooked matrix"); | ||
123 | advance_time(1); | ||
124 | } | ||
125 | |||
126 | debounce_free(); | ||
127 | } | ||
128 | |||
129 | void DebounceTest::runDebounce(bool changed) { | ||
130 | std::copy(std::begin(input_matrix_), std::end(input_matrix_), std::begin(raw_matrix_)); | ||
131 | std::copy(std::begin(output_matrix_), std::end(output_matrix_), std::begin(cooked_matrix_)); | ||
132 | |||
133 | debounce(raw_matrix_, cooked_matrix_, MATRIX_ROWS, changed); | ||
134 | |||
135 | if (!std::equal(std::begin(input_matrix_), std::end(input_matrix_), std::begin(raw_matrix_))) { | ||
136 | FAIL() << "Fatal error: debounce() modified raw matrix at " << strTime() | ||
137 | << "\ninput_matrix: changed=" << changed << "\n" << strMatrix(input_matrix_) | ||
138 | << "\nraw_matrix:\n" << strMatrix(raw_matrix_); | ||
139 | } | ||
140 | } | ||
141 | |||
142 | void DebounceTest::checkCookedMatrix(bool changed, const std::string &error_message) { | ||
143 | if (!std::equal(std::begin(output_matrix_), std::end(output_matrix_), std::begin(cooked_matrix_))) { | ||
144 | FAIL() << "Unexpected event: " << error_message << " at " << strTime() | ||
145 | << "\ninput_matrix: changed=" << changed << "\n" << strMatrix(input_matrix_) | ||
146 | << "\nexpected_matrix:\n" << strMatrix(output_matrix_) | ||
147 | << "\nactual_matrix:\n" << strMatrix(cooked_matrix_); | ||
148 | } | ||
149 | } | ||
150 | |||
151 | std::string DebounceTest::strTime() { | ||
152 | std::stringstream text; | ||
153 | |||
154 | text << "time " << (timer_read_fast() - time_offset_) | ||
155 | << " (extra_iterations=" << extra_iterations_ | ||
156 | << ", auto_advance_time=" << auto_advance_time_ << ")"; | ||
157 | |||
158 | return text.str(); | ||
159 | } | ||
160 | |||
161 | std::string DebounceTest::strMatrix(matrix_row_t matrix[]) { | ||
162 | std::stringstream text; | ||
163 | |||
164 | text << "\t" << std::setw(3) << ""; | ||
165 | for (int col = 0; col < MATRIX_COLS; col++) { | ||
166 | text << " " << std::setw(2) << col; | ||
167 | } | ||
168 | text << "\n"; | ||
169 | |||
170 | for (int row = 0; row < MATRIX_ROWS; row++) { | ||
171 | text << "\t" << std::setw(2) << row << ":"; | ||
172 | for (int col = 0; col < MATRIX_COLS; col++) { | ||
173 | text << ((matrix[row] & (1U << col)) ? " XX" : " __"); | ||
174 | } | ||
175 | |||
176 | text << "\n"; | ||
177 | } | ||
178 | |||
179 | return text.str(); | ||
180 | } | ||
181 | |||
182 | bool DebounceTest::directionValue(Direction direction) { | ||
183 | switch (direction) { | ||
184 | case DOWN: | ||
185 | return true; | ||
186 | |||
187 | case UP: | ||
188 | return false; | ||
189 | } | ||
190 | } | ||
191 | |||
192 | std::string DebounceTest::directionLabel(Direction direction) { | ||
193 | switch (direction) { | ||
194 | case DOWN: | ||
195 | return "DOWN"; | ||
196 | |||
197 | case UP: | ||
198 | return "UP"; | ||
199 | } | ||
200 | } | ||
201 | |||
202 | /* Modify a matrix and verify that events always specify a change */ | ||
203 | void DebounceTest::matrixUpdate(matrix_row_t matrix[], const std::string &name, const MatrixTestEvent &event) { | ||
204 | ASSERT_NE(!!(matrix[event.row_] & (1U << event.col_)), directionValue(event.direction_)) | ||
205 | << "Test " << name << " at " << strTime() | ||
206 | << " sets key " << event.row_ << "," << event.col_ << " " << directionLabel(event.direction_) | ||
207 | << " but it is already " << directionLabel(event.direction_) | ||
208 | << "\n" << name << "_matrix:\n" << strMatrix(matrix); | ||
209 | |||
210 | switch (event.direction_) { | ||
211 | case DOWN: | ||
212 | matrix[event.row_] |= (1U << event.col_); | ||
213 | break; | ||
214 | |||
215 | case UP: | ||
216 | matrix[event.row_] &= ~(1U << event.col_); | ||
217 | break; | ||
218 | } | ||
219 | } | ||
220 | |||
221 | DebounceTestEvent::DebounceTestEvent(fast_timer_t time, | ||
222 | std::initializer_list<MatrixTestEvent> inputs, | ||
223 | std::initializer_list<MatrixTestEvent> outputs) | ||
224 | : time_(time), inputs_(inputs), outputs_(outputs) { | ||
225 | } | ||
226 | |||
227 | MatrixTestEvent::MatrixTestEvent(int row, int col, Direction direction) | ||
228 | : row_(row), col_(col), direction_(direction) { | ||
229 | } | ||