You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

205 lines
7.8 KiB

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