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