From 21035bf5d4e586a93bc282b09c8cb43f3653e1f6 Mon Sep 17 00:00:00 2001 From: numzero Date: Sat, 24 Jun 2023 20:52:39 +0300 Subject: [PATCH] Add unit test on client::ActiveObjectMgr::getActiveSelectableObjects --- src/unittest/test_clientactiveobjectmgr.cpp | 65 +++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/src/unittest/test_clientactiveobjectmgr.cpp b/src/unittest/test_clientactiveobjectmgr.cpp index 2d508cf32..c3ec40637 100644 --- a/src/unittest/test_clientactiveobjectmgr.cpp +++ b/src/unittest/test_clientactiveobjectmgr.cpp @@ -32,6 +32,24 @@ public: virtual void addToScene(ITextureSource *tsrc, scene::ISceneManager *smgr) {} }; +class TestSelectableClientActiveObject : public ClientActiveObject +{ +public: + TestSelectableClientActiveObject(aabb3f _selection_box) + : ClientActiveObject(0, nullptr, nullptr) + , selection_box(_selection_box) + {} + + ~TestSelectableClientActiveObject() = default; + ActiveObjectType getType() const override { return ACTIVEOBJECT_TYPE_TEST; } + void addToScene(ITextureSource *tsrc, scene::ISceneManager *smgr) override {} + bool getSelectionBox(aabb3f *toset) const override { *toset = selection_box; return true; } + const v3f getPosition() const override { return position; } + + v3f position; + aabb3f selection_box; +}; + class TestClientActiveObjectMgr : public TestBase { public: @@ -43,6 +61,7 @@ public: void testFreeID(); void testRegisterObject(); void testRemoveObject(); + void testGetActiveSelectableObjects(); }; static TestClientActiveObjectMgr g_test_instance; @@ -52,6 +71,7 @@ void TestClientActiveObjectMgr::runTests(IGameDef *gamedef) TEST(testFreeID); TEST(testRegisterObject) TEST(testRemoveObject) + TEST(testGetActiveSelectableObjects) } //////////////////////////////////////////////////////////////////////////////// @@ -116,3 +136,48 @@ void TestClientActiveObjectMgr::testRemoveObject() caomgr.clear(); } + +void TestClientActiveObjectMgr::testGetActiveSelectableObjects() +{ + client::ActiveObjectMgr caomgr; + auto obj = new TestSelectableClientActiveObject({v3f{-1, -1, -1}, v3f{1, 1, 1}}); + UASSERT(caomgr.registerObject(obj)); + + auto assert_obj_selected = [&] (v3f a, v3f b) { + auto actual = caomgr.getActiveSelectableObjects({a, b}); + UASSERTEQ(auto, actual.size(), 1u); + UASSERTEQ(auto, actual.at(0).obj, obj); + }; + + auto assert_obj_missed = [&] (v3f a, v3f b) { + auto actual = caomgr.getActiveSelectableObjects({a, b}); + UASSERTEQ(auto, actual.size(), 0u); + }; + + float x = 12, y = 3, z = 6; + obj->position = {x, y, z}; + + assert_obj_selected({0, 0, 0}, {x-1, y-1, z-1}); + assert_obj_selected({0, 0, 0}, {2*(x-1), 2*(y-1), 2*(z-1)}); + assert_obj_selected({0, 0, 0}, {2*(x+1), 2*(y-1), 2*(z+1)}); + assert_obj_selected({0, 0, 0}, {20, 5, 10}); + + assert_obj_selected({30, -12, 17}, {x+1, y+1, z-1}); + assert_obj_selected({30, -12, 17}, {x, y+1, z}); + assert_obj_selected({30, -12, 17}, {-6, 20, -5}); + assert_obj_selected({30, -12, 17}, {-8, 20, -7}); + + assert_obj_selected({-21, 6, -13}, {x+1.4f, y, z}); + assert_obj_selected({-21, 6, -13}, {x-1.4f, y, z}); + assert_obj_missed({-21, 6, -13}, {x-3.f, y, z}); + + assert_obj_selected({-21, 6, -13}, {x, y-1.4f, z}); + assert_obj_selected({-21, 6, -13}, {x, y+1.4f, z}); + assert_obj_missed({-21, 6, -13}, {x, y+3.f, z}); + + assert_obj_selected({-21, 6, -13}, {x, y, z+1.4f}); + assert_obj_selected({-21, 6, -13}, {x, y, z-1.4f}); + assert_obj_missed({-21, 6, -13}, {x, y, z-3.f}); + + caomgr.clear(); +}