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.

253 lines
7.2 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. TEST_F(DebounceTest, OneKeyShort1) {
  19. addEvents({
  20. /* Time, Inputs, Outputs */
  21. {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  22. {1, {{0, 1, UP}}, {}},
  23. {5, {}, {{0, 1, UP}}},
  24. /* Press key again after 1ms delay (debounce has not yet finished) */
  25. {6, {{0, 1, DOWN}}, {}},
  26. {10, {}, {{0, 1, DOWN}}}, /* 5ms after UP at time 5 */
  27. });
  28. runEvents();
  29. }
  30. TEST_F(DebounceTest, OneKeyShort2) {
  31. addEvents({
  32. /* Time, Inputs, Outputs */
  33. {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  34. {1, {{0, 1, UP}}, {}},
  35. {5, {}, {{0, 1, UP}}},
  36. /* Press key again after 2ms delay (debounce has not yet finished) */
  37. {7, {{0, 1, DOWN}}, {}},
  38. {10, {}, {{0, 1, DOWN}}}, /* 5ms after UP at time 5 */
  39. });
  40. runEvents();
  41. }
  42. TEST_F(DebounceTest, OneKeyShort3) {
  43. addEvents({
  44. /* Time, Inputs, Outputs */
  45. {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  46. {1, {{0, 1, UP}}, {}},
  47. {5, {}, {{0, 1, UP}}},
  48. /* Press key again after 3ms delay (debounce has not yet finished) */
  49. {8, {{0, 1, DOWN}}, {}},
  50. {10, {}, {{0, 1, DOWN}}}, /* 5ms after UP at time 5 */
  51. });
  52. runEvents();
  53. }
  54. TEST_F(DebounceTest, OneKeyShort4) {
  55. addEvents({
  56. /* Time, Inputs, Outputs */
  57. {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  58. {1, {{0, 1, UP}}, {}},
  59. {5, {}, {{0, 1, UP}}},
  60. /* Press key again after 4ms delay (debounce has not yet finished) */
  61. {9, {{0, 1, DOWN}}, {}},
  62. {10, {}, {{0, 1, DOWN}}}, /* 5ms after UP at time 5 */
  63. });
  64. runEvents();
  65. }
  66. TEST_F(DebounceTest, OneKeyShort5) {
  67. addEvents({
  68. /* Time, Inputs, Outputs */
  69. {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  70. {1, {{0, 1, UP}}, {}},
  71. {5, {}, {{0, 1, UP}}},
  72. /* Press key again after 5ms delay (debounce has finished) */
  73. {10, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  74. });
  75. runEvents();
  76. }
  77. TEST_F(DebounceTest, OneKeyShort6) {
  78. addEvents({
  79. /* Time, Inputs, Outputs */
  80. {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  81. {1, {{0, 1, UP}}, {}},
  82. {5, {}, {{0, 1, UP}}},
  83. /* Press key after after 6ms delay (debounce has finished) */
  84. {11, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  85. });
  86. runEvents();
  87. }
  88. TEST_F(DebounceTest, OneKeyBouncing1) {
  89. addEvents({
  90. /* Time, Inputs, Outputs */
  91. {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  92. {1, {{0, 1, UP}}, {}},
  93. {2, {{0, 1, DOWN}}, {}},
  94. {3, {{0, 1, UP}}, {}},
  95. {4, {{0, 1, DOWN}}, {}},
  96. {5, {{0, 1, UP}}, {{0, 1, UP}}},
  97. /* Press key again after 1ms delay (debounce has not yet finished) */
  98. {6, {{0, 1, DOWN}}, {}},
  99. {10, {}, {{0, 1, DOWN}}}, /* 5ms after UP at time 5 */
  100. });
  101. runEvents();
  102. }
  103. TEST_F(DebounceTest, OneKeyBouncing2) {
  104. addEvents({
  105. /* Time, Inputs, Outputs */
  106. {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  107. /* Change twice in the same time period */
  108. {1, {{0, 1, UP}}, {}},
  109. {1, {{0, 1, DOWN}}, {}},
  110. /* Change three times in the same time period */
  111. {2, {{0, 1, UP}}, {}},
  112. {2, {{0, 1, DOWN}}, {}},
  113. {2, {{0, 1, UP}}, {}},
  114. /* Change three times in the same time period */
  115. {3, {{0, 1, DOWN}}, {}},
  116. {3, {{0, 1, UP}}, {}},
  117. {3, {{0, 1, DOWN}}, {}},
  118. /* Change twice in the same time period */
  119. {4, {{0, 1, UP}}, {}},
  120. {4, {{0, 1, DOWN}}, {}},
  121. {5, {{0, 1, UP}}, {{0, 1, UP}}},
  122. /* Press key again after 1ms delay (debounce has not yet finished) */
  123. {6, {{0, 1, DOWN}}, {}},
  124. {10, {}, {{0, 1, DOWN}}}, /* 5ms after UP at time 5 */
  125. });
  126. runEvents();
  127. }
  128. TEST_F(DebounceTest, OneKeyLong) {
  129. addEvents({
  130. /* Time, Inputs, Outputs */
  131. {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  132. {25, {{0, 1, UP}}, {{0, 1, UP}}},
  133. {50, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  134. });
  135. runEvents();
  136. }
  137. TEST_F(DebounceTest, TwoKeysShort) {
  138. addEvents({
  139. /* Time, Inputs, Outputs */
  140. {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  141. {1, {{0, 1, UP}}, {}},
  142. {2, {{0, 2, DOWN}}, {{0, 2, DOWN}}},
  143. {3, {{0, 2, UP}}, {}},
  144. {5, {}, {{0, 1, UP}}},
  145. /* Press key again after 1ms delay (debounce has not yet finished) */
  146. {6, {{0, 1, DOWN}}, {}},
  147. {7, {}, {{0, 2, UP}}},
  148. /* Press key again after 1ms delay (debounce has not yet finished) */
  149. {9, {{0, 2, DOWN}}, {}},
  150. {10, {}, {{0, 1, DOWN}}}, /* 5ms after UP at time 5 */
  151. {12, {}, {{0, 2, DOWN}}}, /* 5ms after UP at time 7 */
  152. });
  153. runEvents();
  154. }
  155. TEST_F(DebounceTest, OneKeyDelayedScan1) {
  156. addEvents({
  157. /* Time, Inputs, Outputs */
  158. {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  159. /* Processing is very late but the change will now be accepted */
  160. {300, {{0, 1, UP}}, {{0, 1, UP}}},
  161. });
  162. time_jumps_ = true;
  163. runEvents();
  164. }
  165. TEST_F(DebounceTest, OneKeyDelayedScan2) {
  166. addEvents({
  167. /* Time, Inputs, Outputs */
  168. {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  169. /* Processing is very late but the change will now be accepted even with a 1 scan delay */
  170. {300, {}, {}},
  171. {300, {{0, 1, UP}}, {{0, 1, UP}}},
  172. });
  173. time_jumps_ = true;
  174. runEvents();
  175. }
  176. TEST_F(DebounceTest, OneKeyDelayedScan3) {
  177. addEvents({
  178. /* Time, Inputs, Outputs */
  179. {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  180. /* Processing is very late but the change will now be accepted even with a 1ms delay */
  181. {300, {}, {}},
  182. {301, {{0, 1, UP}}, {{0, 1, UP}}},
  183. });
  184. time_jumps_ = true;
  185. runEvents();
  186. }
  187. TEST_F(DebounceTest, OneKeyDelayedScan4) {
  188. addEvents({
  189. /* Time, Inputs, Outputs */
  190. {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  191. /* Processing is a bit late but the change will now be accepted */
  192. {50, {{0, 1, UP}}, {{0, 1, UP}}},
  193. });
  194. time_jumps_ = true;
  195. runEvents();
  196. }
  197. TEST_F(DebounceTest, OneKeyDelayedScan5) {
  198. addEvents({
  199. /* Time, Inputs, Outputs */
  200. {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  201. /* Processing is very late but the change will now be accepted even with a 1 scan delay */
  202. {50, {}, {}},
  203. {50, {{0, 1, UP}}, {{0, 1, UP}}},
  204. });
  205. time_jumps_ = true;
  206. runEvents();
  207. }
  208. TEST_F(DebounceTest, OneKeyDelayedScan6) {
  209. addEvents({
  210. /* Time, Inputs, Outputs */
  211. {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  212. /* Processing is very late but the change will now be accepted even with a 1ms delay */
  213. {50, {}, {}},
  214. {51, {{0, 1, UP}}, {{0, 1, UP}}},
  215. });
  216. time_jumps_ = true;
  217. runEvents();
  218. }