Translated using Weblate (Italian)
[oweals/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 "objdef.h"
24
25 class TestObjDef : public TestBase
26 {
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         void testClone();
36 };
37
38 static TestObjDef g_test_instance;
39
40 void TestObjDef::runTests(IGameDef *gamedef)
41 {
42         TEST(testHandles);
43         TEST(testAddGetSetClear);
44         TEST(testClone);
45 }
46
47 ////////////////////////////////////////////////////////////////////////////////
48
49 /* Minimal implementation of ObjDef and ObjDefManager subclass */
50
51 class MyObjDef : public ObjDef
52 {
53 public:
54         ObjDef *clone() const
55         {
56                 auto def = new MyObjDef();
57                 ObjDef::cloneTo(def);
58                 def->testvalue = testvalue;
59                 return def;
60         };
61
62         u32 testvalue;
63 };
64
65 class MyObjDefManager : public ObjDefManager
66 {
67 public:
68         MyObjDefManager(ObjDefType type) : ObjDefManager(NULL, type){};
69         MyObjDefManager *clone() const
70         {
71                 auto mgr = new MyObjDefManager();
72                 ObjDefManager::cloneTo(mgr);
73                 return mgr;
74         };
75
76 protected:
77         MyObjDefManager(){};
78 };
79
80 void TestObjDef::testHandles()
81 {
82         u32 uid = 0;
83         u32 index = 0;
84         ObjDefType type = OBJDEF_GENERIC;
85
86         ObjDefHandle handle = ObjDefManager::createHandle(9530, OBJDEF_ORE, 47);
87
88         UASSERTEQ(ObjDefHandle, 0xAF507B55, handle);
89
90         UASSERT(ObjDefManager::decodeHandle(handle, &index, &type, &uid));
91
92         UASSERTEQ(u32, 9530, index);
93         UASSERTEQ(u32, 47, uid);
94         UASSERTEQ(ObjDefHandle, OBJDEF_ORE, type);
95 }
96
97 void TestObjDef::testAddGetSetClear()
98 {
99         ObjDefManager testmgr(NULL, OBJDEF_GENERIC);
100         ObjDefHandle hObj0, hObj1, hObj2, hObj3;
101         ObjDef *obj0, *obj1, *obj2, *obj3;
102
103         UASSERTEQ(ObjDefType, testmgr.getType(), OBJDEF_GENERIC);
104
105         obj0 = new MyObjDef;
106         obj0->name = "foobar";
107         hObj0 = testmgr.add(obj0);
108         UASSERT(hObj0 != OBJDEF_INVALID_HANDLE);
109         UASSERTEQ(u32, obj0->index, 0);
110
111         obj1 = new MyObjDef;
112         obj1->name = "FooBaz";
113         hObj1 = testmgr.add(obj1);
114         UASSERT(hObj1 != OBJDEF_INVALID_HANDLE);
115         UASSERTEQ(u32, obj1->index, 1);
116
117         obj2 = new MyObjDef;
118         obj2->name = "asdf";
119         hObj2 = testmgr.add(obj2);
120         UASSERT(hObj2 != OBJDEF_INVALID_HANDLE);
121         UASSERTEQ(u32, obj2->index, 2);
122
123         obj3 = new MyObjDef;
124         obj3->name = "foobaz";
125         hObj3 = testmgr.add(obj3);
126         UASSERT(hObj3 == OBJDEF_INVALID_HANDLE);
127
128         UASSERTEQ(size_t, testmgr.getNumObjects(), 3);
129
130         UASSERT(testmgr.get(hObj0) == obj0);
131         UASSERT(testmgr.getByName("FOOBAZ") == obj1);
132
133         UASSERT(testmgr.set(hObj0, obj3) == obj0);
134         UASSERT(testmgr.get(hObj0) == obj3);
135         delete obj0;
136
137         testmgr.clear();
138         UASSERTEQ(size_t, testmgr.getNumObjects(), 0);
139 }
140
141 void TestObjDef::testClone()
142 {
143         MyObjDefManager testmgr(OBJDEF_GENERIC);
144         ObjDefManager *mgrcopy;
145         MyObjDef *obj, *temp2;
146         ObjDef *temp1;
147         ObjDefHandle hObj;
148
149         obj = new MyObjDef;
150         obj->testvalue = 0xee00ff11;
151         hObj = testmgr.add(obj);
152         UASSERT(hObj != OBJDEF_INVALID_HANDLE);
153
154         mgrcopy = testmgr.clone();
155         UASSERT(mgrcopy);
156         UASSERTEQ(ObjDefType, mgrcopy->getType(), testmgr.getType());
157         UASSERTEQ(size_t, mgrcopy->getNumObjects(), testmgr.getNumObjects());
158
159         // 1) check that the same handle is still valid on the copy
160         temp1 = mgrcopy->get(hObj);
161         UASSERT(temp1);
162         UASSERT(temp1 == mgrcopy->getRaw(0));
163         // 2) check that the copy has the correct C++ class
164         temp2 = dynamic_cast<MyObjDef *>(temp1);
165         UASSERT(temp2);
166         // 3) check that it was correctly copied
167         UASSERTEQ(u32, obj->testvalue, temp2->testvalue);
168         // 4) check that it was copied AT ALL (not the same)
169         UASSERT(obj != temp2);
170
171         testmgr.clear();
172         mgrcopy->clear();
173         delete mgrcopy;
174 }