forked from Brill-Power/thingsetplusplus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestTextEncodingRecords.cpp
More file actions
152 lines (140 loc) · 9.94 KB
/
TestTextEncodingRecords.cpp
File metadata and controls
152 lines (140 loc) · 9.94 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
/*
* Copyright (c) 2025 Brill Power.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "thingset++/ThingSet.hpp"
#include "thingset++/ThingSetRecordMember.hpp"
#include "gtest/gtest.h"
using namespace ThingSet;
struct SupercellRecord
{
ThingSetReadOnlyRecordMember<0x611, 0x610, "soc", float> soc;
ThingSetReadOnlyRecordMember<0x612, 0x610, "soh", float> soh;
};
struct ModuleRecord
{
bool ignored; // this field will be ignored by the encoder and decoder
ThingSetReadOnlyRecordMember<0x601, 0x600, "voltage", float> voltage;
ThingSetReadOnlyRecordMember<0x602, 0x600, "current", float> current;
ThingSetReadOnlyRecordMember<0x603, 0x600, "error", uint64_t> error;
ThingSetReadOnlyRecordMember<0x604, 0x600, "cellVoltages", std::array<float, 6>> cellVoltages;
ThingSetReadOnlyRecordMember<0x609, 0x600, "supercells", std::array<SupercellRecord, 6>> supercells;
};
#define ASSERT_BUFFER_EQ(expected, actual, actual_len) \
ASSERT_EQ(strlen(expected), actual_len); \
for (size_t i = 0; i < strlen(expected); i++) { \
ASSERT_EQ(expected[i], actual[i]); \
}
#define SETUP() \
ThingSetReadOnlyRecordMember<0x610, 0, "Modules", std::array<ModuleRecord, 2>> moduleRecords = { \
{ (ModuleRecord){ \
.voltage = 24.0f, \
.current = 10.0f, \
.error = 0, \
.cellVoltages = { { 3.2f, 3.1f, 2.9f, 3.3f, 0.0f, 2.8f } }, \
.supercells = { { (SupercellRecord){ \
.soc = 0.1, \
.soh = 1, \
}, \
(SupercellRecord){ .soc = 0.25, .soh = 1 }, \
(SupercellRecord){ \
.soc = 0.5, \
.soh = 1, \
}, \
(SupercellRecord){ \
.soc = 0.75, \
.soh = 1, \
}, \
(SupercellRecord){ \
.soc = 0.8, \
.soh = 1, \
}, \
(SupercellRecord){ \
.soc = 1, \
.soh = 1, \
} } }, \
}, \
(ModuleRecord){ \
.voltage = 24.2f, \
.current = 5.0f, \
.error = 0, \
.cellVoltages = { { 3.1f, 3.3f, 3.0f, 3.1f, 3.2f, 2.95f } }, \
} } \
};
TEST(TextRecords,
EncodeAndDecodeSimpleRecord)
{
SETUP()
ASSERT_EQ(24.0f, moduleRecords[0].voltage.getValue());
float &v1 = moduleRecords[0].voltage;
v1 = 26.0f;
ASSERT_EQ(26.0f, moduleRecords[0].voltage.getValue());
float *vp1 = &moduleRecords[0].voltage;
*vp1 = 27.0f;
ASSERT_EQ(27.0f, moduleRecords[0].voltage.getValue());
moduleRecords[0].voltage = 25.0f;
moduleRecords[1].supercells[0].soc = 0.95f;
moduleRecords[1].supercells[5].soc = 0.95f;
char buffer[TEXT_ENCODER_BUFFER_SIZE];
size_t size = sizeof(buffer);
ThingSetTextEncoder encoder(buffer, size);
encoder.encode(moduleRecords.getValue());
const char *expected =
"[{\"voltage\":25.000000,\"current\":10.000000,\"error\":0,\"cellVoltages\":[3.200000,3.100000,2.900000,3."
"300000,0.000000,2.800000],\"supercells\":[{\"soc\":0.100000,\"soh\":1.000000},{\"soc\":0.250000,\"soh\":1."
"000000},{\"soc\":0.500000,\"soh\":1.000000},{\"soc\":0.750000,\"soh\":1.000000},{\"soc\":0.800000,\"soh\":1."
"000000},{\"soc\":1.000000,\"soh\":1.000000}]},{\"voltage\":24.200001,\"current\":5.000000,\"error\":0,"
"\"cellVoltages\":[3.100000,3.300000,3.000000,3.100000,3.200000,2.950000],\"supercells\":[{\"soc\":0.950000,"
"\"soh\":0.000000},{\"soc\":0.000000,\"soh\":0.000000},{\"soc\":0.000000,\"soh\":0.000000},{\"soc\":0.000000,"
"\"soh\":0.000000},{\"soc\":0.000000,\"soh\":0.000000},{\"soc\":0.950000,\"soh\":0.000000}]}]";
ASSERT_BUFFER_EQ(expected, buffer, encoder.getEncodedLength());
FixedSizeThingSetTextDecoder<128> decoder(buffer, size);
std::array<ModuleRecord, 2> newModuleRecords;
ASSERT_TRUE(decoder.decode(&newModuleRecords));
ASSERT_EQ(moduleRecords[0].voltage.getValue(), newModuleRecords[0].voltage.getValue());
ASSERT_EQ(moduleRecords[1].voltage.getValue(), newModuleRecords[1].voltage.getValue());
ASSERT_EQ(moduleRecords[0].current.getValue(), newModuleRecords[0].current.getValue());
ASSERT_EQ(moduleRecords[1].current.getValue(), newModuleRecords[1].current.getValue());
ASSERT_EQ(moduleRecords[0].supercells.getValue()[0].soc.getValue(),
newModuleRecords[0].supercells.getValue()[0].soc.getValue());
}
TEST(TextRecords, InitialiseRecordArrayCopy)
{
SETUP()
ThingSetReadOnlyRecordMember<0x800, 0x0, "Modules", std::array<ModuleRecord, 2>> records(moduleRecords.getValue());
ASSERT_EQ(24.2f, records[1].voltage.getValue());
char buffer[TEXT_ENCODER_BUFFER_SIZE];
size_t size = sizeof(buffer);
ThingSetTextEncoder encoder(buffer, size);
ASSERT_TRUE(records.encode(encoder));
const char *expected =
"[{\"voltage\":24.000000,\"current\":10.000000,\"error\":0,\"cellVoltages\":[3.200000,3.100000,2.900000,3."
"300000,0.000000,2.800000],\"supercells\":[{\"soc\":0.100000,\"soh\":1.000000},{\"soc\":0.250000,\"soh\":1."
"000000},{\"soc\":0.500000,\"soh\":1.000000},{\"soc\":0.750000,\"soh\":1.000000},{\"soc\":0.800000,\"soh\":1."
"000000},{\"soc\":1.000000,\"soh\":1.000000}]},{\"voltage\":24.200001,\"current\":5.000000,\"error\":0,"
"\"cellVoltages\":[3.100000,3.300000,3.000000,3.100000,3.200000,2.950000],\"supercells\":[{\"soc\":0.000000,"
"\"soh\":0.000000},{\"soc\":0.000000,\"soh\":0.000000},{\"soc\":0.000000,\"soh\":0.000000},{\"soc\":0.000000,"
"\"soh\":0.000000},{\"soc\":0.000000,\"soh\":0.000000},{\"soc\":0.000000,\"soh\":0.000000}]}]";
ASSERT_BUFFER_EQ(expected, buffer, encoder.getEncodedLength());
}
TEST(TextRecords, InitialiseRecordArrayInline)
{
SETUP()
ThingSetReadOnlyRecordMember<0x800, 0x0, "Modules", std::array<ModuleRecord, 1>> records = { { (ModuleRecord){
.voltage = 24.0f,
.current = 10.0f,
.error = 0,
} } };
ASSERT_EQ(24.0f, records[0].voltage.getValue());
char buffer[TEXT_ENCODER_BUFFER_SIZE];
size_t size = sizeof(buffer);
ThingSetTextEncoder encoder(buffer, size);
ASSERT_TRUE(records.encode(encoder));
const char *expected =
"[{\"voltage\":24.000000,\"current\":10.000000,\"error\":0,\"cellVoltages\":[0.000000,0.000000,0.000000,0."
"000000,0.000000,0.000000],\"supercells\":[{\"soc\":0.000000,\"soh\":0.000000},{\"soc\":0.000000,\"soh\":0."
"000000},{\"soc\":0.000000,\"soh\":0.000000},{\"soc\":0.000000,\"soh\":0.000000},{\"soc\":0.000000,\"soh\":0."
"000000},{\"soc\":0.000000,\"soh\":0.000000}]}]";
ASSERT_BUFFER_EQ(expected, buffer, encoder.getEncodedLength());
}