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, ThermalType::margin); 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 std::unique_ptr<PIDController> p; 41 EXPECT_THROW( 42 { 43 p = ThermalController::createThermalPid( 44 &z, "therm1", inputs, setpoint, initial, ThermalType::margin); 45 }, 46 std::exception); 47 EXPECT_TRUE(p == nullptr); 48 } 49 50 TEST(ThermalControllerTest, InputProc_BehavesAsExpected) 51 { 52 // This test just verifies inputProc behaves as expected. 53 54 ZoneMock z; 55 56 std::vector<std::string> inputs = {"fleeting0"}; 57 double setpoint = 10.0; 58 ec::pidinfo initial; 59 60 std::unique_ptr<PIDController> p = ThermalController::createThermalPid( 61 &z, "therm1", inputs, setpoint, initial, ThermalType::margin); 62 EXPECT_FALSE(p == nullptr); 63 64 EXPECT_CALL(z, getCachedValue(StrEq("fleeting0"))).WillOnce(Return(5.0)); 65 66 EXPECT_EQ(5.0, p->inputProc()); 67 } 68 69 TEST(ThermalControllerTest, SetPtProc_BehavesAsExpected) 70 { 71 // This test just verifies inputProc behaves as expected. 72 73 ZoneMock z; 74 75 std::vector<std::string> inputs = {"fleeting0"}; 76 double setpoint = 10.0; 77 ec::pidinfo initial; 78 79 std::unique_ptr<PIDController> p = ThermalController::createThermalPid( 80 &z, "therm1", inputs, setpoint, initial, ThermalType::margin); 81 EXPECT_FALSE(p == nullptr); 82 83 EXPECT_EQ(setpoint, p->setptProc()); 84 } 85 86 TEST(ThermalControllerTest, OutputProc_BehavesAsExpected) 87 { 88 // This test just verifies outputProc behaves as expected. 89 90 ZoneMock z; 91 92 std::vector<std::string> inputs = {"fleeting0"}; 93 double setpoint = 10.0; 94 ec::pidinfo initial; 95 96 std::unique_ptr<PIDController> p = ThermalController::createThermalPid( 97 &z, "therm1", inputs, setpoint, initial, ThermalType::margin); 98 EXPECT_FALSE(p == nullptr); 99 100 double value = 90.0; 101 EXPECT_CALL(z, addRPMSetPoint(value)); 102 103 p->outputProc(value); 104 } 105 106 TEST(ThermalControllerTest, InputProc_MultipleInputsAbsolute) 107 { 108 // This test verifies inputProc behaves as expected with multiple absolute 109 // inputs. 110 111 ZoneMock z; 112 113 std::vector<std::string> inputs = {"fleeting0", "fleeting1"}; 114 double setpoint = 10.0; 115 ec::pidinfo initial; 116 117 std::unique_ptr<PIDController> p = ThermalController::createThermalPid( 118 &z, "therm1", inputs, setpoint, initial, ThermalType::absolute); 119 EXPECT_FALSE(p == nullptr); 120 121 EXPECT_CALL(z, getCachedValue(StrEq("fleeting0"))).WillOnce(Return(5.0)); 122 EXPECT_CALL(z, getCachedValue(StrEq("fleeting1"))).WillOnce(Return(10.0)); 123 124 EXPECT_EQ(10.0, p->inputProc()); 125 } 126 127 TEST(ThermalControllerTest, InputProc_MultipleInputsMargin) 128 { 129 // This test verifies inputProc behaves as expected with multiple margin 130 // inputs. 131 132 ZoneMock z; 133 134 std::vector<std::string> inputs = {"fleeting0", "fleeting1"}; 135 double setpoint = 10.0; 136 ec::pidinfo initial; 137 138 std::unique_ptr<PIDController> p = ThermalController::createThermalPid( 139 &z, "therm1", inputs, setpoint, initial, ThermalType::margin); 140 EXPECT_FALSE(p == nullptr); 141 142 EXPECT_CALL(z, getCachedValue(StrEq("fleeting0"))).WillOnce(Return(5.0)); 143 EXPECT_CALL(z, getCachedValue(StrEq("fleeting1"))).WillOnce(Return(10.0)); 144 145 EXPECT_EQ(5.0, p->inputProc()); 146 }