From e5133de70f67ec604118082e47cc4bbafffea336 Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Sun, 26 Jul 2020 20:33:18 -0700 Subject: [PATCH 01/12] add mock airmass mode --- firmware/controllers/algo/auto_generated_enums.cpp | 2 ++ firmware/controllers/algo/engine.h | 5 +++++ firmware/controllers/algo/fuel_math.cpp | 3 +++ firmware/controllers/algo/rusefi_enums.h | 3 +++ 4 files changed, 13 insertions(+) diff --git a/firmware/controllers/algo/auto_generated_enums.cpp b/firmware/controllers/algo/auto_generated_enums.cpp index 3174670568..f7b61ecdb6 100644 --- a/firmware/controllers/algo/auto_generated_enums.cpp +++ b/firmware/controllers/algo/auto_generated_enums.cpp @@ -735,6 +735,8 @@ case LM_SPEED_DENSITY: return "LM_SPEED_DENSITY"; case LM_ALPHA_N_2: return "LM_ALPHA_N_2"; +case LM_MOCK: + return "LM_MOCK"; } return NULL; } diff --git a/firmware/controllers/algo/engine.h b/firmware/controllers/algo/engine.h index 6ec879af4e..7ecc25b9cf 100644 --- a/firmware/controllers/algo/engine.h +++ b/firmware/controllers/algo/engine.h @@ -16,6 +16,7 @@ #include "accel_enrichment.h" #include "trigger_central.h" #include "local_version_holder.h" +#include "airmass.h" #if EFI_SIGNAL_EXECUTOR_ONE_TIMER // PROD real firmware uses this implementation @@ -334,6 +335,10 @@ public: void knockLogic(float knockVolts DECLARE_ENGINE_PARAMETER_SUFFIX); void printKnockState(void); +#if EFI_UNIT_TEST + AirmassModelBase* mockAirmassModel = nullptr; +#endif + private: /** * By the way: diff --git a/firmware/controllers/algo/fuel_math.cpp b/firmware/controllers/algo/fuel_math.cpp index 7de041a545..4711e33a39 100644 --- a/firmware/controllers/algo/fuel_math.cpp +++ b/firmware/controllers/algo/fuel_math.cpp @@ -179,6 +179,9 @@ AirmassModelBase* getAirmassModel(DECLARE_ENGINE_PARAMETER_SIGNATURE) { case LM_SPEED_DENSITY: return &sdAirmass; case LM_REAL_MAF: return &mafAirmass; case LM_ALPHA_N_2: return &alphaNAirmass; +#if EFI_UNIT_TEST + case LM_MOCK: return engine->mockAirmassModel; +#endif default: return nullptr; } } diff --git a/firmware/controllers/algo/rusefi_enums.h b/firmware/controllers/algo/rusefi_enums.h index 9736c26d92..915318e7b4 100644 --- a/firmware/controllers/algo/rusefi_enums.h +++ b/firmware/controllers/algo/rusefi_enums.h @@ -446,6 +446,9 @@ typedef enum { // todo: rename after LM_ALPHA_N is removed LM_ALPHA_N_2 = 5, + // This mode is for unit testing only, so that tests don't have to rely on a particular real airmass mode + LM_MOCK = 100, + Force_4_bytes_size_engine_load_mode = ENUM_32_BITS, } engine_load_mode_e; From 42c6a641ad6ff7e3e7fd30fb45b697ef1654bf6c Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Sun, 26 Jul 2020 20:35:36 -0700 Subject: [PATCH 02/12] add mock --- unit_tests/mocks.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/unit_tests/mocks.h b/unit_tests/mocks.h index dcaad8ebb4..71c0bc3530 100644 --- a/unit_tests/mocks.h +++ b/unit_tests/mocks.h @@ -52,3 +52,8 @@ public: MOCK_METHOD(void, scheduleByTimestampNt, (scheduling_s *scheduling, efitime_t timeUs, action_s action), (override)); MOCK_METHOD(void, scheduleForLater, (scheduling_s *scheduling, int delayUs, action_s action), (override)); }; + +class MockAirmass : public AirmassModelBase { +public: + MOCK_METHOD(AirmassResult, getAirmass, (int rpm), (override)); +}; From 50fc127555a7cb2c426109ea4abc3a8d41c0c4f4 Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Sun, 26 Jul 2020 20:39:42 -0700 Subject: [PATCH 03/12] put mock in enginetesthelper --- unit_tests/engine_test_helper.cpp | 4 ++++ unit_tests/engine_test_helper.h | 2 ++ 2 files changed, 6 insertions(+) diff --git a/unit_tests/engine_test_helper.cpp b/unit_tests/engine_test_helper.cpp index a62a9ce011..ad223147b6 100644 --- a/unit_tests/engine_test_helper.cpp +++ b/unit_tests/engine_test_helper.cpp @@ -83,6 +83,10 @@ EngineTestHelper::EngineTestHelper(engine_type_e engineType, configuration_callb // this is needed to have valid CLT and IAT. //todo: reuse initPeriodicEvents(PASS_ENGINE_PARAMETER_SIGNATURE) method engine->periodicSlowCallback(PASS_ENGINE_PARAMETER_SIGNATURE); + + // Setup running in mock airmass mode + engineConfiguration->injectionMode = LM_MOCK; + engine->mockAirmassModel = &mockAirmass; } EngineTestHelper::~EngineTestHelper() { diff --git a/unit_tests/engine_test_helper.h b/unit_tests/engine_test_helper.h index 9431eb10ac..799254b36f 100644 --- a/unit_tests/engine_test_helper.h +++ b/unit_tests/engine_test_helper.h @@ -83,6 +83,8 @@ public: Engine engine; persistent_config_s persistentConfig; + MockAirmass mockAirmass; + private: void writeEvents(const char *fileName); }; From 3071ee4334af3cc845b0f9badc3bb843937f5d78 Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Sun, 26 Jul 2020 20:46:23 -0700 Subject: [PATCH 04/12] didn't need that --- firmware/controllers/algo/engine.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/firmware/controllers/algo/engine.h b/firmware/controllers/algo/engine.h index 7ecc25b9cf..0f62747976 100644 --- a/firmware/controllers/algo/engine.h +++ b/firmware/controllers/algo/engine.h @@ -335,9 +335,7 @@ public: void knockLogic(float knockVolts DECLARE_ENGINE_PARAMETER_SUFFIX); void printKnockState(void); -#if EFI_UNIT_TEST AirmassModelBase* mockAirmassModel = nullptr; -#endif private: /** From a2aac60a7623e938b85419089c0077b7c2577daf Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Tue, 28 Jul 2020 11:43:54 -0700 Subject: [PATCH 05/12] fwd decl --- firmware/controllers/algo/engine.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/firmware/controllers/algo/engine.h b/firmware/controllers/algo/engine.h index 0f62747976..a98c07c254 100644 --- a/firmware/controllers/algo/engine.h +++ b/firmware/controllers/algo/engine.h @@ -16,7 +16,6 @@ #include "accel_enrichment.h" #include "trigger_central.h" #include "local_version_holder.h" -#include "airmass.h" #if EFI_SIGNAL_EXECUTOR_ONE_TIMER // PROD real firmware uses this implementation @@ -32,6 +31,7 @@ #define FAST_CALLBACK_PERIOD_MS 5 class RpmCalculator; +class AirmassModelBase; #define MAF_DECODING_CACHE_SIZE 256 From c2bd9087e277ee8d6ebdd9aa1dcfd2d56fbae286 Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Tue, 28 Jul 2020 11:44:09 -0700 Subject: [PATCH 06/12] fwd decl --- firmware/controllers/algo/airmass/airmass.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/firmware/controllers/algo/airmass/airmass.h b/firmware/controllers/algo/airmass/airmass.h index 338741da5f..b9c157a18a 100644 --- a/firmware/controllers/algo/airmass/airmass.h +++ b/firmware/controllers/algo/airmass/airmass.h @@ -2,6 +2,8 @@ #include "engine.h" +class ValueProvider3D; + struct AirmassResult { float CylinderAirmass = 0; float EngineLoadPercent = 100; From 2289c465b53f281c4767f0e8d746845c1f01ba63 Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Tue, 28 Jul 2020 11:56:39 -0700 Subject: [PATCH 07/12] fixes --- unit_tests/engine_test_helper.cpp | 2 +- unit_tests/engine_test_helper.h | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/unit_tests/engine_test_helper.cpp b/unit_tests/engine_test_helper.cpp index ad223147b6..260496a530 100644 --- a/unit_tests/engine_test_helper.cpp +++ b/unit_tests/engine_test_helper.cpp @@ -85,7 +85,7 @@ EngineTestHelper::EngineTestHelper(engine_type_e engineType, configuration_callb engine->periodicSlowCallback(PASS_ENGINE_PARAMETER_SIGNATURE); // Setup running in mock airmass mode - engineConfiguration->injectionMode = LM_MOCK; + engineConfiguration->fuelAlgorithm = LM_MOCK; engine->mockAirmassModel = &mockAirmass; } diff --git a/unit_tests/engine_test_helper.h b/unit_tests/engine_test_helper.h index 799254b36f..a4ff83f055 100644 --- a/unit_tests/engine_test_helper.h +++ b/unit_tests/engine_test_helper.h @@ -13,6 +13,7 @@ #include "main_trigger_callback.h" #include "unit_test_framework.h" #include "sensor.h" +#include "mocks.h" extern EnginePins enginePins; From 53b864bdda3691c5d40912f915a583f10889051d Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Tue, 28 Jul 2020 12:11:56 -0700 Subject: [PATCH 08/12] fixing --- firmware/controllers/algo/fuel_math.cpp | 3 ++- unit_tests/mocks.h | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/firmware/controllers/algo/fuel_math.cpp b/firmware/controllers/algo/fuel_math.cpp index 4711e33a39..7d60e77ea0 100644 --- a/firmware/controllers/algo/fuel_math.cpp +++ b/firmware/controllers/algo/fuel_math.cpp @@ -201,7 +201,8 @@ floatms_t getBaseFuel(int rpm DECLARE_ENGINE_PARAMETER_SUFFIX) { if ((CONFIG(fuelAlgorithm) == LM_SPEED_DENSITY) || (engineConfiguration->fuelAlgorithm == LM_REAL_MAF) || - (engineConfiguration->fuelAlgorithm == LM_ALPHA_N_2)) { + (engineConfiguration->fuelAlgorithm == LM_ALPHA_N_2) || + (engineConfiguration->fuelAlgorithm == LM_MOCK)) { // airmass modes - get airmass first, then convert to fuel auto model = getAirmassModel(PASS_ENGINE_PARAMETER_SIGNATURE); efiAssert(CUSTOM_ERR_ASSERT, model != nullptr, "Invalid airmass mode", 0.0f); diff --git a/unit_tests/mocks.h b/unit_tests/mocks.h index 71c0bc3530..fb60970c94 100644 --- a/unit_tests/mocks.h +++ b/unit_tests/mocks.h @@ -1,7 +1,10 @@ +#pragma once + #include "electronic_throttle.h" #include "dc_motor.h" #include "table_helper.h" #include "pwm_generator_logic.h" +#include "airmass.h" #include "gmock/gmock.h" @@ -55,5 +58,9 @@ public: class MockAirmass : public AirmassModelBase { public: + MockAirmass() : AirmassModelBase(veTable) {} + + MockVp3d veTable; + MOCK_METHOD(AirmassResult, getAirmass, (int rpm), (override)); }; From d57465a6d3c7258360860568d0ab4ef0357745a0 Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Tue, 28 Jul 2020 14:18:50 -0700 Subject: [PATCH 09/12] eth adjustments --- unit_tests/engine_test_helper.cpp | 2 +- unit_tests/engine_test_helper.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/unit_tests/engine_test_helper.cpp b/unit_tests/engine_test_helper.cpp index 260496a530..011eca0d30 100644 --- a/unit_tests/engine_test_helper.cpp +++ b/unit_tests/engine_test_helper.cpp @@ -319,7 +319,7 @@ void setupSimpleTestEngineWithMaf(EngineTestHelper *eth, injection_mode_e inject eth->clearQueue(); - ASSERT_EQ(LM_PLAIN_MAF, engineConfiguration->fuelAlgorithm); + ASSERT_EQ(LM_MOCK, engineConfiguration->fuelAlgorithm); engineConfiguration->isIgnitionEnabled = false; // let's focus on injection engineConfiguration->specs.cylindersCount = 4; // a bit of flexibility - the mode may be changed by some tests diff --git a/unit_tests/engine_test_helper.h b/unit_tests/engine_test_helper.h index a4ff83f055..8f92b36d4f 100644 --- a/unit_tests/engine_test_helper.h +++ b/unit_tests/engine_test_helper.h @@ -84,7 +84,7 @@ public: Engine engine; persistent_config_s persistentConfig; - MockAirmass mockAirmass; + ::testing::NiceMock mockAirmass; private: void writeEvents(const char *fileName); From 8b305acf689b5ab7d8a75757e39de8665c5f3e0d Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Tue, 28 Jul 2020 14:27:34 -0700 Subject: [PATCH 10/12] fix tests --- unit_tests/engine_test_helper.cpp | 1 - .../tests/ignition_injection/test_fuelCut.cpp | 4 ++++ unit_tests/tests/test_fuel_map.cpp | 1 + unit_tests/tests/trigger/test_trigger_decoder.cpp | 14 ++++++++++++-- 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/unit_tests/engine_test_helper.cpp b/unit_tests/engine_test_helper.cpp index 011eca0d30..e0ef38c70b 100644 --- a/unit_tests/engine_test_helper.cpp +++ b/unit_tests/engine_test_helper.cpp @@ -319,7 +319,6 @@ void setupSimpleTestEngineWithMaf(EngineTestHelper *eth, injection_mode_e inject eth->clearQueue(); - ASSERT_EQ(LM_MOCK, engineConfiguration->fuelAlgorithm); engineConfiguration->isIgnitionEnabled = false; // let's focus on injection engineConfiguration->specs.cylindersCount = 4; // a bit of flexibility - the mode may be changed by some tests diff --git a/unit_tests/tests/ignition_injection/test_fuelCut.cpp b/unit_tests/tests/ignition_injection/test_fuelCut.cpp index 21c7f6f540..47396bcdd7 100644 --- a/unit_tests/tests/ignition_injection/test_fuelCut.cpp +++ b/unit_tests/tests/ignition_injection/test_fuelCut.cpp @@ -11,8 +11,12 @@ #include "sensor.h" #include "fsio_impl.h" +using ::testing::_; + TEST(fuelCut, coasting) { WITH_ENGINE_TEST_HELPER(TEST_ENGINE); + EXPECT_CALL(eth.mockAirmass, getAirmass(_)) + .WillRepeatedly(Return(AirmassResult{0.1008f, 50.0f})); // configure coastingFuelCut engineConfiguration->coastingFuelCutEnabled = true; diff --git a/unit_tests/tests/test_fuel_map.cpp b/unit_tests/tests/test_fuel_map.cpp index 1736cc7c89..14dbbcb386 100644 --- a/unit_tests/tests/test_fuel_map.cpp +++ b/unit_tests/tests/test_fuel_map.cpp @@ -21,6 +21,7 @@ using ::testing::FloatNear; TEST(misc, testFuelMap) { printf("Setting up FORD_ASPIRE_1996\r\n"); WITH_ENGINE_TEST_HELPER(FORD_ASPIRE_1996); + engineConfiguration->fuelAlgorithm = LM_PLAIN_MAF; printf("Filling fuel map\r\n"); for (int k = 0; k < FUEL_LOAD_COUNT; k++) { diff --git a/unit_tests/tests/trigger/test_trigger_decoder.cpp b/unit_tests/tests/trigger/test_trigger_decoder.cpp index 58d33dcfe5..5aa73cccec 100644 --- a/unit_tests/tests/trigger/test_trigger_decoder.cpp +++ b/unit_tests/tests/trigger/test_trigger_decoder.cpp @@ -23,6 +23,8 @@ #include "trigger_universal.h" #include "sensor.h" +using ::testing::_; + extern WarningCodeState unitTestWarningCodeState; extern bool printTriggerDebug; extern float actualSynchGap; @@ -295,9 +297,10 @@ static void assertREqualsM(const char *msg, void *expected, void *actual) { extern bool_t debugSignalExecutor; TEST(misc, testRpmCalculator) { - printf("*************************************************** testRpmCalculator\r\n"); - WITH_ENGINE_TEST_HELPER(FORD_INLINE_6_1995); + EXPECT_CALL(eth.mockAirmass, getAirmass(_)) + .WillRepeatedly(Return(AirmassResult{0.1008f, 50.0f})); + IgnitionEventList *ilist = &engine->ignitionEvents; ASSERT_EQ( 0, ilist->isReady) << "size #1"; @@ -588,6 +591,8 @@ static void assertInjectionEventBatch(const char *msg, InjectionEvent *ev, int i } static void setTestBug299(EngineTestHelper *eth) { + // TODO: switch to mock airmass + eth->persistentConfig.engineConfiguration.fuelAlgorithm = LM_PLAIN_MAF; setupSimpleTestEngineWithMafAndTT_ONE_trigger(eth); Engine *engine = ð->engine; EXPAND_Engine @@ -977,6 +982,8 @@ TEST(big, testFuelSchedulerBug299smallAndMedium) { TEST(big, testTwoWireBatch) { WITH_ENGINE_TEST_HELPER(TEST_ENGINE); setupSimpleTestEngineWithMafAndTT_ONE_trigger(ð); + EXPECT_CALL(eth.mockAirmass, getAirmass(_)) + .WillRepeatedly(Return(AirmassResult{0.1008f, 50.0f})); engineConfiguration->injectionMode = IM_BATCH; engineConfiguration->twoWireBatchInjection = true; @@ -1002,6 +1009,9 @@ TEST(big, testTwoWireBatch) { TEST(big, testSequential) { WITH_ENGINE_TEST_HELPER(TEST_ENGINE); + EXPECT_CALL(eth.mockAirmass, getAirmass(_)) + .WillRepeatedly(Return(AirmassResult{0.1008f, 50.0f})); + setupSimpleTestEngineWithMafAndTT_ONE_trigger(ð); engineConfiguration->injectionMode = IM_SEQUENTIAL; From 2253fe6eef71bae6d79eeb9204a2bdab2bbf65f2 Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Tue, 28 Jul 2020 14:42:02 -0700 Subject: [PATCH 11/12] test cleanup --- .../tests/ignition_injection/injection_mode_transition.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/unit_tests/tests/ignition_injection/injection_mode_transition.cpp b/unit_tests/tests/ignition_injection/injection_mode_transition.cpp index e2ece5d105..6f4639963b 100644 --- a/unit_tests/tests/ignition_injection/injection_mode_transition.cpp +++ b/unit_tests/tests/ignition_injection/injection_mode_transition.cpp @@ -31,6 +31,9 @@ TEST(fuelControl, transitionIssue1592) { WITH_ENGINE_TEST_HELPER(TEST_ENGINE); setupSimpleTestEngineWithMafAndTT_ONE_trigger(ð, IM_SEQUENTIAL); + EXPECT_CALL(eth.mockAirmass, getAirmass(400)) + .WillRepeatedly(Return(AirmassResult{0.1008f, 50.0f})); + // This is easiest to trip on a wheel that requires sync engineConfiguration->trigger.customTotalToothCount = 6; engineConfiguration->trigger.customSkippedToothCount = 1; @@ -38,10 +41,6 @@ TEST(fuelControl, transitionIssue1592) { engineConfiguration->ambiguousOperationMode = FOUR_STROKE_CAM_SENSOR; engineConfiguration->isFasterEngineSpinUpEnabled = true; - engineConfiguration->fuelAlgorithm = LM_ALPHA_N; - - extern fuel_Map3D_t fuelMap; - fuelMap.setAll(13); extern fuel_Map3D_t fuelPhaseMap; fuelPhaseMap.setAll(0); setArrayValues(config->crankingFuelCoef, 1.0f); From 610bcce870a6eb41b356e2a0cf1bf1a0dc8578fe Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Tue, 28 Jul 2020 14:56:04 -0700 Subject: [PATCH 12/12] kinetis enum --- .../kinetis/config/controllers/algo/auto_generated_enums.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/firmware/config/boards/kinetis/config/controllers/algo/auto_generated_enums.cpp b/firmware/config/boards/kinetis/config/controllers/algo/auto_generated_enums.cpp index 125570e968..df70037513 100644 --- a/firmware/config/boards/kinetis/config/controllers/algo/auto_generated_enums.cpp +++ b/firmware/config/boards/kinetis/config/controllers/algo/auto_generated_enums.cpp @@ -597,6 +597,8 @@ case LM_SPEED_DENSITY: return "LM_SPEED_DENSITY"; case LM_ALPHA_N_2: return "LM_ALPHA_N_2"; +case LM_MOCK: + return "LM_MOCK"; } return NULL; }