X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=tst%2Fnet%2FPacketTest.cpp;h=5b6deed47974c1285f679d824d227ab603a0bc3d;hb=ca90ec459ca0bd48c1483a45f30496aed61e9c21;hp=3f47d0f9c4132085b420072a119397a0c1fc3207;hpb=1709d12eb27ecf7626d724a3fc14096a93f3cb4c;p=blank.git diff --git a/tst/net/PacketTest.cpp b/tst/net/PacketTest.cpp index 3f47d0f..5b6deed 100644 --- a/tst/net/PacketTest.cpp +++ b/tst/net/PacketTest.cpp @@ -1,10 +1,8 @@ #include "PacketTest.hpp" -#include "model/CompositeModel.hpp" +#include "model/Model.hpp" #include "world/Entity.hpp" -#include - CPPUNIT_TEST_SUITE_REGISTRATION(blank::test::PacketTest); using namespace std; @@ -27,9 +25,27 @@ static constexpr uint32_t TEST_TAG = 0xFB1AB1AF; } +void PacketTest::testSizes() { + CPPUNIT_ASSERT_EQUAL_MESSAGE( + "unexpected size of vec3", + size_t(12), sizeof(glm::vec3) + ); + CPPUNIT_ASSERT_EQUAL_MESSAGE( + "unexpected size of vec3i", + size_t(12), sizeof(glm::ivec3) + ); + CPPUNIT_ASSERT_EQUAL_MESSAGE( + "unexpected size of quat", + size_t(16), sizeof(glm::quat) + ); + CPPUNIT_ASSERT_EQUAL_MESSAGE( + "unexpected size of entity state", + size_t(64), sizeof(EntityState) + ); +} + void PacketTest::testControl() { - Packet::TControl ctrl; - ctrl.ack = 10; + Packet::TControl ctrl{ 0, 10, 0 }; CPPUNIT_ASSERT_MESSAGE( "TControl should ack the packet in the ack field", @@ -149,22 +165,61 @@ void PacketTest::testPart() { void PacketTest::testPlayerUpdate() { auto pack = Packet::Make(udp_pack); - AssertPacket("PlayerUpdate", 4, 64, pack); + AssertPacket("PlayerUpdate", 4, 76, pack); + + EntityState write_state; + write_state.chunk_pos = { 7, 2, -3 }; + write_state.block_pos = { 1.5f, 0.9f, 12.0f }; + write_state.velocity = { 0.025f, 0.001f, 0.0f }; + write_state.orient = { 1.0f, 0.0f, 0.0f, 0.0f }; + write_state.ang_vel = { 0.01f, 0.00302f, 0.0985f }; + glm::vec3 write_movement(0.5f, -1.0f, 1.0f); + float write_pitch = 1.25f; + float write_yaw = -2.5f; + uint8_t write_actions = 0x05; + uint8_t write_slot = 3; + pack.WritePredictedState(write_state); + pack.WriteMovement(write_movement); + pack.WritePitch(write_pitch); + pack.WriteYaw(write_yaw); + pack.WriteActions(write_actions); + pack.WriteSlot(write_slot); - 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); + glm::vec3 read_movement; + float read_pitch; + float read_yaw; + uint8_t read_actions; + uint8_t read_slot; + pack.ReadPredictedState(read_state); + pack.ReadMovement(read_movement); + pack.ReadPitch(read_pitch); + pack.ReadYaw(read_yaw); + pack.ReadActions(read_actions); + pack.ReadSlot(read_slot); AssertEqual( - "player entity state not correctly transported in PlayerUpdate packet", - write_entity.GetState(), read_state + "player predicted entity state not correctly transported in PlayerUpdate packet", + write_state, read_state + ); + AssertEqual( + "player movement input not correctly transported in PlayerUpdate packet", + write_movement, read_movement, 0.0001f + ); + CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE( + "player pitch input not correctly transported in PlayerUpdate packet", + write_pitch, read_pitch, 0.0001f + ); + CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE( + "player yaw input not correctly transported in PlayerUpdate packet", + write_yaw, read_yaw, 0.0001f + ); + CPPUNIT_ASSERT_EQUAL_MESSAGE( + "player actions not correctly transported in PlayerUpdate packet", + int(write_actions), int(read_actions) + ); + CPPUNIT_ASSERT_EQUAL_MESSAGE( + "player inventory slot not correctly transported in PlayerUpdate packet", + int(write_slot), int(read_slot) ); } @@ -174,7 +229,7 @@ void PacketTest::testSpawnEntity() { Entity write_entity; write_entity.ID(534574); - CompositeModel model; + Model model; model.ID(23); model.Instantiate(write_entity.GetModel()); write_entity.GetState().chunk_pos = { 7, 2, -3 }; @@ -237,11 +292,11 @@ void PacketTest::testDespawnEntity() { void PacketTest::testEntityUpdate() { auto pack = Packet::Make(udp_pack); - AssertPacket("EntityUpdate", 7, 4, 452, pack); + AssertPacket("EntityUpdate", 7, 4, 480, pack); pack.length = Packet::EntityUpdate::GetSize(3); CPPUNIT_ASSERT_EQUAL_MESSAGE( - "length not correctly set in DespawnEntity packet", + "length not correctly set in EntityUpdate packet", size_t(4 + 3 * 68), pack.length ); @@ -395,6 +450,106 @@ void PacketTest::testChunkData() { ); } +void PacketTest::testBlockUpdate() { + auto pack = Packet::Make(udp_pack); + AssertPacket("BlockUpdate", 11, 16, 484, pack); + + pack.length = Packet::BlockUpdate::GetSize(3); + CPPUNIT_ASSERT_EQUAL_MESSAGE( + "length not correctly set in BlockUpdate packet", + size_t(16 + 3 * 6), pack.length + ); + + glm::ivec3 write_coords(432, -325, 99998); + uint32_t write_count = 3; + uint16_t write_index = 432; + Block write_block(324, Block::FACE_DOWN, Block::TURN_AROUND); + + pack.WriteChunkCoords(write_coords); + pack.WriteBlockCount(write_count); + pack.WriteIndex(write_index, 1); + pack.WriteBlock(write_block, 1); + pack.WriteIndex(write_index, 0); + pack.WriteBlock(write_block, 0); + pack.WriteIndex(write_index, 2); + pack.WriteBlock(write_block, 2); + + glm::ivec3 read_coords; + uint32_t read_count; + uint16_t read_index; + Block read_block; + + pack.ReadChunkCoords(read_coords); + pack.ReadBlockCount(read_count); + pack.ReadIndex(read_index, 1); + pack.ReadBlock(read_block, 1); + + AssertEqual( + "chunk coordinates not correctly transported in BlockUpdate packet", + write_coords, read_coords + ); + CPPUNIT_ASSERT_EQUAL_MESSAGE( + "block count not correctly transported in BlockUpdate packet", + write_count, read_count + ); + CPPUNIT_ASSERT_EQUAL_MESSAGE( + "block index not correctly transported in BlockUpdate packet", + write_index, read_index + ); + CPPUNIT_ASSERT_EQUAL_MESSAGE( + "block type not correctly transported in BlockUpdate packet", + write_block.type, read_block.type + ); + CPPUNIT_ASSERT_EQUAL_MESSAGE( + "block face not correctly transported in BlockUpdate packet", + write_block.GetFace(), read_block.GetFace() + ); + CPPUNIT_ASSERT_EQUAL_MESSAGE( + "block turn not correctly transported in BlockUpdate packet", + write_block.GetTurn(), read_block.GetTurn() + ); +} + +void PacketTest::testMessage() { + auto pack = Packet::Make(udp_pack); + AssertPacket("Message", 12, 6, 455, pack); + + const uint8_t write_type = 1; + const uint32_t write_ref = 6433235; + const string write_msg("hello, world"); + + pack.length = Packet::Message::GetSize(write_msg); + CPPUNIT_ASSERT_EQUAL_MESSAGE( + "length not correctly set in BlockUpdate packet", + size_t(5 + write_msg.size() + 1), pack.length + ); + + pack.WriteType(write_type); + pack.WriteReferral(write_ref); + pack.WriteMessage(write_msg); + + uint8_t read_type = 5; + uint32_t read_ref = 884373; + string read_msg; + + pack.ReadType(read_type); + pack.ReadReferral(read_ref); + pack.ReadMessage(read_msg); + + CPPUNIT_ASSERT_EQUAL_MESSAGE( + "type not correctly transported in Message packet", + write_type, read_type + ); + CPPUNIT_ASSERT_EQUAL_MESSAGE( + "referral not correctly transported in Message packet", + write_ref, read_ref + ); + CPPUNIT_ASSERT_EQUAL_MESSAGE( + "message not correctly transported in Message packet", + write_msg, read_msg + ); +} + void PacketTest::AssertPacket( const string &name, @@ -501,19 +656,20 @@ void PacketTest::AssertEqual( void PacketTest::AssertEqual( const string &message, const glm::vec3 &expected, - const glm::vec3 &actual + const glm::vec3 &actual, + float epsilon ) { CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE( message + " (X component)", - expected.x, actual.x, numeric_limits::epsilon() + expected.x, actual.x, epsilon ); CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE( message + " (Y component)", - expected.y, actual.y, numeric_limits::epsilon() + expected.y, actual.y, epsilon ); CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE( message + " (Z component)", - expected.z, actual.z, numeric_limits::epsilon() + expected.z, actual.z, epsilon ); }