11#include "gtest/gtest.h"
22TEST(ESITypesTest, VoidTypeSerialization) {
27 EXPECT_NO_THROW(voidType.ensureValid(voidValue));
30 std::any nullptrValue = std::any(
nullptr);
31 EXPECT_NO_THROW(voidType.ensureValid(nullptrValue));
34 std::any invalidValue = std::any(42);
35 EXPECT_THROW(voidType.ensureValid(invalidValue), std::runtime_error);
38 MessageData serialized(voidType.serialize(voidValue).getSpan());
39 EXPECT_EQ(serialized.getSize(), 1UL)
40 <<
"VoidType serialization should produce exactly 1 byte";
41 EXPECT_EQ(serialized.getData()[0], 0)
42 <<
"VoidType serialization should produce a zero byte";
46 auto deserialized = voidType.deserialize(v);
47 EXPECT_FALSE(deserialized.has_value())
48 <<
"VoidType deserialization should not have a value";
49 EXPECT_EQ(v.width(), 0UL)
50 <<
"VoidType deserialization should consume all data";
54TEST(ESITypesTest, BitsTypeSerialization) {
58 std::vector<uint8_t> bitsValue = {0xAB};
59 std::any validBits = std::any(bitsValue);
60 EXPECT_NO_THROW(bitsType.ensureValid(validBits));
63 std::vector<uint8_t> wrongSize = {0xAB, 0xCD};
64 std::any invalidBits = std::any(wrongSize);
65 EXPECT_THROW(bitsType.ensureValid(invalidBits), std::runtime_error);
68 MessageData serialized(bitsType.serialize(validBits).takeStorage());
69 EXPECT_EQ(serialized.getSize(), 1UL)
70 <<
"BitsType(8) serialization should produce exactly 1 byte";
71 EXPECT_EQ(serialized.getData()[0], 0xAB)
72 <<
"BitsType serialization should preserve the input byte value";
75 BitVector serializedBits(serialized.getData());
76 auto deserialized = bitsType.deserialize(serializedBits);
77 auto deserializedBits = std::any_cast<std::vector<uint8_t>>(deserialized);
78 EXPECT_EQ(deserializedBits.size(), 1UL)
79 <<
"BitsType(8) deserialization should produce 1 byte";
80 EXPECT_EQ(deserializedBits[0], 0xAB)
81 <<
"BitsType deserialization should preserve the original value";
82 EXPECT_EQ(serializedBits.size(), 0UL)
83 <<
"BitsType deserialization should consume all data";
87TEST(ESITypesTest, UIntTypeSerialization) {
91 uint64_t uintValue = 0x1234;
92 std::any validUInt = std::any(uintValue);
93 EXPECT_NO_THROW(uintType.ensureValid(validUInt));
96 uint64_t outOfRange = 0x10000;
97 std::any invalidUInt = std::any(outOfRange);
98 EXPECT_THROW(uintType.ensureValid(invalidUInt), std::runtime_error);
101 MessageData serialized(uintType.serialize(validUInt).takeStorage());
102 EXPECT_EQ(serialized.getSize(), 2UL)
103 <<
"UIntType(16) serialization should produce exactly 2 bytes";
104 EXPECT_EQ(serialized.getData()[0], 0x34)
105 <<
"UIntType serialization low byte should be 0x34 (little-endian)";
106 EXPECT_EQ(serialized.getData()[1], 0x12)
107 <<
"UIntType serialization high byte should be 0x12 (little-endian)";
110 BitVector serializedBits(serialized.getData());
111 auto deserialized = uintType.deserialize(serializedBits);
112 auto deserializedUInt =
113 static_cast<uint16_t
>(std::any_cast<UInt>(deserialized));
114 EXPECT_EQ(deserializedUInt, 0x1234)
115 <<
"UIntType deserialization should reconstruct original value 0x1234";
116 EXPECT_EQ(serializedBits.size(), 0UL)
117 <<
"UIntType deserialization should consume all data";
120 uint8_t smallVal = 42;
121 uint16_t mediumVal = 1000;
122 uint32_t largeVal = 50000;
124 EXPECT_NO_THROW(uintType.ensureValid(std::any(smallVal)));
125 EXPECT_NO_THROW(uintType.ensureValid(std::any(mediumVal)));
126 EXPECT_NO_THROW(uintType.ensureValid(std::any(largeVal)));
130TEST(ESITypesTest, SIntTypeSerialization) {
134 int64_t positiveValue = 0x1234;
135 std::any validSInt = std::any(positiveValue);
136 EXPECT_NO_THROW(sintType.ensureValid(validSInt));
139 int64_t negativeValue = -1000;
140 std::any validNegSInt = std::any(negativeValue);
141 EXPECT_NO_THROW(sintType.ensureValid(validNegSInt));
144 MessageData serialized(sintType.serialize(validSInt).takeStorage());
145 EXPECT_EQ(serialized.getSize(), 2UL)
146 <<
"SIntType(16) serialization should produce exactly 2 bytes";
147 EXPECT_EQ(serialized.getData()[0], 0x34)
148 <<
"SIntType serialization low byte should be 0x34 (little-endian)";
149 EXPECT_EQ(serialized.getData()[1], 0x12)
150 <<
"SIntType serialization high byte should be 0x12 (little-endian)";
153 BitVector serializedBits(serialized.getData());
154 auto deserialized = sintType.deserialize(serializedBits);
155 auto deserializedSInt =
156 static_cast<int16_t
>(std::any_cast<Int>(deserialized));
157 EXPECT_EQ(deserializedSInt, 0x1234)
158 <<
"SIntType deserialization should reconstruct original positive value";
159 EXPECT_EQ(serializedBits.size(), 0UL)
160 <<
"SIntType deserialization should consume all data";
163 MessageData negSerialized(sintType.serialize(validNegSInt).takeStorage());
164 EXPECT_EQ(negSerialized.getSize(), 2UL)
165 <<
"SIntType(16) negative value should serialize to 2 bytes";
167 EXPECT_EQ(negSerialized.getData()[0], 0x18)
168 <<
"SIntType negative serialization low byte should be 0x18 "
170 EXPECT_EQ(negSerialized.getData()[1], 0xFC)
171 <<
"SIntType negative serialization high byte should be 0xFC "
174 BitVector negSerializedBits(negSerialized.getData());
175 auto negDeserialized = sintType.deserialize(negSerializedBits);
176 auto deserializedNegSInt =
177 static_cast<int16_t
>(std::any_cast<Int>(negDeserialized));
178 EXPECT_EQ(deserializedNegSInt, -1000)
179 <<
"SIntType deserialization should reconstruct original negative value "
183 int8_t smallVal = -42;
184 int16_t mediumVal = -1000;
185 int32_t largeVal = -30000;
187 EXPECT_NO_THROW(sintType.ensureValid(std::any(smallVal)));
188 EXPECT_NO_THROW(sintType.ensureValid(std::any(mediumVal)));
189 EXPECT_NO_THROW(sintType.ensureValid(std::any(largeVal)));
193TEST(ESITypesTest, SIntTypeSignExtension) {
198 int64_t minusOne = -1;
200 sint8.serialize(std::any(minusOne)).takeStorage());
201 EXPECT_EQ(serializedMinusOne.getSize(), 1UL)
202 <<
"SIntType(8) serialization of -1 should produce 1 byte";
203 EXPECT_EQ(serializedMinusOne.getData()[0], 0xFF)
204 <<
"SIntType(8) serialization of -1 should be 0xFF (all bits set)";
206 BitVector serializedMinusOneBits(serializedMinusOne.getData());
207 auto deserializedMinusOne = sint8.deserialize(serializedMinusOneBits);
208 auto resultMinusOne =
209 static_cast<int8_t
>(std::any_cast<Int>(deserializedMinusOne));
210 EXPECT_EQ(resultMinusOne, -1)
211 <<
"SIntType(8) deserialization of 0xFF should reconstruct -1";
214 int64_t maxNeg8 = -128;
216 sint8.serialize(std::any(maxNeg8)).takeStorage());
217 EXPECT_EQ(serializedMaxNeg.getSize(), 1UL)
218 <<
"SIntType(8) serialization of -128 should produce 1 byte";
219 EXPECT_EQ(serializedMaxNeg.getData()[0], 0x80)
220 <<
"SIntType(8) serialization of -128 should be 0x80";
222 BitVector serializedMaxNegBits(serializedMaxNeg.getData());
223 auto deserializedMaxNeg = sint8.deserialize(serializedMaxNegBits);
225 static_cast<int8_t
>(std::any_cast<Int>(deserializedMaxNeg));
226 EXPECT_EQ(resultMaxNeg, -128) <<
"SIntType(8) deserialization should "
227 "reconstruct maximum negative value (-128)";
230 int64_t maxPos8 = 127;
232 sint8.serialize(std::any(maxPos8)).takeStorage());
233 EXPECT_EQ(serializedMaxPos.getSize(), 1UL)
234 <<
"SIntType(8) serialization of 127 should produce 1 byte";
235 EXPECT_EQ(serializedMaxPos.getData()[0], 0x7F)
236 <<
"SIntType(8) serialization of 127 should be 0x7F";
238 BitVector serializedMaxPosBits(serializedMaxPos.getData());
239 auto deserializedMaxPos = sint8.deserialize(serializedMaxPosBits);
241 static_cast<int8_t
>(std::any_cast<Int>(deserializedMaxPos));
242 EXPECT_EQ(resultMaxPos, 127) <<
"SIntType(8) deserialization should "
243 "reconstruct maximum positive value (127)";
249 int64_t minus1w4bit = -1;
251 sint4.serialize(std::any(minus1w4bit)).takeStorage());
252 EXPECT_EQ(serialized4bit.getSize(), 1UL)
253 <<
"SIntType(4) serialization should produce 1 byte";
254 EXPECT_EQ(serialized4bit.getData()[0] & 0x0F, 0x0F)
255 <<
"SIntType(4) serialization of -1 should have lower 4 bits set to "
258 BitVector serialized4bitBits(serialized4bit.getData());
259 auto deserialized4bit = sint4.deserialize(serialized4bitBits);
260 auto result4bit =
static_cast<int8_t
>(std::any_cast<Int>(deserialized4bit));
261 EXPECT_EQ(result4bit, -1)
262 <<
"SIntType(4) deserialization should sign-extend -1 correctly";
265 int64_t maxNeg4 = -8;
267 sint4.serialize(std::any(maxNeg4)).takeStorage());
268 BitVector serializedMaxNeg4Bits(serializedMaxNeg4.getData());
269 auto deserializedMaxNeg4 = sint4.deserialize(serializedMaxNeg4Bits);
271 static_cast<int8_t
>(std::any_cast<Int>(deserializedMaxNeg4));
272 EXPECT_EQ(resultMaxNeg4, -8)
273 <<
"SIntType(4) should handle maximum negative value (-8) correctly";
278 sint4.serialize(std::any(maxPos4)).takeStorage());
279 BitVector serializedMaxPos4Bits(serializedMaxPos4.getData());
280 auto deserializedMaxPos4 = sint4.deserialize(serializedMaxPos4Bits);
282 static_cast<int8_t
>(std::any_cast<Int>(deserializedMaxPos4));
283 EXPECT_EQ(resultMaxPos4, 7)
284 <<
"SIntType(4) should handle maximum positive value (7) correctly";
290 int64_t minus1w12bit = -1;
292 sint12.serialize(std::any(minus1w12bit)).takeStorage());
293 EXPECT_EQ(serialized12bit.getSize(), 2UL)
294 <<
"SIntType(12) serialization should produce 2 bytes (12 bits = 2 "
296 EXPECT_EQ(serialized12bit.getData()[0], 0xFF)
297 <<
"SIntType(12) serialization of -1 should have lower byte 0xFF";
298 EXPECT_EQ(serialized12bit.getData()[1] & 0x0F, 0x0F)
299 <<
"SIntType(12) serialization of -1 should have upper nibble 0x0F";
301 BitVector serialized12bitBits(serialized12bit.getData());
302 auto deserialized12bit = sint12.deserialize(serialized12bitBits);
304 static_cast<int16_t
>(std::any_cast<Int>(deserialized12bit));
305 EXPECT_EQ(result12bit, -1)
306 <<
"SIntType(12) deserialization should sign-extend -1 correctly";
309 int64_t neg100w12bit = -100;
311 sint12.serialize(std::any(neg100w12bit)).takeStorage());
312 BitVector serializedNeg100Bits(serializedNeg100.getData());
313 auto deserializedNeg100 = sint12.deserialize(serializedNeg100Bits);
315 static_cast<int16_t
>(std::any_cast<Int>(deserializedNeg100));
316 EXPECT_EQ(resultNeg100, -100)
317 <<
"SIntType(12) should correctly handle sign extension for -100";
321TEST(ESITypesTest, SIntTypeSignExtensionBoundaries) {
323 for (
int width = 1; width <= 16; ++width) {
324 SIntType sintType(
"sint" + std::to_string(width), width);
327 int64_t maxVal = (width == 64) ? INT64_MAX : ((1LL << (width - 1)) - 1);
328 int64_t minVal = (width == 64) ? INT64_MIN : (-(1LL << (width - 1)));
332 sintType.serialize(std::any(maxVal)).takeStorage());
333 BitVector serializedMaxBits(serializedMax.getData());
334 auto deserializedMax = sintType.deserialize(serializedMaxBits);
338 auto resultMax =
static_cast<int8_t
>(std::any_cast<Int>(deserializedMax));
339 EXPECT_EQ(resultMax,
static_cast<int8_t
>(maxVal))
340 <<
"Failed for width " << width <<
" max value";
341 }
else if (width <= 16) {
343 static_cast<int16_t
>(std::any_cast<Int>(deserializedMax));
344 EXPECT_EQ(resultMax,
static_cast<int16_t
>(maxVal))
345 <<
"Failed for width " << width <<
" max value";
346 }
else if (width <= 32) {
348 static_cast<int32_t
>(std::any_cast<Int>(deserializedMax));
349 EXPECT_EQ(resultMax,
static_cast<int32_t
>(maxVal))
350 <<
"Failed for width " << width <<
" max value";
353 static_cast<int64_t
>(std::any_cast<Int>(deserializedMax));
354 EXPECT_EQ(resultMax, maxVal)
355 <<
"Failed for width " << width <<
" max value";
360 sintType.serialize(std::any(minVal)).takeStorage());
361 BitVector serializedMinBits(serializedMin.getData());
362 auto deserializedMin = sintType.deserialize(serializedMinBits);
365 auto resultMin =
static_cast<int8_t
>(std::any_cast<Int>(deserializedMin));
366 EXPECT_EQ(resultMin,
static_cast<int8_t
>(minVal))
367 <<
"Failed for width " << width <<
" min value";
368 }
else if (width <= 16) {
370 static_cast<int16_t
>(std::any_cast<Int>(deserializedMin));
371 EXPECT_EQ(resultMin,
static_cast<int16_t
>(minVal))
372 <<
"Failed for width " << width <<
" min value";
373 }
else if (width <= 32) {
375 static_cast<int32_t
>(std::any_cast<Int>(deserializedMin));
376 EXPECT_EQ(resultMin,
static_cast<int32_t
>(minVal))
377 <<
"Failed for width " << width <<
" min value";
380 static_cast<int64_t
>(std::any_cast<Int>(deserializedMin));
381 EXPECT_EQ(resultMin, minVal)
382 <<
"Failed for width " << width <<
" min value";
387 sintType.serialize(std::any(
static_cast<int64_t
>(-1))).takeStorage());
388 BitVector serializedMinusOneBits(serializedMinusOne.getData());
389 auto deserializedMinusOne = sintType.deserialize(serializedMinusOneBits);
392 auto resultMinusOne =
393 static_cast<int8_t
>(std::any_cast<Int>(deserializedMinusOne));
394 EXPECT_EQ(resultMinusOne, -1)
395 <<
"Failed for width " << width <<
" value -1";
396 }
else if (width <= 16) {
397 auto resultMinusOne =
398 static_cast<int16_t
>(std::any_cast<Int>(deserializedMinusOne));
399 EXPECT_EQ(resultMinusOne, -1)
400 <<
"Failed for width " << width <<
" value -1";
401 }
else if (width <= 32) {
402 auto resultMinusOne =
403 static_cast<int32_t
>(std::any_cast<Int>(deserializedMinusOne));
404 EXPECT_EQ(resultMinusOne, -1)
405 <<
"Failed for width " << width <<
" value -1";
407 auto resultMinusOne =
408 static_cast<int64_t
>(std::any_cast<Int>(deserializedMinusOne));
409 EXPECT_EQ(resultMinusOne, -1)
410 <<
"Failed for width " << width <<
" value -1";
416TEST(ESITypesTest, WideUIntTypeSerialization) {
422 uint64_t lowPart = 0xFEDCBA9876543210ULL;
423 uint64_t highPart = 0x123456789ABCDEF0ULL;
426 std::vector<uint8_t> bytes(16, 0);
427 for (
size_t i = 0; i < 8; ++i) {
428 bytes[i] =
static_cast<uint8_t
>((lowPart >> (8 * i)) & 0xFF);
429 bytes[i + 8] =
static_cast<uint8_t
>((highPart >> (8 * i)) & 0xFF);
433 std::any uint128Value = std::any(
UInt(std::move(bytes)));
436 EXPECT_NO_THROW(uint128.ensureValid(uint128Value));
439 MessageData serialized(uint128.serialize(uint128Value).takeStorage());
440 EXPECT_EQ(serialized.getSize(), 16UL)
441 <<
"UIntType(128) serialization should produce exactly 16 bytes";
444 for (
size_t i = 0; i < 8; ++i) {
445 EXPECT_EQ(serialized.getData()[i],
446 static_cast<uint8_t
>((lowPart >> (8 * i)) & 0xFF))
447 <<
"Low part byte " << i <<
" mismatch";
448 EXPECT_EQ(serialized.getData()[i + 8],
449 static_cast<uint8_t
>((highPart >> (8 * i)) & 0xFF))
450 <<
"High part byte " << i <<
" mismatch";
454 BitVector serializedBits(serialized.getData());
455 auto deserialized = uint128.deserialize(serializedBits);
456 auto deserializedUInt = std::any_cast<UInt>(deserialized);
457 EXPECT_EQ(deserializedUInt.width(), 128u)
458 <<
"Deserialized UInt(128) should have width 128";
459 EXPECT_EQ(serializedBits.size(), 0UL)
460 <<
"UIntType(128) deserialization should consume all data";
464 std::vector<uint8_t> bytes80(10, 0);
465 uint64_t val80 = 0x123456789ABCDEFULL;
466 for (
size_t i = 0; i < 10; ++i) {
467 bytes80[i] =
static_cast<uint8_t
>((val80 >> (8 * i)) & 0xFF);
470 std::any uint80Value = std::any(
UInt(std::move(bytes80), 80));
472 EXPECT_NO_THROW(uint80.ensureValid(uint80Value));
474 MessageData serialized80(uint80.serialize(uint80Value).takeStorage());
475 EXPECT_EQ(serialized80.getSize(), 10UL)
476 <<
"UIntType(80) serialization should produce exactly 10 bytes";
478 BitVector serialized80Bits(serialized80.getData());
479 auto deserialized80 = uint80.deserialize(serialized80Bits);
480 auto deserializedUInt80 = std::any_cast<UInt>(deserialized80);
481 EXPECT_EQ(deserializedUInt80.width(), 80u)
482 <<
"Deserialized UInt(80) should have width 80";
483 EXPECT_EQ(serialized80Bits.size(), 0UL)
484 <<
"UIntType(80) deserialization should consume all data";
488TEST(ESITypesTest, WideSIntTypeSerialization) {
493 std::vector<uint8_t> bytes(16, 0);
494 uint64_t val = 0x123456789ABCDEF0ULL;
495 for (
size_t i = 0; i < 8; ++i) {
496 bytes[i] =
static_cast<uint8_t
>((val >> (8 * i)) & 0xFF);
499 std::any sint128Value = std::any(
Int(std::move(bytes), 128));
502 EXPECT_NO_THROW(sint128.ensureValid(sint128Value));
505 MessageData serialized(sint128.serialize(sint128Value).takeStorage());
506 EXPECT_EQ(serialized.getSize(), 16UL)
507 <<
"SIntType(128) serialization should produce exactly 16 bytes";
510 BitVector serializedBits(serialized.getData());
511 auto deserialized = sint128.deserialize(serializedBits);
512 auto deserializedSInt = std::any_cast<Int>(deserialized);
513 EXPECT_EQ(deserializedSInt.width(), 128u)
514 <<
"Deserialized SInt(128) should have width 128";
515 EXPECT_EQ(serializedBits.size(), 0UL)
516 <<
"SIntType(128) deserialization should consume all data";
519 std::vector<uint8_t> bytesNegOne(16, 0xFF);
520 std::any sint128NegOne = std::any(
Int(std::move(bytesNegOne), 128));
522 EXPECT_NO_THROW(sint128.ensureValid(sint128NegOne));
524 MessageData serializedNegOne(sint128.serialize(sint128NegOne).takeStorage());
525 EXPECT_EQ(serializedNegOne.getSize(), 16UL)
526 <<
"SIntType(128) serialization of -1 should produce 16 bytes";
529 for (
size_t i = 0; i < 16; ++i) {
530 EXPECT_EQ(serializedNegOne.getData()[i], 0xFF)
531 <<
"All bytes in -1 should be 0xFF";
535 BitVector serializedNegOneBits(serializedNegOne.getData());
536 auto deserializedNegOne = sint128.deserialize(serializedNegOneBits);
537 auto deserializedSIntNegOne = std::any_cast<Int>(deserializedNegOne);
538 EXPECT_EQ(deserializedSIntNegOne.width(), 128u)
539 <<
"Deserialized SInt(128) of -1 should have width 128";
543 std::vector<uint8_t> bytes192(24, 0);
545 uint64_t val192Low = 0xDEADBEEFCAFEBABEULL;
546 uint64_t val192Mid = 0x0123456789ABCDEFULL;
547 for (
size_t i = 0; i < 8; ++i) {
548 bytes192[i] =
static_cast<uint8_t
>((val192Low >> (8 * i)) & 0xFF);
549 bytes192[i + 8] =
static_cast<uint8_t
>((val192Mid >> (8 * i)) & 0xFF);
552 std::any sint192Value = std::any(
Int(std::move(bytes192), 192));
554 EXPECT_NO_THROW(sint192.ensureValid(sint192Value));
556 MessageData serialized192(sint192.serialize(sint192Value).takeStorage());
557 EXPECT_EQ(serialized192.getSize(), 24UL)
558 <<
"SIntType(192) serialization should produce exactly 24 bytes";
560 BitVector serialized192Bits(serialized192.getData());
561 auto deserialized192 = sint192.deserialize(serialized192Bits);
562 auto deserializedSInt192 = std::any_cast<Int>(deserialized192);
563 EXPECT_EQ(deserializedSInt192.width(), 192u)
564 <<
"Deserialized SInt(192) should have width 192";
565 EXPECT_EQ(serialized192Bits.size(), 0UL)
566 <<
"SIntType(192) deserialization should consume all data";
570 std::vector<uint8_t> bytes72(9, 0);
571 uint64_t val72 = 0x0123456789ABCDEFULL;
572 for (
size_t i = 0; i < 8; ++i) {
573 bytes72[i] =
static_cast<uint8_t
>((val72 >> (8 * i)) & 0xFF);
577 std::any sint72Value = std::any(
Int(std::move(bytes72), 72));
579 EXPECT_NO_THROW(sint72.ensureValid(sint72Value));
581 MessageData serialized72(sint72.serialize(sint72Value).takeStorage());
582 EXPECT_EQ(serialized72.getSize(), 9UL)
583 <<
"SIntType(72) serialization should produce exactly 9 bytes";
585 BitVector serialized72Bits(serialized72.getData());
586 auto deserialized72 = sint72.deserialize(serialized72Bits);
587 auto deserializedSInt72 = std::any_cast<Int>(deserialized72);
588 EXPECT_EQ(deserializedSInt72.width(), 72u)
589 <<
"Deserialized SInt(72) should have width 72";
590 EXPECT_EQ(serialized72Bits.size(), 0UL)
591 <<
"SIntType(72) deserialization should consume all data";
595TEST(ESITypesTest, StructTypeSerialization) {
597 auto uintType = std::make_unique<UIntType>(
"uint8", 8);
598 auto sintType = std::make_unique<SIntType>(
"sint8", 8);
602 {
"field2", sintType.get()}};
606 std::map<std::string, std::any> structValue = {
607 {
"field1", std::any(
static_cast<uint64_t
>(42))},
608 {
"field2", std::any(
static_cast<int64_t
>(-10))}};
609 std::any validStruct = std::any(structValue);
610 EXPECT_NO_THROW(structType.ensureValid(validStruct));
613 std::map<std::string, std::any> incompleteStruct = {
614 {
"field1", std::any(
static_cast<uint64_t
>(42))}};
615 std::any invalidStruct = std::any(incompleteStruct);
616 EXPECT_THROW(structType.ensureValid(invalidStruct), std::runtime_error);
619 std::map<std::string, std::any> incompatibleStruct = {
620 {
"UnknownField", std::any(
static_cast<uint64_t
>(0xdeadbeef))}};
621 std::any incompatibleStructAny = std::any(incompatibleStruct);
622 EXPECT_THROW(structType.ensureValid(incompatibleStructAny),
626 MessageData serialized(structType.serialize(validStruct).takeStorage());
627 EXPECT_EQ(serialized.getSize(), 2UL)
628 <<
"StructType with uint8 + sint8 fields should serialize to 2 bytes";
631 BitVector serializedBits(serialized.getData());
632 auto deserialized = structType.deserialize(serializedBits);
633 auto deserializedStruct =
634 std::any_cast<std::map<std::string, std::any>>(deserialized);
635 EXPECT_EQ(deserializedStruct.size(), 2UL)
636 <<
"Deserialized struct should contain exactly 2 fields";
637 EXPECT_TRUE(deserializedStruct.find(
"field1") != deserializedStruct.end())
638 <<
"Deserialized struct should contain field1";
639 EXPECT_TRUE(deserializedStruct.find(
"field2") != deserializedStruct.end())
640 <<
"Deserialized struct should contain field2";
641 EXPECT_EQ(serializedBits.size(), 0UL)
642 <<
"StructType deserialization should consume all data";
646 static_cast<uint8_t
>(std::any_cast<UInt>(deserializedStruct[
"field1"]));
648 static_cast<int8_t
>(std::any_cast<Int>(deserializedStruct[
"field2"]));
649 EXPECT_EQ(field1Val, 42) <<
"Deserialized field1 should have value 42";
650 EXPECT_EQ(field2Val, -10) <<
"Deserialized field2 should have value -10";
653 auto oddUintType = std::make_unique<UIntType>(
"uint6", 6);
654 auto boolType = std::make_unique<BitsType>(
"bool", 1);
656 {
"bool1", boolType.get()},
657 {
"field2", oddUintType.get()},
658 {
"bool2", boolType.get()}};
661 std::map<std::string, std::any> oddStructValue = {
662 {
"field1", std::any(
static_cast<uint64_t
>(1))},
663 {
"bool1", std::any(std::vector<uint8_t>{1})},
664 {
"field2", std::any(
static_cast<uint64_t
>(2))},
665 {
"bool2", std::any(std::vector<uint8_t>{0})},
668 std::any validOddStruct = std::any(oddStructValue);
669 EXPECT_NO_THROW(oddStruct.ensureValid(validOddStruct));
670 MessageData oddSerialized(oddStruct.serialize(validOddStruct).takeStorage());
672 EXPECT_EQ(oddSerialized.size(), 2UL);
674 BitVector oddSerializedBits(oddSerialized.getData());
675 auto oddDeserialized = oddStruct.deserialize(oddSerializedBits);
676 auto deserializedOddStruct =
677 std::any_cast<std::map<std::string, std::any>>(oddDeserialized);
678 EXPECT_EQ(deserializedOddStruct.size(), 4UL)
679 <<
"Deserialized odd struct should contain exactly 4 fields";
680 EXPECT_EQ(oddSerializedBits.size(), 0UL)
681 <<
"Odd StructType deserialization should consume all data";
683 auto oddField1Val =
static_cast<uint8_t
>(
684 std::any_cast<UInt>(deserializedOddStruct[
"field1"]));
686 std::any_cast<std::vector<uint8_t>>(deserializedOddStruct[
"bool1"]);
687 auto oddField2Val =
static_cast<uint8_t
>(
688 std::any_cast<UInt>(deserializedOddStruct[
"field2"]));
690 std::any_cast<std::vector<uint8_t>>(deserializedOddStruct[
"bool2"]);
692 EXPECT_EQ(oddField1Val, 1) <<
"Deserialized odd field1 should have value 1";
693 EXPECT_EQ(bool1Val.size(), 1ULL)
694 <<
"Deserialized odd bool1 should have size 1";
695 EXPECT_EQ(bool1Val[0], 1) <<
"Deserialized odd bool1 should have value true";
696 EXPECT_EQ(oddField2Val, 2) <<
"Deserialized odd field2 should have value 2";
697 EXPECT_EQ(bool2Val.size(), 1ULL)
698 <<
"Deserialized odd bool2 should have size 1";
699 EXPECT_EQ(bool2Val[0], 0) <<
"Deserialized odd bool2 should have value false";
703TEST(ESITypesTest, ArrayTypeSerialization) {
705 auto uintType = std::make_unique<UIntType>(
"uint8", 8);
708 ArrayType arrayType(
"uint8Array", uintType.get(), 3);
711 std::vector<std::any> arrayValue = {std::any(
static_cast<uint64_t
>(10)),
712 std::any(
static_cast<uint64_t
>(20)),
713 std::any(
static_cast<uint64_t
>(30))};
714 std::any validArray = std::any(arrayValue);
715 EXPECT_NO_THROW(arrayType.ensureValid(validArray));
718 std::vector<std::any> wrongSizeArray = {std::any(
static_cast<uint64_t
>(10)),
719 std::any(
static_cast<uint64_t
>(20))};
720 std::any invalidArray = std::any(wrongSizeArray);
721 EXPECT_THROW(arrayType.ensureValid(invalidArray), std::runtime_error);
724 MessageData serialized(arrayType.serialize(validArray).takeStorage());
725 EXPECT_EQ(serialized.getSize(), 3UL)
726 <<
"ArrayType of 3 uint8 elements should serialize to 3 bytes";
727 EXPECT_EQ(serialized.getData()[0], 30)
728 <<
"First array element should serialize to 30 but got "
729 <<
static_cast<uint32_t
>(serialized.getData()[0]);
730 EXPECT_EQ(serialized.getData()[1], 20)
731 <<
"Second array element should serialize to 20 but got "
732 <<
static_cast<uint32_t
>(serialized.getData()[1]);
733 EXPECT_EQ(serialized.getData()[2], 10)
734 <<
"Third array element should serialize to 10 but got "
735 <<
static_cast<uint32_t
>(serialized.getData()[2]);
738 BitVector serializedBits(serialized.getData());
739 auto deserialized = arrayType.deserialize(serializedBits);
740 auto deserializedArray = std::any_cast<std::vector<std::any>>(deserialized);
741 EXPECT_EQ(deserializedArray.size(), 3UL)
742 <<
"Deserialized array should contain exactly 3 elements";
743 EXPECT_EQ(serializedBits.size(), 0UL)
744 <<
"ArrayType deserialization should consume all data";
747 auto elem0 =
static_cast<uint8_t
>(std::any_cast<UInt>(deserializedArray[0]));
748 auto elem1 =
static_cast<uint8_t
>(std::any_cast<UInt>(deserializedArray[1]));
749 auto elem2 =
static_cast<uint8_t
>(std::any_cast<UInt>(deserializedArray[2]));
750 EXPECT_EQ(elem0, 10) <<
"First array element should have value 10";
751 EXPECT_EQ(elem1, 20) <<
"Second array element should have value 20";
752 EXPECT_EQ(elem2, 30) <<
"Third array element should have value 30";
756TEST(ESITypesTest, BitWidthCalculations) {
758 EXPECT_EQ(voidType.getBitWidth(), 1) <<
"VoidType should have bit width of 1";
761 EXPECT_EQ(bitsType.getBitWidth(), 16)
762 <<
"BitsType(16) should have bit width of 16";
765 EXPECT_EQ(uintType.getBitWidth(), 32)
766 <<
"UIntType(32) should have bit width of 32";
769 EXPECT_EQ(sintType.getBitWidth(), 64)
770 <<
"SIntType(64) should have bit width of 64";
773 auto uintType8 = std::make_unique<UIntType>(
"uint8", 8);
774 auto sintType16 = std::make_unique<SIntType>(
"sint16", 16);
776 {
"field2", sintType16.get()}};
778 EXPECT_EQ(structType.getBitWidth(), 24)
779 <<
"StructType with uint8 + sint16 should have bit width of 24 (8 + "
783 ArrayType arrayType(
"uint8Array", uintType8.get(), 5);
784 EXPECT_EQ(arrayType.getBitWidth(), 40)
785 <<
"ArrayType of 5 uint8 elements should have bit width of 40 (8 * 5)";
Arrays have a compile time specified (static) size and an element type.
A lightweight, non-owning bit vector view backed by a byte array span.
Bits are just an array of bits.
A logical chunk of data representing serialized data.
Structs are an ordered collection of fields, each with a name and a type.
std::vector< std::pair< std::string, const Type * > > FieldVector
The "void" type is a special type which can be used to represent no type.