better serializer tests
This commit is contained in:
@@ -5,6 +5,8 @@
|
|||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
|
|
||||||
|
using FpFloat = fpm::fixed_16_16;
|
||||||
|
|
||||||
enum class Team : std::uint8_t {
|
enum class Team : std::uint8_t {
|
||||||
Blue = 0,
|
Blue = 0,
|
||||||
Red,
|
Red,
|
||||||
@@ -59,8 +61,8 @@ struct TowerCoords {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct EntityCoords {
|
struct EntityCoords {
|
||||||
fpm::fixed_16_16 x;
|
FpFloat x;
|
||||||
fpm::fixed_16_16 y;
|
FpFloat y;
|
||||||
};
|
};
|
||||||
|
|
||||||
using PeerID = std::uint16_t;
|
using PeerID = std::uint16_t;
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ DataBuffer& DataBuffer::operator>>(std::string& str) {
|
|||||||
std::copy(m_Buffer.begin() + static_cast<difference_type>(m_ReadOffset),
|
std::copy(m_Buffer.begin() + static_cast<difference_type>(m_ReadOffset),
|
||||||
m_Buffer.begin() + static_cast<difference_type>(m_ReadOffset + stringSize), str.begin());
|
m_Buffer.begin() + static_cast<difference_type>(m_ReadOffset + stringSize), str.begin());
|
||||||
m_ReadOffset += stringSize;
|
m_ReadOffset += stringSize;
|
||||||
|
str.resize(stringSize - 1);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -137,7 +137,8 @@ void Deserializer::DeserializeCommandData(cdata::End& a_Command) {}
|
|||||||
|
|
||||||
|
|
||||||
void Serializer::SerializeCommandData(const cdata::PlaceTower& a_Command) {
|
void Serializer::SerializeCommandData(const cdata::PlaceTower& a_Command) {
|
||||||
m_Buffer << (static_cast<std::uint8_t>(a_Command.m_Type) << 4 | a_Command.m_Placer & 0xF) << a_Command.m_Position;
|
m_Buffer << static_cast<std::uint8_t>((static_cast<std::uint8_t>(a_Command.m_Type) << 4 | a_Command.m_Placer & 0xF))
|
||||||
|
<< a_Command.m_Position;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Deserializer::DeserializeCommandData(cdata::PlaceTower& a_Command) {
|
void Deserializer::DeserializeCommandData(cdata::PlaceTower& a_Command) {
|
||||||
|
|||||||
@@ -5,9 +5,36 @@
|
|||||||
|
|
||||||
namespace tp = td::protocol;
|
namespace tp = td::protocol;
|
||||||
|
|
||||||
|
class Comparator {
|
||||||
|
public:
|
||||||
|
bool operator()(const tp::cdata::End& left, const tp::cdata::End& right) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
bool operator()(const tp::cdata::PlaceTower& left, const tp::cdata::PlaceTower& right) {
|
||||||
|
return left.m_Placer == right.m_Placer && left.m_Position.x == right.m_Position.x && left.m_Position.y == right.m_Position.y &&
|
||||||
|
left.m_Type == right.m_Type;
|
||||||
|
}
|
||||||
|
bool operator()(const tp::cdata::PlayerJoin& left, const tp::cdata::PlayerJoin& right) {
|
||||||
|
return left.m_ID == right.m_ID && left.m_Name == right.m_Name;
|
||||||
|
}
|
||||||
|
bool operator()(const tp::cdata::SpawnTroop& left, const tp::cdata::SpawnTroop& right) {
|
||||||
|
return left.m_Level == right.m_Level && left.m_Position.x == right.m_Position.x && left.m_Position.y == right.m_Position.y &&
|
||||||
|
left.m_Sender == right.m_Sender && left.m_Type == right.m_Type;
|
||||||
|
}
|
||||||
|
bool operator()(const tp::cdata::TeamChange& left, const tp::cdata::TeamChange& right) {
|
||||||
|
return left.m_NewTeam == right.m_NewTeam && left.m_Player == right.m_Player;
|
||||||
|
}
|
||||||
|
bool operator()(const tp::cdata::UpgradeTower& left, const tp::cdata::UpgradeTower& right) {
|
||||||
|
return left.m_Tower == right.m_Tower && left.m_Upgrade == right.m_Upgrade;
|
||||||
|
}
|
||||||
|
bool operator()(const tp::cdata::UseItem& left, const tp::cdata::UseItem& right) {
|
||||||
|
return left.m_Item == right.m_Item && left.m_Position.x == right.m_Position.x && left.m_Position.y == right.m_Position.y &&
|
||||||
|
left.m_User == right.m_User;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
template <typename Command_T, typename Command_Data_T = typename Command_T::CommandDataType>
|
template <typename Command_T, typename Command_Data_T = typename Command_T::CommandDataType>
|
||||||
static int TestCommand() {
|
static int TestCommand(const Command_T& command) {
|
||||||
Command_T command;
|
|
||||||
|
|
||||||
td::DataBuffer buffer = tp::CommandSerializer::Serialize(command);
|
td::DataBuffer buffer = tp::CommandSerializer::Serialize(command);
|
||||||
|
|
||||||
@@ -20,32 +47,47 @@ static int TestCommand() {
|
|||||||
td_test_assert(command2);
|
td_test_assert(command2);
|
||||||
td_test_assert(command.GetType() == command2->GetType());
|
td_test_assert(command.GetType() == command2->GetType());
|
||||||
|
|
||||||
return std::memcmp(&command.m_Data, &command2->m_Data, sizeof(Command_Data_T));
|
return Comparator{}(command.m_Data, command2->m_Data);
|
||||||
}
|
}
|
||||||
|
|
||||||
#define DeclareCommand(Command, ...) TestCommand<tp::commands::Command>();
|
#define DeclareCommand(Command, ...) td_test_assert(TestCommand<tp::commands::Command>({}));
|
||||||
|
|
||||||
static int TestAllCommands() {
|
static int TestAllCommands() {
|
||||||
|
td_test_assert(TestCommand<tp::commands::End>({}));
|
||||||
|
td_test_assert(TestCommand<tp::commands::PlaceTower>({tp::cdata::PlaceTower{
|
||||||
|
td::TowerType::Necromancer,
|
||||||
|
8,
|
||||||
|
td::TowerCoords{-50, 69},
|
||||||
|
}}));
|
||||||
|
td_test_assert(TestCommand<tp::commands::PlayerJoin>({tp::cdata::PlayerJoin{
|
||||||
|
4,
|
||||||
|
"Persson",
|
||||||
|
}}));
|
||||||
|
td_test_assert(TestCommand<tp::commands::SpawnTroop>({tp::cdata::SpawnTroop{
|
||||||
|
td::EntityType::Blaze,
|
||||||
|
4,
|
||||||
|
td::EntityCoords{td::FpFloat{54}, td::FpFloat{58}},
|
||||||
|
2,
|
||||||
|
}}));
|
||||||
|
td_test_assert(TestCommand<tp::commands::TeamChange>({tp::cdata::TeamChange{
|
||||||
|
7,
|
||||||
|
td::Team::Red,
|
||||||
|
}}));
|
||||||
|
td_test_assert(TestCommand<tp::commands::UpgradeTower>({tp::cdata::UpgradeTower{
|
||||||
|
69,
|
||||||
|
3,
|
||||||
|
}}));
|
||||||
|
td_test_assert(TestCommand<tp::commands::UseItem>({tp::cdata::UseItem{
|
||||||
|
td::ShopItem::Freeze,
|
||||||
|
5,
|
||||||
|
td::EntityCoords{td::FpFloat{24}, td::FpFloat{-69}},
|
||||||
|
}}));
|
||||||
|
|
||||||
DeclareAllCommand()
|
DeclareAllCommand()
|
||||||
|
|
||||||
return TD_TEST_SUCCESSFUL;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int TestNewTeam() {
|
|
||||||
tp::commands::TeamChange tc;
|
|
||||||
tc.m_Data.m_Player = 69;
|
|
||||||
tc.m_Data.m_NewTeam = td::Team::Red;
|
|
||||||
|
|
||||||
td::DataBuffer db = tp::CommandSerializer::Serialize(tc);
|
|
||||||
|
|
||||||
auto packet = tp::CommandSerializer::Deserialize(db);
|
|
||||||
|
|
||||||
tp::commands::TeamChange* tc2 = dynamic_cast<tp::commands::TeamChange*>(packet.get());
|
|
||||||
|
|
||||||
return TD_TEST_SUCCESSFUL;
|
return TD_TEST_SUCCESSFUL;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
TestNewTeam();
|
|
||||||
return TestAllCommands();
|
return TestAllCommands();
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user