forked from rusefi/rusefi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_tunerstudio.cpp
More file actions
180 lines (133 loc) · 5.37 KB
/
Copy pathtest_tunerstudio.cpp
File metadata and controls
180 lines (133 loc) · 5.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "pch.h"
#include "tunerstudio.h"
#include "tunerstudio_io.h"
#include "tunerstudio_impl.h"
static uint8_t st5TestBuffer[16000];
class BufferTsChannel : public TsChannelBase {
public:
BufferTsChannel() : TsChannelBase("Test") { }
void write(const uint8_t* buffer, size_t size, bool /*isLastWriteInTransaction*/) override {
memcpy(&st5TestBuffer[writeIdx], buffer, size);
writeIdx += size;
}
size_t readTimeout(uint8_t* buffer, size_t size, int timeout) override {
// nothing to do here
return size;
}
void reset() {
writeIdx = 0;
}
size_t writeIdx = 0;
};
#define CODE 2
#define PAYLOAD "123"
#define SIZE strlen(PAYLOAD)
static void assertCrcPacket(BufferTsChannel& dut) {
ASSERT_EQ(dut.writeIdx, SIZE + 7);
// todo: proper uint16 comparison
ASSERT_EQ(st5TestBuffer[0], 0);
ASSERT_EQ(st5TestBuffer[1], SIZE + 1);
ASSERT_EQ(st5TestBuffer[2], CODE);
ASSERT_EQ(memcmp(&st5TestBuffer[3], PAYLOAD, SIZE), 0);
// todo: proper uint32 comparison
ASSERT_EQ(st5TestBuffer[6], 252);
ASSERT_EQ(st5TestBuffer[7], 68);
ASSERT_EQ(st5TestBuffer[8], 173);
ASSERT_EQ(st5TestBuffer[9], 87);
}
TEST(binary, testIsTouching) {
EngineTestHelper eth(engine_type_e::TEST_ENGINE);
ASSERT_FALSE(isTouchingArea(0, 0, offsetof(persistent_config_s, veTable), sizeof(config->veTable)));
ASSERT_TRUE(isTouchingArea(offsetof(persistent_config_s, veTable), 5, offsetof(persistent_config_s, veTable), sizeof(config->veTable)));
}
TEST(binary, testWriteCrc) {
BufferTsChannel test;
// Let it pick which impl (small vs large) to use
test.reset();
test.writeCrcPacket(CODE, (const uint8_t*)PAYLOAD, SIZE);
assertCrcPacket(test);
// Force the large impl
test.reset();
test.writeCrcPacket(CODE, (const uint8_t*)PAYLOAD, SIZE);
assertCrcPacket(test);
// Force the small impl
test.reset();
test.writeCrcPacket(CODE, (const uint8_t*)PAYLOAD, SIZE);
assertCrcPacket(test);
}
TEST(TunerstudioCommands, writeChunkEngineConfig) {
EngineTestHelper eth(engine_type_e::TEST_ENGINE);
::testing::NiceMock<MockTsChannel> channel;
uint8_t* configBytes = reinterpret_cast<uint8_t*>(config);
// Contains zero before the write
configBytes[100] = 0;
EXPECT_EQ(configBytes[100], 0);
// two step - writes to the engineConfiguration section require a burn
uint8_t val = 50;
TunerStudio instance;
instance.handleWriteChunkCommand(&channel, 0, 100, 1, &val);
EXPECT_EQ(configBytes[100], 50);
}
static constexpr size_t TS_ERROR_PACKET_SIZE = 7;
static constexpr uint16_t OVERSIZED_COUNT = 0xFFFF;
TEST(TunerstudioCommands, outOfRangePageReadSendsExactlyOneError) {
EngineTestHelper eth(engine_type_e::TEST_ENGINE);
BufferTsChannel channel;
int prevErrors = tsState.errorOutOfRange;
TunerStudio ts;
ts.handlePageReadCommand(&channel, TS_PAGE_SETTINGS, 0, OVERSIZED_COUNT);
EXPECT_EQ(channel.writeIdx, TS_ERROR_PACKET_SIZE);
EXPECT_EQ(st5TestBuffer[2], TS_RESPONSE_OUT_OF_RANGE);
EXPECT_EQ(tsState.errorOutOfRange - prevErrors, 1);
}
TEST(TunerstudioCommands, outOfRangeCrc32CheckSendsExactlyOneError) {
EngineTestHelper eth(engine_type_e::TEST_ENGINE);
BufferTsChannel channel;
int prevErrors = tsState.errorOutOfRange;
TunerStudio ts;
ts.handleCrc32Check(&channel, TS_PAGE_SETTINGS, 0, OVERSIZED_COUNT);
EXPECT_EQ(channel.writeIdx, TS_ERROR_PACKET_SIZE);
EXPECT_EQ(st5TestBuffer[2], TS_RESPONSE_OUT_OF_RANGE);
EXPECT_EQ(tsState.errorOutOfRange - prevErrors, 1);
}
TEST(TunerstudioCommands, outOfRangeWriteChunkSendsExactlyOneError) {
EngineTestHelper eth(engine_type_e::TEST_ENGINE);
BufferTsChannel channel;
int prevErrors = tsState.errorOutOfRange;
uint8_t dummy[1] = {};
TunerStudio ts;
ts.handleWriteChunkCommand(&channel, TS_PAGE_SETTINGS, 0, OVERSIZED_COUNT, dummy);
EXPECT_EQ(channel.writeIdx, TS_ERROR_PACKET_SIZE);
EXPECT_EQ(st5TestBuffer[2], TS_RESPONSE_OUT_OF_RANGE);
EXPECT_EQ(tsState.errorOutOfRange - prevErrors, 1);
}
// ---------------------------------------------------------------------------
// isTuningVeNow — detect tuning and suspend STFT/LTFT
// ---------------------------------------------------------------------------
// Verify that writing to the VE table region triggers tuning detection when the
// detector is enabled, and does NOT when disabled.
TEST(TunerstudioCommands, tuningDetectorEnabledSuspendsAfterVeWrite) {
EngineTestHelper eth(engine_type_e::TEST_ENGINE);
::testing::NiceMock<MockTsChannel> channel;
engineConfiguration->isTuningDetectorEnabled = true;
// Write 1 byte to the start of the VE table to reset calibrationsVeWriteTimer
const uint16_t veOffset = static_cast<uint16_t>(offsetof(persistent_config_s, veTable));
uint8_t dummy = 0;
TunerStudio ts;
ts.handleWriteChunkCommand(&channel, TS_PAGE_SETTINGS, veOffset, 1, &dummy);
// With enabled=true and timer just reset, isTuningVeNow() must return true
EXPECT_TRUE(isTuningVeNow());
resetCalibrationTimerForTest();
}
TEST(TunerstudioCommands, tuningDetectorDisabledDoesNotSuspendAfterVeWrite) {
EngineTestHelper eth(engine_type_e::TEST_ENGINE);
::testing::NiceMock<MockTsChannel> channel;
engineConfiguration->isTuningDetectorEnabled = false;
const uint16_t veOffset = static_cast<uint16_t>(offsetof(persistent_config_s, veTable));
uint8_t dummy = 0;
TunerStudio ts;
ts.handleWriteChunkCommand(&channel, TS_PAGE_SETTINGS, veOffset, 1, &dummy);
// With enabled=false, tuning detection uses 0 s threshold — always elapsed → not tuning
EXPECT_FALSE(isTuningVeNow());
resetCalibrationTimerForTest();
}