forked from rusefi/rusefi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_tooth_logger.cpp
More file actions
92 lines (75 loc) · 2.03 KB
/
Copy pathtest_tooth_logger.cpp
File metadata and controls
92 lines (75 loc) · 2.03 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
#include "pch.h"
#include "tooth_logger.h"
#include <string>
namespace {
class StringWriter : public Writer {
public:
std::string data;
size_t write(const char* buffer, size_t count) override {
data.append(buffer, count);
return count;
}
size_t flush() override {
return 0;
}
};
} // namespace
TEST(ToothLogger, WriteCsvHeader) {
StringWriter w;
ToothLoggerWriteCsvHeader(w);
EXPECT_EQ(w.data,
"Time[s], Primary, Cam 1, Cam 2, Cam 3, Cam 4, Sync, TDC, Coils, Injectors, ACR, VBatt, ET\r\n");
}
TEST(ToothLogger, WriteCsvRows) {
// Seed mock sensor values so we get deterministic VBatt/ET columns
Sensor::setMockValue(SensorType::BatteryVoltage, 12.34f);
Sensor::setMockValue(SensorType::Clt, 56.78f);
CompositeBuffer buf{};
buf.nextIdx = 2;
// Row 0: timestamp 1.000002s, all flags zero
{
composite_logger_s c{};
c.timestamp = 1000002; // microseconds
c.priLevel = true;
c.cam1 = false;
c.sync = true;
c.tdc = false;
c.coil = 3;
c.injector = 5;
c.acr = true;
// On-the-wire format is byte-reversed; ToothLoggerWriteCsv swaps it back.
buf.buffer[0].x = SWAP_UINT64(c.x);
}
// Row 1
{
composite_logger_s c{};
c.timestamp = 2000500;
c.priLevel = false;
c.cam1 = true;
c.cam2 = true;
c.sync = false;
c.tdc = true;
c.coil = 0;
c.injector = 0;
c.acr = false;
buf.buffer[1].x = SWAP_UINT64(c.x);
}
StringWriter w;
int total = ToothLoggerWriteCsv(w, &buf);
EXPECT_GT(total, 0);
EXPECT_EQ((size_t)total, w.data.size());
// Each row ends with the per-row VBatt and ET (formatted with %.2f)
// and contains the timestamp prefix.
EXPECT_NE(w.data.find("1.000002,"), std::string::npos);
EXPECT_NE(w.data.find("2.000500,"), std::string::npos);
EXPECT_NE(w.data.find("12.34"), std::string::npos);
EXPECT_NE(w.data.find("56.78"), std::string::npos);
// Number of CSV rows (CRLF-terminated) must match the buffer entries.
size_t crlfCount = 0;
for (size_t i = 0; i + 1 < w.data.size(); i++) {
if (w.data[i] == '\r' && w.data[i + 1] == '\n') {
crlfCount++;
}
}
EXPECT_EQ(crlfCount, 2u);
}