debounce_test_common.cpp 8.2 KB

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