/*************************************************************************** * Copyright (C) 2022 PCSX-Redux authors * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * ***************************************************************************/ #include "support/binstruct.h" #include "gtest/gtest.h" #include "support/file.h" #include "support/typestring-wrapper.h" using namespace PCSX; using namespace PCSX::BinStruct; typedef Field Field1; typedef Field Field2; typedef Field Field3; typedef Field, TYPESTRING("astring")> Field4; typedef Field Field5; typedef Struct Struct1; TEST(BasicBinStruct, Deserialize) { IO f(new BufferFile(FileOps::READWRITE)); f->write(42); f->write(0x1234); f->write(28); f->writeString("Hello World!"); f->write(3); f->writeString("Hi!"); Struct1 s1; s1.deserialize(f); EXPECT_EQ(s1.get(), 42); EXPECT_EQ(s1.get(), 0x3412); EXPECT_EQ(s1.get(), 28); EXPECT_EQ(std::string_view(s1.get()), "Hello World!"); EXPECT_EQ(s1.get().value, "Hi!"); } TEST(BasicBinStruct, Serialize) { Struct1 s1; s1.get().value = 42; s1.get().value = 0x3412; s1.get().value = 28; s1.get().set("Hello World!"); s1.get().value = "Hi!"; IO f(new BufferFile(FileOps::READWRITE)); s1.serialize(f); EXPECT_EQ(f->read(), 42); EXPECT_EQ(f->read(), 0x1234); EXPECT_EQ(f->read(), 28); EXPECT_EQ(f->readString(12), "Hello World!"); EXPECT_EQ(f->read(), 3); EXPECT_EQ(f->readString(3), "Hi!"); } typedef Field SuperField1; typedef Field, TYPESTRING("superfield2")> SuperField2; typedef StructField SuperField3; typedef Struct Struct2; TEST(NestedBinStruct, Deserialize) { IO f(new BufferFile(FileOps::READWRITE)); f->write(0x12345678); f->writeString("1234567"); f->write(42); f->write(0x1234); f->write(28); f->writeString("Hello World!"); f->write(3); f->writeString("Hi!"); Struct2 s2; s2.deserialize(f); auto& s1 = s2.get(); EXPECT_EQ(s2.get(), 0x12345678); EXPECT_EQ(std::string_view(s2.get()), "1234567"); EXPECT_EQ(s1.get(), 42); EXPECT_EQ(s1.get(), 0x3412); EXPECT_EQ(s1.get(), 28); EXPECT_EQ(std::string_view(s1.get()), "Hello World!"); EXPECT_EQ(s1.get().value, "Hi!"); } TEST(NestedBinStruct, Serialize) { Struct2 s2; auto& s1 = s2.get(); s2.get().value = 0x12345678; s2.get().set("1234567"); s1.get().value = 42; s1.get().value = 0x3412; s1.get().value = 28; s1.get().set("Hello World!"); s1.get().value = "Hi!"; IO f(new BufferFile(FileOps::READWRITE)); s2.serialize(f); EXPECT_EQ(f->read(), 0x12345678); EXPECT_EQ(f->readString(7), "1234567"); EXPECT_EQ(f->read(), 42); EXPECT_EQ(f->read(), 0x1234); EXPECT_EQ(f->read(), 28); EXPECT_EQ(f->readString(12), "Hello World!"); EXPECT_EQ(f->read(), 3); EXPECT_EQ(f->readString(3), "Hi!"); }