aboutsummaryrefslogtreecommitdiff
path: root/quantum/encoder/tests/encoder_tests.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'quantum/encoder/tests/encoder_tests.cpp')
-rw-r--r--quantum/encoder/tests/encoder_tests.cpp144
1 files changed, 144 insertions, 0 deletions
diff --git a/quantum/encoder/tests/encoder_tests.cpp b/quantum/encoder/tests/encoder_tests.cpp
new file mode 100644
index 000000000..1888fdab8
--- /dev/null
+++ b/quantum/encoder/tests/encoder_tests.cpp
@@ -0,0 +1,144 @@
1/* Copyright 2021 Balz Guenat
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#include "gmock/gmock.h"
19#include <vector>
20#include <algorithm>
21#include <stdio.h>
22
23extern "C" {
24#include "encoder.h"
25#include "encoder/tests/mock.h"
26}
27
28struct update {
29 int8_t index;
30 bool clockwise;
31};
32
33uint8_t uidx = 0;
34update updates[32];
35
36bool encoder_update_kb(uint8_t index, bool clockwise) {
37 updates[uidx % 32] = {index, clockwise};
38 uidx++;
39 return true;
40}
41
42bool setAndRead(pin_t pin, bool val) {
43 setPin(pin, val);
44 return encoder_read();
45}
46
47class EncoderTest : public ::testing::Test {};
48
49TEST_F(EncoderTest, TestInit) {
50 uidx = 0;
51 encoder_init();
52 EXPECT_EQ(pinIsInputHigh[0], true);
53 EXPECT_EQ(pinIsInputHigh[1], true);
54 EXPECT_EQ(uidx, 0);
55}
56
57TEST_F(EncoderTest, TestOneClockwise) {
58 uidx = 0;
59 encoder_init();
60 // send 4 pulses. with resolution 4, that's one step and we should get 1 update.
61 setAndRead(0, false);
62 setAndRead(1, false);
63 setAndRead(0, true);
64 setAndRead(1, true);
65
66 EXPECT_EQ(uidx, 1);
67 EXPECT_EQ(updates[0].index, 0);
68 EXPECT_EQ(updates[0].clockwise, true);
69}
70
71TEST_F(EncoderTest, TestOneCounterClockwise) {
72 uidx = 0;
73 encoder_init();
74 setAndRead(1, false);
75 setAndRead(0, false);
76 setAndRead(1, true);
77 setAndRead(0, true);
78
79 EXPECT_EQ(uidx, 1);
80 EXPECT_EQ(updates[0].index, 0);
81 EXPECT_EQ(updates[0].clockwise, false);
82}
83
84TEST_F(EncoderTest, TestTwoClockwiseOneCC) {
85 uidx = 0;
86 encoder_init();
87 setAndRead(0, false);
88 setAndRead(1, false);
89 setAndRead(0, true);
90 setAndRead(1, true);
91 setAndRead(0, false);
92 setAndRead(1, false);
93 setAndRead(0, true);
94 setAndRead(1, true);
95 setAndRead(1, false);
96 setAndRead(0, false);
97 setAndRead(1, true);
98 setAndRead(0, true);
99
100 EXPECT_EQ(uidx, 3);
101 EXPECT_EQ(updates[0].index, 0);
102 EXPECT_EQ(updates[0].clockwise, true);
103 EXPECT_EQ(updates[1].index, 0);
104 EXPECT_EQ(updates[1].clockwise, true);
105 EXPECT_EQ(updates[2].index, 0);
106 EXPECT_EQ(updates[2].clockwise, false);
107}
108
109TEST_F(EncoderTest, TestNoEarly) {
110 uidx = 0;
111 encoder_init();
112 // send 3 pulses. with resolution 4, that's not enough for a step.
113 setAndRead(0, false);
114 setAndRead(1, false);
115 setAndRead(0, true);
116 EXPECT_EQ(uidx, 0);
117 // now send last pulse
118 setAndRead(1, true);
119 EXPECT_EQ(uidx, 1);
120 EXPECT_EQ(updates[0].index, 0);
121 EXPECT_EQ(updates[0].clockwise, true);
122}
123
124TEST_F(EncoderTest, TestHalfway) {
125 uidx = 0;
126 encoder_init();
127 // go halfway
128 setAndRead(0, false);
129 setAndRead(1, false);
130 EXPECT_EQ(uidx, 0);
131 // back off
132 setAndRead(1, true);
133 setAndRead(0, true);
134 EXPECT_EQ(uidx, 0);
135 // go all the way
136 setAndRead(0, false);
137 setAndRead(1, false);
138 setAndRead(0, true);
139 setAndRead(1, true);
140 // should result in 1 update
141 EXPECT_EQ(uidx, 1);
142 EXPECT_EQ(updates[0].index, 0);
143 EXPECT_EQ(updates[0].clockwise, true);
144}