GCC Code Coverage Report


Directory: ./
File: unit_tests/test-framework/engine_test_helper.h
Date: 2025-11-16 14:52:24
Coverage Exec Excl Total
Lines: 100.0% 2 0 2
Functions: 100.0% 1 0 1
Branches: -% 0 0 0
Decisions: -% 0 - 0

Line Branch Decision Exec Source
1 /**
2 * @file engine_test_helper.h
3 *
4 * @date Jun 26, 2014
5 * @author Andrey Belomutskiy, (c) 2012-2020
6 */
7
8 #pragma once
9
10 #include "trigger_central.h"
11 #include "main_trigger_callback.h"
12 #include "unit_test_framework.h"
13 #include "engine.h"
14
15 #include <unordered_map>
16
17 extern EnginePins enginePins;
18
19 class EngineTestHelperBase
20 {
21 public:
22 // we have the base method and base constructor in order to better control order if initialization
23 // base constructor contains things which need to be executed first
24 EngineTestHelperBase(Engine * eng, engine_configuration_s * config, persistent_config_s * pers);
25 ~EngineTestHelperBase();
26 };
27
28 /**
29 * Mock engine with trigger signal simulation infrastructure
30 */
31 class EngineTestHelper : public EngineTestHelperBase {
32 public:
33 explicit EngineTestHelper(engine_type_e engineType);
34 EngineTestHelper(engine_type_e engineType, const std::unordered_map<SensorType, float>& sensorValues);
35 EngineTestHelper(engine_type_e engineType, configuration_callback_t boardCallback);
36 EngineTestHelper(engine_type_e engineType, configuration_callback_t boardCallback, const std::unordered_map<SensorType, float>& sensorValues);
37 ~EngineTestHelper();
38
39 // convert ms time to angle at current RPM
40 angle_t timeToAngle(float timeMs);
41 18 float angleToTimeUs(angle_t angle) {
42 18 return angle * engine.rpmCalculator.oneDegreeUs;
43 }
44
45 float angleToTimeMs(angle_t angle) {
46 return US2MS(angleToTimeUs(angle));
47 }
48
49 warningBuffer_t *recentWarnings();
50 int getWarningCounter();
51
52 void applyTriggerWaveform();
53 void setTriggerType(trigger_type_e trigger);
54 /**
55 * DEPRECATED these methods do not execute events on the queue
56 */
57 void fireRise(float delayMs);
58 void fireFall(float delayMs);
59 void moveTimeForwardUs(int deltaTimeUs);
60 void fireTriggerEvents2(int count, float delayMs);
61
62 /**
63 * these methods execute events while moving time forward
64 * todo: better naming convention?
65 */
66 void smartFireRise(float delayMs);
67 void smartFireFall(float delayMs);
68 void moveTimeForwardAndInvokeEventsUs(int deltaTimeUs);
69 void setTimeNtAndInvokeCallBacks(efitick_t nt);
70 void setTimeAndInvokeEventsUs(int timeNowUs);
71 void moveTimeForwardAndInvokeEventsSec(int deltaTimeSeconds);
72 /**
73 * both Rise and Fall
74 */
75 void smartFireTriggerEvents2(int count, float delayMs);
76
77 /**
78 * See also #fireRise() which would also move time forward
79 */
80 void firePrimaryTriggerRise();
81 /**
82 * See also #fireFall() which would also move time forward
83 */
84 void firePrimaryTriggerFall();
85 void fireTriggerEvents(int count);
86 void fireTriggerEventsWithDuration(float delayMs);
87 /**
88 * todo: better method name since this method executes events in the FUTURE
89 * looks like such a method should be used only in some pretty narrow circumstances
90 * a healthy test should probably use executeActions instead?
91 */
92 void clearQueue();
93
94 scheduling_s * assertEvent5(const char *msg, int index, action_s const& action, efitimeus_t expectedTimestamp);
95 scheduling_s * assertScheduling(const char *msg, int index, scheduling_s *expected, action_s const& action, efitimeus_t expectedTimestamp);
96
97 const AngleBasedEvent* assertTriggerEvent(const char *msg, int index, AngleBasedEvent *expected, action_s const& action, angle_t enginePhase);
98
99 void assertEvent(const char *msg, int index, action_s const& action, efitimeus_t momentUs, InjectionEvent *event);
100 void assertInjectorUpEvent(const char *msg, int eventIndex, efitimeus_t momentUs, long injectorIndex);
101 void assertInjectorDownEvent(const char *msg, int eventIndex, efitimeus_t momentUs, long injectorIndex);
102 // todo: open question if this is worth a helper method or should be inlined?
103 void assertRpm(int expectedRpm, const char *msg = "RPM");
104
105 // read all scheluder queue and search for the requested callback, then asserts the expected angle, return true if we found the callback
106 bool assertEventExistsAtEnginePhase(const char *msg, action_s const& action, angle_t expectedEventEnginePhase);
107
108 // spins the engine using 60-2 trigger pattern, at target RPM, by X crank degree, not engine phase degree
109 void spin60_2UntilDeg(struct testSpinEngineUntilData& spinInfo, int targetRpm, float targetDegree);
110
111 int executeActions();
112 void moveTimeForwardMs(float deltaTimeMs);
113 void moveTimeForwardSec(float deltaTimeSec);
114
115 Engine engine;
116 persistent_config_s persistentConfig;
117
118 std::unique_ptr<::testing::NiceMock<MockAirmass>> mockAirmass;
119
120 private:
121 void writeEventsLogicData(const char *fileName);
122 void writeEvents2(const char *fileName);
123 };
124
125 void setupSimpleTestEngineWithMafAndTT_ONE_trigger(EngineTestHelper *eth, injection_mode_e injMode = IM_BATCH);
126 void setupSimpleTestEngineWithMaf(EngineTestHelper *eth, injection_mode_e injectionMode, trigger_type_e trigger);
127
128 void setVerboseTrigger(bool isEnabled);
129
130 warningBuffer_t * getRecentWarnings();
131
132 // used by EngineTestHelper::spin60_2UntilDeg func
133 struct testSpinEngineUntilData {
134 float currentDegree;
135 int currentTooth;
136 int toothCount;
137 };
138