Add an activeobject manager to hold active objects (#7939)
[oweals/minetest.git] / src / unittest / test_clientactiveobjectmgr.cpp
1 /*
2 Minetest
3 Copyright (C) 2018 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "client/activeobjectmgr.h"
21 #include <algorithm>
22 #include "test.h"
23
24 #include "profiler.h"
25
26 class TestClientActiveObject : public ClientActiveObject
27 {
28 public:
29         TestClientActiveObject() : ClientActiveObject(0, nullptr, nullptr) {}
30         ~TestClientActiveObject() = default;
31         ActiveObjectType getType() const { return ACTIVEOBJECT_TYPE_TEST; }
32 };
33
34 class TestClientActiveObjectMgr : public TestBase
35 {
36 public:
37         TestClientActiveObjectMgr() { TestManager::registerTestModule(this); }
38         const char *getName() { return "TestClientActiveObjectMgr"; }
39
40         void runTests(IGameDef *gamedef);
41
42         void testFreeID();
43         void testRegisterObject();
44         void testRemoveObject();
45 };
46
47 static TestClientActiveObjectMgr g_test_instance;
48
49 void TestClientActiveObjectMgr::runTests(IGameDef *gamedef)
50 {
51         TEST(testFreeID);
52         TEST(testRegisterObject)
53         TEST(testRemoveObject)
54 }
55
56 ////////////////////////////////////////////////////////////////////////////////
57
58 void TestClientActiveObjectMgr::testFreeID()
59 {
60         client::ActiveObjectMgr caomgr;
61         std::vector<u16> aoids;
62
63         u16 aoid = caomgr.getFreeId();
64         // Ensure it's not the same id
65         UASSERT(caomgr.getFreeId() != aoid);
66
67         aoids.push_back(aoid);
68
69         // Register basic objects, ensure we never found
70         for (u8 i = 0; i < UINT8_MAX; i++) {
71                 // Register an object
72                 auto tcao = new TestClientActiveObject();
73                 caomgr.registerObject(tcao);
74                 aoids.push_back(tcao->getId());
75
76                 // Ensure next id is not in registered list
77                 UASSERT(std::find(aoids.begin(), aoids.end(), caomgr.getFreeId()) ==
78                                 aoids.end());
79         }
80
81         caomgr.clear();
82 }
83
84 void TestClientActiveObjectMgr::testRegisterObject()
85 {
86         client::ActiveObjectMgr caomgr;
87         auto tcao = new TestClientActiveObject();
88         UASSERT(caomgr.registerObject(tcao));
89
90         u16 id = tcao->getId();
91
92         auto tcaoToCompare = caomgr.getActiveObject(id);
93         UASSERT(tcaoToCompare->getId() == id);
94         UASSERT(tcaoToCompare == tcao);
95
96         tcao = new TestClientActiveObject();
97         UASSERT(caomgr.registerObject(tcao));
98         UASSERT(caomgr.getActiveObject(tcao->getId()) == tcao);
99         UASSERT(caomgr.getActiveObject(tcao->getId()) != tcaoToCompare);
100
101         caomgr.clear();
102 }
103
104 void TestClientActiveObjectMgr::testRemoveObject()
105 {
106         client::ActiveObjectMgr caomgr;
107         auto tcao = new TestClientActiveObject();
108         UASSERT(caomgr.registerObject(tcao));
109
110         u16 id = tcao->getId();
111         UASSERT(caomgr.getActiveObject(id) != nullptr)
112
113         caomgr.removeObject(tcao->getId());
114         UASSERT(caomgr.getActiveObject(id) == nullptr)
115
116         caomgr.clear();
117 }