qmk_firmware

QMK firmware for my keyboards (Corne, Sweep Ferris) and trackball (Ploopy Adept)
Log | Files | Refs | Submodules | LICENSE

asym_eager_defer_pk_tests.cpp (11861B)


      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 
     17 #include "gtest/gtest.h"
     18 
     19 #include "debounce_test_common.h"
     20 
     21 TEST_F(DebounceTest, OneKeyShort1) {
     22     addEvents({
     23         /* Time, Inputs, Outputs */
     24         {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
     25         /* Release key after 1ms delay */
     26         {1, {{0, 1, UP}}, {}},
     27 
     28         /*
     29          * Until the eager timer on DOWN is observed to finish, the defer timer
     30          * on UP can't start. There's no workaround for this because it's not
     31          * possible to debounce an event that isn't being tracked.
     32          *
     33          * sym_defer_pk has the same problem but the test has to track that the
     34          * key changed state so the DOWN timer is always allowed to finish
     35          * before starting the UP timer.
     36          */
     37         {5, {}, {}},
     38 
     39         {10, {}, {{0, 1, UP}}}, /* 5ms+5ms after DOWN at time 0 */
     40         /* Press key again after 1ms delay */
     41         {11, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
     42     });
     43     runEvents();
     44 }
     45 
     46 TEST_F(DebounceTest, OneKeyShort2) {
     47     addEvents({
     48         /* Time, Inputs, Outputs */
     49         {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
     50         /* Release key after 2ms delay */
     51         {2, {{0, 1, UP}}, {}},
     52 
     53         {5, {}, {}}, /* See OneKeyShort1 */
     54 
     55         {10, {}, {{0, 1, UP}}}, /* 5ms+5ms after DOWN at time 0 */
     56         /* Press key again after 1ms delay */
     57         {11, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
     58     });
     59     runEvents();
     60 }
     61 
     62 TEST_F(DebounceTest, OneKeyShort3) {
     63     addEvents({
     64         /* Time, Inputs, Outputs */
     65         {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
     66         /* Release key after 3ms delay */
     67         {3, {{0, 1, UP}}, {}},
     68 
     69         {5, {}, {}}, /* See OneKeyShort1 */
     70 
     71         {10, {}, {{0, 1, UP}}}, /* 5ms+5ms after DOWN at time 0 */
     72         /* Press key again after 1ms delay */
     73         {11, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
     74     });
     75     runEvents();
     76 }
     77 
     78 TEST_F(DebounceTest, OneKeyShort4) {
     79     addEvents({
     80         /* Time, Inputs, Outputs */
     81         {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
     82         /* Release key after 4ms delay */
     83         {4, {{0, 1, UP}}, {}},
     84 
     85         {5, {}, {}}, /* See OneKeyShort1 */
     86 
     87         {10, {}, {{0, 1, UP}}}, /* 5ms+5ms after DOWN at time 0 */
     88         /* Press key again after 1ms delay */
     89         {11, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
     90     });
     91     runEvents();
     92 }
     93 
     94 TEST_F(DebounceTest, OneKeyShort5) {
     95     addEvents({
     96         /* Time, Inputs, Outputs */
     97         {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
     98 
     99         /* Release key after 5ms delay */
    100         {5, {{0, 1, UP}}, {}},
    101 
    102         {10, {}, {{0, 1, UP}}}, /* 5ms+5ms after DOWN at time 0 */
    103         /* Press key again after 1ms delay */
    104         {11, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
    105     });
    106     runEvents();
    107 }
    108 
    109 TEST_F(DebounceTest, OneKeyShort6) {
    110     addEvents({
    111         /* Time, Inputs, Outputs */
    112         {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
    113 
    114         /* Release key after 6ms delay */
    115         {6, {{0, 1, UP}}, {}},
    116 
    117         {11, {}, {{0, 1, UP}}}, /* 5ms after UP at time 6 */
    118         /* Press key again after 1ms delay */
    119         {12, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
    120     });
    121     runEvents();
    122 }
    123 
    124 TEST_F(DebounceTest, OneKeyShort7) {
    125     addEvents({
    126         /* Time, Inputs, Outputs */
    127         {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
    128 
    129         /* Release key after 7ms delay */
    130         {7, {{0, 1, UP}}, {}},
    131 
    132         {12, {}, {{0, 1, UP}}}, /* 5ms after UP at time 7 */
    133         /* Press key again after 1ms delay */
    134         {13, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
    135     });
    136     runEvents();
    137 }
    138 
    139 TEST_F(DebounceTest, OneKeyShort8) {
    140     addEvents({
    141         /* Time, Inputs, Outputs */
    142         {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
    143         /* Release key after 1ms delay */
    144         {1, {{0, 1, UP}}, {}},
    145 
    146         {5, {}, {}}, /* See OneKeyShort1 */
    147 
    148         {10, {}, {{0, 1, UP}}}, /* 5ms after UP at time 7 */
    149         /* Press key again after 0ms delay (scan 2) */
    150         {10, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
    151     });
    152     runEvents();
    153 }
    154 
    155 TEST_F(DebounceTest, OneKeyShort9) {
    156     addEvents({
    157         /* Time, Inputs, Outputs */
    158         {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
    159         /* Release key after 1ms delay */
    160         {1, {{0, 1, UP}}, {}},
    161 
    162         {5, {}, {}}, /* See OneKeyShort1 */
    163 
    164         /* Press key again after 0ms delay (same scan) before debounce finishes */
    165         {10, {{0, 1, DOWN}}, {}},
    166     });
    167     runEvents();
    168 }
    169 
    170 TEST_F(DebounceTest, OneKeyBouncing1) {
    171     addEvents({
    172         /* Time, Inputs, Outputs */
    173         {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
    174         {1, {{0, 1, UP}}, {}},
    175         {2, {{0, 1, DOWN}}, {}},
    176         {3, {{0, 1, UP}}, {}},
    177         {4, {{0, 1, DOWN}}, {}},
    178         {5, {{0, 1, UP}}, {}},
    179         {6, {{0, 1, DOWN}}, {}},
    180         {7, {{0, 1, UP}}, {}},
    181         {8, {{0, 1, DOWN}}, {}},
    182         {9, {{0, 1, UP}}, {}},
    183         {10, {{0, 1, DOWN}}, {}},
    184         {11, {{0, 1, UP}}, {}},
    185         {12, {{0, 1, DOWN}}, {}},
    186         {13, {{0, 1, UP}}, {}},
    187         {14, {{0, 1, DOWN}}, {}},
    188         {15, {{0, 1, UP}}, {}},
    189 
    190         {20, {}, {{0, 1, UP}}},
    191         /* Press key again after 1ms delay */
    192         {21, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
    193     });
    194     runEvents();
    195 }
    196 
    197 TEST_F(DebounceTest, OneKeyBouncing2) {
    198     addEvents({
    199         /* Time, Inputs, Outputs */
    200         {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
    201         /* Change twice in the same time period */
    202         {1, {{0, 1, UP}}, {}},
    203         {1, {{0, 1, DOWN}}, {}},
    204         /* Change three times in the same time period */
    205         {2, {{0, 1, UP}}, {}},
    206         {2, {{0, 1, DOWN}}, {}},
    207         {2, {{0, 1, UP}}, {}},
    208         /* Change twice in the same time period */
    209         {6, {{0, 1, DOWN}}, {}},
    210         {6, {{0, 1, UP}}, {}},
    211         /* Change three times in the same time period */
    212         {7, {{0, 1, DOWN}}, {}},
    213         {7, {{0, 1, UP}}, {}},
    214         {7, {{0, 1, DOWN}}, {}},
    215         /* Change twice in the same time period */
    216         {8, {{0, 1, UP}}, {}},
    217         {8, {{0, 1, DOWN}}, {}},
    218         /* Change three times in the same time period */
    219         {9, {{0, 1, UP}}, {}},
    220         {9, {{0, 1, DOWN}}, {}},
    221         {9, {{0, 1, UP}}, {}},
    222 
    223         {14, {}, {{0, 1, UP}}},
    224         /* Press key again after 1ms delay */
    225         {15, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
    226     });
    227     runEvents();
    228 }
    229 
    230 TEST_F(DebounceTest, OneKeyLong) {
    231     addEvents({
    232         /* Time, Inputs, Outputs */
    233         {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
    234 
    235         {25, {{0, 1, UP}}, {}},
    236 
    237         {30, {}, {{0, 1, UP}}},
    238 
    239         {50, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
    240 
    241         {75, {{0, 1, UP}}, {}},
    242 
    243         {80, {}, {{0, 1, UP}}},
    244 
    245         {100, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
    246     });
    247     runEvents();
    248 }
    249 
    250 TEST_F(DebounceTest, TwoKeysShort) {
    251     addEvents({
    252         /* Time, Inputs, Outputs */
    253         {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
    254         {1, {{0, 2, DOWN}}, {{0, 2, DOWN}}},
    255         /* Release key after 2ms delay */
    256         {2, {{0, 1, UP}}, {}},
    257         {3, {{0, 2, UP}}, {}},
    258 
    259         {5, {}, {}}, /* See OneKeyShort1 */
    260         {6, {}, {}}, /* See OneKeyShort1 */
    261 
    262         {10, {}, {{0, 1, UP}}}, /* 5ms+5ms after DOWN at time 0 */
    263         /* Press key again after 1ms delay */
    264         {11, {{0, 1, DOWN}}, {{0, 1, DOWN}, {0, 2, UP}}}, /* 5ms+5ms after DOWN at time 0 */
    265         {12, {{0, 2, DOWN}}, {{0, 2, DOWN}}},             /* 5ms+5ms after DOWN at time 0 */
    266     });
    267     runEvents();
    268 }
    269 
    270 TEST_F(DebounceTest, OneKeyDelayedScan1) {
    271     addEvents({
    272         /* Time, Inputs, Outputs */
    273         {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
    274 
    275         /* Processing is very late, immediately release key */
    276         {300, {{0, 1, UP}}, {}},
    277 
    278         {305, {}, {{0, 1, UP}}},
    279     });
    280     time_jumps_ = true;
    281     runEvents();
    282 }
    283 
    284 TEST_F(DebounceTest, OneKeyDelayedScan2) {
    285     addEvents({
    286         /* Time, Inputs, Outputs */
    287         {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
    288 
    289         /* Processing is very late, immediately release key */
    290         {300, {{0, 1, UP}}, {}},
    291 
    292         /* Processing is very late again */
    293         {600, {}, {{0, 1, UP}}},
    294     });
    295     time_jumps_ = true;
    296     runEvents();
    297 }
    298 
    299 TEST_F(DebounceTest, OneKeyDelayedScan3) {
    300     addEvents({
    301         /* Time, Inputs, Outputs */
    302         {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
    303 
    304         /* Processing is very late */
    305         {300, {}, {}},
    306         /* Release key after 1ms */
    307         {301, {{0, 1, UP}}, {}},
    308 
    309         {306, {}, {{0, 1, UP}}},
    310     });
    311     time_jumps_ = true;
    312     runEvents();
    313 }
    314 
    315 TEST_F(DebounceTest, OneKeyDelayedScan4) {
    316     addEvents({
    317         /* Time, Inputs, Outputs */
    318         {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
    319 
    320         /* Processing is very late */
    321         {300, {}, {}},
    322         /* Release key after 1ms */
    323         {301, {{0, 1, UP}}, {}},
    324 
    325         /* Processing is very late again */
    326         {600, {}, {{0, 1, UP}}},
    327     });
    328     time_jumps_ = true;
    329     runEvents();
    330 }
    331 
    332 TEST_F(DebounceTest, OneKeyDelayedScan5) {
    333     addEvents({
    334         /* Time, Inputs, Outputs */
    335         {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
    336 
    337         {5, {{0, 1, UP}}, {}},
    338 
    339         /* Processing is very late */
    340         {300, {}, {{0, 1, UP}}},
    341         /* Immediately press key again */
    342         {300, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
    343     });
    344     time_jumps_ = true;
    345     runEvents();
    346 }
    347 
    348 TEST_F(DebounceTest, OneKeyDelayedScan6) {
    349     addEvents({
    350         /* Time, Inputs, Outputs */
    351         {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
    352 
    353         {5, {{0, 1, UP}}, {}},
    354 
    355         /* Processing is very late */
    356         {300, {}, {{0, 1, UP}}},
    357 
    358         /* Press key again after 1ms */
    359         {301, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
    360     });
    361     time_jumps_ = true;
    362     runEvents();
    363 }
    364 
    365 TEST_F(DebounceTest, OneKeyDelayedScan7) {
    366     addEvents({
    367         /* Time, Inputs, Outputs */
    368         {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
    369 
    370         {5, {{0, 1, UP}}, {}},
    371 
    372         /* Press key again before debounce expires */
    373         {300, {{0, 1, DOWN}}, {}},
    374     });
    375     time_jumps_ = true;
    376     runEvents();
    377 }
    378 
    379 TEST_F(DebounceTest, OneKeyDelayedScan8) {
    380     addEvents({
    381         /* Time, Inputs, Outputs */
    382         {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
    383 
    384         /* Processing is a bit late */
    385         {50, {}, {}},
    386         /* Release key after 1ms */
    387         {51, {{0, 1, UP}}, {}},
    388 
    389         /* Processing is a bit late again */
    390         {100, {}, {{0, 1, UP}}},
    391     });
    392     time_jumps_ = true;
    393     runEvents();
    394 }
    395 
    396 TEST_F(DebounceTest, AsyncTickOneKeyShort1) {
    397     addEvents({
    398         /* Time, Inputs, Outputs */
    399         {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
    400         /* Release key after 1ms delay */
    401         {1, {{0, 1, UP}}, {}},
    402 
    403         /*
    404          * Until the eager timer on DOWN is observed to finish, the defer timer
    405          * on UP can't start. There's no workaround for this because it's not
    406          * possible to debounce an event that isn't being tracked.
    407          *
    408          * sym_defer_pk has the same problem but the test has to track that the
    409          * key changed state so the DOWN timer is always allowed to finish
    410          * before starting the UP timer.
    411          */
    412         {5, {}, {}},
    413 
    414         {10, {}, {{0, 1, UP}}}, /* 5ms+5ms after DOWN at time 0 */
    415         /* Press key again after 1ms delay */
    416         {11, {{0, 1, DOWN}}, {{0, 1, DOWN}}},
    417     });
    418     /*
    419      * Debounce implementations should never read the timer more than once per invocation
    420      */
    421     async_time_jumps_ = DEBOUNCE;
    422     runEvents();
    423 }