diff --git a/firmware/util/timer.h b/firmware/util/timer.h index 6334f704f5..04e1fd399d 100644 --- a/firmware/util/timer.h +++ b/firmware/util/timer.h @@ -2,6 +2,10 @@ #include "efitime.h" +/** + * Helper class with "has X amount of time elapsed since most recent reset" methods + * Brand new instances have most recent reset time far in the past, i.e. "hasElapse" is true for any reasonable range + */ class Timer { public: void reset(); diff --git a/unit_tests/engine_test_helper.cpp b/unit_tests/engine_test_helper.cpp index 8c41d9fadb..1b7e7b0d5f 100644 --- a/unit_tests/engine_test_helper.cpp +++ b/unit_tests/engine_test_helper.cpp @@ -127,7 +127,7 @@ void EngineTestHelper::fireRise(float delayMs) { } void EngineTestHelper::smartFireRise(float delayMs) { - smartMoveTimeForwardUs(MS2US(delayMs)); + moveTimeForwardAndInvokeEventsUs(MS2US(delayMs)); firePrimaryTriggerRise(); } @@ -137,7 +137,7 @@ void EngineTestHelper::fireFall(float delayMs) { } void EngineTestHelper::smartFireFall(float delayMs) { - smartMoveTimeForwardUs(MS2US(delayMs)); + moveTimeForwardAndInvokeEventsUs(MS2US(delayMs)); firePrimaryTriggerFall(); } @@ -196,6 +196,10 @@ void EngineTestHelper::moveTimeForwardMs(float deltaTimeMs) { moveTimeForwardUs(MS2US(deltaTimeMs)); } +void EngineTestHelper::moveTimeForwardSec(float deltaTimeSec) { + moveTimeForwardUs(MS2US(1000 * deltaTimeSec)); +} + void EngineTestHelper::moveTimeForwardUs(int deltaTimeUs) { if (printTriggerDebug || printFuelDebug) { printf("moveTimeForwardUs %.1fms\r\n", deltaTimeUs / 1000.0); @@ -203,16 +207,16 @@ void EngineTestHelper::moveTimeForwardUs(int deltaTimeUs) { timeNowUs += deltaTimeUs; } -void EngineTestHelper::smartMoveTimeForwardSeconds(int deltaTimeSeconds) { - smartMoveTimeForwardUs(MS2US(1000 * deltaTimeSeconds)); +void EngineTestHelper::moveTimeForwardAndInvokeEventsSec(int deltaTimeSeconds) { + moveTimeForwardAndInvokeEventsUs(MS2US(1000 * deltaTimeSeconds)); } /** - * this method executed all pending events wile + * this method executes all pending events while moving time forward */ -void EngineTestHelper::smartMoveTimeForwardUs(int deltaTimeUs) { +void EngineTestHelper::moveTimeForwardAndInvokeEventsUs(int deltaTimeUs) { if (printTriggerDebug || printFuelDebug) { - printf("smartMoveTimeForwardUs %.1fms\r\n", deltaTimeUs / 1000.0); + printf("moveTimeForwardAndInvokeEventsUs %.1fms\r\n", deltaTimeUs / 1000.0); } int targetTime = timeNowUs + deltaTimeUs; diff --git a/unit_tests/engine_test_helper.h b/unit_tests/engine_test_helper.h index 0bf755b03b..ca4d4d6c43 100644 --- a/unit_tests/engine_test_helper.h +++ b/unit_tests/engine_test_helper.h @@ -53,8 +53,8 @@ public: */ void smartFireRise(float delayMs); void smartFireFall(float delayMs); - void smartMoveTimeForwardUs(int deltaTimeUs); - void smartMoveTimeForwardSeconds(int deltaTimeSeconds); + void moveTimeForwardAndInvokeEventsUs(int deltaTimeUs); + void moveTimeForwardAndInvokeEventsSec(int deltaTimeSeconds); void smartFireTriggerEvents2(int count, float delayMs); /** @@ -82,6 +82,7 @@ public: int executeActions(); void moveTimeForwardMs(float deltaTimeMs); + void moveTimeForwardSec(float deltaTimeSec); efitimeus_t getTimeNowUs(void); Engine engine; diff --git a/unit_tests/tests/test_dynoview.cpp b/unit_tests/tests/test_dynoview.cpp index a55a9a3f37..35d73a2e5b 100644 --- a/unit_tests/tests/test_dynoview.cpp +++ b/unit_tests/tests/test_dynoview.cpp @@ -33,7 +33,7 @@ TEST(DynoView, VSS_T1) { setMockVehicleSpeed(18.0); dut.update(ICU); - eth.smartMoveTimeForwardSeconds(20); + eth.moveTimeForwardAndInvokeEventsSec(20); setMockVehicleSpeed(126.0); dut.update(ICU); diff --git a/unit_tests/tests/test_launch.cpp b/unit_tests/tests/test_launch.cpp index 86ff2fb34b..72f43e6281 100644 --- a/unit_tests/tests/test_launch.cpp +++ b/unit_tests/tests/test_launch.cpp @@ -165,7 +165,7 @@ TEST(LaunchControl, CompleteRun) { //we have a 3 seconds delay to actually enable it! - eth.smartMoveTimeForwardSeconds(1); + eth.moveTimeForwardAndInvokeEventsSec(1); updateLaunchConditions(PASS_ENGINE_PARAMETER_SIGNATURE); spark = false; fuel = false; @@ -174,7 +174,7 @@ TEST(LaunchControl, CompleteRun) { EXPECT_FALSE(spark); EXPECT_FALSE(fuel); - eth.smartMoveTimeForwardSeconds(3); + eth.moveTimeForwardAndInvokeEventsSec(3); updateLaunchConditions(PASS_ENGINE_PARAMETER_SIGNATURE); spark = false; fuel = false; diff --git a/unit_tests/tests/test_start_stop.cpp b/unit_tests/tests/test_start_stop.cpp index 165e5bdc96..1cc9ba0ef3 100644 --- a/unit_tests/tests/test_start_stop.cpp +++ b/unit_tests/tests/test_start_stop.cpp @@ -11,7 +11,7 @@ TEST(start, startStop) { std::unordered_map sensorVals = {{ SensorType::AcceleratorPedal, 0 }}; WITH_ENGINE_TEST_HELPER_SENS(BMW_M73_PROTEUS, sensorVals); - eth.smartMoveTimeForwardSeconds(1); // '0' time has special meaning for implementation so let's move forward + eth.moveTimeForwardAndInvokeEventsSec(1); // '0' time has special meaning for implementation so let's move forward // this is a pull-up, so 'true' on start-up setMockState(engineConfiguration->startStopButtonPin, true); @@ -22,25 +22,25 @@ TEST(start, startStop) { ASSERT_FALSE(efiReadPin(engineConfiguration->starterControlPin)); // startup 'timeout' duration of time is a special case so let's sleep a bit - eth.smartMoveTimeForwardSeconds(10); + eth.moveTimeForwardAndInvokeEventsSec(10); slowStartStopButtonCallback(PASS_ENGINE_PARAMETER_SIGNATURE); ASSERT_FALSE(efiReadPin(engineConfiguration->starterControlPin)); - eth.smartMoveTimeForwardSeconds(10); + eth.moveTimeForwardAndInvokeEventsSec(10); // hit 'start' button! inverted since pull-up setMockState(engineConfiguration->startStopButtonPin, false); slowStartStopButtonCallback(PASS_ENGINE_PARAMETER_SIGNATURE); ASSERT_TRUE(efiReadPin(engineConfiguration->starterControlPin)); - eth.smartMoveTimeForwardSeconds(5); + eth.moveTimeForwardAndInvokeEventsSec(5); slowStartStopButtonCallback(PASS_ENGINE_PARAMETER_SIGNATURE); - eth.smartMoveTimeForwardSeconds(5); + eth.moveTimeForwardAndInvokeEventsSec(5); slowStartStopButtonCallback(PASS_ENGINE_PARAMETER_SIGNATURE); - eth.smartMoveTimeForwardSeconds(5); + eth.moveTimeForwardAndInvokeEventsSec(5); slowStartStopButtonCallback(PASS_ENGINE_PARAMETER_SIGNATURE); // starter is now OFF due to timeout ASSERT_FALSE(efiReadPin(engineConfiguration->starterControlPin)); diff --git a/unit_tests/tests/tests.mk b/unit_tests/tests/tests.mk index 07438cda00..d7b0d9fd1c 100644 --- a/unit_tests/tests/tests.mk +++ b/unit_tests/tests/tests.mk @@ -18,6 +18,7 @@ TESTS_SRC_CPP = \ tests/sensor/test_cj125.cpp \ tests/util/test_buffered_writer.cpp \ tests/util/test_error_accumulator.cpp \ + tests/util/test_timer.cpp \ tests/test_util.cpp \ tests/test_start_stop.cpp \ tests/test_hardware_reinit.cpp \ diff --git a/unit_tests/tests/util/test_timer.cpp b/unit_tests/tests/util/test_timer.cpp new file mode 100644 index 0000000000..b8601dc8f9 --- /dev/null +++ b/unit_tests/tests/util/test_timer.cpp @@ -0,0 +1,14 @@ +#include "engine_test_helper.h" +#include "timer.h" + + +TEST(util, timer) { + WITH_ENGINE_TEST_HELPER(TEST_ENGINE); + Timer timer; + ASSERT_TRUE(timer.hasElapsedSec(3)); + timer.reset(); + ASSERT_FALSE(timer.hasElapsedSec(3)); + + eth.moveTimeForwardSec(4); + ASSERT_TRUE(timer.hasElapsedSec(3)); +}