aboutsummaryrefslogtreecommitdiff
path: root/quantum
diff options
context:
space:
mode:
Diffstat (limited to 'quantum')
-rw-r--r--quantum/encoder/tests/encoder_tests.cpp144
-rw-r--r--quantum/encoder/tests/encoder_tests_split.cpp143
-rw-r--r--quantum/encoder/tests/mock.c34
-rw-r--r--quantum/encoder/tests/mock.h40
-rw-r--r--quantum/encoder/tests/mock_split.c36
-rw-r--r--quantum/encoder/tests/mock_split.h48
-rw-r--r--quantum/encoder/tests/rules.mk13
-rw-r--r--quantum/encoder/tests/testlist.mk3
8 files changed, 461 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}
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}
diff --git a/quantum/encoder/tests/mock.c b/quantum/encoder/tests/mock.c
new file mode 100644
index 000000000..d0506a938
--- /dev/null
+++ b/quantum/encoder/tests/mock.c
@@ -0,0 +1,34 @@
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 "mock.h"
18
19bool pins[32] = {0};
20bool pinIsInputHigh[32] = {0};
21
22uint8_t mockSetPinInputHigh(pin_t pin) {
23 // dprintf("Setting pin %d input high.", pin);
24 pins[pin] = true;
25 pinIsInputHigh[pin] = true;
26 return 0;
27}
28
29bool mockReadPin(pin_t pin) { return pins[pin]; }
30
31bool setPin(pin_t pin, bool val) {
32 pins[pin] = val;
33 return val;
34}
diff --git a/quantum/encoder/tests/mock.h b/quantum/encoder/tests/mock.h
new file mode 100644
index 000000000..dbc25a084
--- /dev/null
+++ b/quantum/encoder/tests/mock.h
@@ -0,0 +1,40 @@
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#pragma once
18
19#include <stdint.h>
20#include <stdbool.h>
21
22/* Here, "pins" from 0 to 31 are allowed. */
23#define ENCODERS_PAD_A \
24 { 0 }
25#define ENCODERS_PAD_B \
26 { 1 }
27
28typedef uint8_t pin_t;
29
30extern bool pins[];
31extern bool pinIsInputHigh[];
32
33#define setPinInputHigh(pin) (mockSetPinInputHigh(pin))
34#define readPin(pin) (mockReadPin(pin))
35
36uint8_t mockSetPinInputHigh(pin_t pin);
37
38bool mockReadPin(pin_t pin);
39
40bool setPin(pin_t pin, bool val);
diff --git a/quantum/encoder/tests/mock_split.c b/quantum/encoder/tests/mock_split.c
new file mode 100644
index 000000000..68bf3af59
--- /dev/null
+++ b/quantum/encoder/tests/mock_split.c
@@ -0,0 +1,36 @@
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 "mock_split.h"
18
19bool pins[32] = {0};
20bool pinIsInputHigh[32] = {0};
21
22uint8_t mockSetPinInputHigh(pin_t pin) {
23 // dprintf("Setting pin %d input high.", pin);
24 pins[pin] = true;
25 pinIsInputHigh[pin] = true;
26 return 0;
27}
28
29bool mockReadPin(pin_t pin) { return pins[pin]; }
30
31bool setPin(pin_t pin, bool val) {
32 pins[pin] = val;
33 return val;
34}
35
36void last_encoder_activity_trigger(void) {}
diff --git a/quantum/encoder/tests/mock_split.h b/quantum/encoder/tests/mock_split.h
new file mode 100644
index 000000000..0ae62652f
--- /dev/null
+++ b/quantum/encoder/tests/mock_split.h
@@ -0,0 +1,48 @@
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#pragma once
18
19#include <stdint.h>
20#include <stdbool.h>
21
22#define SPLIT_KEYBOARD
23/* Here, "pins" from 0 to 31 are allowed. */
24#define ENCODERS_PAD_A \
25 { 0 }
26#define ENCODERS_PAD_B \
27 { 1 }
28#define ENCODERS_PAD_A_RIGHT \
29 { 2 }
30#define ENCODERS_PAD_B_RIGHT \
31 { 3 }
32
33typedef uint8_t pin_t;
34extern bool isLeftHand;
35void encoder_state_raw(uint8_t* slave_state);
36void encoder_update_raw(uint8_t* slave_state);
37
38extern bool pins[];
39extern bool pinIsInputHigh[];
40
41#define setPinInputHigh(pin) (mockSetPinInputHigh(pin))
42#define readPin(pin) (mockReadPin(pin))
43
44uint8_t mockSetPinInputHigh(pin_t pin);
45
46bool mockReadPin(pin_t pin);
47
48bool setPin(pin_t pin, bool val);
diff --git a/quantum/encoder/tests/rules.mk b/quantum/encoder/tests/rules.mk
new file mode 100644
index 000000000..b826ce3ae
--- /dev/null
+++ b/quantum/encoder/tests/rules.mk
@@ -0,0 +1,13 @@
1encoder_DEFS := -DENCODER_MOCK_SINGLE
2
3encoder_SRC := \
4 $(QUANTUM_PATH)/encoder/tests/mock.c \
5 $(QUANTUM_PATH)/encoder/tests/encoder_tests.cpp \
6 $(QUANTUM_PATH)/encoder.c
7
8encoder_split_DEFS := -DENCODER_MOCK_SPLIT
9
10encoder_split_SRC := \
11 $(QUANTUM_PATH)/encoder/tests/mock_split.c \
12 $(QUANTUM_PATH)/encoder/tests/encoder_tests_split.cpp \
13 $(QUANTUM_PATH)/encoder.c
diff --git a/quantum/encoder/tests/testlist.mk b/quantum/encoder/tests/testlist.mk
new file mode 100644
index 000000000..1be9f4a05
--- /dev/null
+++ b/quantum/encoder/tests/testlist.mk
@@ -0,0 +1,3 @@
1TEST_LIST += \
2 encoder \
3 encoder_split