/** * Copyright (c) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "serializer/serializer.h" #include namespace panda { class SerializatorTest : public testing::Test { protected: void SetUp() { buffer.resize(0); } std::vector buffer; }; template void SerializerTypeToBuffer(const T &type, /* out */ std::vector &buffer, size_t ret_val) { auto ret = serializer::TypeToBuffer(type, buffer); ASSERT_TRUE(ret); ASSERT_EQ(ret.Value(), ret_val); } template void SerializerBufferToType(const std::vector &buffer, /* out */ T &type, size_t ret_val) { auto ret = serializer::BufferToType(buffer.data(), buffer.size(), type); ASSERT_TRUE(ret); ASSERT_EQ(ret.Value(), ret_val); } template void DoTest(T value, int ret_val) { constexpr const int64_t IMM_FOUR = 4; T a = value; T b; std::vector buffer; SerializerTypeToBuffer(a, buffer, ret_val); buffer.resize(IMM_FOUR * buffer.size()); SerializerBufferToType(buffer, b, ret_val); ASSERT_EQ(a, value); ASSERT_EQ(b, value); ASSERT_EQ(a, b); } template void TestPod(T value) { static_assert(std::is_pod::value, "Type is not supported"); DoTest(value, sizeof(value)); } struct PodStruct { uint8_t a; int16_t b; uint32_t c; int64_t d; float e; long double f; }; bool operator==(const PodStruct &lhs, const PodStruct &rhs) { return lhs.a == rhs.a && lhs.b == rhs.b && lhs.c == rhs.c && lhs.d == rhs.d && lhs.e == rhs.e && lhs.f == rhs.f; } TEST_F(SerializatorTest, TestPodTypes) { TestPod(0xac); TestPod(0xc0de); TestPod(0x123f567f); TestPod(0xff12345789103c4b); TestPod(0x1c); TestPod(0x1ebd); TestPod(0xfe52567f); TestPod(0xff1234fdec57891b); TestPod(0.234664); TestPod(22345.3453453); TestPod(99453.64345); TestPod({0xff, -23458, 10345893, -98343451, -3.54634, 1.44e6}); } TEST_F(SerializatorTest, TestString) { DoTest({}, 4); DoTest("", 4); DoTest("Hello World!", 4 + 12); DoTest("1", 4 + 1); DoTest({}, 4); } TEST_F(SerializatorTest, TestVectorPod) { DoTest>({1, 2, 3, 4}, 4 + 1 * 4); DoTest>({143, 452, 334}, 4 + 2 * 3); DoTest>({15434, 4564562, 33453, 43456, 346346}, 4 + 5 * 4); DoTest>({14345665644345, 34645345465}, 4 + 8 * 2); DoTest>({}, 4 + 1 * 0); } TEST_F(SerializatorTest, TestUnorderedMap1) { using Map = std::unordered_map; DoTest( { {12343526, 23424}, {3, 234356}, {45764746, 4}, }, 4 + 3 * (4 + 2)); } TEST_F(SerializatorTest, TestUnorderedMap2) { using Map = std::unordered_map; DoTest( { {"one", {}}, {"two", "123"}, {"three", ""}, {"", {}}, }, 4 + 4 + 3 + 4 + 0 + 4 + 3 + 4 + 3 + 4 + 5 + 4 + 0 + 4 + 0 + 4 + 0); } TEST_F(SerializatorTest, TestUnorderedMap3) { using Map = std::unordered_map>; DoTest( { {"one", {}}, {"two", {1, 2, 3, 4}}, {"three", {9, 34, 45335}}, {"", {}}, }, 4 + 4 + 3 + 4 + 4 * 0 + 4 + 3 + 4 + 4 * 4 + 4 + 5 + 4 + 4 * 3 + 4 + 0 + 4 + 4 * 0); } struct TestStruct { uint8_t a; uint16_t b; uint32_t c; uint64_t d; std::string e; std::vector f; }; bool operator==(const TestStruct &lhs, const TestStruct &rhs) { return lhs.a == rhs.a && lhs.b == rhs.b && lhs.c == rhs.c && lhs.d == rhs.d && lhs.e == rhs.e && lhs.f == rhs.f; } TEST_F(SerializatorTest, TestStruct) { TestStruct test_struct {1, 2, 3, 4, "Liza", {8, 9, 5}}; unsigned test_ret = 1 + 2 + 4 + 8 + 4 + 4 + 4 + sizeof(int) * 3; TestStruct a = test_struct; TestStruct b; ASSERT_EQ(serializer::StructToBuffer<6>(a, buffer), true); buffer.resize(4 * buffer.size()); auto ret = serializer::RawBufferToStruct<6>(buffer.data(), buffer.size(), b); ASSERT_TRUE(ret.HasValue()); ASSERT_EQ(ret.Value(), test_ret); ASSERT_EQ(a, test_struct); ASSERT_EQ(b, test_struct); ASSERT_EQ(a, b); } } // namespace panda