]> git.lizzy.rs Git - dragonfireclient.git/blob - src/unittest/test_clientactiveobjectmgr.cpp
Require 'basic_debug' priv to view gameplay-relevant debug info, require 'debug'...
[dragonfireclient.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         virtual void addToScene(ITextureSource *tsrc, scene::ISceneManager *smgr) {}
33 };
34
35 class TestClientActiveObjectMgr : public TestBase
36 {
37 public:
38         TestClientActiveObjectMgr() { TestManager::registerTestModule(this); }
39         const char *getName() { return "TestClientActiveObjectMgr"; }
40
41         void runTests(IGameDef *gamedef);
42
43         void testFreeID();
44         void testRegisterObject();
45         void testRemoveObject();
46 };
47
48 static TestClientActiveObjectMgr g_test_instance;
49
50 void TestClientActiveObjectMgr::runTests(IGameDef *gamedef)
51 {
52         TEST(testFreeID);
53         TEST(testRegisterObject)
54         TEST(testRemoveObject)
55 }
56
57 ////////////////////////////////////////////////////////////////////////////////
58
59 void TestClientActiveObjectMgr::testFreeID()
60 {
61         client::ActiveObjectMgr caomgr;
62         std::vector<u16> aoids;
63
64         u16 aoid = caomgr.getFreeId();
65         // Ensure it's not the same id
66         UASSERT(caomgr.getFreeId() != aoid);
67
68         aoids.push_back(aoid);
69
70         // Register basic objects, ensure we never found
71         for (u8 i = 0; i < UINT8_MAX; i++) {
72                 // Register an object
73                 auto tcao = new TestClientActiveObject();
74                 caomgr.registerObject(tcao);
75                 aoids.push_back(tcao->getId());
76
77                 // Ensure next id is not in registered list
78                 UASSERT(std::find(aoids.begin(), aoids.end(), caomgr.getFreeId()) ==
79                                 aoids.end());
80         }
81
82         caomgr.clear();
83 }
84
85 void TestClientActiveObjectMgr::testRegisterObject()
86 {
87         client::ActiveObjectMgr caomgr;
88         auto tcao = new TestClientActiveObject();
89         UASSERT(caomgr.registerObject(tcao));
90
91         u16 id = tcao->getId();
92
93         auto tcaoToCompare = caomgr.getActiveObject(id);
94         UASSERT(tcaoToCompare->getId() == id);
95         UASSERT(tcaoToCompare == tcao);
96
97         tcao = new TestClientActiveObject();
98         UASSERT(caomgr.registerObject(tcao));
99         UASSERT(caomgr.getActiveObject(tcao->getId()) == tcao);
100         UASSERT(caomgr.getActiveObject(tcao->getId()) != tcaoToCompare);
101
102         caomgr.clear();
103 }
104
105 void TestClientActiveObjectMgr::testRemoveObject()
106 {
107         client::ActiveObjectMgr caomgr;
108         auto tcao = new TestClientActiveObject();
109         UASSERT(caomgr.registerObject(tcao));
110
111         u16 id = tcao->getId();
112         UASSERT(caomgr.getActiveObject(id) != nullptr)
113
114         caomgr.removeObject(tcao->getId());
115         UASSERT(caomgr.getActiveObject(id) == nullptr)
116
117         caomgr.clear();
118 }