test_timer

This commit is contained in:
rusefillc 2021-03-12 00:43:48 -05:00
parent 9f90387d09
commit 3651883bcf
8 changed files with 42 additions and 18 deletions

View File

@ -2,6 +2,10 @@
#include "efitime.h" #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 { class Timer {
public: public:
void reset(); void reset();

View File

@ -127,7 +127,7 @@ void EngineTestHelper::fireRise(float delayMs) {
} }
void EngineTestHelper::smartFireRise(float delayMs) { void EngineTestHelper::smartFireRise(float delayMs) {
smartMoveTimeForwardUs(MS2US(delayMs)); moveTimeForwardAndInvokeEventsUs(MS2US(delayMs));
firePrimaryTriggerRise(); firePrimaryTriggerRise();
} }
@ -137,7 +137,7 @@ void EngineTestHelper::fireFall(float delayMs) {
} }
void EngineTestHelper::smartFireFall(float delayMs) { void EngineTestHelper::smartFireFall(float delayMs) {
smartMoveTimeForwardUs(MS2US(delayMs)); moveTimeForwardAndInvokeEventsUs(MS2US(delayMs));
firePrimaryTriggerFall(); firePrimaryTriggerFall();
} }
@ -196,6 +196,10 @@ void EngineTestHelper::moveTimeForwardMs(float deltaTimeMs) {
moveTimeForwardUs(MS2US(deltaTimeMs)); moveTimeForwardUs(MS2US(deltaTimeMs));
} }
void EngineTestHelper::moveTimeForwardSec(float deltaTimeSec) {
moveTimeForwardUs(MS2US(1000 * deltaTimeSec));
}
void EngineTestHelper::moveTimeForwardUs(int deltaTimeUs) { void EngineTestHelper::moveTimeForwardUs(int deltaTimeUs) {
if (printTriggerDebug || printFuelDebug) { if (printTriggerDebug || printFuelDebug) {
printf("moveTimeForwardUs %.1fms\r\n", deltaTimeUs / 1000.0); printf("moveTimeForwardUs %.1fms\r\n", deltaTimeUs / 1000.0);
@ -203,16 +207,16 @@ void EngineTestHelper::moveTimeForwardUs(int deltaTimeUs) {
timeNowUs += deltaTimeUs; timeNowUs += deltaTimeUs;
} }
void EngineTestHelper::smartMoveTimeForwardSeconds(int deltaTimeSeconds) { void EngineTestHelper::moveTimeForwardAndInvokeEventsSec(int deltaTimeSeconds) {
smartMoveTimeForwardUs(MS2US(1000 * 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) { if (printTriggerDebug || printFuelDebug) {
printf("smartMoveTimeForwardUs %.1fms\r\n", deltaTimeUs / 1000.0); printf("moveTimeForwardAndInvokeEventsUs %.1fms\r\n", deltaTimeUs / 1000.0);
} }
int targetTime = timeNowUs + deltaTimeUs; int targetTime = timeNowUs + deltaTimeUs;

View File

@ -53,8 +53,8 @@ public:
*/ */
void smartFireRise(float delayMs); void smartFireRise(float delayMs);
void smartFireFall(float delayMs); void smartFireFall(float delayMs);
void smartMoveTimeForwardUs(int deltaTimeUs); void moveTimeForwardAndInvokeEventsUs(int deltaTimeUs);
void smartMoveTimeForwardSeconds(int deltaTimeSeconds); void moveTimeForwardAndInvokeEventsSec(int deltaTimeSeconds);
void smartFireTriggerEvents2(int count, float delayMs); void smartFireTriggerEvents2(int count, float delayMs);
/** /**
@ -82,6 +82,7 @@ public:
int executeActions(); int executeActions();
void moveTimeForwardMs(float deltaTimeMs); void moveTimeForwardMs(float deltaTimeMs);
void moveTimeForwardSec(float deltaTimeSec);
efitimeus_t getTimeNowUs(void); efitimeus_t getTimeNowUs(void);
Engine engine; Engine engine;

View File

@ -33,7 +33,7 @@ TEST(DynoView, VSS_T1) {
setMockVehicleSpeed(18.0); setMockVehicleSpeed(18.0);
dut.update(ICU); dut.update(ICU);
eth.smartMoveTimeForwardSeconds(20); eth.moveTimeForwardAndInvokeEventsSec(20);
setMockVehicleSpeed(126.0); setMockVehicleSpeed(126.0);
dut.update(ICU); dut.update(ICU);

View File

@ -165,7 +165,7 @@ TEST(LaunchControl, CompleteRun) {
//we have a 3 seconds delay to actually enable it! //we have a 3 seconds delay to actually enable it!
eth.smartMoveTimeForwardSeconds(1); eth.moveTimeForwardAndInvokeEventsSec(1);
updateLaunchConditions(PASS_ENGINE_PARAMETER_SIGNATURE); updateLaunchConditions(PASS_ENGINE_PARAMETER_SIGNATURE);
spark = false; spark = false;
fuel = false; fuel = false;
@ -174,7 +174,7 @@ TEST(LaunchControl, CompleteRun) {
EXPECT_FALSE(spark); EXPECT_FALSE(spark);
EXPECT_FALSE(fuel); EXPECT_FALSE(fuel);
eth.smartMoveTimeForwardSeconds(3); eth.moveTimeForwardAndInvokeEventsSec(3);
updateLaunchConditions(PASS_ENGINE_PARAMETER_SIGNATURE); updateLaunchConditions(PASS_ENGINE_PARAMETER_SIGNATURE);
spark = false; spark = false;
fuel = false; fuel = false;

View File

@ -11,7 +11,7 @@
TEST(start, startStop) { TEST(start, startStop) {
std::unordered_map<SensorType, float> sensorVals = {{ SensorType::AcceleratorPedal, 0 }}; std::unordered_map<SensorType, float> sensorVals = {{ SensorType::AcceleratorPedal, 0 }};
WITH_ENGINE_TEST_HELPER_SENS(BMW_M73_PROTEUS, sensorVals); 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 // this is a pull-up, so 'true' on start-up
setMockState(engineConfiguration->startStopButtonPin, true); setMockState(engineConfiguration->startStopButtonPin, true);
@ -22,25 +22,25 @@ TEST(start, startStop) {
ASSERT_FALSE(efiReadPin(engineConfiguration->starterControlPin)); ASSERT_FALSE(efiReadPin(engineConfiguration->starterControlPin));
// startup 'timeout' duration of time is a special case so let's sleep a bit // 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); slowStartStopButtonCallback(PASS_ENGINE_PARAMETER_SIGNATURE);
ASSERT_FALSE(efiReadPin(engineConfiguration->starterControlPin)); ASSERT_FALSE(efiReadPin(engineConfiguration->starterControlPin));
eth.smartMoveTimeForwardSeconds(10); eth.moveTimeForwardAndInvokeEventsSec(10);
// hit 'start' button! inverted since pull-up // hit 'start' button! inverted since pull-up
setMockState(engineConfiguration->startStopButtonPin, false); setMockState(engineConfiguration->startStopButtonPin, false);
slowStartStopButtonCallback(PASS_ENGINE_PARAMETER_SIGNATURE); slowStartStopButtonCallback(PASS_ENGINE_PARAMETER_SIGNATURE);
ASSERT_TRUE(efiReadPin(engineConfiguration->starterControlPin)); ASSERT_TRUE(efiReadPin(engineConfiguration->starterControlPin));
eth.smartMoveTimeForwardSeconds(5); eth.moveTimeForwardAndInvokeEventsSec(5);
slowStartStopButtonCallback(PASS_ENGINE_PARAMETER_SIGNATURE); slowStartStopButtonCallback(PASS_ENGINE_PARAMETER_SIGNATURE);
eth.smartMoveTimeForwardSeconds(5); eth.moveTimeForwardAndInvokeEventsSec(5);
slowStartStopButtonCallback(PASS_ENGINE_PARAMETER_SIGNATURE); slowStartStopButtonCallback(PASS_ENGINE_PARAMETER_SIGNATURE);
eth.smartMoveTimeForwardSeconds(5); eth.moveTimeForwardAndInvokeEventsSec(5);
slowStartStopButtonCallback(PASS_ENGINE_PARAMETER_SIGNATURE); slowStartStopButtonCallback(PASS_ENGINE_PARAMETER_SIGNATURE);
// starter is now OFF due to timeout // starter is now OFF due to timeout
ASSERT_FALSE(efiReadPin(engineConfiguration->starterControlPin)); ASSERT_FALSE(efiReadPin(engineConfiguration->starterControlPin));

View File

@ -18,6 +18,7 @@ TESTS_SRC_CPP = \
tests/sensor/test_cj125.cpp \ tests/sensor/test_cj125.cpp \
tests/util/test_buffered_writer.cpp \ tests/util/test_buffered_writer.cpp \
tests/util/test_error_accumulator.cpp \ tests/util/test_error_accumulator.cpp \
tests/util/test_timer.cpp \
tests/test_util.cpp \ tests/test_util.cpp \
tests/test_start_stop.cpp \ tests/test_start_stop.cpp \
tests/test_hardware_reinit.cpp \ tests/test_hardware_reinit.cpp \

View File

@ -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));
}