]> git.localhorst.tv Git - blank.git/commitdiff
more unit tests for Packet
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Mon, 21 Sep 2015 07:02:01 +0000 (09:02 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Mon, 21 Sep 2015 07:02:01 +0000 (09:02 +0200)
doc/protocol
tst/net/PacketTest.cpp
tst/net/PacketTest.hpp

index 607bbb164fdcb588719f69f09fcd522311a51a4a..4dc245f1be0981a4669240dda0d98ecb390b8d0e 100644 (file)
@@ -108,7 +108,7 @@ Payload:
         96 flags, 32bit bitfield with boolean values
            1: world collision
        100 entity name, max 32 byte UTF-8 string
-Length: 132
+Length: 100 - 132
 
 
 Despawn Entity
index 60a19c33a8202421c8090224beb429a37d7cc8f5..a95090a87978cbe86a21627148eb20fed5b68c0b 100644 (file)
@@ -1,10 +1,9 @@
 #include "PacketTest.hpp"
 
-#include "net/Packet.hpp"
+#include "model/CompositeModel.hpp"
 #include "world/Entity.hpp"
-#include "world/EntityState.hpp"
 
-#include <string>
+#include <limits>
 
 CPPUNIT_TEST_SUITE_REGISTRATION(blank::test::PacketTest);
 
@@ -74,34 +73,12 @@ void PacketTest::testControl() {
 
 void PacketTest::testPing() {
        auto pack = Packet::Make<Packet::Ping>(udp_pack);
-       CPPUNIT_ASSERT_EQUAL_MESSAGE(
-               "Ping packet not correctly tagged",
-               TEST_TAG, pack.GetHeader().tag
-       );
-       CPPUNIT_ASSERT_EQUAL_MESSAGE(
-               "wrong type code for Ping packet",
-               uint8_t(0), pack.GetHeader().type
-       );
-       CPPUNIT_ASSERT_EQUAL_MESSAGE(
-               "bad payload length for Ping packet",
-               size_t(0), pack.length
-       );
+       AssertPacket("Ping", 0, 0, pack);
 }
 
 void PacketTest::testLogin() {
        auto pack = Packet::Make<Packet::Login>(udp_pack);
-       CPPUNIT_ASSERT_EQUAL_MESSAGE(
-               "Login packet not correctly tagged",
-               TEST_TAG, pack.GetHeader().tag
-       );
-       CPPUNIT_ASSERT_EQUAL_MESSAGE(
-               "wrong type code for Login packet",
-               uint8_t(1), pack.GetHeader().type
-       );
-       CPPUNIT_ASSERT_MESSAGE(
-               "bad payload length for Login packet",
-               pack.length <= 32
-       );
+       AssertPacket("Login", 1, 0, 32, pack);
 
        string write_name = "test";
        string read_name;
@@ -123,22 +100,17 @@ void PacketTest::testLogin() {
 
 void PacketTest::testJoin() {
        auto pack = Packet::Make<Packet::Join>(udp_pack);
-       CPPUNIT_ASSERT_EQUAL_MESSAGE(
-               "Join packet not correctly tagged",
-               TEST_TAG, pack.GetHeader().tag
-       );
-       CPPUNIT_ASSERT_EQUAL_MESSAGE(
-               "wrong type code for Join packet",
-               uint8_t(2), pack.GetHeader().type
-       );
-       CPPUNIT_ASSERT_MESSAGE(
-               "bad payload length for Join packet",
-               pack.length >= 68 && pack.length <= 100
-       );
+       AssertPacket("Join", 2, 68, 100, pack);
 
        Entity write_entity;
        write_entity.ID(534574);
+       write_entity.GetState().chunk_pos = { 7, 2, -3 };
+       write_entity.GetState().block_pos = { 1.5f, 0.9f, 12.0f };
+       write_entity.GetState().velocity = { 0.025f, 0.001f, 0.0f };
+       write_entity.GetState().orient = { 1.0f, 0.0f, 0.0f, 0.0f };
+       write_entity.GetState().ang_vel = { 0.01f, 0.00302f, 0.0985f };
        uint32_t read_id = 0;
+       EntityState read_state;
        pack.WritePlayer(write_entity);
 
        pack.ReadPlayerID(read_id);
@@ -146,6 +118,11 @@ void PacketTest::testJoin() {
                "player entity ID not correctly transported in Join packet",
                write_entity.ID(), read_id
        );
+       pack.ReadPlayerState(read_state);
+       AssertEqual(
+               "player entity state not correctly transported in Join packet",
+               write_entity.GetState(), read_state
+       );
 
        string write_name = "test";
        string read_name;
@@ -167,17 +144,239 @@ void PacketTest::testJoin() {
 
 void PacketTest::testPart() {
        auto pack = Packet::Make<Packet::Part>(udp_pack);
+       AssertPacket("Part", 3, 0, pack);
+}
+
+void PacketTest::testPlayerUpdate() {
+       auto pack = Packet::Make<Packet::PlayerUpdate>(udp_pack);
+       AssertPacket("PlayerUpdate", 4, 64, pack);
+
+       Entity write_entity;
+       write_entity.ID(534574);
+       write_entity.GetState().chunk_pos = { 7, 2, -3 };
+       write_entity.GetState().block_pos = { 1.5f, 0.9f, 12.0f };
+       write_entity.GetState().velocity = { 0.025f, 0.001f, 0.0f };
+       write_entity.GetState().orient = { 1.0f, 0.0f, 0.0f, 0.0f };
+       write_entity.GetState().ang_vel = { 0.01f, 0.00302f, 0.0985f };
+       EntityState read_state;
+       pack.WritePlayer(write_entity);
+
+       pack.ReadPlayerState(read_state);
+       AssertEqual(
+               "player entity state not correctly transported in PlayerUpdate packet",
+               write_entity.GetState(), read_state
+       );
+}
+
+void PacketTest::testSpawnEntity() {
+       auto pack = Packet::Make<Packet::SpawnEntity>(udp_pack);
+       AssertPacket("SpawnEntity", 5, 100, 132, pack);
+
+       Entity write_entity;
+       write_entity.ID(534574);
+       CompositeModel model;
+       model.ID(23);
+       model.Instantiate(write_entity.GetModel());
+       write_entity.GetState().chunk_pos = { 7, 2, -3 };
+       write_entity.GetState().block_pos = { 1.5f, 0.9f, 12.0f };
+       write_entity.GetState().velocity = { 0.025f, 0.001f, 0.0f };
+       write_entity.GetState().orient = { 1.0f, 0.0f, 0.0f, 0.0f };
+       write_entity.GetState().ang_vel = { 0.01f, 0.00302f, 0.0985f };
+       write_entity.Bounds({{ -1, -1, -1 }, { 1, 1, 1 }});
+       write_entity.WorldCollidable(true);
+       write_entity.Name("blah");
+       pack.WriteEntity(write_entity);
+
+       uint32_t entity_id;
+       uint32_t skeleton_id;
+       Entity read_entity;
+       pack.ReadEntityID(entity_id);
+       pack.ReadSkeletonID(skeleton_id);
+       pack.ReadEntity(read_entity);
+
        CPPUNIT_ASSERT_EQUAL_MESSAGE(
-               "Part packet not correctly tagged",
-               TEST_TAG, pack.GetHeader().tag
+               "entity ID not correctly transported in SpawnEntity packet",
+               write_entity.ID(), entity_id
        );
        CPPUNIT_ASSERT_EQUAL_MESSAGE(
-               "wrong type code for Part packet",
-               uint8_t(3), pack.GetHeader().type
+               "skeleton ID not correctly transported in SpawnEntity packet",
+               write_entity.GetModel().GetModel().ID(), skeleton_id
+       );
+       AssertEqual(
+               "entity state not correctly transported in PlayerUpdate packet",
+               write_entity.GetState(), read_entity.GetState()
+       );
+       AssertEqual(
+               "entity bounds not correctly transported in PlayerUpdate packet",
+               write_entity.Bounds(), read_entity.Bounds()
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "entity flags not correctly transported in SpawnEntity packet",
+               read_entity.WorldCollidable()
        );
        CPPUNIT_ASSERT_EQUAL_MESSAGE(
-               "bad payload length for Part packet",
-               size_t(0), pack.length
+               "entity name not correctly transported in SpawnEntity packet",
+               write_entity.Name(), read_entity.Name()
+       );
+}
+
+void PacketTest::testDespawnEntity() {
+       auto pack = Packet::Make<Packet::DespawnEntity>(udp_pack);
+       AssertPacket("DespawnEntity", 6, 4, pack);
+
+       uint32_t write_id = 5437;
+       uint32_t read_id;
+       pack.WriteEntityID(write_id);
+       pack.ReadEntityID(read_id);
+
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               "entity ID not correctly transported in DespawnEntity packet",
+               write_id, read_id
+       );
+}
+
+
+void PacketTest::AssertPacket(
+       const string &name,
+       uint8_t expected_type,
+       size_t expected_length,
+       const Packet::Payload &actual
+) {
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               name + " packet not correctly tagged",
+               TEST_TAG, actual.GetHeader().tag
+       );
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               "wrong type code for " + name + " packet",
+               int(expected_type), int(actual.GetHeader().type)
+       );
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               "bad payload length for " + name + " packet",
+               expected_length, actual.length
+       );
+}
+
+void PacketTest::AssertPacket(
+       const string &name,
+       uint8_t expected_type,
+       size_t min_length,
+       size_t max_length,
+       const Packet::Payload &actual
+) {
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               name + " packet not correctly tagged",
+               TEST_TAG, actual.GetHeader().tag
+       );
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               "wrong type code for " + name + " packet",
+               expected_type, actual.GetHeader().type
+       );
+       CPPUNIT_ASSERT_MESSAGE(
+               "bad payload length for " + name + " packet",
+               actual.length >= min_length && actual.length <= max_length
+       );
+}
+
+void PacketTest::AssertEqual(
+       const string &message,
+       const EntityState &expected,
+       const EntityState &actual
+) {
+       AssertEqual(
+               message + ": bad chunk position",
+               expected.chunk_pos, actual.chunk_pos
+       );
+       AssertEqual(
+               message + ": bad block position",
+               expected.block_pos, actual.block_pos
+       );
+       AssertEqual(
+               message + ": bad velocity",
+               expected.velocity, actual.velocity
+       );
+       AssertEqual(
+               message + ": bad orientation",
+               expected.orient, actual.orient
+       );
+       AssertEqual(
+               message + ": bad angular velocity",
+               expected.ang_vel, actual.ang_vel
+       );
+}
+
+void PacketTest::AssertEqual(
+       const string &message,
+       const AABB &expected,
+       const AABB &actual
+) {
+       AssertEqual(
+               message + ": bad lower bound",
+               expected.min, actual.min
+       );
+       AssertEqual(
+               message + ": bad upper bound",
+               expected.max, actual.max
+       );
+}
+
+void PacketTest::AssertEqual(
+       const string &message,
+       const glm::ivec3 &expected,
+       const glm::ivec3 &actual
+) {
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               message + " (X component)",
+               expected.x, actual.x
+       );
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               message + " (Y component)",
+               expected.y, actual.y
+       );
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               message + " (Z component)",
+               expected.z, actual.z
+       );
+}
+
+void PacketTest::AssertEqual(
+       const string &message,
+       const glm::vec3 &expected,
+       const glm::vec3 &actual
+) {
+       CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
+               message + " (X component)",
+               expected.x, actual.x, numeric_limits<float>::epsilon()
+       );
+       CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
+               message + " (Y component)",
+               expected.y, actual.y, numeric_limits<float>::epsilon()
+       );
+       CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
+               message + " (Z component)",
+               expected.z, actual.z, numeric_limits<float>::epsilon()
+       );
+}
+
+void PacketTest::AssertEqual(
+       const string &message,
+       const glm::quat &expected,
+       const glm::quat &actual
+) {
+       CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
+               message + " (W component)",
+               expected.w, actual.w, numeric_limits<float>::epsilon()
+       );
+       CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
+               message + " (X component)",
+               expected.x, actual.x, numeric_limits<float>::epsilon()
+       );
+       CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
+               message + " (Y component)",
+               expected.y, actual.y, numeric_limits<float>::epsilon()
+       );
+       CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
+               message + " (Z component)",
+               expected.z, actual.z, numeric_limits<float>::epsilon()
        );
 }
 
