]> git.lizzy.rs Git - minetest.git/blob - src/unittest/test_objdef.cpp
Tests: Add ObjDef unittests
[minetest.git] / src / unittest / test_objdef.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2014 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
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 "test.h"
21
22 #include "exceptions.h"
23 #include "mapgen.h"
24
25
26 class TestObjDef : public TestBase {
27 public:
28         TestObjDef() { TestManager::registerTestModule(this); }
29         const char *getName() { return "TestObjDef"; }
30
31         void runTests(IGameDef *gamedef);
32
33         void testHandles();
34         void testAddGetSetClear();
35 };
36
37 static TestObjDef g_test_instance;
38
39 void TestObjDef::runTests(IGameDef *gamedef)
40 {
41         TEST(testHandles);
42         TEST(testAddGetSetClear);
43 }
44
45 ////////////////////////////////////////////////////////////////////////////////
46
47 void TestObjDef::testHandles()
48 {
49         u32 uid = 0;
50         u32 index = 0;
51         ObjDefType type = OBJDEF_GENERIC;
52
53         ObjDefHandle handle = ObjDefManager::createHandle(9530, OBJDEF_ORE, 47);
54
55         UASSERTEQ(ObjDefHandle, 0xAF507B55, handle);
56
57         UASSERT(ObjDefManager::decodeHandle(handle, &index, &type, &uid));
58
59         UASSERTEQ(u32, 9530, index);
60         UASSERTEQ(u32, 47, uid);
61         UASSERTEQ(ObjDefHandle, OBJDEF_ORE, type);
62 }
63
64
65 void TestObjDef::testAddGetSetClear()
66 {
67         ObjDefManager testmgr(NULL, OBJDEF_GENERIC);
68         ObjDefHandle hObj0, hObj1, hObj2, hObj3;
69         ObjDef *obj0, *obj1, *obj2, *obj3;
70
71         UASSERTEQ(ObjDefType, testmgr.getType(), OBJDEF_GENERIC);
72
73         obj0 = new ObjDef;
74         obj0->name = "foobar";
75         hObj0 = testmgr.add(obj0);
76         UASSERT(hObj0 != OBJDEF_INVALID_HANDLE);
77         UASSERTEQ(u32, obj0->index, 0);
78
79         obj1 = new ObjDef;
80         obj1->name = "FooBaz";
81         hObj1 = testmgr.add(obj1);
82         UASSERT(hObj1 != OBJDEF_INVALID_HANDLE);
83         UASSERTEQ(u32, obj1->index, 1);
84
85         obj2 = new ObjDef;
86         obj2->name = "asdf";
87         hObj2 = testmgr.add(obj2);
88         UASSERT(hObj2 != OBJDEF_INVALID_HANDLE);
89         UASSERTEQ(u32, obj2->index, 2);
90
91         obj3 = new ObjDef;
92         obj3->name = "foobaz";
93         hObj3 = testmgr.add(obj3);
94         UASSERT(hObj3 == OBJDEF_INVALID_HANDLE);
95
96         UASSERTEQ(size_t, testmgr.getNumObjects(), 3);
97
98         UASSERT(testmgr.get(hObj0) == obj0);
99         UASSERT(testmgr.getByName("FOOBAZ") == obj1);
100
101         UASSERT(testmgr.set(hObj0, obj3) == obj0);
102         UASSERT(testmgr.get(hObj0) == obj3);
103         delete obj0;
104
105         testmgr.clear();
106         UASSERTEQ(size_t, testmgr.getNumObjects(), 0);
107 }