debounce_test_common.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 <initializer_list>
  18. #include <list>
  19. #include <string>
  20. extern "C" {
  21. #include "quantum.h"
  22. #include "timer.h"
  23. }
  24. enum Direction {
  25. DOWN,
  26. UP,
  27. };
  28. class MatrixTestEvent {
  29. public:
  30. MatrixTestEvent(int row, int col, Direction direction);
  31. const int row_;
  32. const int col_;
  33. const Direction direction_;
  34. };
  35. class DebounceTestEvent {
  36. public:
  37. // 0, {{0, 1, DOWN}}, {{0, 1, DOWN}})
  38. DebounceTestEvent(fast_timer_t time,
  39. std::initializer_list<MatrixTestEvent> inputs,
  40. std::initializer_list<MatrixTestEvent> outputs);
  41. const fast_timer_t time_;
  42. const std::list<MatrixTestEvent> inputs_;
  43. const std::list<MatrixTestEvent> outputs_;
  44. };
  45. class DebounceTest : public ::testing::Test {
  46. protected:
  47. void addEvents(std::initializer_list<DebounceTestEvent> events);
  48. void runEvents();
  49. fast_timer_t time_offset_ = 7777;
  50. bool time_jumps_ = false;
  51. private:
  52. static bool directionValue(Direction direction);
  53. static std::string directionLabel(Direction direction);
  54. void runEventsInternal();
  55. void runDebounce(bool changed);
  56. void checkCookedMatrix(bool changed, const std::string &error_message);
  57. void matrixUpdate(matrix_row_t matrix[], const std::string &name, const MatrixTestEvent &event);
  58. std::string strTime();
  59. std::string strMatrix(matrix_row_t matrix[]);
  60. std::list<DebounceTestEvent> events_;
  61. matrix_row_t input_matrix_[MATRIX_ROWS];
  62. matrix_row_t raw_matrix_[MATRIX_ROWS];
  63. matrix_row_t cooked_matrix_[MATRIX_ROWS];
  64. matrix_row_t output_matrix_[MATRIX_ROWS];
  65. int extra_iterations_;
  66. bool auto_advance_time_;
  67. };