index 12b93da38372779330246f4c86bed21d0d0e055b..618df1e68d9d75fe3eb8b6377a71473b188ec55a 100644 (file)
@@ -1,7 +1,14 @@
 #ifndef BLANK_TEST_NET_PACKETTEST_HPP_
 #define BLANK_TEST_NET_PACKETTEST_HPP_
 
+#include "model/geometry.hpp"
+#include "net/Packet.hpp"
+#include "world/EntityState.hpp"
+
+#include <cstdint>
+#include <string>
 #include <SDL_net.h>
+#include <glm/glm.hpp>
 #include <cppunit/extensions/HelperMacros.h>
 
 
@@ -18,6 +25,9 @@ CPPUNIT_TEST(testPing);
 CPPUNIT_TEST(testLogin);
 CPPUNIT_TEST(testJoin);
 CPPUNIT_TEST(testPart);
+CPPUNIT_TEST(testPlayerUpdate);
+CPPUNIT_TEST(testSpawnEntity);
+CPPUNIT_TEST(testDespawnEntity);
 
 CPPUNIT_TEST_SUITE_END();
 
@@ -30,6 +40,43 @@ public:
        void testLogin();
        void testJoin();
        void testPart();
+       void testPlayerUpdate();
+       void testSpawnEntity();
+       void testDespawnEntity();
+
+private:
+       static void AssertPacket(
+               const std::string &name,
+               std::uint8_t expected_type,
+               std::size_t expected_length,
+               const Packet::Payload &actual);
+       static void AssertPacket(
+               const std::string &name,
+               std::uint8_t expected_type,
+               std::size_t min_length,
+               std::size_t max_length,
+               const Packet::Payload &actual);
+
+       static void AssertEqual(
+               const std::string &message,
+               const EntityState &expected,
+               const EntityState &actual);
+       static void AssertEqual(
+               const std::string &message,
+               const AABB &expected,
+               const AABB &actual);
+       static void AssertEqual(
+               const std::string &message,
+               const glm::ivec3 &expected,
+               const glm::ivec3 &actual);
+       static void AssertEqual(
+               const std::string &message,
+               const glm::vec3 &expected,
+               const glm::vec3 &actual);
+       static void AssertEqual(
+               const std::string &message,
+               const glm::quat &expected,
+               const glm::quat &actual);
 
 private:
        UDPpacket udp_pack;