3 Copyright (C) 2018 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
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.
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.
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.
20 #include "client/activeobjectmgr.h"
26 class TestClientActiveObject : public ClientActiveObject
29 TestClientActiveObject() : ClientActiveObject(0, nullptr, nullptr) {}
30 ~TestClientActiveObject() = default;
31 ActiveObjectType getType() const { return ACTIVEOBJECT_TYPE_TEST; }
34 class TestClientActiveObjectMgr : public TestBase
37 TestClientActiveObjectMgr() { TestManager::registerTestModule(this); }
38 const char *getName() { return "TestClientActiveObjectMgr"; }
40 void runTests(IGameDef *gamedef);
43 void testRegisterObject();
44 void testRemoveObject();
47 static TestClientActiveObjectMgr g_test_instance;
49 void TestClientActiveObjectMgr::runTests(IGameDef *gamedef)
52 TEST(testRegisterObject)
53 TEST(testRemoveObject)
56 ////////////////////////////////////////////////////////////////////////////////
58 void TestClientActiveObjectMgr::testFreeID()
60 client::ActiveObjectMgr caomgr;
61 std::vector<u16> aoids;
63 u16 aoid = caomgr.getFreeId();
64 // Ensure it's not the same id
65 UASSERT(caomgr.getFreeId() != aoid);
67 aoids.push_back(aoid);
69 // Register basic objects, ensure we never found
70 for (u8 i = 0; i < UINT8_MAX; i++) {
72 auto tcao = new TestClientActiveObject();
73 caomgr.registerObject(tcao);
74 aoids.push_back(tcao->getId());
76 // Ensure next id is not in registered list
77 UASSERT(std::find(aoids.begin(), aoids.end(), caomgr.getFreeId()) ==
84 void TestClientActiveObjectMgr::testRegisterObject()
86 client::ActiveObjectMgr caomgr;
87 auto tcao = new TestClientActiveObject();
88 UASSERT(caomgr.registerObject(tcao));
90 u16 id = tcao->getId();
92 auto tcaoToCompare = caomgr.getActiveObject(id);
93 UASSERT(tcaoToCompare->getId() == id);
94 UASSERT(tcaoToCompare == tcao);
96 tcao = new TestClientActiveObject();
97 UASSERT(caomgr.registerObject(tcao));
98 UASSERT(caomgr.getActiveObject(tcao->getId()) == tcao);
99 UASSERT(caomgr.getActiveObject(tcao->getId()) != tcaoToCompare);
104 void TestClientActiveObjectMgr::testRemoveObject()
106 client::ActiveObjectMgr caomgr;
107 auto tcao = new TestClientActiveObject();
108 UASSERT(caomgr.registerObject(tcao));
110 u16 id = tcao->getId();
111 UASSERT(caomgr.getActiveObject(id) != nullptr)
113 caomgr.removeObject(tcao->getId());
114 UASSERT(caomgr.getActiveObject(id) == nullptr)