Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 1 | #include "pid/ec/pid.hpp" |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 2 | #include "pid/thermalcontroller.hpp" |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 3 | #include "test/zone_mock.hpp" |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 4 | |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 5 | #include <string> |
| 6 | #include <vector> |
| 7 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 8 | #include <gmock/gmock.h> |
| 9 | #include <gtest/gtest.h> |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 10 | |
| 11 | using ::testing::Return; |
| 12 | using ::testing::StrEq; |
| 13 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 14 | TEST(ThermalControllerTest, BoringFactoryTest) |
| 15 | { |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 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"}; |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 22 | double setpoint = 10.0; |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 23 | ec::pidinfo initial; |
| 24 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 25 | std::unique_ptr<PIDController> p = ThermalController::createThermalPid( |
James Feist | 734f953 | 2018-11-15 12:13:18 -0800 | [diff] [blame] | 26 | &z, "therm1", inputs, setpoint, initial, ThermalType::margin); |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 27 | // Success |
| 28 | EXPECT_FALSE(p == nullptr); |
| 29 | } |
| 30 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 31 | TEST(ThermalControllerTest, VerifyFactoryFailsWithZeroInputs) |
| 32 | { |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 33 | // A thermal controller needs at least one input. |
| 34 | |
| 35 | ZoneMock z; |
| 36 | |
| 37 | std::vector<std::string> inputs = {}; |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 38 | double setpoint = 10.0; |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 39 | ec::pidinfo initial; |
James Feist | 734f953 | 2018-11-15 12:13:18 -0800 | [diff] [blame] | 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); |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 47 | EXPECT_TRUE(p == nullptr); |
| 48 | } |
| 49 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 50 | TEST(ThermalControllerTest, InputProc_BehavesAsExpected) |
| 51 | { |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 52 | // This test just verifies inputProc behaves as expected. |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 53 | |
| 54 | ZoneMock z; |
| 55 | |
| 56 | std::vector<std::string> inputs = {"fleeting0"}; |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 57 | double setpoint = 10.0; |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 58 | ec::pidinfo initial; |
| 59 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 60 | std::unique_ptr<PIDController> p = ThermalController::createThermalPid( |
James Feist | 734f953 | 2018-11-15 12:13:18 -0800 | [diff] [blame] | 61 | &z, "therm1", inputs, setpoint, initial, ThermalType::margin); |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 62 | EXPECT_FALSE(p == nullptr); |
| 63 | |
| 64 | EXPECT_CALL(z, getCachedValue(StrEq("fleeting0"))).WillOnce(Return(5.0)); |
| 65 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 66 | EXPECT_EQ(5.0, p->inputProc()); |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 69 | TEST(ThermalControllerTest, SetPtProc_BehavesAsExpected) |
| 70 | { |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 71 | // This test just verifies inputProc behaves as expected. |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 72 | |
| 73 | ZoneMock z; |
| 74 | |
| 75 | std::vector<std::string> inputs = {"fleeting0"}; |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 76 | double setpoint = 10.0; |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 77 | ec::pidinfo initial; |
| 78 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 79 | std::unique_ptr<PIDController> p = ThermalController::createThermalPid( |
James Feist | 734f953 | 2018-11-15 12:13:18 -0800 | [diff] [blame] | 80 | &z, "therm1", inputs, setpoint, initial, ThermalType::margin); |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 81 | EXPECT_FALSE(p == nullptr); |
| 82 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 83 | EXPECT_EQ(setpoint, p->setptProc()); |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 86 | TEST(ThermalControllerTest, OutputProc_BehavesAsExpected) |
| 87 | { |
James Feist | 734f953 | 2018-11-15 12:13:18 -0800 | [diff] [blame] | 88 | // This test just verifies outputProc behaves as expected. |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 89 | |
| 90 | ZoneMock z; |
| 91 | |
| 92 | std::vector<std::string> inputs = {"fleeting0"}; |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 93 | double setpoint = 10.0; |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 94 | ec::pidinfo initial; |
| 95 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 96 | std::unique_ptr<PIDController> p = ThermalController::createThermalPid( |
James Feist | 734f953 | 2018-11-15 12:13:18 -0800 | [diff] [blame] | 97 | &z, "therm1", inputs, setpoint, initial, ThermalType::margin); |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 98 | EXPECT_FALSE(p == nullptr); |
| 99 | |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 100 | double value = 90.0; |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 101 | EXPECT_CALL(z, addRPMSetPoint(value)); |
| 102 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 103 | p->outputProc(value); |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 104 | } |
James Feist | 734f953 | 2018-11-15 12:13:18 -0800 | [diff] [blame] | 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 | } |