// Copyright (c) FIRST and other WPILib contributors. // Open Source Software; you can modify and/or share it under the terms of // the WPILib BSD license file in the root directory of this project. #include "wpi/util/struct/Struct.hpp" #include #include #include #include #include using namespace wpi::util; static_assert(StructSerializable); static_assert(StructSerializable>); static_assert(!StructSerializable>); static_assert(!StructSerializable); namespace { struct TestStruct { int32_t a; int32_t b; }; } // namespace template <> struct wpi::util::Struct { static constexpr std::string_view GetTypeName() { return "TestStruct"; } static constexpr size_t GetSize() { return 8; } static constexpr std::string_view GetSchema() { return "int32 a;int32 b"; } static TestStruct Unpack(std::span data) { return {wpi::util::UnpackStruct(data), wpi::util::UnpackStruct(data)}; } static void Pack(std::span data, TestStruct value) { wpi::util::PackStruct<0>(data, value.a); wpi::util::PackStruct<4>(data, value.b); } }; TEST(StructTest, Basic) { constexpr auto typeName = GetStructTypeName(); EXPECT_EQ(typeName, "TestStruct"); constexpr auto typeString = GetStructTypeString(); EXPECT_EQ(typeString, "struct:TestStruct"); constexpr auto schema = GetStructSchema(); EXPECT_EQ(schema, "int32 a;int32 b"); constexpr auto arrayTypeName = MakeStructArrayTypeName(); EXPECT_EQ(arrayTypeName, "TestStruct[5]"); constexpr auto arrayTypeString = MakeStructArrayTypeString(); EXPECT_EQ(arrayTypeString, "struct:TestStruct[5]"); TestStruct s{12345, 67890}; std::array data; Struct::Pack(data, s); auto s2 = Struct::Unpack(data); EXPECT_EQ(s.a, s2.a); EXPECT_EQ(s.b, s2.b); }