Unit tests for vehicle speed.cpp #3081 (#3103)

* Add test_vehicle_speed.cpp

* Add test_vehicle_speed.cpp to makefile

* Add tests for vehicle_speed.cpp

* Enable vehicle_speed.cpp testing

* Edit make some constants explicit and format code style

* Revert "Enable vehicle_speed.cpp testing"

This reverts commit bcba5219a1.

* Remove verbose variables

* Remove dead code strings

* Fix tests coupling by setMockVehicleSpeed

Co-authored-by: alxrMironov <330OMcorporative>
This commit is contained in:
alxrMironov 2021-08-04 02:49:16 +03:00 committed by GitHub
parent 8fe2d99254
commit 6d12533120
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 75 additions and 0 deletions

View File

@ -0,0 +1,74 @@
#include "pch.h"
#include "vehicle_speed.h"
extern void vsCallback(DECLARE_ENGINE_PARAMETER_SIGNATURE);
typedef void(*vss_callback_fp)(DECLARE_ENGINE_PARAMETER_SIGNATURE) ;
static constexpr brain_pin_e anyPin = GPIOA_0;
static constexpr const char* const vehicleSpeedSensorMessage = "VSS";
/*
* Used to convert expected speed into simulation frequency
*/
static float speedToSimulationFrequency(float speedCoef, float speed) {
return speed/speedCoef;
}
/*
* Use engine test helper as time source,
* to simulate periodic signal on input pin with passed callback.
*/
static void simulatePeriodicSignalForCallback(
EngineTestHelper& eth,
float freqHz,
vss_callback_fp cb
DECLARE_ENGINE_PARAMETER_SUFFIX)
{
constexpr auto periods = 50;
auto period = (1 / freqHz);
for (auto i = 0; i < periods; i++) {
cb(PASS_ENGINE_PARAMETER_SIGNATURE);
// Time rewind after the callback, due internal vehicle_speed.cpp logic
// (last signal time check for stop tracking)
eth.moveTimeForwardSec(period);
}
}
TEST(VehicleSpeedSensor, testValidSpeedDetection) {
// Setup
setMockVehicleSpeed(-1);
WITH_ENGINE_TEST_HELPER(TEST_ENGINE);
CONFIG(vehicleSpeedSensorInputPin) = anyPin;
engineConfiguration->vehicleSpeedCoef = 0.5f;
// Valid speed 15kmh should be returned
float freq = speedToSimulationFrequency(engineConfiguration->vehicleSpeedCoef, 15.0f);
simulatePeriodicSignalForCallback(eth, freq, vsCallback PASS_ENGINE_PARAMETER_SUFFIX);
float measuredSpeed = getVehicleSpeed(PASS_ENGINE_PARAMETER_SIGNATURE);
EXPECT_NEAR(15.0f, measuredSpeed, 0.01);
// Tear down
setMockVehicleSpeed(-1);
}
TEST(VehicleSpeedSensor, testInvalidSpeed) {
// Setup
setMockVehicleSpeed(-1);
WITH_ENGINE_TEST_HELPER(TEST_ENGINE);
CONFIG(vehicleSpeedSensorInputPin) = anyPin;
engineConfiguration->vehicleSpeedCoef = 0.5f;
// Invalid (slow) interval, should return 0 speed
simulatePeriodicSignalForCallback(eth, 0.5f, vsCallback PASS_ENGINE_PARAMETER_SUFFIX);
float measuredSpeed = getVehicleSpeed(PASS_ENGINE_PARAMETER_SIGNATURE);
// Direct comparasion as invalid speed shoud return true zero
EXPECT_EQ(0.0f, measuredSpeed);
// Tear down
setMockVehicleSpeed(-1);
}

View File

@ -86,4 +86,5 @@ TESTS_SRC_CPP = \
tests/trigger/test_all_triggers.cpp \
tests/test_stepper.cpp \
tests/sensor/test_frequency_sensor.cpp \
tests/sensor/test_vehicle_speed.cpp \