#include #include #include using cpputils::serialize; using cpputils::deserialize; using cpputils::deserializeWithOffset; using cpputils::Data; TEST(SerializationHelperTest, uint8) { Data data(1); serialize(data.data(), 5u); EXPECT_EQ(5u, deserialize(data.data())); } TEST(SerializationHelperTest, int8_positive) { Data data(1); serialize(data.data(), 5); EXPECT_EQ(5, deserialize(data.data())); } TEST(SerializationHelperTest, int8_negative) { Data data(1); serialize(data.data(), -5); EXPECT_EQ(-5, deserialize(data.data())); } TEST(SerializationHelperTest, uint16_aligned) { Data data(2); serialize(data.data(), 1000u); EXPECT_EQ(1000u, deserialize(data.data())); } TEST(SerializationHelperTest, uint16_unaligned) { Data data(3); serialize(data.dataOffset(1), 1000u); EXPECT_EQ(1000u, deserialize(data.dataOffset(1))); } TEST(SerializationHelperTest, int16_postive_aligned) { Data data(2); serialize(data.data(), 1000); EXPECT_EQ(1000, deserialize(data.data())); } TEST(SerializationHelperTest, int16_positive_unaligned) { Data data(3); serialize(data.dataOffset(1), 1000); EXPECT_EQ(1000, deserialize(data.dataOffset(1))); } TEST(SerializationHelperTest, int16_negative_aligned) { Data data(2); serialize(data.data(), -1000); EXPECT_EQ(-1000, deserialize(data.data())); } TEST(SerializationHelperTest, int16_negative_unaligned) { Data data(3); serialize(data.dataOffset(1), -1000); EXPECT_EQ(-1000, deserialize(data.dataOffset(1))); } TEST(SerializationHelperTest, uint32_aligned) { Data data(4); serialize(data.data(), 100000u); EXPECT_EQ(100000u, deserialize(data.data())); } TEST(SerializationHelperTest, uint32_unaligned) { Data data(5); serialize(data.dataOffset(1), 100000u); EXPECT_EQ(100000u, deserialize(data.dataOffset(1))); } TEST(SerializationHelperTest, int32_positive_aligned) { Data data(4); serialize(data.data(), 100000); EXPECT_EQ(100000, deserialize(data.data())); } TEST(SerializationHelperTest, int32_positive_unaligned) { Data data(5); serialize(data.dataOffset(1), 100000); EXPECT_EQ(100000, deserialize(data.dataOffset(1))); } TEST(SerializationHelperTest, int32_negative_aligned) { Data data(4); serialize(data.data(), -100000); EXPECT_EQ(-100000, deserialize(data.data())); } TEST(SerializationHelperTest, int32_negative_unaligned) { Data data(5); serialize(data.dataOffset(1), -100000); EXPECT_EQ(-100000, deserialize(data.dataOffset(1))); } TEST(SerializationHelperTest, uint64_aligned) { Data data(8); serialize(data.data(), 10000000000u); EXPECT_EQ(10000000000u, deserialize(data.data())); } TEST(SerializationHelperTest, uint64_unaligned) { Data data(9); serialize(data.dataOffset(1), 10000000000u); EXPECT_EQ(10000000000u, deserialize(data.dataOffset(1))); } TEST(SerializationHelperTest, int64_positive_aligned) { Data data(8); serialize(data.data(), 10000000000); EXPECT_EQ(10000000000, deserialize(data.data())); } TEST(SerializationHelperTest, int64_positive_unaligned) { Data data(9); serialize(data.dataOffset(1), 10000000000); EXPECT_EQ(10000000000, deserialize(data.dataOffset(1))); } TEST(SerializationHelperTest, int64_negative_aligned) { Data data(8); serialize(data.data(), -10000000000); EXPECT_EQ(-10000000000, deserialize(data.data())); } TEST(SerializationHelperTest, int64_negative_unaligned) { Data data(9); serialize(data.dataOffset(1), -10000000000); EXPECT_EQ(-10000000000, deserialize(data.dataOffset(1))); } TEST(SerializationHelperTest, float_aligned) { Data data(sizeof(float)); serialize(data.data(), 3.1415f); EXPECT_EQ(3.1415f, deserialize(data.data())); } TEST(SerializationHelperTest, float_unaligned) { Data data(sizeof(float) + 1); serialize(data.dataOffset(1), 3.1415f); EXPECT_EQ(3.1415f, deserialize(data.dataOffset(1))); } TEST(SerializationHelperTest, double_aligned) { Data data(sizeof(double)); serialize(data.data(), 3.1415); EXPECT_EQ(3.1415, deserialize(data.data())); } TEST(SerializationHelperTest, double_unaligned) { Data data(sizeof(double) + 1); serialize(data.dataOffset(1), 3.1415); EXPECT_EQ(3.1415, deserialize(data.dataOffset(1))); } namespace { struct DataStructure final { uint64_t v1; uint32_t v2; uint16_t v3; uint8_t v4; }; bool operator==(const DataStructure &lhs, const DataStructure &rhs) { return lhs.v1 == rhs.v1 && lhs.v2 == rhs.v2 && lhs.v3 == rhs.v3 && lhs.v4 == rhs.v4; } } TEST(SerializationHelperTest, struct_aligned) { Data data(sizeof(DataStructure)); const DataStructure fixture {10000000000u, 100000u, 1000u, 5u}; serialize(data.data(), fixture); EXPECT_EQ(fixture, deserialize(data.data())); } TEST(SerializationHelperTest, struct_unaligned) { Data data(sizeof(DataStructure) + 1); const DataStructure fixture {10000000000u, 100000u, 1000u, 5u}; serialize(data.dataOffset(1), fixture); EXPECT_EQ(fixture, deserialize(data.dataOffset(1))); } namespace { struct OneByteStruct final { uint8_t v; }; static_assert(sizeof(OneByteStruct) == 1, ""); } TEST(SerializationHelperTest, onebytestruct) { Data data(1); const OneByteStruct fixture {5}; serialize(data.data(), fixture); EXPECT_EQ(fixture.v, deserialize(data.data()).v); } TEST(SerializationHelperTest, deserializeWithOffset) { Data data(5); serialize(data.dataOffset(1), 1000); EXPECT_EQ(1000, deserializeWithOffset(data.data(), 1)); }