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 TEST(StepwiseControllerTest, HysteresisTestPositive)
16 {
17     // Verifies positive hysteresis works as expected
18 
19     ZoneMock z;
20 
21     std::vector<std::string> inputs = {"test"};
22     ec::StepwiseInfo initial;
23     initial.negativeHysteresis = 3.0;
24     initial.positiveHysteresis = 2.0;
25     initial.reading[0] = 20.0;
26     initial.reading[1] = 30.0;
27     initial.reading[2] = std::numeric_limits<double>::quiet_NaN();
28     initial.output[0] = 40.0;
29     initial.output[1] = 60.0;
30 
31     std::unique_ptr<Controller> p =
32         StepwiseController::createStepwiseController(&z, "foo", inputs,
33                                                      initial);
34 
35     EXPECT_CALL(z, getCachedValue(StrEq("test")))
36         .Times(3)
37         .WillOnce(Return(29.0))  // return 40
38         .WillOnce(Return(31.0))  // return 40
39         .WillOnce(Return(32.0)); // return 60
40 
41     EXPECT_CALL(z, addRPMSetPoint(40.0)).Times(2);
42     EXPECT_CALL(z, addRPMSetPoint(60.0)).Times(1);
43 
44     for (int ii = 0; ii < 3; ii++)
45     {
46         p->process();
47     }
48 }
49 
50 TEST(StepwiseControllerTest, HysteresisTestNegative)
51 {
52     // Verifies negative hysteresis works as expected
53 
54     ZoneMock z;
55 
56     std::vector<std::string> inputs = {"test"};
57     ec::StepwiseInfo initial;
58     initial.negativeHysteresis = 3.0;
59     initial.positiveHysteresis = 2.0;
60     initial.reading[0] = 20.0;
61     initial.reading[1] = 30.0;
62     initial.reading[2] = std::numeric_limits<double>::quiet_NaN();
63     initial.output[0] = 40.0;
64     initial.output[1] = 60.0;
65 
66     std::unique_ptr<Controller> p =
67         StepwiseController::createStepwiseController(&z, "foo", inputs,
68                                                      initial);
69 
70     EXPECT_CALL(z, getCachedValue(StrEq("test")))
71         .Times(3)
72         .WillOnce(Return(30.0))  // return 60
73         .WillOnce(Return(27.0))  // return 60
74         .WillOnce(Return(26.0)); // return 40
75 
76     EXPECT_CALL(z, addRPMSetPoint(40.0)).Times(1);
77     EXPECT_CALL(z, addRPMSetPoint(60.0)).Times(2);
78 
79     for (int ii = 0; ii < 3; ii++)
80     {
81         p->process();
82     }
83 }
84