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"}; |
| 22 | float setpoint = 10.0; |
| 23 | ec::pidinfo initial; |
| 24 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 25 | std::unique_ptr<PIDController> p = ThermalController::CreateThermalPid( |
| 26 | &z, "therm1", inputs, setpoint, initial); |
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 = {}; |
| 38 | float setpoint = 10.0; |
| 39 | ec::pidinfo initial; |
| 40 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 41 | std::unique_ptr<PIDController> p = ThermalController::CreateThermalPid( |
| 42 | &z, "therm1", inputs, setpoint, initial); |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 43 | EXPECT_TRUE(p == nullptr); |
| 44 | } |
| 45 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 46 | TEST(ThermalControllerTest, VerifyFactoryFailsForMoreThanOneInput) |
| 47 | { |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 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 | float setpoint = 10.0; |
| 55 | ec::pidinfo initial; |
| 56 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 57 | std::unique_ptr<PIDController> p = ThermalController::CreateThermalPid( |
| 58 | &z, "therm1", inputs, setpoint, initial); |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 59 | EXPECT_TRUE(p == nullptr); |
| 60 | } |
| 61 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 62 | TEST(ThermalControllerTest, InputProc_BehavesAsExpected) |
| 63 | { |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 64 | // This test just verifies input_proc behaves as expected. |
| 65 | |
| 66 | ZoneMock z; |
| 67 | |
| 68 | std::vector<std::string> inputs = {"fleeting0"}; |
| 69 | float setpoint = 10.0; |
| 70 | ec::pidinfo initial; |
| 71 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 72 | std::unique_ptr<PIDController> p = ThermalController::CreateThermalPid( |
| 73 | &z, "therm1", inputs, setpoint, initial); |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 74 | EXPECT_FALSE(p == nullptr); |
| 75 | |
| 76 | EXPECT_CALL(z, getCachedValue(StrEq("fleeting0"))).WillOnce(Return(5.0)); |
| 77 | |
| 78 | EXPECT_EQ(5.0, p->input_proc()); |
| 79 | } |
| 80 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 81 | TEST(ThermalControllerTest, SetPtProc_BehavesAsExpected) |
| 82 | { |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 83 | // This test just verifies input_proc behaves as expected. |
| 84 | |
| 85 | ZoneMock z; |
| 86 | |
| 87 | std::vector<std::string> inputs = {"fleeting0"}; |
| 88 | float setpoint = 10.0; |
| 89 | ec::pidinfo initial; |
| 90 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 91 | std::unique_ptr<PIDController> p = ThermalController::CreateThermalPid( |
| 92 | &z, "therm1", inputs, setpoint, initial); |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 93 | EXPECT_FALSE(p == nullptr); |
| 94 | |
| 95 | EXPECT_EQ(setpoint, p->setpt_proc()); |
| 96 | } |
| 97 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 98 | TEST(ThermalControllerTest, OutputProc_BehavesAsExpected) |
| 99 | { |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 100 | // This test just verifies input_proc behaves as expected. |
| 101 | |
| 102 | ZoneMock z; |
| 103 | |
| 104 | std::vector<std::string> inputs = {"fleeting0"}; |
| 105 | float setpoint = 10.0; |
| 106 | ec::pidinfo initial; |
| 107 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 108 | std::unique_ptr<PIDController> p = ThermalController::CreateThermalPid( |
| 109 | &z, "therm1", inputs, setpoint, initial); |
Patrick Venture | 3349ef2 | 2018-06-12 14:09:29 -0700 | [diff] [blame] | 110 | EXPECT_FALSE(p == nullptr); |
| 111 | |
| 112 | float value = 90.0; |
| 113 | EXPECT_CALL(z, addRPMSetPoint(value)); |
| 114 | |
| 115 | p->output_proc(value); |
| 116 | } |