You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
3.8 KiB

  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. #include "gtest/gtest.h"
  17. #include "gmock/gmock.h"
  18. #include <vector>
  19. #include <algorithm>
  20. #include <stdio.h>
  21. extern "C" {
  22. #include "encoder.h"
  23. #include "encoder/tests/mock_split.h"
  24. }
  25. struct update {
  26. int8_t index;
  27. bool clockwise;
  28. };
  29. uint8_t uidx = 0;
  30. update updates[32];
  31. bool isLeftHand;
  32. bool encoder_update_kb(uint8_t index, bool clockwise) {
  33. if (!isLeftHand) {
  34. // this method has no effect on slave half
  35. printf("ignoring update on right hand (%d,%s)\n", index, clockwise ? "CW" : "CC");
  36. return true;
  37. }
  38. updates[uidx % 32] = {index, clockwise};
  39. uidx++;
  40. return true;
  41. }
  42. bool setAndRead(pin_t pin, bool val) {
  43. setPin(pin, val);
  44. return encoder_read();
  45. }
  46. class EncoderTest : public ::testing::Test {
  47. protected:
  48. void SetUp() override {
  49. uidx = 0;
  50. for (int i = 0; i < 32; i++) {
  51. pinIsInputHigh[i] = 0;
  52. pins[i] = 0;
  53. }
  54. }
  55. };
  56. TEST_F(EncoderTest, TestInitLeft) {
  57. isLeftHand = true;
  58. encoder_init();
  59. EXPECT_EQ(pinIsInputHigh[0], true);
  60. EXPECT_EQ(pinIsInputHigh[1], true);
  61. EXPECT_EQ(pinIsInputHigh[2], false);
  62. EXPECT_EQ(pinIsInputHigh[3], false);
  63. EXPECT_EQ(uidx, 0);
  64. }
  65. TEST_F(EncoderTest, TestInitRight) {
  66. isLeftHand = false;
  67. encoder_init();
  68. EXPECT_EQ(pinIsInputHigh[0], false);
  69. EXPECT_EQ(pinIsInputHigh[1], false);
  70. EXPECT_EQ(pinIsInputHigh[2], true);
  71. EXPECT_EQ(pinIsInputHigh[3], true);
  72. EXPECT_EQ(uidx, 0);
  73. }
  74. TEST_F(EncoderTest, TestOneClockwiseLeft) {
  75. isLeftHand = true;
  76. encoder_init();
  77. // send 4 pulses. with resolution 4, that's one step and we should get 1 update.
  78. setAndRead(0, false);
  79. setAndRead(1, false);
  80. setAndRead(0, true);
  81. setAndRead(1, true);
  82. EXPECT_EQ(uidx, 1);
  83. EXPECT_EQ(updates[0].index, 0);
  84. EXPECT_EQ(updates[0].clockwise, true);
  85. }
  86. TEST_F(EncoderTest, TestOneClockwiseRightSent) {
  87. isLeftHand = false;
  88. encoder_init();
  89. // send 4 pulses. with resolution 4, that's one step and we should get 1 update.
  90. setAndRead(2, false);
  91. setAndRead(3, false);
  92. setAndRead(2, true);
  93. setAndRead(3, true);
  94. uint8_t slave_state[2] = {0};
  95. encoder_state_raw(slave_state);
  96. EXPECT_EQ((int8_t)slave_state[0], -1);
  97. }
  98. /* this test will not work after the previous test.
  99. * this is due to encoder_value[1] already being set to -1 when simulating the right half.
  100. * When we now receive this update acting as the left half, there is no change.
  101. * This is hard to mock, as the static values inside encoder.c normally exist twice, once on each half,
  102. * but here, they only exist once.
  103. */
  104. // TEST_F(EncoderTest, TestOneClockwiseRightReceived) {
  105. // isLeftHand = true;
  106. // encoder_init();
  107. // uint8_t slave_state[2] = {255, 0};
  108. // encoder_update_raw(slave_state);
  109. // EXPECT_EQ(uidx, 1);
  110. // EXPECT_EQ(updates[0].index, 1);
  111. // EXPECT_EQ(updates[0].clockwise, true);
  112. // }
  113. TEST_F(EncoderTest, TestOneCounterClockwiseRightReceived) {
  114. isLeftHand = true;
  115. encoder_init();
  116. uint8_t slave_state[2] = {0, 0};
  117. encoder_update_raw(slave_state);
  118. EXPECT_EQ(uidx, 1);
  119. EXPECT_EQ(updates[0].index, 1);
  120. EXPECT_EQ(updates[0].clockwise, false);
  121. }