test/zserio/SerializeUtilTest.cpp
Line | Count | Source |
1 | | #include <array> |
2 | | |
3 | | #include "gtest/gtest.h" |
4 | | #include "test_object/polymorphic_allocator/SerializeEnum.h" |
5 | | #include "test_object/polymorphic_allocator/SerializeNested.h" |
6 | | #include "test_object/polymorphic_allocator/SerializeObject.h" |
7 | | #include "test_object/std_allocator/SerializeEnum.h" |
8 | | #include "test_object/std_allocator/SerializeNested.h" |
9 | | #include "test_object/std_allocator/SerializeObject.h" |
10 | | #include "zserio/Enums.h" |
11 | | #include "zserio/SerializeUtil.h" |
12 | | #include "zserio/pmr/PolymorphicAllocator.h" |
13 | | |
14 | | namespace zserio |
15 | | { |
16 | | |
17 | | TEST(SerializeUtilTest, serializeEnum) |
18 | 1 | { |
19 | | // without allocator |
20 | 1 | { |
21 | 1 | const test_object::std_allocator::SerializeEnum serializeEnum = |
22 | 1 | test_object::std_allocator::SerializeEnum::VALUE3; |
23 | 1 | const BitBuffer bitBuffer = serialize(serializeEnum); |
24 | 1 | ASSERT_EQ(8, bitBuffer.getBitSize()); |
25 | 1 | ASSERT_EQ(0x02, bitBuffer.getData()[0]); |
26 | 1 | } |
27 | | |
28 | | // with std allocator |
29 | 1 | { |
30 | 1 | const std::allocator<uint8_t> allocator; |
31 | 1 | const test_object::std_allocator::SerializeEnum serializeEnum = |
32 | 1 | test_object::std_allocator::SerializeEnum::VALUE3; |
33 | 1 | const BitBuffer bitBuffer = serialize(serializeEnum, allocator); |
34 | 1 | ASSERT_EQ(8, bitBuffer.getBitSize()); |
35 | 1 | ASSERT_EQ(0x02, bitBuffer.getData()[0]); |
36 | 1 | } |
37 | | |
38 | | // with polymorphic allocator |
39 | 1 | { |
40 | 1 | const pmr::PropagatingPolymorphicAllocator<> allocator; |
41 | 1 | const test_object::polymorphic_allocator::SerializeEnum serializeEnum = |
42 | 1 | test_object::polymorphic_allocator::SerializeEnum::VALUE3; |
43 | 1 | const BasicBitBuffer<pmr::PropagatingPolymorphicAllocator<>> bitBuffer = |
44 | 1 | serialize(serializeEnum, allocator); |
45 | 1 | ASSERT_EQ(8, bitBuffer.getBitSize()); |
46 | 1 | ASSERT_EQ(0x02, bitBuffer.getData()[0]); |
47 | 1 | } |
48 | 1 | } |
49 | | |
50 | | TEST(SerializeUtilTest, serializeParameterizedObject) |
51 | 1 | { |
52 | 1 | const int8_t param = 0x12; |
53 | 1 | const uint8_t offset = 0; |
54 | 1 | const uint32_t optionalValue = 0xDEADCAFE; |
55 | 1 | { |
56 | | // without allocator |
57 | 1 | test_object::std_allocator::SerializeNested serializeNested(offset, optionalValue); |
58 | 1 | const BitBuffer bitBuffer = serialize(serializeNested, param); |
59 | 1 | ASSERT_EQ(40, bitBuffer.getBitSize()); |
60 | 1 | ASSERT_EQ(0x01, bitBuffer.getData()[0]); |
61 | 1 | ASSERT_EQ(0xDE, bitBuffer.getData()[1]); |
62 | 1 | ASSERT_EQ(0xAD, bitBuffer.getData()[2]); |
63 | 1 | ASSERT_EQ(0xCA, bitBuffer.getData()[3]); |
64 | 1 | ASSERT_EQ(0xFE, bitBuffer.getData()[4]); |
65 | 1 | } |
66 | | |
67 | 1 | { |
68 | | // with std allocator |
69 | 1 | const std::allocator<uint8_t> allocator; |
70 | 1 | test_object::std_allocator::SerializeNested serializeNested(offset, optionalValue, allocator); |
71 | 1 | const BitBuffer bitBuffer = serialize(serializeNested, allocator, param); |
72 | 1 | ASSERT_EQ(40, bitBuffer.getBitSize()); |
73 | 1 | ASSERT_EQ(0x01, bitBuffer.getData()[0]); |
74 | 1 | ASSERT_EQ(0xDE, bitBuffer.getData()[1]); |
75 | 1 | ASSERT_EQ(0xAD, bitBuffer.getData()[2]); |
76 | 1 | ASSERT_EQ(0xCA, bitBuffer.getData()[3]); |
77 | 1 | ASSERT_EQ(0xFE, bitBuffer.getData()[4]); |
78 | 1 | } |
79 | | |
80 | 1 | { |
81 | | // with polymorphic allocator |
82 | 1 | const pmr::PropagatingPolymorphicAllocator<> allocator; |
83 | 1 | test_object::polymorphic_allocator::SerializeNested serializeNested(offset, optionalValue, allocator); |
84 | 1 | const BasicBitBuffer<pmr::PropagatingPolymorphicAllocator<>> bitBuffer = |
85 | 1 | serialize(serializeNested, allocator, param); |
86 | 1 | ASSERT_EQ(40, bitBuffer.getBitSize()); |
87 | 1 | ASSERT_EQ(0x01, bitBuffer.getData()[0]); |
88 | 1 | ASSERT_EQ(0xDE, bitBuffer.getData()[1]); |
89 | 1 | ASSERT_EQ(0xAD, bitBuffer.getData()[2]); |
90 | 1 | ASSERT_EQ(0xCA, bitBuffer.getData()[3]); |
91 | 1 | ASSERT_EQ(0xFE, bitBuffer.getData()[4]); |
92 | 1 | } |
93 | 1 | } |
94 | | |
95 | | TEST(SerializeUtilTest, serializeObject) |
96 | 1 | { |
97 | 1 | const int8_t param = 0x12; |
98 | 1 | const uint8_t offset = 0; |
99 | 1 | const uint32_t optionalValue = 0xDEADCAFE; |
100 | 1 | { |
101 | | // without allocator |
102 | 1 | test_object::std_allocator::SerializeNested serializeNested(offset, optionalValue); |
103 | 1 | test_object::std_allocator::SerializeObject serializeObject(param, serializeNested); |
104 | 1 | const BitBuffer bitBuffer = serialize(serializeObject); |
105 | 1 | ASSERT_EQ(48, bitBuffer.getBitSize()); |
106 | 1 | ASSERT_EQ(0x12, bitBuffer.getData()[0]); |
107 | 1 | ASSERT_EQ(0x02, bitBuffer.getData()[1]); |
108 | 1 | ASSERT_EQ(0xDE, bitBuffer.getData()[2]); |
109 | 1 | ASSERT_EQ(0xAD, bitBuffer.getData()[3]); |
110 | 1 | ASSERT_EQ(0xCA, bitBuffer.getData()[4]); |
111 | 1 | ASSERT_EQ(0xFE, bitBuffer.getData()[5]); |
112 | 1 | } |
113 | | |
114 | 1 | { |
115 | | // with std allocator |
116 | 1 | const std::allocator<uint8_t> allocator; |
117 | 1 | test_object::std_allocator::SerializeNested serializeNested(offset, optionalValue, allocator); |
118 | 1 | test_object::std_allocator::SerializeObject serializeObject(param, serializeNested, allocator); |
119 | 1 | const BitBuffer bitBuffer = serialize(serializeObject, allocator); |
120 | 1 | ASSERT_EQ(48, bitBuffer.getBitSize()); |
121 | 1 | ASSERT_EQ(0x12, bitBuffer.getData()[0]); |
122 | 1 | ASSERT_EQ(0x02, bitBuffer.getData()[1]); |
123 | 1 | ASSERT_EQ(0xDE, bitBuffer.getData()[2]); |
124 | 1 | ASSERT_EQ(0xAD, bitBuffer.getData()[3]); |
125 | 1 | ASSERT_EQ(0xCA, bitBuffer.getData()[4]); |
126 | 1 | ASSERT_EQ(0xFE, bitBuffer.getData()[5]); |
127 | 1 | } |
128 | | |
129 | 1 | { |
130 | | // with polymorphic allocator |
131 | 1 | const pmr::PropagatingPolymorphicAllocator<> allocator; |
132 | 1 | test_object::polymorphic_allocator::SerializeNested serializeNested(offset, optionalValue, allocator); |
133 | 1 | test_object::polymorphic_allocator::SerializeObject serializeObject(param, serializeNested, allocator); |
134 | 1 | const BasicBitBuffer<pmr::PropagatingPolymorphicAllocator<>> bitBuffer = |
135 | 1 | serialize(serializeObject, allocator); |
136 | 1 | ASSERT_EQ(48, bitBuffer.getBitSize()); |
137 | 1 | ASSERT_EQ(0x12, bitBuffer.getData()[0]); |
138 | 1 | ASSERT_EQ(0x02, bitBuffer.getData()[1]); |
139 | 1 | ASSERT_EQ(0xDE, bitBuffer.getData()[2]); |
140 | 1 | ASSERT_EQ(0xAD, bitBuffer.getData()[3]); |
141 | 1 | ASSERT_EQ(0xCA, bitBuffer.getData()[4]); |
142 | 1 | ASSERT_EQ(0xFE, bitBuffer.getData()[5]); |
143 | 1 | } |
144 | 1 | } |
145 | | |
146 | | TEST(SerializeUtilTest, deserializeEnum) |
147 | 1 | { |
148 | 1 | const std::array<uint8_t, 1> buffer = {0x02}; |
149 | | // without allocator |
150 | 1 | { |
151 | 1 | const BitBuffer bitBuffer(buffer.data(), buffer.size() * 8); |
152 | 1 | const test_object::std_allocator::SerializeEnum serializeEnum = |
153 | 1 | deserialize<test_object::std_allocator::SerializeEnum>(bitBuffer); |
154 | 1 | ASSERT_EQ(test_object::std_allocator::SerializeEnum::VALUE3, serializeEnum); |
155 | 1 | } |
156 | | |
157 | | // with std allocator |
158 | 1 | { |
159 | 1 | const std::allocator<uint8_t> allocator; |
160 | 1 | const BitBuffer bitBuffer(buffer.data(), buffer.size() * 8, allocator); |
161 | 1 | const test_object::std_allocator::SerializeEnum serializeEnum = |
162 | 1 | deserialize<test_object::std_allocator::SerializeEnum>(bitBuffer); |
163 | 1 | ASSERT_EQ(test_object::std_allocator::SerializeEnum::VALUE3, serializeEnum); |
164 | 1 | } |
165 | | |
166 | | // with polymorphic allocator |
167 | 1 | { |
168 | 1 | const pmr::PropagatingPolymorphicAllocator<> allocator; |
169 | 1 | const BasicBitBuffer<pmr::PropagatingPolymorphicAllocator<>> bitBuffer( |
170 | 1 | buffer.data(), buffer.size() * 8, allocator); |
171 | 1 | const test_object::polymorphic_allocator::SerializeEnum serializeEnum = |
172 | 1 | deserialize<test_object::polymorphic_allocator::SerializeEnum>(bitBuffer); |
173 | 1 | ASSERT_EQ(test_object::polymorphic_allocator::SerializeEnum::VALUE3, serializeEnum); |
174 | 1 | } |
175 | 1 | } |
176 | | |
177 | | TEST(SerializeUtilTest, deserializeNestedObject) |
178 | 1 | { |
179 | 1 | const std::array<uint8_t, 5> buffer = {0x01, 0xDE, 0xAD, 0xCA, 0xFE}; |
180 | 1 | const int8_t param = 0x12; |
181 | | // without allocator |
182 | 1 | { |
183 | 1 | const BitBuffer bitBuffer(buffer.data(), buffer.size() * 8); |
184 | 1 | const auto serializeNested = deserialize<test_object::std_allocator::SerializeNested>(bitBuffer, param); |
185 | 1 | ASSERT_EQ(param, serializeNested.getParam()); |
186 | 1 | ASSERT_EQ(0x01, serializeNested.getOffset()); |
187 | 1 | ASSERT_EQ(0xDEADCAFE, serializeNested.getOptionalValue()); |
188 | | |
189 | 1 | const auto serializeNested2 = deserialize<test_object::std_allocator::SerializeNested>( |
190 | 1 | bitBuffer, ArrayPreallocation(1024), param); |
191 | 1 | ASSERT_EQ(serializeNested, serializeNested2); |
192 | | |
193 | 1 | const BitBuffer wrongBitBuffer(buffer.data(), buffer.size() * 8 - 1); |
194 | 1 | ASSERT_THROW(deserialize<test_object::std_allocator::SerializeNested>(wrongBitBuffer, param), |
195 | 1 | CppRuntimeException); |
196 | 1 | } |
197 | | |
198 | | // with std allocator |
199 | 1 | { |
200 | 1 | const std::allocator<uint8_t> allocator; |
201 | 1 | const BitBuffer bitBuffer(buffer.data(), buffer.size() * 8, allocator); |
202 | 1 | const auto serializeNested = |
203 | 1 | deserialize<test_object::std_allocator::SerializeNested>(bitBuffer, param, allocator); |
204 | 1 | ASSERT_EQ(param, serializeNested.getParam()); |
205 | 1 | ASSERT_EQ(0x01, serializeNested.getOffset()); |
206 | 1 | ASSERT_EQ(0xDEADCAFE, serializeNested.getOptionalValue()); |
207 | | |
208 | 1 | const auto serializeNested2 = deserialize<test_object::std_allocator::SerializeNested>( |
209 | 1 | bitBuffer, ArrayPreallocation(1024), param, allocator); |
210 | 1 | ASSERT_EQ(serializeNested, serializeNested2); |
211 | | |
212 | 1 | const BitBuffer wrongBitBuffer(buffer.data(), buffer.size() * 8 - 1, allocator); |
213 | 1 | ASSERT_THROW(deserialize<test_object::std_allocator::SerializeNested>(wrongBitBuffer, param, allocator), |
214 | 1 | CppRuntimeException); |
215 | 1 | } |
216 | | |
217 | | // with polymorphic allocator |
218 | 1 | { |
219 | 1 | const pmr::PropagatingPolymorphicAllocator<> allocator; |
220 | 1 | const BasicBitBuffer<pmr::PropagatingPolymorphicAllocator<>> bitBuffer( |
221 | 1 | buffer.data(), buffer.size() * 8, allocator); |
222 | 1 | const auto serializeNested = |
223 | 1 | deserialize<test_object::polymorphic_allocator::SerializeNested>(bitBuffer, param, allocator); |
224 | 1 | ASSERT_EQ(param, serializeNested.getParam()); |
225 | 1 | ASSERT_EQ(0x01, serializeNested.getOffset()); |
226 | 1 | ASSERT_EQ(0xDEADCAFE, serializeNested.getOptionalValue()); |
227 | | |
228 | 1 | const auto serializeNested2 = deserialize<test_object::polymorphic_allocator::SerializeNested>( |
229 | 1 | bitBuffer, ArrayPreallocation(1024), param, allocator); |
230 | 1 | ASSERT_EQ(serializeNested, serializeNested2); |
231 | | |
232 | 1 | const BasicBitBuffer<pmr::PropagatingPolymorphicAllocator<>> wrongBitBuffer( |
233 | 1 | buffer.data(), buffer.size() * 8 - 1, allocator); |
234 | 1 | ASSERT_THROW(deserialize<test_object::polymorphic_allocator::SerializeNested>( |
235 | 1 | wrongBitBuffer, param, allocator), |
236 | 1 | CppRuntimeException); |
237 | 1 | } |
238 | 1 | } |
239 | | |
240 | | TEST(SerializeUtilTest, deserializeObject) |
241 | 1 | { |
242 | 1 | const std::array<uint8_t, 6> buffer = {0x12, 0x02, 0xDE, 0xAD, 0xCA, 0xFE}; |
243 | | // without allocator |
244 | 1 | { |
245 | 1 | const BitBuffer bitBuffer(buffer.data(), buffer.size() * 8); |
246 | 1 | const auto serializeObject = deserialize<test_object::std_allocator::SerializeObject>(bitBuffer); |
247 | 1 | ASSERT_EQ(0x12, serializeObject.getParam()); |
248 | 1 | const test_object::std_allocator::SerializeNested& serializeNested = serializeObject.getNested(); |
249 | 1 | ASSERT_EQ(0x12, serializeNested.getParam()); |
250 | 1 | ASSERT_EQ(0x02, serializeNested.getOffset()); |
251 | 1 | ASSERT_EQ(0xDEADCAFE, serializeNested.getOptionalValue()); |
252 | | |
253 | 1 | const auto serializeObject2 = |
254 | 1 | deserialize<test_object::std_allocator::SerializeObject>(bitBuffer, ArrayPreallocation(1024)); |
255 | 1 | ASSERT_EQ(serializeObject, serializeObject2); |
256 | 1 | } |
257 | | |
258 | | // with std allocator |
259 | 1 | { |
260 | 1 | const std::allocator<uint8_t> allocator; |
261 | 1 | const BitBuffer bitBuffer(buffer.data(), buffer.size() * 8, allocator); |
262 | 1 | const auto serializeObject = |
263 | 1 | deserialize<test_object::std_allocator::SerializeObject>(bitBuffer, allocator); |
264 | 1 | ASSERT_EQ(0x12, serializeObject.getParam()); |
265 | 1 | const test_object::std_allocator::SerializeNested& serializeNested = serializeObject.getNested(); |
266 | 1 | ASSERT_EQ(0x12, serializeNested.getParam()); |
267 | 1 | ASSERT_EQ(0x02, serializeNested.getOffset()); |
268 | 1 | ASSERT_EQ(0xDEADCAFE, serializeNested.getOptionalValue()); |
269 | | |
270 | 1 | const auto serializeObject2 = deserialize<test_object::std_allocator::SerializeObject>( |
271 | 1 | bitBuffer, ArrayPreallocation(1024), allocator); |
272 | 1 | ASSERT_EQ(serializeObject, serializeObject2); |
273 | 1 | } |
274 | | |
275 | | // with polymorphic allocator |
276 | 1 | { |
277 | 1 | const pmr::PropagatingPolymorphicAllocator<> allocator; |
278 | 1 | const BasicBitBuffer<pmr::PropagatingPolymorphicAllocator<>> bitBuffer( |
279 | 1 | buffer.data(), buffer.size() * 8, allocator); |
280 | 1 | const auto serializeObject = |
281 | 1 | deserialize<test_object::polymorphic_allocator::SerializeObject>(bitBuffer, allocator); |
282 | 1 | ASSERT_EQ(0x12, serializeObject.getParam()); |
283 | 1 | const test_object::polymorphic_allocator::SerializeNested& serializeNested = |
284 | 1 | serializeObject.getNested(); |
285 | 1 | ASSERT_EQ(0x12, serializeNested.getParam()); |
286 | 1 | ASSERT_EQ(0x02, serializeNested.getOffset()); |
287 | 1 | ASSERT_EQ(0xDEADCAFE, serializeNested.getOptionalValue()); |
288 | | |
289 | 1 | const auto serializeObject2 = deserialize<test_object::polymorphic_allocator::SerializeObject>( |
290 | 1 | bitBuffer, ArrayPreallocation(1024), allocator); |
291 | 1 | ASSERT_EQ(serializeObject, serializeObject2); |
292 | 1 | } |
293 | 1 | } |
294 | | |
295 | | TEST(SerializeUtilTest, serializeEnumToBytes) |
296 | 1 | { |
297 | | // without allocator |
298 | 1 | { |
299 | 1 | const test_object::std_allocator::SerializeEnum serializeEnum = |
300 | 1 | test_object::std_allocator::SerializeEnum::VALUE3; |
301 | 1 | const vector<uint8_t> buffer = serializeToBytes(serializeEnum); |
302 | 1 | ASSERT_EQ(1, buffer.size()); |
303 | 1 | ASSERT_EQ(0x02, buffer[0]); |
304 | 1 | } |
305 | | |
306 | | // with std allocator |
307 | 1 | { |
308 | 1 | const std::allocator<uint8_t> allocator; |
309 | 1 | const test_object::std_allocator::SerializeEnum serializeEnum = |
310 | 1 | test_object::std_allocator::SerializeEnum::VALUE3; |
311 | 1 | const vector<uint8_t> buffer = serializeToBytes(serializeEnum, allocator); |
312 | 1 | ASSERT_EQ(1, buffer.size()); |
313 | 1 | ASSERT_EQ(0x02, buffer[0]); |
314 | 1 | } |
315 | | |
316 | | // with polymorphic allocator |
317 | 1 | { |
318 | 1 | const pmr::PropagatingPolymorphicAllocator<> allocator; |
319 | 1 | const test_object::polymorphic_allocator::SerializeEnum serializeEnum = |
320 | 1 | test_object::polymorphic_allocator::SerializeEnum::VALUE3; |
321 | 1 | const vector<uint8_t, pmr::PropagatingPolymorphicAllocator<>> buffer = |
322 | 1 | serializeToBytes(serializeEnum, allocator); |
323 | 1 | ASSERT_EQ(1, buffer.size()); |
324 | 1 | ASSERT_EQ(0x02, buffer[0]); |
325 | 1 | } |
326 | 1 | } |
327 | | |
328 | | TEST(SerializeUtilTest, serializeParameterizedObjectToBytes) |
329 | 1 | { |
330 | 1 | const int8_t param = 0x12; |
331 | 1 | const uint8_t offset = 0; |
332 | 1 | const uint32_t optionalValue = 0xDEADCAFE; |
333 | 1 | { |
334 | | // without allocator |
335 | 1 | test_object::std_allocator::SerializeNested serializeNested(offset, optionalValue); |
336 | 1 | const vector<uint8_t> buffer = serializeToBytes(serializeNested, param); |
337 | 1 | ASSERT_EQ(5, buffer.size()); |
338 | 1 | ASSERT_EQ(0x01, buffer[0]); |
339 | 1 | ASSERT_EQ(0xDE, buffer[1]); |
340 | 1 | ASSERT_EQ(0xAD, buffer[2]); |
341 | 1 | ASSERT_EQ(0xCA, buffer[3]); |
342 | 1 | ASSERT_EQ(0xFE, buffer[4]); |
343 | 1 | } |
344 | | |
345 | 1 | { |
346 | | // with std allocator |
347 | 1 | const std::allocator<uint8_t> allocator; |
348 | 1 | test_object::std_allocator::SerializeNested serializeNested(offset, optionalValue, allocator); |
349 | 1 | const vector<uint8_t> buffer = serializeToBytes(serializeNested, allocator, param); |
350 | 1 | ASSERT_EQ(5, buffer.size()); |
351 | 1 | ASSERT_EQ(0x01, buffer[0]); |
352 | 1 | ASSERT_EQ(0xDE, buffer[1]); |
353 | 1 | ASSERT_EQ(0xAD, buffer[2]); |
354 | 1 | ASSERT_EQ(0xCA, buffer[3]); |
355 | 1 | ASSERT_EQ(0xFE, buffer[4]); |
356 | 1 | } |
357 | | |
358 | 1 | { |
359 | | // with polymorphic allocator |
360 | 1 | const pmr::PropagatingPolymorphicAllocator<> allocator; |
361 | 1 | test_object::polymorphic_allocator::SerializeNested serializeNested(offset, optionalValue, allocator); |
362 | 1 | const vector<uint8_t, pmr::PropagatingPolymorphicAllocator<>> buffer = |
363 | 1 | serializeToBytes(serializeNested, allocator, param); |
364 | 1 | ASSERT_EQ(5, buffer.size()); |
365 | 1 | ASSERT_EQ(0x01, buffer[0]); |
366 | 1 | ASSERT_EQ(0xDE, buffer[1]); |
367 | 1 | ASSERT_EQ(0xAD, buffer[2]); |
368 | 1 | ASSERT_EQ(0xCA, buffer[3]); |
369 | 1 | ASSERT_EQ(0xFE, buffer[4]); |
370 | 1 | } |
371 | 1 | } |
372 | | |
373 | | TEST(SerializeUtilTest, serializeObjectToBytes) |
374 | 1 | { |
375 | 1 | const int8_t param = 0x12; |
376 | 1 | const uint8_t offset = 0; |
377 | 1 | const uint32_t optionalValue = 0xDEADCAFE; |
378 | 1 | { |
379 | | // without allocator |
380 | 1 | test_object::std_allocator::SerializeNested serializeNested(offset, optionalValue); |
381 | 1 | test_object::std_allocator::SerializeObject serializeObject(param, serializeNested); |
382 | 1 | const vector<uint8_t> buffer = serializeToBytes(serializeObject); |
383 | 1 | ASSERT_EQ(6, buffer.size()); |
384 | 1 | ASSERT_EQ(0x12, buffer[0]); |
385 | 1 | ASSERT_EQ(0x02, buffer[1]); |
386 | 1 | ASSERT_EQ(0xDE, buffer[2]); |
387 | 1 | ASSERT_EQ(0xAD, buffer[3]); |
388 | 1 | ASSERT_EQ(0xCA, buffer[4]); |
389 | 1 | ASSERT_EQ(0xFE, buffer[5]); |
390 | 1 | } |
391 | | |
392 | 1 | { |
393 | | // with std allocator |
394 | 1 | const std::allocator<uint8_t> allocator; |
395 | 1 | test_object::std_allocator::SerializeNested serializeNested(offset, optionalValue, allocator); |
396 | 1 | test_object::std_allocator::SerializeObject serializeObject(param, serializeNested, allocator); |
397 | 1 | const vector<uint8_t> buffer = serializeToBytes(serializeObject, allocator); |
398 | 1 | ASSERT_EQ(6, buffer.size()); |
399 | 1 | ASSERT_EQ(0x12, buffer[0]); |
400 | 1 | ASSERT_EQ(0x02, buffer[1]); |
401 | 1 | ASSERT_EQ(0xDE, buffer[2]); |
402 | 1 | ASSERT_EQ(0xAD, buffer[3]); |
403 | 1 | ASSERT_EQ(0xCA, buffer[4]); |
404 | 1 | ASSERT_EQ(0xFE, buffer[5]); |
405 | 1 | } |
406 | | |
407 | 1 | { |
408 | | // with polymorphic allocator |
409 | 1 | const pmr::PropagatingPolymorphicAllocator<> allocator; |
410 | 1 | test_object::polymorphic_allocator::SerializeNested serializeNested(offset, optionalValue, allocator); |
411 | 1 | test_object::polymorphic_allocator::SerializeObject serializeObject(param, serializeNested, allocator); |
412 | 1 | const vector<uint8_t, pmr::PropagatingPolymorphicAllocator<>> buffer = |
413 | 1 | serializeToBytes(serializeObject, allocator); |
414 | 1 | ASSERT_EQ(6, buffer.size()); |
415 | 1 | ASSERT_EQ(0x12, buffer[0]); |
416 | 1 | ASSERT_EQ(0x02, buffer[1]); |
417 | 1 | ASSERT_EQ(0xDE, buffer[2]); |
418 | 1 | ASSERT_EQ(0xAD, buffer[3]); |
419 | 1 | ASSERT_EQ(0xCA, buffer[4]); |
420 | 1 | ASSERT_EQ(0xFE, buffer[5]); |
421 | 1 | } |
422 | 1 | } |
423 | | |
424 | | TEST(SerializeUtilTest, deserializeEnumFromBytes) |
425 | 1 | { |
426 | | // without allocator |
427 | 1 | { |
428 | 1 | const vector<uint8_t> buffer({0x02}); |
429 | 1 | const test_object::std_allocator::SerializeEnum serializeEnum = |
430 | 1 | deserializeFromBytes<test_object::std_allocator::SerializeEnum>(buffer); |
431 | 1 | ASSERT_EQ(test_object::std_allocator::SerializeEnum::VALUE3, serializeEnum); |
432 | 1 | } |
433 | | |
434 | | // with std allocator |
435 | 1 | { |
436 | 1 | const std::allocator<uint8_t> allocator; |
437 | 1 | const vector<uint8_t> buffer({0x02}, allocator); |
438 | 1 | const test_object::std_allocator::SerializeEnum serializeEnum = |
439 | 1 | deserializeFromBytes<test_object::std_allocator::SerializeEnum>(buffer); |
440 | 1 | ASSERT_EQ(test_object::std_allocator::SerializeEnum::VALUE3, serializeEnum); |
441 | 1 | } |
442 | | |
443 | | // with polymorphic allocator |
444 | 1 | { |
445 | 1 | const pmr::PropagatingPolymorphicAllocator<> allocator; |
446 | 1 | const vector<uint8_t, pmr::PropagatingPolymorphicAllocator<>> buffer({0x02}, allocator); |
447 | 1 | const test_object::polymorphic_allocator::SerializeEnum serializeEnum = |
448 | 1 | deserializeFromBytes<test_object::polymorphic_allocator::SerializeEnum>(buffer); |
449 | 1 | ASSERT_EQ(test_object::polymorphic_allocator::SerializeEnum::VALUE3, serializeEnum); |
450 | 1 | } |
451 | 1 | } |
452 | | |
453 | | TEST(SerializeUtilTest, deserializeNestedObjectFromBytes) |
454 | 1 | { |
455 | 1 | const int8_t param = 0x12; |
456 | | // without allocator |
457 | 1 | { |
458 | 1 | const vector<uint8_t> buffer({0x01, 0xDE, 0xAD, 0xCA, 0xFE}); |
459 | 1 | const test_object::std_allocator::SerializeNested serializeNested = |
460 | 1 | deserializeFromBytes<test_object::std_allocator::SerializeNested>(buffer, param); |
461 | 1 | ASSERT_EQ(param, serializeNested.getParam()); |
462 | 1 | ASSERT_EQ(0x01, serializeNested.getOffset()); |
463 | 1 | ASSERT_EQ(0xDEADCAFE, serializeNested.getOptionalValue()); |
464 | | |
465 | 1 | const test_object::std_allocator::SerializeNested serializeNested2 = |
466 | 1 | deserializeFromBytes<test_object::std_allocator::SerializeNested>( |
467 | 1 | buffer, ArrayPreallocation(1024), param); |
468 | 1 | ASSERT_EQ(serializeNested, serializeNested2); |
469 | | |
470 | 1 | const vector<uint8_t> wrongBuffer = {0x00, 0xDE, 0xAD, 0xCA, 0xFE}; |
471 | 1 | ASSERT_THROW(deserializeFromBytes<test_object::std_allocator::SerializeNested>(wrongBuffer, param), |
472 | 1 | CppRuntimeException); |
473 | 1 | } |
474 | | |
475 | | // with std allocator |
476 | 1 | { |
477 | 1 | const std::allocator<uint8_t> allocator; |
478 | 1 | const vector<uint8_t> buffer({0x01, 0xDE, 0xAD, 0xCA, 0xFE}, allocator); |
479 | 1 | const auto serializeNested = |
480 | 1 | deserializeFromBytes<test_object::std_allocator::SerializeNested>(buffer, param, allocator); |
481 | 1 | ASSERT_EQ(param, serializeNested.getParam()); |
482 | 1 | ASSERT_EQ(0x01, serializeNested.getOffset()); |
483 | 1 | ASSERT_EQ(0xDEADCAFE, serializeNested.getOptionalValue()); |
484 | | |
485 | 1 | const auto serializeNested2 = deserializeFromBytes<test_object::std_allocator::SerializeNested>( |
486 | 1 | buffer, ArrayPreallocation(1024), param, allocator); |
487 | 1 | ASSERT_EQ(serializeNested, serializeNested2); |
488 | | |
489 | 1 | const vector<uint8_t> wrongBuffer = {0x00, 0xDE, 0xAD, 0xCA, 0xFE}; |
490 | 1 | ASSERT_THROW(deserializeFromBytes<test_object::std_allocator::SerializeNested>(wrongBuffer, param), |
491 | 1 | CppRuntimeException); |
492 | 1 | } |
493 | | |
494 | | // with polymorphic allocator |
495 | 1 | { |
496 | 1 | const pmr::PropagatingPolymorphicAllocator<> allocator; |
497 | 1 | const vector<uint8_t, pmr::PropagatingPolymorphicAllocator<>> buffer( |
498 | 1 | {0x01, 0xDE, 0xAD, 0xCA, 0xFE}, allocator); |
499 | 1 | const test_object::polymorphic_allocator::SerializeNested serializeNested = |
500 | 1 | deserializeFromBytes<test_object::polymorphic_allocator::SerializeNested>( |
501 | 1 | buffer, param, allocator); |
502 | 1 | ASSERT_EQ(param, serializeNested.getParam()); |
503 | 1 | ASSERT_EQ(0x01, serializeNested.getOffset()); |
504 | 1 | ASSERT_EQ(0xDEADCAFE, serializeNested.getOptionalValue()); |
505 | | |
506 | 1 | const auto serializeNested2 = deserializeFromBytes<test_object::polymorphic_allocator::SerializeNested>( |
507 | 1 | buffer, ArrayPreallocation(1024), param, allocator); |
508 | 1 | ASSERT_EQ(serializeNested, serializeNested2); |
509 | | |
510 | 1 | const vector<uint8_t> wrongBuffer = {0x00, 0xDE, 0xAD, 0xCA, 0xFE}; |
511 | 1 | ASSERT_THROW( |
512 | 1 | deserializeFromBytes<test_object::polymorphic_allocator::SerializeNested>(wrongBuffer, param), |
513 | 1 | CppRuntimeException); |
514 | 1 | } |
515 | 1 | } |
516 | | |
517 | | TEST(SerializeUtilTest, deserializeObjectFromBytes) |
518 | 1 | { |
519 | | // without allocator |
520 | 1 | { |
521 | 1 | const vector<uint8_t> buffer({0x12, 0x02, 0xDE, 0xAD, 0xCA, 0xFE}); |
522 | 1 | const auto serializeObject = deserializeFromBytes<test_object::std_allocator::SerializeObject>(buffer); |
523 | 1 | ASSERT_EQ(0x12, serializeObject.getParam()); |
524 | 1 | const test_object::std_allocator::SerializeNested& serializeNested = serializeObject.getNested(); |
525 | 1 | ASSERT_EQ(0x12, serializeNested.getParam()); |
526 | 1 | ASSERT_EQ(0x02, serializeNested.getOffset()); |
527 | 1 | ASSERT_EQ(0xDEADCAFE, serializeNested.getOptionalValue()); |
528 | | |
529 | 1 | const auto serializeObject2 = deserializeFromBytes<test_object::std_allocator::SerializeObject>( |
530 | 1 | buffer, ArrayPreallocation(1024)); |
531 | 1 | ASSERT_EQ(serializeObject, serializeObject2); |
532 | 1 | } |
533 | | |
534 | | // with std allocator |
535 | 1 | { |
536 | 1 | const std::allocator<uint8_t> allocator; |
537 | 1 | const vector<uint8_t> buffer({0x12, 0x02, 0xDE, 0xAD, 0xCA, 0xFE}, allocator); |
538 | 1 | const auto serializeObject = |
539 | 1 | deserializeFromBytes<test_object::std_allocator::SerializeObject>(buffer, allocator); |
540 | 1 | ASSERT_EQ(0x12, serializeObject.getParam()); |
541 | 1 | const test_object::std_allocator::SerializeNested& serializeNested = serializeObject.getNested(); |
542 | 1 | ASSERT_EQ(0x12, serializeNested.getParam()); |
543 | 1 | ASSERT_EQ(0x02, serializeNested.getOffset()); |
544 | 1 | ASSERT_EQ(0xDEADCAFE, serializeNested.getOptionalValue()); |
545 | | |
546 | 1 | const auto serializeObject2 = deserializeFromBytes<test_object::std_allocator::SerializeObject>( |
547 | 1 | buffer, ArrayPreallocation(1024), allocator); |
548 | 1 | ASSERT_EQ(serializeObject, serializeObject2); |
549 | 1 | } |
550 | | |
551 | | // with polymorphic allocator |
552 | 1 | { |
553 | 1 | const pmr::PropagatingPolymorphicAllocator<> allocator; |
554 | 1 | const vector<uint8_t, pmr::PropagatingPolymorphicAllocator<>> buffer( |
555 | 1 | {0x12, 0x02, 0xDE, 0xAD, 0xCA, 0xFE}, allocator); |
556 | 1 | const auto serializeObject = |
557 | 1 | deserializeFromBytes<test_object::polymorphic_allocator::SerializeObject>(buffer, allocator); |
558 | 1 | ASSERT_EQ(0x12, serializeObject.getParam()); |
559 | 1 | const test_object::polymorphic_allocator::SerializeNested& serializeNested = |
560 | 1 | serializeObject.getNested(); |
561 | 1 | ASSERT_EQ(0x12, serializeNested.getParam()); |
562 | 1 | ASSERT_EQ(0x02, serializeNested.getOffset()); |
563 | 1 | ASSERT_EQ(0xDEADCAFE, serializeNested.getOptionalValue()); |
564 | | |
565 | 1 | const auto serializeObject2 = deserializeFromBytes<test_object::polymorphic_allocator::SerializeObject>( |
566 | 1 | buffer, ArrayPreallocation(1024), allocator); |
567 | 1 | ASSERT_EQ(serializeObject, serializeObject2); |
568 | 1 | } |
569 | 1 | } |
570 | | |
571 | | TEST(SerializeUtilTest, serializeToFileFromFile) |
572 | 1 | { |
573 | 1 | const int8_t param = 0x12; |
574 | 1 | const uint8_t offset = 0; |
575 | 1 | const uint32_t optionalValue = 0xDEADCAFE; |
576 | 1 | const std::string fileName = "SerializationTest.bin"; |
577 | 1 | { |
578 | | // without allocator |
579 | 1 | test_object::std_allocator::SerializeNested serializeNested(offset, optionalValue); |
580 | 1 | test_object::std_allocator::SerializeObject serializeObject(param, serializeNested); |
581 | 1 | serializeToFile(serializeObject, fileName); |
582 | 1 | const test_object::std_allocator::SerializeObject readSerializeObject = |
583 | 1 | deserializeFromFile<test_object::std_allocator::SerializeObject>(fileName); |
584 | 1 | ASSERT_EQ(serializeObject, readSerializeObject); |
585 | 1 | } |
586 | | |
587 | 1 | { |
588 | | // with std allocator |
589 | 1 | const std::allocator<uint8_t> allocator; |
590 | 1 | test_object::std_allocator::SerializeNested serializeNested(offset, optionalValue, allocator); |
591 | 1 | test_object::std_allocator::SerializeObject serializeObject(param, serializeNested, allocator); |
592 | 1 | serializeToFile(serializeObject, fileName); |
593 | 1 | const test_object::std_allocator::SerializeObject readSerializeObject = |
594 | 1 | deserializeFromFile<test_object::std_allocator::SerializeObject>(fileName); |
595 | 1 | ASSERT_EQ(serializeObject, readSerializeObject); |
596 | 1 | } |
597 | | |
598 | 1 | { |
599 | | // with polymorphic allocator |
600 | 1 | const pmr::PropagatingPolymorphicAllocator<> allocator; |
601 | 1 | test_object::polymorphic_allocator::SerializeNested serializeNested(offset, optionalValue, allocator); |
602 | 1 | test_object::polymorphic_allocator::SerializeObject serializeObject(param, serializeNested, allocator); |
603 | 1 | serializeToFile(serializeObject, fileName); |
604 | 1 | const test_object::polymorphic_allocator::SerializeObject readSerializeObject = |
605 | 1 | deserializeFromFile<test_object::polymorphic_allocator::SerializeObject>(fileName); |
606 | 1 | ASSERT_EQ(serializeObject, readSerializeObject); |
607 | 1 | } |
608 | 1 | } |
609 | | |
610 | | } // namespace zserio |