1 #include "pid/ec/pid.hpp"
2 #include "pid/thermalcontroller.hpp"
3 #include "test/zone_mock.hpp"
4 
5 #include <string>
6 #include <vector>
7 
8 #include <gmock/gmock.h>
9 #include <gtest/gtest.h>
10 
11 using ::testing::Return;
12 using ::testing::StrEq;
13 
14 TEST(ThermalControllerTest, BoringFactoryTest)
15 {
16     // Verifies building a ThermalPIDController with the factory works as
17     // expected in the boring (uninteresting) case.
18 
19     ZoneMock z;
20 
21     std::vector<std::string> inputs = {"fleeting0"};
22     double setpoint = 10.0;
23     ec::pidinfo initial;
24 
25     std::unique_ptr<PIDController> p = ThermalController::createThermalPid(
26         &z, "therm1", inputs, setpoint, initial);
27     // Success
28     EXPECT_FALSE(p == nullptr);
29 }
30 
31 TEST(ThermalControllerTest, VerifyFactoryFailsWithZeroInputs)
32 {
33     // A thermal controller needs at least one input.
34 
35     ZoneMock z;
36 
37     std::vector<std::string> inputs = {};
38     double setpoint = 10.0;
39     ec::pidinfo initial;
40 
41     std::unique_ptr<PIDController> p = ThermalController::createThermalPid(
42         &z, "therm1", inputs, setpoint, initial);
43     EXPECT_TRUE(p == nullptr);
44 }
45 
46 TEST(ThermalControllerTest, VerifyFactoryFailsForMoreThanOneInput)
47 {
48     // ThermalControllers currently only support one input, so don't let
49     // someone accidentally specify more.
50 
51     ZoneMock z;
52 
53     std::vector<std::string> inputs = {"fleeting0", "asdf"};
54     double setpoint = 10.0;
55     ec::pidinfo initial;
56 
57     std::unique_ptr<PIDController> p = ThermalController::createThermalPid(
58         &z, "therm1", inputs, setpoint, initial);
59     EXPECT_TRUE(p == nullptr);
60 }
61 
62 TEST(ThermalControllerTest, InputProc_BehavesAsExpected)
63 {
64     // This test just verifies inputProc behaves as expected.
65 
66     ZoneMock z;
67 
68     std::vector<std::string> inputs = {"fleeting0"};
69     double setpoint = 10.0;
70     ec::pidinfo initial;
71 
72     std::unique_ptr<PIDController> p = ThermalController::createThermalPid(
73         &z, "therm1", inputs, setpoint, initial);
74     EXPECT_FALSE(p == nullptr);
75 
76     EXPECT_CALL(z, getCachedValue(StrEq("fleeting0"))).WillOnce(Return(5.0));
77 
78     EXPECT_EQ(5.0, p->inputProc());
79 }
80 
81 TEST(ThermalControllerTest, SetPtProc_BehavesAsExpected)
82 {
83     // This test just verifies inputProc behaves as expected.
84 
85     ZoneMock z;
86 
87     std::vector<std::string> inputs = {"fleeting0"};
88     double setpoint = 10.0;
89     ec::pidinfo initial;
90 
91     std::unique_ptr<PIDController> p = ThermalController::createThermalPid(
92         &z, "therm1", inputs, setpoint, initial);
93     EXPECT_FALSE(p == nullptr);
94 
95     EXPECT_EQ(setpoint, p->setptProc());
96 }
97 
98 TEST(ThermalControllerTest, OutputProc_BehavesAsExpected)
99 {
100     // This test just verifies inputProc behaves as expected.
101 
102     ZoneMock z;
103 
104     std::vector<std::string> inputs = {"fleeting0"};
105     double setpoint = 10.0;
106     ec::pidinfo initial;
107 
108     std::unique_ptr<PIDController> p = ThermalController::createThermalPid(
109         &z, "therm1", inputs, setpoint, initial);
110     EXPECT_FALSE(p == nullptr);
111 
112     double value = 90.0;
113     EXPECT_CALL(z, addRPMSetPoint(value));
114 
115     p->outputProc(value);
116 }
117