keyboard stuff
at master 219 lines 8.9 kB view raw
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 25extern "C" { 26#include "debounce.h" 27#include "timer.h" 28 29void simulate_async_tick(uint32_t t); 30void reset_access_counter(void); 31uint32_t current_access_counter(void); 32uint32_t timer_read_internal(void); 33void set_time(uint32_t t); 34void advance_time(uint32_t ms); 35} 36 37void DebounceTest::addEvents(std::initializer_list<DebounceTestEvent> events) { 38 events_.insert(events_.end(), events.begin(), events.end()); 39} 40 41void DebounceTest::runEvents() { 42 /* Run the test multiple times, from 1kHz to 10kHz scan rate */ 43 for (extra_iterations_ = 0; extra_iterations_ < 10; extra_iterations_++) { 44 if (time_jumps_) { 45 /* Don't advance time smoothly, jump to the next event (some tests require this) */ 46 auto_advance_time_ = false; 47 runEventsInternal(); 48 } else { 49 /* Run the test with both smooth and irregular time; it must produce the same result */ 50 auto_advance_time_ = true; 51 runEventsInternal(); 52 auto_advance_time_ = false; 53 runEventsInternal(); 54 } 55 } 56} 57 58void DebounceTest::runEventsInternal() { 59 fast_timer_t previous = 0; 60 bool first = true; 61 62 /* Initialise keyboard with start time (offset to avoid testing at 0) and all keys UP */ 63 debounce_init(); 64 set_time(time_offset_); 65 simulate_async_tick(async_time_jumps_); 66 std::fill(std::begin(input_matrix_), std::end(input_matrix_), 0); 67 std::fill(std::begin(output_matrix_), std::end(output_matrix_), 0); 68 69 for (auto &event : events_) { 70 if (!auto_advance_time_) { 71 /* Jump to the next event */ 72 set_time(time_offset_ + event.time_); 73 } else if (!first && event.time_ == previous + 1) { 74 /* This event immediately follows the previous one, don't make extra debounce() calls */ 75 advance_time(1); 76 } else { 77 /* Fast forward to the time for this event, calling debounce() with no changes */ 78 ASSERT_LT((time_offset_ + event.time_) - timer_read_internal(), 60000) << "Test tries to advance more than 1 minute of time"; 79 80 while (timer_read_internal() != time_offset_ + event.time_) { 81 runDebounce(false); 82 checkCookedMatrix(false, "debounce() modified cooked matrix"); 83 advance_time(1); 84 } 85 } 86 87 first = false; 88 previous = event.time_; 89 90 /* Prepare input matrix */ 91 for (auto &input : event.inputs_) { 92 matrixUpdate(input_matrix_, "input", input); 93 } 94 95 /* Call debounce */ 96 runDebounce(!event.inputs_.empty()); 97 98 /* Prepare output matrix */ 99 for (auto &output : event.outputs_) { 100 matrixUpdate(output_matrix_, "output", output); 101 } 102 103 /* Check output matrix has expected change events */ 104 for (auto &output : event.outputs_) { 105 EXPECT_EQ(!!(cooked_matrix_[output.row_] & (1U << output.col_)), directionValue(output.direction_)) << "Missing event at " << strTime() << " expected key " << output.row_ << "," << output.col_ << " " << directionLabel(output.direction_) << "\ninput_matrix: changed=" << !event.inputs_.empty() << "\n" << strMatrix(input_matrix_) << "\nexpected_matrix:\n" << strMatrix(output_matrix_) << "\nactual_matrix:\n" << strMatrix(cooked_matrix_); 106 } 107 108 /* Check output matrix has no other changes */ 109 checkCookedMatrix(!event.inputs_.empty(), "debounce() cooked matrix does not match expected output matrix"); 110 111 /* Perform some extra iterations of the matrix scan with no changes */ 112 for (int i = 0; i < extra_iterations_; i++) { 113 runDebounce(false); 114 checkCookedMatrix(false, "debounce() modified cooked matrix"); 115 } 116 } 117 118 /* Check that no further changes happen for 1 minute */ 119 for (int i = 0; i < 60000; i++) { 120 runDebounce(false); 121 checkCookedMatrix(false, "debounce() modified cooked matrix"); 122 advance_time(1); 123 } 124} 125 126void DebounceTest::runDebounce(bool changed) { 127 std::copy(std::begin(input_matrix_), std::end(input_matrix_), std::begin(raw_matrix_)); 128 std::copy(std::begin(output_matrix_), std::end(output_matrix_), std::begin(cooked_matrix_)); 129 130 reset_access_counter(); 131 132 bool cooked_changed = debounce(raw_matrix_, cooked_matrix_, changed); 133 134 if (!std::equal(std::begin(input_matrix_), std::end(input_matrix_), std::begin(raw_matrix_))) { 135 FAIL() << "Fatal error: debounce() modified raw matrix at " << strTime() << "\ninput_matrix: changed=" << changed << "\n" << strMatrix(input_matrix_) << "\nraw_matrix:\n" << strMatrix(raw_matrix_); 136 } 137 138 if (std::equal(std::begin(output_matrix_), std::end(output_matrix_), std::begin(cooked_matrix_)) == cooked_changed) { 139 FAIL() << "Fatal error: debounce() reported a wrong cooked matrix change result at " << strTime() << "\noutput_matrix: cooked_changed=" << cooked_changed << "\n" << strMatrix(output_matrix_) << "\ncooked_matrix:\n" << strMatrix(cooked_matrix_); 140 } 141 142 if (current_access_counter() > 1) { 143 FAIL() << "Fatal error: debounce() read the timer multiple times, which is not allowed, at " << strTime() << "\ntimer: access_count=" << current_access_counter() << "\noutput_matrix: cooked_changed=" << cooked_changed << "\n" << strMatrix(output_matrix_) << "\ncooked_matrix:\n" << strMatrix(cooked_matrix_); 144 } 145} 146 147void DebounceTest::checkCookedMatrix(bool changed, const std::string &error_message) { 148 if (!std::equal(std::begin(output_matrix_), std::end(output_matrix_), std::begin(cooked_matrix_))) { 149 FAIL() << "Unexpected event: " << error_message << " at " << strTime() << "\ninput_matrix: changed=" << changed << "\n" << strMatrix(input_matrix_) << "\nexpected_matrix:\n" << strMatrix(output_matrix_) << "\nactual_matrix:\n" << strMatrix(cooked_matrix_); 150 } 151} 152 153std::string DebounceTest::strTime() { 154 std::stringstream text; 155 156 text << "time " << (timer_read_internal() - time_offset_) << " (extra_iterations=" << extra_iterations_ << ", auto_advance_time=" << auto_advance_time_ << ")"; 157 158 return text.str(); 159} 160 161std::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 182bool DebounceTest::directionValue(Direction direction) { 183 switch (direction) { 184 case DOWN: 185 return true; 186 187 case UP: 188 return false; 189 } 190} 191 192std::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 */ 203void 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_)) << "Test " << name << " at " << strTime() << " sets key " << event.row_ << "," << event.col_ << " " << directionLabel(event.direction_) << " but it is already " << directionLabel(event.direction_) << "\n" << name << "_matrix:\n" << strMatrix(matrix); 205 206 switch (event.direction_) { 207 case DOWN: 208 matrix[event.row_] |= (1U << event.col_); 209 break; 210 211 case UP: 212 matrix[event.row_] &= ~(1U << event.col_); 213 break; 214 } 215} 216 217DebounceTestEvent::DebounceTestEvent(fast_timer_t time, std::initializer_list<MatrixTestEvent> inputs, std::initializer_list<MatrixTestEvent> outputs) : time_(time), inputs_(inputs), outputs_(outputs) {} 218 219MatrixTestEvent::MatrixTestEvent(int row, int col, Direction direction) : row_(row), col_(col), direction_(direction) {}