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