aboutsummaryrefslogtreecommitdiff
path: root/quantum/encoder/tests/encoder_tests_split.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'quantum/encoder/tests/encoder_tests_split.cpp')
-rw-r--r--quantum/encoder/tests/encoder_tests_split.cpp143
1 files changed, 143 insertions, 0 deletions
diff --git a/quantum/encoder/tests/encoder_tests_split.cpp b/quantum/encoder/tests/encoder_tests_split.cpp
new file mode 100644
index 000000000..25e52c83f
--- /dev/null
+++ b/quantum/encoder/tests/encoder_tests_split.cpp
@@ -0,0 +1,143 @@
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_split.h"
26}
27
28struct update {
29 int8_t index;
30 bool clockwise;
31};
32
33uint8_t uidx = 0;
34update updates[32];
35
36bool isLeftHand;
37
38bool encoder_update_kb(uint8_t index, bool clockwise) {
39 if (!isLeftHand) {
40 // this method has no effect on slave half
41 printf("ignoring update on right hand (%d,%s)\n", index, clockwise ? "CW" : "CC");
42 return true;
43 }
44 updates[uidx % 32] = {index, clockwise};
45 uidx++;
46 return true;
47}
48
49bool setAndRead(pin_t pin, bool val) {
50 setPin(pin, val);
51 return encoder_read();
52}
53
54class EncoderTest : public ::testing::Test {
55 protected:
56 void SetUp() override {
57 uidx = 0;
58 for (int i = 0; i < 32; i++) {
59 pinIsInputHigh[i] = 0;
60 pins[i] = 0;
61 }
62 }
63};
64
65TEST_F(EncoderTest, TestInitLeft) {
66 isLeftHand = true;
67 encoder_init();
68 EXPECT_EQ(pinIsInputHigh[0], true);
69 EXPECT_EQ(pinIsInputHigh[1], true);
70 EXPECT_EQ(pinIsInputHigh[2], false);
71 EXPECT_EQ(pinIsInputHigh[3], false);
72 EXPECT_EQ(uidx, 0);
73}
74
75TEST_F(EncoderTest, TestInitRight) {
76 isLeftHand = false;
77 encoder_init();
78 EXPECT_EQ(pinIsInputHigh[0], false);
79 EXPECT_EQ(pinIsInputHigh[1], false);
80 EXPECT_EQ(pinIsInputHigh[2], true);
81 EXPECT_EQ(pinIsInputHigh[3], true);
82 EXPECT_EQ(uidx, 0);
83}
84
85TEST_F(EncoderTest, TestOneClockwiseLeft) {
86 isLeftHand = true;
87 encoder_init();
88 // send 4 pulses. with resolution 4, that's one step and we should get 1 update.
89 setAndRead(0, false);
90 setAndRead(1, false);
91 setAndRead(0, true);
92 setAndRead(1, true);
93
94 EXPECT_EQ(uidx, 1);
95 EXPECT_EQ(updates[0].index, 0);
96 EXPECT_EQ(updates[0].clockwise, true);
97}
98
99TEST_F(EncoderTest, TestOneClockwiseRightSent) {
100 isLeftHand = false;
101 encoder_init();
102 // send 4 pulses. with resolution 4, that's one step and we should get 1 update.
103 setAndRead(2, false);
104 setAndRead(3, false);
105 setAndRead(2, true);
106 setAndRead(3, true);
107
108 uint8_t slave_state[2] = {0};
109 encoder_state_raw(slave_state);
110
111 EXPECT_EQ((int8_t)slave_state[0], -1);
112}
113
114/* this test will not work after the previous test.
115 * this is due to encoder_value[1] already being set to -1 when simulating the right half.
116 * When we now receive this update acting as the left half, there is no change.
117 * This is hard to mock, as the static values inside encoder.c normally exist twice, once on each half,
118 * but here, they only exist once.
119 */
120
121// TEST_F(EncoderTest, TestOneClockwiseRightReceived) {
122// isLeftHand = true;
123// encoder_init();
124
125// uint8_t slave_state[2] = {255, 0};
126// encoder_update_raw(slave_state);
127
128// EXPECT_EQ(uidx, 1);
129// EXPECT_EQ(updates[0].index, 1);
130// EXPECT_EQ(updates[0].clockwise, true);
131// }
132
133TEST_F(EncoderTest, TestOneCounterClockwiseRightReceived) {
134 isLeftHand = true;
135 encoder_init();
136
137 uint8_t slave_state[2] = {0, 0};
138 encoder_update_raw(slave_state);
139
140 EXPECT_EQ(uidx, 1);
141 EXPECT_EQ(updates[0].index, 1);
142 EXPECT_EQ(updates[0].clockwise, false);
143}