debounce_test_common.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. #include "gtest/gtest.h"
  17. #include "debounce_test_common.h"
  18. #include <algorithm>
  19. #include <iomanip>
  20. #include <sstream>
  21. extern "C" {
  22. #include "quantum.h"
  23. #include "timer.h"
  24. #include "debounce.h"
  25. void set_time(uint32_t t);
  26. void advance_time(uint32_t ms);
  27. }
  28. void DebounceTest::addEvents(std::initializer_list<DebounceTestEvent> events) {
  29. events_.insert(events_.end(), events.begin(), events.end());
  30. }
  31. void DebounceTest::runEvents() {
  32. /* Run the test multiple times, from 1kHz to 10kHz scan rate */
  33. for (extra_iterations_ = 0; extra_iterations_ < 10; extra_iterations_++) {
  34. if (time_jumps_) {
  35. /* Don't advance time smoothly, jump to the next event (some tests require this) */
  36. auto_advance_time_ = false;
  37. runEventsInternal();
  38. } else {
  39. /* Run the test with both smooth and irregular time; it must produce the same result */
  40. auto_advance_time_ = true;
  41. runEventsInternal();
  42. auto_advance_time_ = false;
  43. runEventsInternal();
  44. }
  45. }
  46. }
  47. void DebounceTest::runEventsInternal() {
  48. fast_timer_t previous = 0;
  49. bool first = true;
  50. /* Initialise keyboard with start time (offset to avoid testing at 0) and all keys UP */
  51. debounce_init(MATRIX_ROWS);
  52. set_time(time_offset_);
  53. std::fill(std::begin(input_matrix_), std::end(input_matrix_), 0);
  54. std::fill(std::begin(output_matrix_), std::end(output_matrix_), 0);
  55. for (auto &event : events_) {
  56. if (!auto_advance_time_) {
  57. /* Jump to the next event */
  58. set_time(time_offset_ + event.time_);
  59. } else if (!first && event.time_ == previous + 1) {
  60. /* This event immediately follows the previous one, don't make extra debounce() calls */
  61. advance_time(1);
  62. } else {
  63. /* Fast forward to the time for this event, calling debounce() with no changes */
  64. ASSERT_LT((time_offset_ + event.time_) - timer_read_fast(), 60000) << "Test tries to advance more than 1 minute of time";
  65. while (timer_read_fast() != time_offset_ + event.time_) {
  66. runDebounce(false);
  67. checkCookedMatrix(false, "debounce() modified cooked matrix");
  68. advance_time(1);
  69. }
  70. }
  71. first = false;
  72. previous = event.time_;
  73. /* Prepare input matrix */
  74. for (auto &input : event.inputs_) {
  75. matrixUpdate(input_matrix_, "input", input);
  76. }
  77. /* Call debounce */
  78. runDebounce(!event.inputs_.empty());
  79. /* Prepare output matrix */
  80. for (auto &output : event.outputs_) {
  81. matrixUpdate(output_matrix_, "output", output);
  82. }
  83. /* Check output matrix has expected change events */
  84. for (auto &output : event.outputs_) {
  85. EXPECT_EQ(!!(cooked_matrix_[output.row_] & (1U << output.col_)), directionValue(output.direction_))
  86. << "Missing event at " << strTime()
  87. << " expected key " << output.row_ << "," << output.col_ << " " << directionLabel(output.direction_)
  88. << "\ninput_matrix: changed=" << !event.inputs_.empty() << "\n" << strMatrix(input_matrix_)
  89. << "\nexpected_matrix:\n" << strMatrix(output_matrix_)
  90. << "\nactual_matrix:\n" << strMatrix(cooked_matrix_);
  91. }
  92. /* Check output matrix has no other changes */
  93. checkCookedMatrix(!event.inputs_.empty(), "debounce() cooked matrix does not match expected output matrix");
  94. /* Perform some extra iterations of the matrix scan with no changes */
  95. for (int i = 0; i < extra_iterations_; i++) {
  96. runDebounce(false);
  97. checkCookedMatrix(false, "debounce() modified cooked matrix");
  98. }
  99. }
  100. /* Check that no further changes happen for 1 minute */
  101. for (int i = 0; i < 60000; i++) {
  102. runDebounce(false);
  103. checkCookedMatrix(false, "debounce() modified cooked matrix");
  104. advance_time(1);
  105. }
  106. debounce_free();
  107. }
  108. void DebounceTest::runDebounce(bool changed) {
  109. std::copy(std::begin(input_matrix_), std::end(input_matrix_), std::begin(raw_matrix_));
  110. std::copy(std::begin(output_matrix_), std::end(output_matrix_), std::begin(cooked_matrix_));
  111. debounce(raw_matrix_, cooked_matrix_, MATRIX_ROWS, changed);
  112. if (!std::equal(std::begin(input_matrix_), std::end(input_matrix_), std::begin(raw_matrix_))) {
  113. FAIL() << "Fatal error: debounce() modified raw matrix at " << strTime()
  114. << "\ninput_matrix: changed=" << changed << "\n" << strMatrix(input_matrix_)
  115. << "\nraw_matrix:\n" << strMatrix(raw_matrix_);
  116. }
  117. }
  118. void DebounceTest::checkCookedMatrix(bool changed, const std::string &error_message) {
  119. if (!std::equal(std::begin(output_matrix_), std::end(output_matrix_), std::begin(cooked_matrix_))) {
  120. FAIL() << "Unexpected event: " << error_message << " at " << strTime()
  121. << "\ninput_matrix: changed=" << changed << "\n" << strMatrix(input_matrix_)
  122. << "\nexpected_matrix:\n" << strMatrix(output_matrix_)
  123. << "\nactual_matrix:\n" << strMatrix(cooked_matrix_);
  124. }
  125. }
  126. std::string DebounceTest::strTime() {
  127. std::stringstream text;
  128. text << "time " << (timer_read_fast() - time_offset_)
  129. << " (extra_iterations=" << extra_iterations_
  130. << ", auto_advance_time=" << auto_advance_time_ << ")";
  131. return text.str();
  132. }
  133. std::string DebounceTest::strMatrix(matrix_row_t matrix[]) {
  134. std::stringstream text;
  135. text << "\t" << std::setw(3) << "";
  136. for (int col = 0; col < MATRIX_COLS; col++) {
  137. text << " " << std::setw(2) << col;
  138. }
  139. text << "\n";
  140. for (int row = 0; row < MATRIX_ROWS; row++) {
  141. text << "\t" << std::setw(2) << row << ":";
  142. for (int col = 0; col < MATRIX_COLS; col++) {
  143. text << ((matrix[row] & (1U << col)) ? " XX" : " __");
  144. }
  145. text << "\n";
  146. }
  147. return text.str();
  148. }
  149. bool DebounceTest::directionValue(Direction direction) {
  150. switch (direction) {
  151. case DOWN:
  152. return true;
  153. case UP:
  154. return false;
  155. }
  156. }
  157. std::string DebounceTest::directionLabel(Direction direction) {
  158. switch (direction) {
  159. case DOWN:
  160. return "DOWN";
  161. case UP:
  162. return "UP";
  163. }
  164. }
  165. /* Modify a matrix and verify that events always specify a change */
  166. void DebounceTest::matrixUpdate(matrix_row_t matrix[], const std::string &name, const MatrixTestEvent &event) {
  167. ASSERT_NE(!!(matrix[event.row_] & (1U << event.col_)), directionValue(event.direction_))
  168. << "Test " << name << " at " << strTime()
  169. << " sets key " << event.row_ << "," << event.col_ << " " << directionLabel(event.direction_)
  170. << " but it is already " << directionLabel(event.direction_)
  171. << "\n" << name << "_matrix:\n" << strMatrix(matrix);
  172. switch (event.direction_) {
  173. case DOWN:
  174. matrix[event.row_] |= (1U << event.col_);
  175. break;
  176. case UP:
  177. matrix[event.row_] &= ~(1U << event.col_);
  178. break;
  179. }
  180. }
  181. DebounceTestEvent::DebounceTestEvent(fast_timer_t time,
  182. std::initializer_list<MatrixTestEvent> inputs,
  183. std::initializer_list<MatrixTestEvent> outputs)
  184. : time_(time), inputs_(inputs), outputs_(outputs) {
  185. }
  186. MatrixTestEvent::MatrixTestEvent(int row, int col, Direction direction)
  187. : row_(row), col_(col), direction_(direction) {
  188. }