improve tests

This commit is contained in:
2024-10-16 16:13:15 +02:00
parent a0fcd8b985
commit 69d96b40ec
3 changed files with 25 additions and 18 deletions

View File

@@ -18,10 +18,10 @@ namespace test {
#define TD_TEST_SUCCESSFUL 0 #define TD_TEST_SUCCESSFUL 0
/** /**
* \def BLITZ_TEST_FAILED * \def TD_TEST_FAILED
* \brief Used in tests to indicate that a test failed * \brief Used in tests to indicate that a test failed
*/ */
#define BLITZ_TEST_FAILED 1 #define TD_TEST_FAILED 1
#ifndef __FUNCTION_NAME__ #ifndef __FUNCTION_NAME__
#ifdef _WIN32 #ifdef _WIN32
@@ -40,7 +40,7 @@ namespace test {
#define td_test_assert(...) \ #define td_test_assert(...) \
if (!static_cast<bool>(__VA_ARGS__)) { \ if (!static_cast<bool>(__VA_ARGS__)) { \
td::test::LogAssert(#__VA_ARGS__, __FILE__, __LINE__, __FUNCTION_NAME__); \ td::test::LogAssert(#__VA_ARGS__, __FILE__, __LINE__, __FUNCTION_NAME__); \
std::exit(BLITZ_TEST_FAILED); \ std::exit(TD_TEST_FAILED); \
} }

View File

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

View File

@@ -5,27 +5,32 @@
namespace tp = td::protocol; namespace tp = td::protocol;
static int TestAllPackets() { template <typename Packet_T, typename Packet_Data_T = typename Packet_T::PacketDataType>
for (std::size_t i = 0; i < static_cast<std::size_t>(tp::PacketType::PACKET_COUNT); i++) { static int TestPacket() {
const auto& packet = tp::PacketFactory::CreateReadOnlyPacket(tp::PacketType(i)); Packet_T packet;
td::DataBuffer buffer = tp::PacketSerializer::Serialize(*packet.get()); td::DataBuffer buffer = tp::PacketSerializer::Serialize(packet);
tp::PacketPtr packet2 = tp::PacketSerializer::Deserialize(buffer); auto abstractPacket = tp::PacketSerializer::Deserialize(buffer);
td_test_assert(packet2 != nullptr); td_test_assert(abstractPacket);
td_test_assert(packet2->GetType() == packet->GetType()); Packet_T* packet2 = dynamic_cast<Packet_T*>(abstractPacket.get());
}
return 0; td_test_assert(packet2);
td_test_assert(packet.GetType() == packet2->GetType());
return std::memcmp(&packet.m_Data, &packet2->m_Data, sizeof(Packet_Data_T));
} }
static void Test() { #define DeclarePacket(Packet, ...) TestPacket<tp::packets::Packet>();
tp::packets::ChatMessage packet({"caca"});
td::DataBuffer buffer = tp::PacketSerializer::Serialize(packet); static int TestAllPackets() {
DeclareAllPacket()
return TD_TEST_SUCCESSFUL;
} }
int main() { int main() {
Test();
return TestAllPackets(); return TestAllPackets();
} }