add tests

This commit is contained in:
2024-10-16 12:35:45 +02:00
parent fc405eeba1
commit a0fcd8b985
14 changed files with 515 additions and 67 deletions

View File

@@ -10,8 +10,8 @@
#include <cstdint> #include <cstdint>
#include <cstring> #include <cstring>
#include <string> #include <string>
#include <vector>
#include <td/common/VarInt.h> #include <td/common/VarInt.h>
#include <vector>
namespace td { namespace td {
@@ -76,7 +76,7 @@ class DataBuffer {
/** /**
* \brief Append a vector to the buffer by first writing the size * \brief Append a vector to the buffer by first writing the size
* \param data The buffer to append * \param data The vector to append
*/ */
template <typename T> template <typename T>
DataBuffer& operator<<(const std::vector<T>& data) { DataBuffer& operator<<(const std::vector<T>& data) {
@@ -87,6 +87,18 @@ class DataBuffer {
return *this; return *this;
} }
/**
* \brief Append an array to the buffer by first writing the size
* \param data The buffer to append
*/
template <typename T, std::size_t Size>
DataBuffer& operator<<(const std::array<T, Size>& data) {
for (const auto& element : data) {
*this << element;
}
return *this;
}
/** /**
* \brief Read some data from the buffer and assign to desired variable * \brief Read some data from the buffer and assign to desired variable
*/ */
@@ -127,6 +139,19 @@ class DataBuffer {
return *this; return *this;
} }
/**
* \brief Read an array from the buffer
*/
template <std::size_t Size, typename T>
DataBuffer& operator>>(std::array<T, Size>& data) {
for (std::size_t i = 0; i < Size; i++) {
T newElement;
*this >> newElement;
data[i] = newElement;
}
return *this;
}
/** /**
* \brief Write some data to the buffer * \brief Write some data to the buffer
* \param buffer The buffer to write * \param buffer The buffer to write

View File

@@ -2,7 +2,7 @@
/** /**
* \file VarInt.h * \file VarInt.h
* \brief File containing the blitz::VarInt class * \brief File containing the td::VarInt class
*/ */
#include <cstddef> #include <cstddef>

70
include/td/misc/Test.h Normal file
View File

@@ -0,0 +1,70 @@
#pragma once
/**
* \file Test.h
* \brief File containing unit testing utilities
*/
#include <cstdlib>
#include <td/misc/Log.h>
namespace td {
namespace test {
/**
* \def TD_TEST_SUCCESSFUL
* \brief Used in tests to indicate that a test was successful
*/
#define TD_TEST_SUCCESSFUL 0
/**
* \def BLITZ_TEST_FAILED
* \brief Used in tests to indicate that a test failed
*/
#define BLITZ_TEST_FAILED 1
#ifndef __FUNCTION_NAME__
#ifdef _WIN32
#define __FUNCTION_NAME__ __FUNCTION__
#else
#define __FUNCTION_NAME__ __PRETTY_FUNCTION__
#endif
#endif
/**
* \def blitz_test_assert
* \param ... The expression to evaluate
* \brief Evaluates the expression and exits the program if not valid.
* \note It works like a basic assert() but also in release mode
*/
#define td_test_assert(...) \
if (!static_cast<bool>(__VA_ARGS__)) { \
td::test::LogAssert(#__VA_ARGS__, __FILE__, __LINE__, __FUNCTION_NAME__); \
std::exit(BLITZ_TEST_FAILED); \
}
/**
* \def blitz_debug_assert
* \param ... The expression to execute
* \brief Assertion without checks in release mode
* \note The expression is always executed. However, in release, no checks are made !
*/
#ifdef NDEBUG
#define td_debug_assert(...) __VA_ARGS__
#else
#define td_debug_assert td_test_assert
#endif
/**
* \brief Prints an error message associated with a failed assertion
* \param expression The expression that was tested
* \param file The file in which the assertion failed
* \param line The line in the file in which the assertion failed
* \param function The function in which the assertion failed
*/
void LogAssert(const char* expression, const char* file, int line, const char* function);
} // namespace test
} // namespace td

View File

@@ -1,59 +0,0 @@
#pragma once
#include <td/protocol/packet/PacketVisitor.h>
namespace td {
class NetworkInterface;
namespace protocol {
#define DeclarePacket(PacketName, Reliability, ...) void Visit(const protocol::packets::PacketName& a_Packet) override;
///////////////////////
/* PacketBroadcaster */
///////////////////////
class PacketBroadcaster : public protocol::PacketVisitor {
private:
NetworkInterface& m_NetworkInterface;
public:
PacketBroadcaster(NetworkInterface& a_NetworkInterface) : m_NetworkInterface(a_NetworkInterface) {}
void BroadcastPacket(const protocol::Packet& a_Packet) {
Check(a_Packet);
}
DeclareAllPacket()
};
//////////////////
/* PacketSender */
//////////////////
class PacketSender : public protocol::PacketVisitor {
private:
NetworkInterface& m_NetworkInterface;
PeerID m_PeerId;
public:
PacketSender(PeerID a_PeerId, NetworkInterface& a_NetworkInterface) : m_PeerId(a_PeerId), m_NetworkInterface(a_NetworkInterface) {}
void SendPacket(const protocol::Packet& a_Packet) {
Check(a_Packet);
}
DeclareAllPacket()
};
#undef DeclarePacket
} // namespace protocol
} // namespace td

14
src/td/misc/Test.cpp Normal file
View File

@@ -0,0 +1,14 @@
#include <td/misc/Format.h>
#include <td/misc/Log.h>
#include <td/misc/Test.h>
namespace td {
namespace test {
void LogAssert(const char* expression, const char* file, int line, const char* function) {
utils::LOGE(utils::Format("%s:%i: %s: Assertion failed !", file, line, function));
utils::LOGE(utils::Format(" %i |\t%s;", line, expression));
}
} // namespace test
} // namespace td

View File

@@ -0,0 +1,26 @@
#include <td/protocol/command/CommandFactory.h>
#include <array>
#include <cassert>
#include <functional>
namespace td {
namespace protocol {
namespace CommandFactory {
using CommandCreator = std::function<std::unique_ptr<Command>()>;
#define DeclareCommand(CommandName, ...) std::make_unique<commands::CommandName>(),
static std::array<std::unique_ptr<Command>, static_cast<std::size_t>(CommandType::COMMAND_COUNT)> Commands = {
DeclareAllCommand()
};
const std::unique_ptr<Command>& CreateReadOnlyCommand(CommandType a_Type) {
assert(a_Type < CommandType::COMMAND_COUNT);
return Commands[static_cast<std::size_t>(a_Type)];
}
} // namespace CommandFactory
} // namespace protocol
} // namespace td

View File

@@ -0,0 +1,11 @@
#include <td/protocol/command/CommandVisitor.h>
namespace td {
namespace protocol {
void CommandVisitor::Check(const Command& a_Command) {
a_Command.Accept(*this);
}
} // namespace protocol
} // namespace td

View File

@@ -0,0 +1,26 @@
#include <td/protocol/packet/PacketFactory.h>
#include <array>
#include <cassert>
#include <functional>
namespace td {
namespace protocol {
namespace PacketFactory {
using PacketCreator = std::function<std::unique_ptr<Packet>()>;
#define DeclarePacket(PacketName, ...) std::make_unique<packets::PacketName>(),
static std::array<std::unique_ptr<Packet>, static_cast<std::size_t>(PacketType::PACKET_COUNT)> Packets = {
DeclareAllPacket()
};
const std::unique_ptr<Packet>& CreateReadOnlyPacket(PacketType a_Type) {
assert(a_Type < PacketType::PACKET_COUNT);
return Packets[static_cast<std::size_t>(a_Type)];
}
} // namespace PacketFactory
} // namespace protocol
} // namespace td

View File

@@ -0,0 +1,239 @@
#include <td/protocol/packet/PacketSerializer.h>
#include <td/protocol/packet/PacketFactory.h>
#include <td/protocol/packet/PacketVisitor.h>
#include <td/misc/Format.h>
#include <td/misc/Log.h>
namespace td {
namespace protocol {
namespace PacketSerializer {
#define DeclarePacket(PacketName, ...) \
void Visit(const packets::PacketName& a_Packet) override { \
const auto& packetData = a_Packet.m_Data; \
SerializePacketData(packetData); \
} \
\
void SerializePacketData(const packets::PacketName::PacketDataType& a_Packet);
class Serializer : public PacketVisitor {
private:
DataBuffer& m_Buffer;
public:
Serializer(DataBuffer& a_Buffer) : m_Buffer(a_Buffer) {}
void Serialize(const Packet& a_Packet) {
m_Buffer << static_cast<PacketID>(a_Packet.GetType());
Check(a_Packet);
}
DeclareAllPacket()
};
#undef DeclarePacket
#define DeclarePacket(PacketName, ...) \
void Visit(const packets::PacketName& a_Packet) override { \
auto packetPtr = PacketFactory::CreatePacket<packets::PacketName>(); \
auto& packetData = packetPtr->m_Data; \
\
DeserializePacketData(packetData); \
\
m_Packet = std::move(packetPtr); \
} \
\
void DeserializePacketData(packets::PacketName::PacketDataType& a_Packet);
class Deserializer : public PacketVisitor {
private:
DataBuffer& m_Buffer;
PacketPtr m_Packet;
public:
Deserializer(DataBuffer& a_Buffer) : m_Buffer(a_Buffer) {}
bool Deserialize(const PacketPtr& a_Packet) {
try {
Check(*a_Packet.get());
} catch (std::exception& e) {
utils::LOGE(utils::Format("[PacketSerializer::Deserializer] %s", e.what()));
return false;
}
return true;
}
PacketPtr& GetPacket() {
return m_Packet;
}
DeclareAllPacket()
};
DataBuffer Serialize(const Packet& a_Packet) {
DataBuffer buffer;
Serializer serializer(buffer);
serializer.Serialize(a_Packet);
return buffer;
}
std::unique_ptr<Packet> Deserialize(DataBuffer& a_Data) {
PacketID packetId;
a_Data >> packetId;
if (packetId >= static_cast<PacketID>(PacketType::PACKET_COUNT))
return nullptr;
PacketType packetType = PacketType(packetId);
// for double-dispatch
const PacketPtr& emptyPacket = PacketFactory::CreateReadOnlyPacket(packetType);
Deserializer deserializer(a_Data);
if (deserializer.Deserialize(emptyPacket)) {
PacketPtr packet = std::move(deserializer.GetPacket());
return packet;
}
return nullptr;
}
//---------------------------------------------
// Packet serializer implementation
//----------------------------------------------
void Serializer::SerializePacketData(const pdata::PlayerLogin& a_Packet) {
m_Buffer << a_Packet.m_PlayerName;
}
void Deserializer::DeserializePacketData(pdata::PlayerLogin& a_Packet) {
m_Buffer >> a_Packet.m_PlayerName;
}
void Serializer::SerializePacketData(const pdata::LoggingSuccess& a_Packet) {
m_Buffer << a_Packet.m_PlayerId;
}
void Deserializer::DeserializePacketData(pdata::LoggingSuccess& a_Packet) {
m_Buffer >> a_Packet.m_PlayerId;
}
void Serializer::SerializePacketData(const pdata::PlayerJoin& a_Packet) {
m_Buffer << a_Packet.m_Player;
}
void Deserializer::DeserializePacketData(pdata::PlayerJoin& a_Packet) {
m_Buffer >> a_Packet.m_Player;
}
void Serializer::SerializePacketData(const pdata::PlayerLeave& a_Packet) {
m_Buffer << a_Packet.m_PlayerId;
}
void Deserializer::DeserializePacketData(pdata::PlayerLeave& a_Packet) {
m_Buffer >> a_Packet.m_PlayerId;
}
void Serializer::SerializePacketData(const pdata::KeepAlive& a_Packet) {
m_Buffer << a_Packet.m_KeepAliveId;
}
void Deserializer::DeserializePacketData(pdata::KeepAlive& a_Packet) {
m_Buffer >> a_Packet.m_KeepAliveId;
}
void Serializer::SerializePacketData(const pdata::Disconnect& a_Packet) {
m_Buffer << a_Packet.m_Reason;
}
void Deserializer::DeserializePacketData(pdata::Disconnect& a_Packet) {
m_Buffer >> a_Packet.m_Reason;
}
void Serializer::SerializePacketData(const pdata::ChatMessage& a_Packet) {
m_Buffer << a_Packet.m_Text;
}
void Deserializer::DeserializePacketData(pdata::ChatMessage& a_Packet) {
m_Buffer >> a_Packet.m_Text;
}
void Serializer::SerializePacketData(const pdata::LockSteps& a_Packet) {
m_Buffer << a_Packet.m_FirstFrameNumber << a_Packet.m_LockSteps;
}
void Deserializer::DeserializePacketData(pdata::LockSteps& a_Packet) {
m_Buffer >> a_Packet.m_FirstFrameNumber >> a_Packet.m_LockSteps;
}
void Serializer::SerializePacketData(const pdata::BeginGame& a_Packet) {
m_Buffer << a_Packet.m_Map << a_Packet.m_BlueTeam << a_Packet.m_RedTeam << a_Packet.m_Map;
}
void Deserializer::DeserializePacketData(pdata::BeginGame& a_Packet) {
m_Buffer >> a_Packet.m_Map >> a_Packet.m_BlueTeam >> a_Packet.m_RedTeam >> a_Packet.m_Map;
}
} // namespace PacketSerializer
} // namespace protocol
} // namespace td

View File

@@ -0,0 +1,11 @@
#include <td/protocol/packet/PacketVisitor.h>
namespace td {
namespace protocol {
void PacketVisitor::Check(const Packet& a_Packet) {
a_Packet.Accept(*this);
}
} // namespace protocol
} // namespace td

View File

@@ -0,0 +1,23 @@
#define TD_INSTANCIATE_PACKETS
#include <td/protocol/packet/Packets.h>
#include <td/protocol/packet/PacketVisitor.h>
namespace td {
namespace protocol {
template <PacketType PT, typename Data>
packets::ConcretePacket<PT, Data>::ConcretePacket(const PacketDataType& a_Data) : m_Data(a_Data) {}
template <PacketType PT, typename Data>
void packets::ConcretePacket<PT, Data>::Accept(PacketVisitor& a_Visitor) const {
a_Visitor.Visit(*this);
}
#define DeclarePacket(PacketName, packetSendType, packetSenderType) \
static_assert(static_cast<unsigned>(PacketSendType::packetSendType) && static_cast<unsigned>(PacketSenderType::packetSenderType));
DeclareAllPacket()
} // namespace protocol
} // namespace td

View File

@@ -0,0 +1,15 @@
#include <td/protocol/packet/PacketFactory.h>
static int Test() {
for (std::size_t i = 0; i < static_cast<int>(td::protocol::PacketType::PACKET_COUNT); i++) {
td::protocol::PacketType packetType = td::protocol::PacketType(i);
if (td::protocol::PacketFactory::CreateReadOnlyPacket(packetType)->GetType() != packetType)
return 1;
}
return 0;
}
int main() {
return Test();
}

View File

@@ -0,0 +1,31 @@
#include <td/protocol/packet/PacketSerializer.h>
#include <td/misc/Test.h>
#include <td/protocol/packet/PacketFactory.h>
namespace tp = td::protocol;
static int TestAllPackets() {
for (std::size_t i = 0; i < static_cast<std::size_t>(tp::PacketType::PACKET_COUNT); i++) {
const auto& packet = tp::PacketFactory::CreateReadOnlyPacket(tp::PacketType(i));
td::DataBuffer buffer = tp::PacketSerializer::Serialize(*packet.get());
tp::PacketPtr packet2 = tp::PacketSerializer::Deserialize(buffer);
td_test_assert(packet2 != nullptr);
td_test_assert(packet2->GetType() == packet->GetType());
}
return 0;
}
static void Test() {
tp::packets::ChatMessage packet({"caca"});
td::DataBuffer buffer = tp::PacketSerializer::Serialize(packet);
}
int main() {
Test();
return TestAllPackets();
}

View File

@@ -3,11 +3,27 @@ add_rules("mode.debug", "mode.release")
add_requires("fpm") add_requires("fpm")
target("Tower-Defense2") target("Tower-Defense2")
add_includedirs("include") add_includedirs("include", {public = true})
set_kind("binary") set_kind("static")
add_files("src/**.cpp") add_files("src/**.cpp")
add_packages("fpm") add_packages("fpm", {public = true})
-- Tests
for _, file in ipairs(os.files("test/**.cpp")) do
local name = path.basename(file)
target(name)
set_kind("binary")
add_files(file)
set_default(false)
add_deps("Tower-Defense2")
add_tests("compile_and_run")
end
-- --
-- If you want to known more usage about xmake, please see https://xmake.io -- If you want to known more usage about xmake, please see https://xmake.io
-- --