forked from Brill-Power/thingsetplusplus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestBinaryEncodingRecords.cpp
More file actions
135 lines (123 loc) · 8.09 KB
/
TestBinaryEncodingRecords.cpp
File metadata and controls
135 lines (123 loc) · 8.09 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
/*
* Copyright (c) 2024 Brill Power.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "thingset++/ThingSet.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() \
ThingSetReadOnlyProperty<std::array<ModuleRecord, 2>> moduleRecords { 0x610, 0, "Modules", \
{ (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(BinaryRecords, 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;
uint8_t buffer[512];
FixedDepthThingSetBinaryEncoder encoder(buffer, sizeof(buffer));
encoder.encode(moduleRecords.getValue());
ASSERT_EQ(0x82, buffer[0]); // array with 2 elements
ASSERT_EQ(0xA5, buffer[1]); // map with 5 elements
FixedDepthThingSetBinaryDecoder decoder(buffer, sizeof(buffer));
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(BinaryRecords, InitialiseRecordArrayCopy)
{
SETUP()
ThingSetReadOnlyProperty<std::array<ModuleRecord, 2>> records(0x800, 0x0, "Modules", moduleRecords.getValue());
ASSERT_EQ(24.2f, records[1].voltage.getValue());
uint8_t buffer[512];
FixedDepthThingSetBinaryEncoder encoder(buffer, sizeof(buffer));
ASSERT_TRUE(records.encode(encoder));
ASSERT_EQ(0x82, buffer[0]); // array with 2 elements
ASSERT_EQ(0xA5, buffer[1]); // map with 5 elements
}
TEST(BinaryRecords, RecordArrayIndexing)
{
SETUP()
ThingSetReadOnlyProperty<std::array<ModuleRecord, 2>> records(0x800, 0x0, "Modules", moduleRecords.getValue());
ASSERT_EQ(24.2f, records[1].voltage.getValue());
ModuleRecord *mod = &records[1];
ASSERT_EQ(24.2f, mod->voltage.getValue());
}
TEST(BinaryRecords, InitialiseRecordArrayInline)
{
SETUP()
ThingSetReadOnlyProperty<std::array<ModuleRecord, 1>> records { 0x800, 0x0, "Modules", { (ModuleRecord){
.voltage = 24.0f,
.current = 10.0f,
.error = 0,
} } };
ASSERT_EQ(24.0f, records[0].voltage.getValue());
uint8_t buffer[512];
FixedDepthThingSetBinaryEncoder encoder(buffer, sizeof(buffer));
ASSERT_TRUE(records.encode(encoder));
ASSERT_EQ(0x81, buffer[0]); // array with 2 elements
ASSERT_EQ(0xA5, buffer[1]); // map with 5 elements
}