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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
#include <gtest/gtest.h>
#include <sstream>
#include <string>
#include "archive.h"
using namespace std::string_literals;
class ArchiveTest: public ::testing::Test
{
protected:
ArchiveTest(){}
~ArchiveTest() override{}
void SetUp() override{}
void TearDown() override{}
};
struct MyStruct
{
uint8_t member1;
uint16_t member2;
uint32_t member3;
uint64_t member4;
int64_t member5;
std::vector<uint8_t> member6;
std::string member7;
template <class Archive>
void serialize (Archive& ar)
{
ar & member1;
ar & member2;
ar & member3;
ar & member4;
ar & member5;
ar & member6;
ar & member7;
}
};
bool operator==(const MyStruct& lhs, const MyStruct& rhs)
{
return lhs.member1 == rhs.member1 &&
lhs.member2 == rhs.member2 &&
lhs.member3 == rhs.member3 &&
lhs.member4 == rhs.member4 &&
lhs.member5 == rhs.member5 &&
lhs.member6 == rhs.member6 &&
lhs.member7 == rhs.member7;
}
TEST_F(ArchiveTest, archive)
{
std::stringstream ss;
Reichwein::Serialization::OArchive oa(ss);
MyStruct mystruct0{123, 45678, 3, 4, 5, {1, 2}, "abc"};
oa << mystruct0;
EXPECT_EQ(ss.str().size(), 36);
Reichwein::Serialization::IArchive ia(ss);
MyStruct mystruct1{};
ia >> mystruct1;
EXPECT_EQ(mystruct0, mystruct1);
}
TEST_F(ArchiveTest, out)
{
std::stringstream ss(std::ios_base::in|std::ios_base::out|std::ios_base::binary);
Reichwein::Serialization::OArchive oarchive(ss);
uint64_t v1(123456789);
oarchive << v1;
ASSERT_EQ(ss.str().size(), sizeof(uint64_t));
uint32_t v2(87654321);
oarchive << v2;
ASSERT_EQ(ss.str().size(), sizeof(uint64_t) + sizeof(uint32_t));
uint16_t v3(9876);
oarchive << v3;
ASSERT_EQ(ss.str().size(), sizeof(uint64_t) + sizeof(uint32_t) + sizeof(uint16_t));
uint8_t v4(2);
oarchive << v4;
ASSERT_EQ(ss.str().size(), sizeof(uint64_t) + sizeof(uint32_t) + sizeof(uint16_t) + sizeof(uint8_t));
std::string v5("abcd");
oarchive << v5;
ASSERT_EQ(ss.str().size(), sizeof(uint64_t) + sizeof(uint32_t) + sizeof(uint16_t) + sizeof(uint8_t) + 4+4);
std::vector<uint8_t> v6 = {1, 2, 3, 4, 5};
oarchive << v6;
ASSERT_EQ(ss.str().size(), sizeof(uint64_t) + sizeof(uint32_t) + sizeof(uint16_t) + sizeof(uint8_t) + 4+4 + 4+5);
ASSERT_EQ(ss.str(), std::string("\x00\x00\x00\x00\x07\x5b\xcd\x15\x05\x39\x7f\xb1\x26\x94\x02" "\x00\x00\x00\x04" "abcd\x00\x00\x00\x05\x01\x02\x03\x04\x05", 32));
}
TEST_F(ArchiveTest, in)
{
std::stringstream ss(std::ios_base::in|std::ios_base::out|std::ios_base::binary);
std::string v1("\x00\x00\x00\x00\x07\x5b\xcd\x15\x05\x39\x7f\xb1\x26\x94\x02" "\x00\x00\x00\x04" "abcd\x00\x00\x00\x05\x01\x02\x03\x04\x05", 32);
ss.write(v1.data(), v1.size());
Reichwein::Serialization::IArchive iarchive(ss);
ASSERT_EQ(ss.str().size(), 32);
uint64_t x1;
iarchive >> x1;
ASSERT_EQ(x1, 123456789);
uint32_t x2;
iarchive >> x2;
ASSERT_EQ(x2, 87654321);
uint16_t x3;
iarchive >> x3;
ASSERT_EQ(x3, 9876);
uint8_t x4;
iarchive >> x4;
ASSERT_EQ(x4, 2);
std::string x5;
iarchive >> x5;
EXPECT_EQ(x5, "abcd");
std::vector<uint8_t> x6;
iarchive >> x6;
std::vector<uint8_t> v{1, 2, 3, 4, 5};
ASSERT_EQ(x6, v);
}
TEST_F(ArchiveTest, in_coro)
{
std::stringstream ss(std::ios_base::in|std::ios_base::out|std::ios_base::binary);
coro_t::pull_type coro([&](coro_t::push_type& sink)
{
sink();
sink();
std::string v1("\x00\x00\x00\x00\x07\x5b\xcd\x15\x05\x39\x7f\xb1\x26\x94\x02" "\x00\x00\x00\x04" "abcd\x00\x00\x00\x05\x01\x02\x03\x04\x05", 32);
for (int i = 0; i < v1.size(); i++) {
ss.write(v1.data() + i, 1);
sink();
}
sink();
});
Reichwein::Serialization::IArchive iarchive(ss, coro);
uint64_t x1;
iarchive >> x1;
ASSERT_EQ(x1, 123456789);
uint32_t x2;
iarchive >> x2;
ASSERT_EQ(x2, 87654321);
uint16_t x3;
iarchive >> x3;
ASSERT_EQ(x3, 9876);
uint8_t x4;
iarchive >> x4;
ASSERT_EQ(x4, 2);
std::string x5;
iarchive >> x5;
EXPECT_EQ(x5, "abcd");
std::vector<uint8_t> x6;
iarchive >> x6;
std::vector<uint8_t> v{1, 2, 3, 4, 5};
ASSERT_EQ(x6, v);
}
|