From 4e3e4d75d4073edae20b8eb1fbebbb514c487e75 Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Tue, 24 Sep 2024 19:45:58 -0700 Subject: [PATCH] fix heater control timer test --- firmware/util/timer.cpp | 4 ++++ firmware/util/timer.h | 1 + test/tests/test_heater.cpp | 24 ++++++++++++++++++++++-- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/firmware/util/timer.cpp b/firmware/util/timer.cpp index d6013e83..9192304c 100644 --- a/firmware/util/timer.cpp +++ b/firmware/util/timer.cpp @@ -24,6 +24,10 @@ int64_t Timer::getTimestamp() const { Timer::mockTimeStamp = stamp; } +/*static*/ void Timer::advanceMockTime(int64_t increment) { + Timer::mockTimeStamp += increment; +} + #else #include "ch.hpp" diff --git a/firmware/util/timer.h b/firmware/util/timer.h index 0fa473aa..e0edfaac 100644 --- a/firmware/util/timer.h +++ b/firmware/util/timer.h @@ -29,6 +29,7 @@ class Timer final { float getElapsedSecondsAndReset(); static void setMockTime(int64_t stamp); + static void advanceMockTime(int64_t increment); private: int64_t getTimestamp() const; diff --git a/test/tests/test_heater.cpp b/test/tests/test_heater.cpp index a3dc5e95..d3de9272 100644 --- a/test/tests/test_heater.cpp +++ b/test/tests/test_heater.cpp @@ -123,15 +123,35 @@ TEST(HeaterStateMachine, WarmupTimeout) TEST(HeaterStateMachine, ClosedLoop) { MockHeater dut; + Timer::setMockTime(0); dut.Configure(780, 300); // Temperature is reasonable, stay in closed loop EXPECT_EQ(HeaterState::ClosedLoop, dut.GetNextState(HeaterState::ClosedLoop, HeaterAllow::Allowed, 12, 780)); + Timer::advanceMockTime(10e6); + EXPECT_EQ(HeaterState::ClosedLoop, dut.GetNextState(HeaterState::ClosedLoop, HeaterAllow::Allowed, 12, 780)); + + // Allow too hot briefly + EXPECT_EQ(HeaterState::ClosedLoop, dut.GetNextState(HeaterState::ClosedLoop, HeaterAllow::Allowed, 12, 1000)); + Timer::advanceMockTime(0.1e6); + EXPECT_EQ(HeaterState::ClosedLoop, dut.GetNextState(HeaterState::ClosedLoop, HeaterAllow::Allowed, 12, 1000)); - // Temperature is too hot, overheat + // Wait too long, overheat not allowed + Timer::advanceMockTime(1e6); EXPECT_EQ(HeaterState::Stopped, dut.GetNextState(HeaterState::ClosedLoop, HeaterAllow::Allowed, 12, 1000)); - // Temperature is too cold, underheat + // Back to normal + dut.GetNextState(HeaterState::ClosedLoop, HeaterAllow::Allowed, 12, 780); + Timer::advanceMockTime(1e6); + EXPECT_EQ(HeaterState::ClosedLoop, dut.GetNextState(HeaterState::ClosedLoop, HeaterAllow::Allowed, 12, 780)); + + // Allow too cold briefly + EXPECT_EQ(HeaterState::ClosedLoop, dut.GetNextState(HeaterState::ClosedLoop, HeaterAllow::Allowed, 12, 600)); + Timer::advanceMockTime(0.1e6); + EXPECT_EQ(HeaterState::ClosedLoop, dut.GetNextState(HeaterState::ClosedLoop, HeaterAllow::Allowed, 12, 600)); + + // Wait too long, underheat not allowed + Timer::advanceMockTime(1e6); EXPECT_EQ(HeaterState::Stopped, dut.GetNextState(HeaterState::ClosedLoop, HeaterAllow::Allowed, 12, 600)); }