123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- #include "gtest/gtest.h"
- #include "gmock/gmock.h"
- #include <vector>
- #include <algorithm>
- #include <stdio.h>
- extern "C" {
- #include "encoder.h"
- #include "encoder/tests/mock_split.h"
- }
- struct update {
- int8_t index;
- bool clockwise;
- };
- uint8_t uidx = 0;
- update updates[32];
- bool isLeftHand;
- bool encoder_update_kb(uint8_t index, bool clockwise) {
- if (!isLeftHand) {
-
- printf("ignoring update on right hand (%d,%s)\n", index, clockwise ? "CW" : "CC");
- return true;
- }
- updates[uidx % 32] = {index, clockwise};
- uidx++;
- return true;
- }
- bool setAndRead(pin_t pin, bool val) {
- setPin(pin, val);
- return encoder_read();
- }
- class EncoderTest : public ::testing::Test {
- protected:
- void SetUp() override {
- uidx = 0;
- for (int i = 0; i < 32; i++) {
- pinIsInputHigh[i] = 0;
- pins[i] = 0;
- }
- }
- };
- TEST_F(EncoderTest, TestInitLeft) {
- isLeftHand = true;
- encoder_init();
- EXPECT_EQ(pinIsInputHigh[0], true);
- EXPECT_EQ(pinIsInputHigh[1], true);
- EXPECT_EQ(pinIsInputHigh[2], false);
- EXPECT_EQ(pinIsInputHigh[3], false);
- EXPECT_EQ(uidx, 0);
- }
- TEST_F(EncoderTest, TestInitRight) {
- isLeftHand = false;
- encoder_init();
- EXPECT_EQ(pinIsInputHigh[0], false);
- EXPECT_EQ(pinIsInputHigh[1], false);
- EXPECT_EQ(pinIsInputHigh[2], true);
- EXPECT_EQ(pinIsInputHigh[3], true);
- EXPECT_EQ(uidx, 0);
- }
- TEST_F(EncoderTest, TestOneClockwiseLeft) {
- isLeftHand = true;
- encoder_init();
-
- setAndRead(0, false);
- setAndRead(1, false);
- setAndRead(0, true);
- setAndRead(1, true);
- EXPECT_EQ(uidx, 1);
- EXPECT_EQ(updates[0].index, 0);
- EXPECT_EQ(updates[0].clockwise, true);
- }
- TEST_F(EncoderTest, TestOneClockwiseRightSent) {
- isLeftHand = false;
- encoder_init();
-
- setAndRead(2, false);
- setAndRead(3, false);
- setAndRead(2, true);
- setAndRead(3, true);
- uint8_t slave_state[2] = {0};
- encoder_state_raw(slave_state);
- EXPECT_EQ((int8_t)slave_state[0], -1);
- }
- TEST_F(EncoderTest, TestOneCounterClockwiseRightReceived) {
- isLeftHand = true;
- encoder_init();
- uint8_t slave_state[2] = {0, 0};
- encoder_update_raw(slave_state);
- EXPECT_EQ(uidx, 1);
- EXPECT_EQ(updates[0].index, 1);
- EXPECT_EQ(updates[0].clockwise, false);
- }
|