asym_eager_defer_pk_tests.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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({ /* Time, Inputs, Outputs */
  20. {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  21. /* Release key after 1ms delay */
  22. {1, {{0, 1, UP}}, {}},
  23. /*
  24. * Until the eager timer on DOWN is observed to finish, the defer timer
  25. * on UP can't start. There's no workaround for this because it's not
  26. * possible to debounce an event that isn't being tracked.
  27. *
  28. * sym_defer_pk has the same problem but the test has to track that the
  29. * key changed state so the DOWN timer is always allowed to finish
  30. * before starting the UP timer.
  31. */
  32. {5, {}, {}},
  33. {10, {}, {{0, 1, UP}}}, /* 5ms+5ms after DOWN at time 0 */
  34. /* Press key again after 1ms delay */
  35. {11, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  36. });
  37. runEvents();
  38. }
  39. TEST_F(DebounceTest, OneKeyShort2) {
  40. addEvents({ /* Time, Inputs, Outputs */
  41. {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  42. /* Release key after 2ms delay */
  43. {2, {{0, 1, UP}}, {}},
  44. {5, {}, {}}, /* See OneKeyShort1 */
  45. {10, {}, {{0, 1, UP}}}, /* 5ms+5ms after DOWN at time 0 */
  46. /* Press key again after 1ms delay */
  47. {11, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  48. });
  49. runEvents();
  50. }
  51. TEST_F(DebounceTest, OneKeyShort3) {
  52. addEvents({ /* Time, Inputs, Outputs */
  53. {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  54. /* Release key after 3ms delay */
  55. {3, {{0, 1, UP}}, {}},
  56. {5, {}, {}}, /* See OneKeyShort1 */
  57. {10, {}, {{0, 1, UP}}}, /* 5ms+5ms after DOWN at time 0 */
  58. /* Press key again after 1ms delay */
  59. {11, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  60. });
  61. runEvents();
  62. }
  63. TEST_F(DebounceTest, OneKeyShort4) {
  64. addEvents({ /* Time, Inputs, Outputs */
  65. {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  66. /* Release key after 4ms delay */
  67. {4, {{0, 1, UP}}, {}},
  68. {5, {}, {}}, /* See OneKeyShort1 */
  69. {10, {}, {{0, 1, UP}}}, /* 5ms+5ms after DOWN at time 0 */
  70. /* Press key again after 1ms delay */
  71. {11, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  72. });
  73. runEvents();
  74. }
  75. TEST_F(DebounceTest, OneKeyShort5) {
  76. addEvents({ /* Time, Inputs, Outputs */
  77. {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  78. /* Release key after 5ms delay */
  79. {5, {{0, 1, UP}}, {}},
  80. {10, {}, {{0, 1, UP}}}, /* 5ms+5ms after DOWN at time 0 */
  81. /* Press key again after 1ms delay */
  82. {11, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  83. });
  84. runEvents();
  85. }
  86. TEST_F(DebounceTest, OneKeyShort6) {
  87. addEvents({ /* Time, Inputs, Outputs */
  88. {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  89. /* Release key after 6ms delay */
  90. {6, {{0, 1, UP}}, {}},
  91. {11, {}, {{0, 1, UP}}}, /* 5ms after UP at time 6 */
  92. /* Press key again after 1ms delay */
  93. {12, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  94. });
  95. runEvents();
  96. }
  97. TEST_F(DebounceTest, OneKeyShort7) {
  98. addEvents({ /* Time, Inputs, Outputs */
  99. {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  100. /* Release key after 7ms delay */
  101. {7, {{0, 1, UP}}, {}},
  102. {12, {}, {{0, 1, UP}}}, /* 5ms after UP at time 7 */
  103. /* Press key again after 1ms delay */
  104. {13, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  105. });
  106. runEvents();
  107. }
  108. TEST_F(DebounceTest, OneKeyShort8) {
  109. addEvents({ /* Time, Inputs, Outputs */
  110. {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  111. /* Release key after 1ms delay */
  112. {1, {{0, 1, UP}}, {}},
  113. {5, {}, {}}, /* See OneKeyShort1 */
  114. {10, {}, {{0, 1, UP}}}, /* 5ms after UP at time 7 */
  115. /* Press key again after 0ms delay (scan 2) */
  116. {10, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  117. });
  118. runEvents();
  119. }
  120. TEST_F(DebounceTest, OneKeyShort9) {
  121. addEvents({ /* Time, Inputs, Outputs */
  122. {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  123. /* Release key after 1ms delay */
  124. {1, {{0, 1, UP}}, {}},
  125. {5, {}, {}}, /* See OneKeyShort1 */
  126. /* Press key again after 0ms delay (same scan) before debounce finishes */
  127. {10, {{0, 1, DOWN}}, {}},
  128. });
  129. runEvents();
  130. }
  131. TEST_F(DebounceTest, OneKeyBouncing1) {
  132. addEvents({ /* Time, Inputs, Outputs */
  133. {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  134. {1, {{0, 1, UP}}, {}},
  135. {2, {{0, 1, DOWN}}, {}},
  136. {3, {{0, 1, UP}}, {}},
  137. {4, {{0, 1, DOWN}}, {}},
  138. {5, {{0, 1, UP}}, {}},
  139. {6, {{0, 1, DOWN}}, {}},
  140. {7, {{0, 1, UP}}, {}},
  141. {8, {{0, 1, DOWN}}, {}},
  142. {9, {{0, 1, UP}}, {}},
  143. {10, {{0, 1, DOWN}}, {}},
  144. {11, {{0, 1, UP}}, {}},
  145. {12, {{0, 1, DOWN}}, {}},
  146. {13, {{0, 1, UP}}, {}},
  147. {14, {{0, 1, DOWN}}, {}},
  148. {15, {{0, 1, UP}}, {}},
  149. {20, {}, {{0, 1, UP}}},
  150. /* Press key again after 1ms delay */
  151. {21, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  152. });
  153. runEvents();
  154. }
  155. TEST_F(DebounceTest, OneKeyBouncing2) {
  156. addEvents({ /* Time, Inputs, Outputs */
  157. {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  158. /* Change twice in the same time period */
  159. {1, {{0, 1, UP}}, {}},
  160. {1, {{0, 1, DOWN}}, {}},
  161. /* Change three times in the same time period */
  162. {2, {{0, 1, UP}}, {}},
  163. {2, {{0, 1, DOWN}}, {}},
  164. {2, {{0, 1, UP}}, {}},
  165. /* Change twice in the same time period */
  166. {6, {{0, 1, DOWN}}, {}},
  167. {6, {{0, 1, UP}}, {}},
  168. /* Change three times in the same time period */
  169. {7, {{0, 1, DOWN}}, {}},
  170. {7, {{0, 1, UP}}, {}},
  171. {7, {{0, 1, DOWN}}, {}},
  172. /* Change twice in the same time period */
  173. {8, {{0, 1, UP}}, {}},
  174. {8, {{0, 1, DOWN}}, {}},
  175. /* Change three times in the same time period */
  176. {9, {{0, 1, UP}}, {}},
  177. {9, {{0, 1, DOWN}}, {}},
  178. {9, {{0, 1, UP}}, {}},
  179. {14, {}, {{0, 1, UP}}},
  180. /* Press key again after 1ms delay */
  181. {15, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  182. });
  183. runEvents();
  184. }
  185. TEST_F(DebounceTest, OneKeyLong) {
  186. addEvents({ /* Time, Inputs, Outputs */
  187. {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  188. {25, {{0, 1, UP}}, {}},
  189. {30, {}, {{0, 1, UP}}},
  190. {50, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  191. {75, {{0, 1, UP}}, {}},
  192. {80, {}, {{0, 1, UP}}},
  193. {100, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  194. });
  195. runEvents();
  196. }
  197. TEST_F(DebounceTest, TwoKeysShort) {
  198. addEvents({ /* Time, Inputs, Outputs */
  199. {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  200. {1, {{0, 2, DOWN}}, {{0, 2, DOWN}}},
  201. /* Release key after 2ms delay */
  202. {2, {{0, 1, UP}}, {}},
  203. {3, {{0, 2, UP}}, {}},
  204. {5, {}, {}}, /* See OneKeyShort1 */
  205. {6, {}, {}}, /* See OneKeyShort1 */
  206. {10, {}, {{0, 1, UP}}}, /* 5ms+5ms after DOWN at time 0 */
  207. /* Press key again after 1ms delay */
  208. {11, {{0, 1, DOWN}}, {{0, 1, DOWN}, {0, 2, UP}}}, /* 5ms+5ms after DOWN at time 0 */
  209. {12, {{0, 2, DOWN}}, {{0, 2, DOWN}}}, /* 5ms+5ms after DOWN at time 0 */
  210. });
  211. runEvents();
  212. }
  213. TEST_F(DebounceTest, OneKeyDelayedScan1) {
  214. addEvents({ /* Time, Inputs, Outputs */
  215. {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  216. /* Processing is very late, immediately release key */
  217. {300, {{0, 1, UP}}, {}},
  218. {305, {}, {{0, 1, UP}}},
  219. });
  220. time_jumps_ = true;
  221. runEvents();
  222. }
  223. TEST_F(DebounceTest, OneKeyDelayedScan2) {
  224. addEvents({ /* Time, Inputs, Outputs */
  225. {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  226. /* Processing is very late, immediately release key */
  227. {300, {{0, 1, UP}}, {}},
  228. /* Processing is very late again */
  229. {600, {}, {{0, 1, UP}}},
  230. });
  231. time_jumps_ = true;
  232. runEvents();
  233. }
  234. TEST_F(DebounceTest, OneKeyDelayedScan3) {
  235. addEvents({ /* Time, Inputs, Outputs */
  236. {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  237. /* Processing is very late */
  238. {300, {}, {}},
  239. /* Release key after 1ms */
  240. {301, {{0, 1, UP}}, {}},
  241. {306, {}, {{0, 1, UP}}},
  242. });
  243. time_jumps_ = true;
  244. runEvents();
  245. }
  246. TEST_F(DebounceTest, OneKeyDelayedScan4) {
  247. addEvents({ /* Time, Inputs, Outputs */
  248. {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  249. /* Processing is very late */
  250. {300, {}, {}},
  251. /* Release key after 1ms */
  252. {301, {{0, 1, UP}}, {}},
  253. /* Processing is very late again */
  254. {600, {}, {{0, 1, UP}}},
  255. });
  256. time_jumps_ = true;
  257. runEvents();
  258. }
  259. TEST_F(DebounceTest, OneKeyDelayedScan5) {
  260. addEvents({ /* Time, Inputs, Outputs */
  261. {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  262. {5, {{0, 1, UP}}, {}},
  263. /* Processing is very late */
  264. {300, {}, {{0, 1, UP}}},
  265. /* Immediately press key again */
  266. {300, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  267. });
  268. time_jumps_ = true;
  269. runEvents();
  270. }
  271. TEST_F(DebounceTest, OneKeyDelayedScan6) {
  272. addEvents({ /* Time, Inputs, Outputs */
  273. {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  274. {5, {{0, 1, UP}}, {}},
  275. /* Processing is very late */
  276. {300, {}, {{0, 1, UP}}},
  277. /* Press key again after 1ms */
  278. {301, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  279. });
  280. time_jumps_ = true;
  281. runEvents();
  282. }
  283. TEST_F(DebounceTest, OneKeyDelayedScan7) {
  284. addEvents({ /* Time, Inputs, Outputs */
  285. {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  286. {5, {{0, 1, UP}}, {}},
  287. /* Press key again before debounce expires */
  288. {300, {{0, 1, DOWN}}, {}},
  289. });
  290. time_jumps_ = true;
  291. runEvents();
  292. }
  293. TEST_F(DebounceTest, OneKeyDelayedScan8) {
  294. addEvents({ /* Time, Inputs, Outputs */
  295. {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
  296. /* Processing is a bit late */
  297. {50, {}, {}},
  298. /* Release key after 1ms */
  299. {51, {{0, 1, UP}}, {}},
  300. /* Processing is a bit late again */
  301. {100, {}, {{0, 1, UP}}},
  302. });
  303. time_jumps_ = true;
  304. runEvents();
  305. }