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.

299 lines
8.6 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, TwoRowsShort) {
  138. addEvents({
  139. /* Time, Inputs, Outputs */
  140. {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  141. {1, {{0, 1, UP}}, {}},
  142. {2, {{2, 0, DOWN}}, {{2, 0, DOWN}}},
  143. {3, {{2, 0, 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, {}, {{2, 0, UP}}},
  148. /* Press key again after 1ms delay (debounce has not yet finished) */
  149. {9, {{2, 0, DOWN}}, {}},
  150. {10, {}, {{0, 1, DOWN}}}, /* 5ms after UP at time 5 */
  151. {12, {}, {{2, 0, DOWN}}}, /* 5ms after UP at time 7 */
  152. });
  153. runEvents();
  154. }
  155. TEST_F(DebounceTest, TwoKeysOverlap) {
  156. addEvents({
  157. /* Time, Inputs, Outputs */
  158. {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  159. {1, {{0, 1, UP}}, {}},
  160. /* Press a second key during the first debounce */
  161. {2, {{0, 2, DOWN}}, {}},
  162. /* Key registers as soon as debounce finishes, 5ms after time 0 */
  163. {5, {}, {{0, 1, UP}, {0, 2, DOWN}}},
  164. {6, {{0, 1, DOWN}}, {}},
  165. /* Key registers as soon as debounce finishes, 5ms after time 5 */
  166. {10, {}, {{0, 1, DOWN}}},
  167. /* Release both keys */
  168. {11, {{0, 1, UP}}, {}},
  169. {12, {{0, 2, UP}}, {}},
  170. /* Keys register as soon as debounce finishes, 5ms after time 10 */
  171. {15, {}, {{0, 1, UP}, {0, 2, UP}}},
  172. });
  173. runEvents();
  174. }
  175. TEST_F(DebounceTest, TwoKeysSimultaneous1) {
  176. addEvents({
  177. /* Time, Inputs, Outputs */
  178. {0, {{0, 1, DOWN}, {0, 2, DOWN}}, {{0, 1, DOWN}, {0, 2, DOWN}}},
  179. {20, {{0, 1, UP}}, {{0, 1, UP}}},
  180. {21, {{0, 2, UP}}, {}},
  181. /* Key registers as soon as debounce finishes, 5ms after time 20 */
  182. {25, {}, {{0, 2, UP}}},
  183. });
  184. runEvents();
  185. }
  186. TEST_F(DebounceTest, TwoKeysSimultaneous2) {
  187. addEvents({
  188. /* Time, Inputs, Outputs */
  189. {0, {{0, 1, DOWN}, {0, 2, DOWN}}, {{0, 1, DOWN}, {0, 2, DOWN}}},
  190. {20, {{0, 1, UP}, {0, 2, UP}}, {{0, 1, UP}, {0, 2, UP}}},
  191. });
  192. runEvents();
  193. }
  194. TEST_F(DebounceTest, OneKeyDelayedScan1) {
  195. addEvents({
  196. /* Time, Inputs, Outputs */
  197. {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  198. /* Processing is very late but the change will now be accepted */
  199. {300, {{0, 1, UP}}, {{0, 1, UP}}},
  200. });
  201. time_jumps_ = true;
  202. runEvents();
  203. }
  204. TEST_F(DebounceTest, OneKeyDelayedScan2) {
  205. addEvents({
  206. /* Time, Inputs, Outputs */
  207. {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  208. /* Processing is very late but the change will now be accepted even with a 1 scan delay */
  209. {300, {}, {}},
  210. {300, {{0, 1, UP}}, {{0, 1, UP}}},
  211. });
  212. time_jumps_ = true;
  213. runEvents();
  214. }
  215. TEST_F(DebounceTest, OneKeyDelayedScan3) {
  216. addEvents({
  217. /* Time, Inputs, Outputs */
  218. {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  219. /* Processing is very late but the change will now be accepted even with a 1ms delay */
  220. {300, {}, {}},
  221. {301, {{0, 1, UP}}, {{0, 1, UP}}},
  222. });
  223. time_jumps_ = true;
  224. runEvents();
  225. }
  226. TEST_F(DebounceTest, OneKeyDelayedScan4) {
  227. addEvents({
  228. /* Time, Inputs, Outputs */
  229. {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  230. /* Processing is a bit late but the change will now be accepted */
  231. {50, {{0, 1, UP}}, {{0, 1, UP}}},
  232. });
  233. time_jumps_ = true;
  234. runEvents();
  235. }
  236. TEST_F(DebounceTest, OneKeyDelayedScan5) {
  237. addEvents({
  238. /* Time, Inputs, Outputs */
  239. {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  240. /* Processing is very late but the change will now be accepted even with a 1 scan delay */
  241. {50, {}, {}},
  242. {50, {{0, 1, UP}}, {{0, 1, UP}}},
  243. });
  244. time_jumps_ = true;
  245. runEvents();
  246. }
  247. TEST_F(DebounceTest, OneKeyDelayedScan6) {
  248. addEvents({
  249. /* Time, Inputs, Outputs */
  250. {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  251. /* Processing is very late but the change will now be accepted even with a 1ms delay */
  252. {50, {}, {}},
  253. {51, {{0, 1, UP}}, {{0, 1, UP}}},
  254. });
  255. time_jumps_ = true;
  256. runEvents();
  257. }