-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest.cpp
More file actions
121 lines (100 loc) · 3.79 KB
/
Test.cpp
File metadata and controls
121 lines (100 loc) · 3.79 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
#include <iostream>
#include "BaseCom.hpp"
#include <array>
using namespace std;
using namespace translib;
struct PlainTestField : public ComPacket<uint8_t, uint16_t, int, long>
{
uint8_t &var1 = get<0>(elements);
uint16_t &var2 = get<1>(elements);
int &var3 = get<2>(elements);
long &var4 = get<3>(elements);
};
struct TestBitfield : public Bitfield<1> // Should always allocate one instance of the backing type
{
static const size_t TestBit_Offset = 0;
static const size_t TestBit_Length = 1;
uint8_t ReadTestBit()
{
return GetData<uint8_t>(TestBit_Offset, TestBit_Length);
}
void WriteTestBit(uint8_t data)
{
return WriteData(TestBit_Offset, TestBit_Length, data);
}
};
struct LargeBitField : public Bitfield<70> // Should always allocate at least two instances of the backing data type
{
static const size_t TestBit1_Offset = 0;
static const size_t TestBit1_Length = 5;
static const size_t TestBit2_Offset = 13;
static const size_t TestBit2_Length = 3;
uint8_t ReadTestBit1()
{
return GetData<uint8_t>(TestBit1_Offset, TestBit1_Length);
}
void WriteTestBit1(uint8_t data)
{
return WriteData(TestBit1_Offset, TestBit1_Length, data);
}
uint8_t ReadTestBit2()
{
return GetData<uint8_t>(TestBit2_Offset, TestBit2_Length);
}
void WriteTestBit2(uint8_t data)
{
return WriteData(TestBit2_Offset, TestBit2_Length, data);
}
};
struct MixedDataMessage : public ComPacket<int32_t, etl::string<10>, TestBitfield, LargeBitField, std::array<uint8_t, 10>>
{
int &Testfield1 = get<0>(elements);
etl::string<10> &Testfield2 = get<1>(elements);
TestBitfield &Testfield3 = get<2>(elements);
LargeBitField &Testfield4 = get<3>(elements);
std::array<uint8_t, 10> &TestArray = get<4>(elements);
};
int main(void)
{
PlainTestField plaintest;
plaintest.var1 = 10;
plaintest.var2 = 100;
assert(plaintest.var3 == 0 && plaintest.var1 == 10 && plaintest.var2 == 100);
TestBitfield bitfieldtest;
bitfieldtest.WriteTestBit(1);
assert(bitfieldtest.ReadTestBit() == 1);
bitfieldtest.WriteTestBit(5);
assert(bitfieldtest.ReadTestBit() == 1);
bitfieldtest.WriteTestBit(4);
assert(bitfieldtest.ReadTestBit() == 0);
LargeBitField largebitfield;
MixedDataMessage mixed;
mixed.Testfield1 = -10;
const etl::string<10> teststring("HELLO WORLD");
mixed.Testfield2 = teststring;
mixed.Testfield3.WriteTestBit(1);
mixed.TestArray.fill(5);
std::array<uint8_t, 2> id = {2,3};
std::array<uint8_t, mixed.GetMaxSize() + sizeof(id)> dataarray;
size_t packageLength = mixed.Serialize(dataarray, id);
assert(packageLength == (sizeof(int) + (mixed.Testfield2.size() + 1) + TestBitfield::BYTE_LENGTH + LargeBitField::BYTE_LENGTH + sizeof(id) + mixed.TestArray.size() * sizeof(uint8_t)));
MixedDataMessage deserializeTest;
size_t usedData;
auto [valid, packetStart, datalength] = MixedDataMessage::CheckIDMatch(dataarray, packageLength, id);
assert(valid);
typename std::array<uint8_t, mixed.GetMaxSize() + sizeof(id)>::const_iterator it;
std::tie(usedData, valid, it) = deserializeTest.Unserialize<dataarray.max_size()>(packetStart, dataarray.end(), datalength);
assert(valid);
assert(deserializeTest.Testfield1 == -10);
assert(deserializeTest.Testfield2 == teststring);
assert(deserializeTest.Testfield3.ReadTestBit() == 1);
for(const auto& d : deserializeTest.TestArray)
{
assert(d == 5);
}
std::array<uint8_t, 6> falseData = {10,10,20,20,30,30};
decltype(falseData.cbegin()) falseIterator;
std::tie(usedData, valid, falseIterator) = MixedDataMessage::Unserialize<falseData.max_size()>(falseData, falseData.max_size(), deserializeTest);
assert(!valid);
return 0;
}