From 3fa782383ef514b0dffd3ad24e664c9924b4dc70 Mon Sep 17 00:00:00 2001 From: Josh Yelon Date: Tue, 3 Aug 2021 12:40:12 -0400 Subject: [PATCH] more unit testing of diff xmit --- luprex/core/cpp/animqueue.cpp | 38 +++++++++++------- luprex/core/cpp/animqueue.hpp | 5 ++- luprex/core/cpp/world.cpp | 75 +++++++++++++++++++++++++++++++---- luprex/core/cpp/world.hpp | 10 ++++- 4 files changed, 104 insertions(+), 24 deletions(-) diff --git a/luprex/core/cpp/animqueue.cpp b/luprex/core/cpp/animqueue.cpp index 98b3ecb1..9f563eb2 100644 --- a/luprex/core/cpp/animqueue.cpp +++ b/luprex/core/cpp/animqueue.cpp @@ -415,13 +415,21 @@ bool AnimQueue::valid() const { return true; } -std::string AnimQueue::debug_string() const { +std::string AnimQueue::steps_debug_string() const { std::ostringstream oss; + for (int i = 0; i < int(size()); i++) { + oss << nth(i).debug_string() << "; "; + } + return oss.str(); +} + +std::string AnimQueue::full_debug_string() const { + std::ostringstream oss; + for (int i = 0; i < int(size()); i++) { + oss << nth(i).debug_string() << "; "; + } oss << "version=" << version_number(); oss << "; limit=" << size_limit(); - for (int i = 0; i < int(size()); i++) { - oss << "; " << nth(i).debug_string(); - } return oss.str(); } @@ -553,7 +561,7 @@ LuaDefine(unittests_animqueue, "c") { // Debug string of a newly initialized queue LuaAssert(L, aq.valid()); - LuaAssertStrEq(L, aq.debug_string(), "version=1; limit=10; id=0 action= plane= x=0 y=0 z=0 facing=0 graphic="); + LuaAssertStrEq(L, aq.full_debug_string(), "id=0 action= plane= x=0 y=0 z=0 facing=0 graphic=; version=1; limit=10"); // Test the step setters. stp.clear(); @@ -578,16 +586,16 @@ LuaDefine(unittests_animqueue, "c") { // Test that we can clear a queue. aq.full_clear_and_set_limit(3); LuaAssert(L, aq.valid()); - LuaAssertStrEq(L, aq.debug_string(), "version=1; limit=3; id=0 action= plane= x=0 y=0 z=0 facing=0 graphic="); + LuaAssertStrEq(L, aq.full_debug_string(), "id=0 action= plane= x=0 y=0 z=0 facing=0 graphic=; version=1; limit=3"); // Add a step to a queue. aq.full_clear_and_set_limit(3); LuaAssert(L, stp.from_string("action=walk")); aq.add(12345, stp); - LuaAssertStrEq(L, aq.debug_string(), - "version=2; limit=3; " + LuaAssertStrEq(L, aq.full_debug_string(), "id=0 action= plane= x=0 y=0 z=0 facing=0 graphic=; " - "id=12345 action=walk"); + "id=12345 action=walk; " + "version=2; limit=3"); // Exceed the length limit, dropping first element. aq.full_clear_and_set_limit(3); @@ -595,11 +603,11 @@ LuaDefine(unittests_animqueue, "c") { aq.add(12345, stp); aq.add(12346, stp); aq.add(12347, stp); - LuaAssertStrEq(L, aq.debug_string(), - "version=4; limit=3; " + LuaAssertStrEq(L, aq.full_debug_string(), "id=0 action= plane=foo x=0 y=0 z=0 facing=0 graphic=; " "id=12346 action=walk plane=foo; " - "id=12347 action=walk plane=foo" + "id=12347 action=walk plane=foo; " + "version=4; limit=3" ); // Test serialization and deserialization. @@ -614,12 +622,12 @@ LuaDefine(unittests_animqueue, "c") { aq.serialize(&sb); aqds.deserialize(&sb); - LuaAssertStrEq(L, aqds.debug_string(), - "version=0; limit=5; " + LuaAssertStrEq(L, aqds.full_debug_string(), "id=0 action= plane= x=0 y=0 z=0 facing=0 graphic=; " "id=12345 action=walk x=3 y=4 z=5; " "id=12346 action=setgraphic graphic=banana; " - "id=12347 action=setfacing facing=301" + "id=12347 action=setfacing facing=301; " + "version=0; limit=5" ); // Test difference transmission diff --git a/luprex/core/cpp/animqueue.hpp b/luprex/core/cpp/animqueue.hpp index d202f553..fcf0bfcc 100644 --- a/luprex/core/cpp/animqueue.hpp +++ b/luprex/core/cpp/animqueue.hpp @@ -216,7 +216,10 @@ public: bool valid() const; // Convert to a string for debugging purposes. - std::string debug_string() const; + std::string steps_debug_string() const; + std::string full_debug_string() const; + + // Convert to a private: bool version_autoinc_; diff --git a/luprex/core/cpp/world.cpp b/luprex/core/cpp/world.cpp index fb3cbf89..4b716b02 100644 --- a/luprex/core/cpp/world.cpp +++ b/luprex/core/cpp/world.cpp @@ -126,14 +126,16 @@ Tangible *World::tangible_get(lua_State *L, int idx) { return result; } -void World::tangible_delete(lua_State *L, int64_t id) { +void World::tangible_delete(int64_t id) { + lua_State *L = state(); LuaVar tangibles, database; LuaStack LS(L, tangibles, database); // Fetch the C++ side of the tangible. auto iter = tangibles_.find(id); if (iter == tangibles_.end()) { - luaL_error(L, "Not a tangible: %lld", id); + LS.result(); + return; // Nothing to delete. } // Fetch the lua side of the tangible. @@ -145,10 +147,10 @@ void World::tangible_delete(lua_State *L, int64_t id) { LS.clearmetatable(database); LS.cleartable(database); - // Remove the lua portion from the table. + // Remove the lua portion from the tangibles table. LS.rawset(tangibles, id, LuaNil); - // Remove the C++ portion from the table. + // Remove the C++ portion from the tangibles table. tangibles_.erase(iter); LS.result(); } @@ -166,6 +168,13 @@ void World::tangible_walkto(int64_t id, int64_t animid, float x, float y) { } +std::string World::tangible_anim_debug_string(int64_t id) const { + const Tangible *t = tangible_get(id); + if (t == 0) return "no such tangible"; + return t->anim_queue_.steps_debug_string(); +} + + std::string World::tangible_ids_debug_string() const { util::IdVector idv; for (const auto &pair : tangibles_) { @@ -664,7 +673,7 @@ void World::patch_visible_animations(StreamBuffer *sb) { count = sb->read_int32(); for (int i = 0; i < count; i++) { int64_t id = sb->read_int64(); - tangible_delete(state(), id); + tangible_delete(id); } // Receive update messages @@ -727,11 +736,12 @@ LuaDefine(tangible_delete, "c") { LuaStack LS(L, tanobj); World *w = World::fetch_global_pointer(L); Tangible *tan = w->tangible_get(L, tanobj.index()); + assert(tan != nullptr); // this should be checked above. if (tan->is_an_actor()) { luaL_error(L, "Cannot delete a player using tangible.delete, use tangible.redirect instead."); return 0; } - w->tangible_delete(L, tan->id()); + w->tangible_delete(tan->id()); return LS.result(); } @@ -811,7 +821,7 @@ LuaDefine(tangible_redirect, "c") { w->redirects_[tan1->id()] = tan2->id(); } if (bulldoze) { - w->tangible_delete(L, tan1->id()); + w->tangible_delete(tan1->id()); } return LS.result(); } @@ -843,14 +853,65 @@ LuaDefine(unittests_world, "c") { ss.reset(new World(util::WORLD_TYPE_S_SYNC)); cs.reset(new World(util::WORLD_TYPE_C_SYNC)); + // Create some tangibles, and add some animations. m->tangible_make(0, 123, false); m->tangible_make(0, 345, false); + m->tangible_walkto(123, 770, 3, 4); + m->tangible_walkto(345, 771, 6, 2); LuaAssertStrEq(L, m->tangible_ids_debug_string(), "123,345"); + LuaAssertStrEq(L, m->tangible_anim_debug_string(123), + "id=0 action= plane= x=0 y=0 z=0 facing=0 graphic=; " + "id=770 action=walkto x=3 y=4; "); + LuaAssertStrEq(L, m->tangible_anim_debug_string(345), + "id=0 action= plane= x=0 y=0 z=0 facing=0 graphic=; " + "id=771 action=walkto x=6 y=2; "); + // Now difference transmit all that to the client. ss->difference_transmit(123, m.get(), &sb); cs->apply_differences(&sb); LuaAssertStrEq(L, ss->tangible_ids_debug_string(), "123,345"); + LuaAssertStrEq(L, ss->tangible_anim_debug_string(123), + "id=0 action= plane= x=0 y=0 z=0 facing=0 graphic=; " + "id=770 action=walkto x=3 y=4; "); + LuaAssertStrEq(L, ss->tangible_anim_debug_string(345), + "id=0 action= plane= x=0 y=0 z=0 facing=0 graphic=; " + "id=771 action=walkto x=6 y=2; "); LuaAssert(L, worlds_identical(ss, cs)); + // Now add some more animation records to the master. + m->tangible_walkto(123, 772, 7, 3); + m->tangible_walkto(345, 773, 2, 5); + LuaAssertStrEq(L, m->tangible_anim_debug_string(123), + "id=0 action= plane= x=0 y=0 z=0 facing=0 graphic=; " + "id=770 action=walkto x=3 y=4; " + "id=772 action=walkto x=7 y=3; "); + LuaAssertStrEq(L, m->tangible_anim_debug_string(345), + "id=0 action= plane= x=0 y=0 z=0 facing=0 graphic=; " + "id=771 action=walkto x=6 y=2; " + "id=773 action=walkto x=2 y=5; "); + + // Now difference transmit all that to the client again. + ss->difference_transmit(123, m.get(), &sb); + cs->apply_differences(&sb); + LuaAssertStrEq(L, ss->tangible_anim_debug_string(123), + "id=0 action= plane= x=0 y=0 z=0 facing=0 graphic=; " + "id=770 action=walkto x=3 y=4; " + "id=772 action=walkto x=7 y=3; "); + LuaAssertStrEq(L, ss->tangible_anim_debug_string(345), + "id=0 action= plane= x=0 y=0 z=0 facing=0 graphic=; " + "id=771 action=walkto x=6 y=2; " + "id=773 action=walkto x=2 y=5; "); + LuaAssert(L, worlds_identical(ss, cs)); + + // Delete tangible 345. + m->tangible_delete(345); + LuaAssertStrEq(L, m->tangible_ids_debug_string(), "123"); + + // And difference transmit + ss->difference_transmit(123, m.get(), &sb); + cs->apply_differences(&sb); + LuaAssertStrEq(L, ss->tangible_ids_debug_string(), "123"); + LuaAssert(L, worlds_identical(ss, cs)); + return 0; } \ No newline at end of file diff --git a/luprex/core/cpp/world.hpp b/luprex/core/cpp/world.hpp index 7ec2b77f..950a2d22 100644 --- a/luprex/core/cpp/world.hpp +++ b/luprex/core/cpp/world.hpp @@ -117,6 +117,8 @@ public: // Get a pointer to the specified tangible. // + // If there's no such tangible, returns nullptr. + // Tangible *tangible_get(int64_t id); const Tangible *tangible_get(int64_t id) const; @@ -133,7 +135,9 @@ public: // Delete the specified tangible. // - void tangible_delete(lua_State *L, int64_t id); + // If there's no such tangible, this is a no-op. + // + void tangible_delete(int64_t id); // Create a login actor. // @@ -212,6 +216,10 @@ public: // void tangible_walkto(int64_t id, int64_t animid, float x, float y); + // Get the tangible's animation queue as a debug string. + // + std::string tangible_anim_debug_string(int64_t id) const; + // Get a list of all existing tangibles as a comma-separated string. // std::string tangible_ids_debug_string() const;