1 #include "pid/controller.hpp"
2 #include "pid/ec/stepwise.hpp"
3 #include "pid/stepwisecontroller.hpp"
4 #include "test/zone_mock.hpp"
5 
6 #include <string>
7 #include <vector>
8 
9 #include <gmock/gmock.h>
10 #include <gtest/gtest.h>
11 
12 using ::testing::Return;
13 using ::testing::StrEq;
14 
15 constexpr size_t scale = 100; // values are 10 for 10%
16 
17 TEST(StepwiseControllerTest, HysteresisTestPositive)
18 {
19     // Verifies positive hysteresis works as expected
20 
21     ZoneMock z;
22 
23     std::vector<std::string> inputs = {"test"};
24     ec::StepwiseInfo initial;
25     initial.negativeHysteresis = 3.0;
26     initial.positiveHysteresis = 2.0;
27     initial.reading[0] = 20.0;
28     initial.reading[1] = 30.0;
29     initial.reading[2] = std::numeric_limits<double>::quiet_NaN();
30     initial.output[0] = 40.0;
31     initial.output[1] = 60.0;
32 
33     std::unique_ptr<Controller> p =
34         StepwiseController::createStepwiseController(&z, "foo", inputs,
35                                                      initial);
36 
37     EXPECT_CALL(z, getCachedValue(StrEq("test")))
38         .Times(3)
39         .WillOnce(Return(29.0))  // return 40
40         .WillOnce(Return(31.0))  // return 40
41         .WillOnce(Return(32.0)); // return 60
42 
43     EXPECT_CALL(z, addRPMSetPoint(40.0 * scale)).Times(2);
44     EXPECT_CALL(z, addRPMSetPoint(60.0 * scale)).Times(1);
45 
46     for (int ii = 0; ii < 3; ii++)
47     {
48         p->process();
49     }
50 }
51 
52 TEST(StepwiseControllerTest, HysteresisTestNegative)
53 {
54     // Verifies negative hysteresis works as expected
55 
56     ZoneMock z;
57 
58     std::vector<std::string> inputs = {"test"};
59     ec::StepwiseInfo initial;
60     initial.negativeHysteresis = 3.0;
61     initial.positiveHysteresis = 2.0;
62     initial.reading[0] = 20.0;
63     initial.reading[1] = 30.0;
64     initial.reading[2] = std::numeric_limits<double>::quiet_NaN();
65     initial.output[0] = 40.0;
66     initial.output[1] = 60.0;
67 
68     std::unique_ptr<Controller> p =
69         StepwiseController::createStepwiseController(&z, "foo", inputs,
70                                                      initial);
71 
72     EXPECT_CALL(z, getCachedValue(StrEq("test")))
73         .Times(3)
74         .WillOnce(Return(30.0))  // return 60
75         .WillOnce(Return(27.0))  // return 60
76         .WillOnce(Return(26.0)); // return 40
77 
78     EXPECT_CALL(z, addRPMSetPoint(40.0 * scale)).Times(1);
79     EXPECT_CALL(z, addRPMSetPoint(60.0 * scale)).Times(2);
80 
81     for (int ii = 0; ii < 3; ii++)
82     {
83         p->process();
84     }
85 }
86