Skip to content

Commit 21035bf

Browse files
numberZerosfan5
authored andcommitted
Add unit test on client::ActiveObjectMgr::getActiveSelectableObjects
1 parent d7291e0 commit 21035bf

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

src/unittest/test_clientactiveobjectmgr.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,24 @@ class TestClientActiveObject : public ClientActiveObject
3232
virtual void addToScene(ITextureSource *tsrc, scene::ISceneManager *smgr) {}
3333
};
3434

35+
class TestSelectableClientActiveObject : public ClientActiveObject
36+
{
37+
public:
38+
TestSelectableClientActiveObject(aabb3f _selection_box)
39+
: ClientActiveObject(0, nullptr, nullptr)
40+
, selection_box(_selection_box)
41+
{}
42+
43+
~TestSelectableClientActiveObject() = default;
44+
ActiveObjectType getType() const override { return ACTIVEOBJECT_TYPE_TEST; }
45+
void addToScene(ITextureSource *tsrc, scene::ISceneManager *smgr) override {}
46+
bool getSelectionBox(aabb3f *toset) const override { *toset = selection_box; return true; }
47+
const v3f getPosition() const override { return position; }
48+
49+
v3f position;
50+
aabb3f selection_box;
51+
};
52+
3553
class TestClientActiveObjectMgr : public TestBase
3654
{
3755
public:
@@ -43,6 +61,7 @@ class TestClientActiveObjectMgr : public TestBase
4361
void testFreeID();
4462
void testRegisterObject();
4563
void testRemoveObject();
64+
void testGetActiveSelectableObjects();
4665
};
4766

4867
static TestClientActiveObjectMgr g_test_instance;
@@ -52,6 +71,7 @@ void TestClientActiveObjectMgr::runTests(IGameDef *gamedef)
5271
TEST(testFreeID);
5372
TEST(testRegisterObject)
5473
TEST(testRemoveObject)
74+
TEST(testGetActiveSelectableObjects)
5575
}
5676

5777
////////////////////////////////////////////////////////////////////////////////
@@ -116,3 +136,48 @@ void TestClientActiveObjectMgr::testRemoveObject()
116136

117137
caomgr.clear();
118138
}
139+
140+
void TestClientActiveObjectMgr::testGetActiveSelectableObjects()
141+
{
142+
client::ActiveObjectMgr caomgr;
143+
auto obj = new TestSelectableClientActiveObject({v3f{-1, -1, -1}, v3f{1, 1, 1}});
144+
UASSERT(caomgr.registerObject(obj));
145+
146+
auto assert_obj_selected = [&] (v3f a, v3f b) {
147+
auto actual = caomgr.getActiveSelectableObjects({a, b});
148+
UASSERTEQ(auto, actual.size(), 1u);
149+
UASSERTEQ(auto, actual.at(0).obj, obj);
150+
};
151+
152+
auto assert_obj_missed = [&] (v3f a, v3f b) {
153+
auto actual = caomgr.getActiveSelectableObjects({a, b});
154+
UASSERTEQ(auto, actual.size(), 0u);
155+
};
156+
157+
float x = 12, y = 3, z = 6;
158+
obj->position = {x, y, z};
159+
160+
assert_obj_selected({0, 0, 0}, {x-1, y-1, z-1});
161+
assert_obj_selected({0, 0, 0}, {2*(x-1), 2*(y-1), 2*(z-1)});
162+
assert_obj_selected({0, 0, 0}, {2*(x+1), 2*(y-1), 2*(z+1)});
163+
assert_obj_selected({0, 0, 0}, {20, 5, 10});
164+
165+
assert_obj_selected({30, -12, 17}, {x+1, y+1, z-1});
166+
assert_obj_selected({30, -12, 17}, {x, y+1, z});
167+
assert_obj_selected({30, -12, 17}, {-6, 20, -5});
168+
assert_obj_selected({30, -12, 17}, {-8, 20, -7});
169+
170+
assert_obj_selected({-21, 6, -13}, {x+1.4f, y, z});
171+
assert_obj_selected({-21, 6, -13}, {x-1.4f, y, z});
172+
assert_obj_missed({-21, 6, -13}, {x-3.f, y, z});
173+
174+
assert_obj_selected({-21, 6, -13}, {x, y-1.4f, z});
175+
assert_obj_selected({-21, 6, -13}, {x, y+1.4f, z});
176+
assert_obj_missed({-21, 6, -13}, {x, y+3.f, z});
177+
178+
assert_obj_selected({-21, 6, -13}, {x, y, z+1.4f});
179+
assert_obj_selected({-21, 6, -13}, {x, y, z-1.4f});
180+
assert_obj_missed({-21, 6, -13}, {x, y, z-3.f});
181+
182+
caomgr.clear();
183+
}

0 commit comments

Comments
 (0)