Tests: Add ObjDef unittests
[oweals/minetest.git] / src / unittest / test.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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 "debug.h"
23 #include "log.h"
24 #include "nodedef.h"
25 #include "itemdef.h"
26 #include "gamedef.h"
27
28 content_t CONTENT_STONE;
29 content_t CONTENT_GRASS;
30 content_t CONTENT_TORCH;
31
32 ////////////////////////////////////////////////////////////////////////////////
33
34 ////
35 //// TestGameDef
36 ////
37
38 class TestGameDef : public IGameDef {
39 public:
40         TestGameDef();
41         ~TestGameDef();
42
43         IItemDefManager *getItemDefManager() { return m_itemdef; }
44         INodeDefManager *getNodeDefManager() { return m_nodedef; }
45         ICraftDefManager *getCraftDefManager() { return m_craftdef; }
46         ITextureSource *getTextureSource() { return m_texturesrc; }
47         IShaderSource *getShaderSource() { return m_shadersrc; }
48         ISoundManager *getSoundManager() { return m_soundmgr; }
49         MtEventManager *getEventManager() { return m_eventmgr; }
50         scene::ISceneManager *getSceneManager() { return m_scenemgr; }
51         IRollbackManager *getRollbackManager() { return m_rollbackmgr; }
52         EmergeManager *getEmergeManager() { return m_emergemgr; }
53
54         scene::IAnimatedMesh *getMesh(const std::string &filename) { return NULL; }
55         bool checkLocalPrivilege(const std::string &priv) { return false; }
56         u16 allocateUnknownNodeId(const std::string &name) { return 0; }
57
58         void defineSomeNodes();
59
60 private:
61         IItemDefManager *m_itemdef;
62         INodeDefManager *m_nodedef;
63         ICraftDefManager *m_craftdef;
64         ITextureSource *m_texturesrc;
65         IShaderSource *m_shadersrc;
66         ISoundManager *m_soundmgr;
67         MtEventManager *m_eventmgr;
68         scene::ISceneManager *m_scenemgr;
69         IRollbackManager *m_rollbackmgr;
70         EmergeManager *m_emergemgr;
71 };
72
73
74 TestGameDef::TestGameDef()
75 {
76         m_itemdef = createItemDefManager();
77         m_nodedef = createNodeDefManager();
78
79         defineSomeNodes();
80 }
81
82
83 TestGameDef::~TestGameDef()
84 {
85         delete m_itemdef;
86         delete m_nodedef;
87 }
88
89
90 void TestGameDef::defineSomeNodes()
91 {
92         IWritableItemDefManager *idef = (IWritableItemDefManager *)m_itemdef;
93         IWritableNodeDefManager *ndef = (IWritableNodeDefManager *)m_nodedef;
94
95         ItemDefinition itemdef;
96         ContentFeatures f;
97
98         //// Stone
99         itemdef = ItemDefinition();
100         itemdef.type = ITEM_NODE;
101         itemdef.name = "default:stone";
102         itemdef.description = "Stone";
103         itemdef.groups["cracky"] = 3;
104         itemdef.inventory_image = "[inventorycube"
105                 "{default_stone.png"
106                 "{default_stone.png"
107                 "{default_stone.png";
108         f = ContentFeatures();
109         f.name = itemdef.name;
110         for(int i = 0; i < 6; i++)
111                 f.tiledef[i].name = "default_stone.png";
112         f.is_ground_content = true;
113         idef->registerItem(itemdef);
114         CONTENT_STONE = ndef->set(f.name, f);
115
116         //// Grass
117         itemdef = ItemDefinition();
118         itemdef.type = ITEM_NODE;
119         itemdef.name = "default:dirt_with_grass";
120         itemdef.description = "Dirt with grass";
121         itemdef.groups["crumbly"] = 3;
122         itemdef.inventory_image = "[inventorycube"
123                 "{default_grass.png"
124                 "{default_dirt.png&default_grass_side.png"
125                 "{default_dirt.png&default_grass_side.png";
126         f = ContentFeatures();
127         f.name = itemdef.name;
128         f.tiledef[0].name = "default_grass.png";
129         f.tiledef[1].name = "default_dirt.png";
130         for(int i = 2; i < 6; i++)
131                 f.tiledef[i].name = "default_dirt.png^default_grass_side.png";
132         f.is_ground_content = true;
133         idef->registerItem(itemdef);
134         CONTENT_GRASS = ndef->set(f.name, f);
135
136         //// Torch (minimal definition for lighting tests)
137         itemdef = ItemDefinition();
138         itemdef.type = ITEM_NODE;
139         itemdef.name = "default:torch";
140         f = ContentFeatures();
141         f.name = itemdef.name;
142         f.param_type = CPT_LIGHT;
143         f.light_propagates = true;
144         f.sunlight_propagates = true;
145         f.light_source = LIGHT_MAX-1;
146         idef->registerItem(itemdef);
147         CONTENT_TORCH = ndef->set(f.name, f);
148 }
149
150 ////
151 //// run_tests
152 ////
153
154 void run_tests()
155 {
156         DSTACK(__FUNCTION_NAME);
157
158         u32 t1 = porting::getTime(PRECISION_MILLI);
159         TestGameDef gamedef;
160
161         log_set_lev_silence(LMT_ERROR, true);
162
163         u32 num_modules_failed     = 0;
164         u32 num_total_tests_failed = 0;
165         u32 num_total_tests_run    = 0;
166         std::vector<TestBase *> &testmods = TestManager::getTestModules();
167         for (size_t i = 0; i != testmods.size(); i++) {
168                 if (!testmods[i]->testModule(&gamedef))
169                         num_modules_failed++;
170
171                 num_total_tests_failed += testmods[i]->num_tests_failed;
172                 num_total_tests_run += testmods[i]->num_tests_run;
173         }
174
175         u32 tdiff = porting::getTime(PRECISION_MILLI) - t1;
176
177         log_set_lev_silence(LMT_ERROR, false);
178
179         const char *overall_status = (num_modules_failed == 0) ? "PASSED" : "FAILED";
180
181         dstream
182                 << "++++++++++++++++++++++++++++++++++++++++"
183                 << "++++++++++++++++++++++++++++++++++++++++" << std::endl
184                 << "Unit Test Results: " << overall_status << std::endl
185                 << "    " << num_modules_failed << " / " << testmods.size()
186                 << " failed modules (" << num_total_tests_failed << " / "
187                 << num_total_tests_run << " failed individual tests)." << std::endl
188                 << "    Testing took " << tdiff << "ms total." << std::endl
189                 << "++++++++++++++++++++++++++++++++++++++++"
190                 << "++++++++++++++++++++++++++++++++++++++++" << std::endl;
191
192         if (num_modules_failed)
193                 abort();
194 }
195
196 ////
197 //// TestBase
198 ////
199
200 bool TestBase::testModule(IGameDef *gamedef)
201 {
202         dstream << "======== Testing module " << getName() << std::endl;
203         u32 t1 = porting::getTime(PRECISION_MILLI);
204
205
206         runTests(gamedef);
207
208         u32 tdiff = porting::getTime(PRECISION_MILLI) - t1;
209         dstream << "======== Module " << getName() << " "
210                 << (num_tests_failed ? "failed" : "passed") << " (" << num_tests_failed
211                 << " failures / " << num_tests_run << " tests) - " << tdiff
212                 << "ms" << std::endl;
213
214         return num_tests_failed == 0;
215 }
216
217
218 /*
219         NOTE: These tests became non-working then NodeContainer was removed.
220               These should be redone, utilizing some kind of a virtual
221                   interface for Map (IMap would be fine).
222 */
223 #if 0
224 struct TestMapBlock: public TestBase
225 {
226         class TC : public NodeContainer
227         {
228         public:
229
230                 MapNode node;
231                 bool position_valid;
232                 core::list<v3s16> validity_exceptions;
233
234                 TC()
235                 {
236                         position_valid = true;
237                 }
238
239                 virtual bool isValidPosition(v3s16 p)
240                 {
241                         //return position_valid ^ (p==position_valid_exception);
242                         bool exception = false;
243                         for(core::list<v3s16>::Iterator i=validity_exceptions.begin();
244                                         i != validity_exceptions.end(); i++)
245                         {
246                                 if(p == *i)
247                                 {
248                                         exception = true;
249                                         break;
250                                 }
251                         }
252                         return exception ? !position_valid : position_valid;
253                 }
254
255                 virtual MapNode getNode(v3s16 p)
256                 {
257                         if(isValidPosition(p) == false)
258                                 throw InvalidPositionException();
259                         return node;
260                 }
261
262                 virtual void setNode(v3s16 p, MapNode & n)
263                 {
264                         if(isValidPosition(p) == false)
265                                 throw InvalidPositionException();
266                 };
267
268                 virtual u16 nodeContainerId() const
269                 {
270                         return 666;
271                 }
272         };
273
274         void Run()
275         {
276                 TC parent;
277
278                 MapBlock b(&parent, v3s16(1,1,1));
279                 v3s16 relpos(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
280
281                 UASSERT(b.getPosRelative() == relpos);
282
283                 UASSERT(b.getBox().MinEdge.X == MAP_BLOCKSIZE);
284                 UASSERT(b.getBox().MaxEdge.X == MAP_BLOCKSIZE*2-1);
285                 UASSERT(b.getBox().MinEdge.Y == MAP_BLOCKSIZE);
286                 UASSERT(b.getBox().MaxEdge.Y == MAP_BLOCKSIZE*2-1);
287                 UASSERT(b.getBox().MinEdge.Z == MAP_BLOCKSIZE);
288                 UASSERT(b.getBox().MaxEdge.Z == MAP_BLOCKSIZE*2-1);
289
290                 UASSERT(b.isValidPosition(v3s16(0,0,0)) == true);
291                 UASSERT(b.isValidPosition(v3s16(-1,0,0)) == false);
292                 UASSERT(b.isValidPosition(v3s16(-1,-142,-2341)) == false);
293                 UASSERT(b.isValidPosition(v3s16(-124,142,2341)) == false);
294                 UASSERT(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
295                 UASSERT(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE,MAP_BLOCKSIZE-1)) == false);
296
297                 /*
298                         TODO: this method should probably be removed
299                         if the block size isn't going to be set variable
300                 */
301                 /*UASSERT(b.getSizeNodes() == v3s16(MAP_BLOCKSIZE,
302                                 MAP_BLOCKSIZE, MAP_BLOCKSIZE));*/
303
304                 // Changed flag should be initially set
305                 UASSERT(b.getModified() == MOD_STATE_WRITE_NEEDED);
306                 b.resetModified();
307                 UASSERT(b.getModified() == MOD_STATE_CLEAN);
308
309                 // All nodes should have been set to
310                 // .d=CONTENT_IGNORE and .getLight() = 0
311                 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
312                 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
313                 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
314                 {
315                         //UASSERT(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_AIR);
316                         UASSERT(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_IGNORE);
317                         UASSERT(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_DAY) == 0);
318                         UASSERT(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_NIGHT) == 0);
319                 }
320
321                 {
322                         MapNode n(CONTENT_AIR);
323                         for(u16 z=0; z<MAP_BLOCKSIZE; z++)
324                         for(u16 y=0; y<MAP_BLOCKSIZE; y++)
325                         for(u16 x=0; x<MAP_BLOCKSIZE; x++)
326                         {
327                                 b.setNode(v3s16(x,y,z), n);
328                         }
329                 }
330
331                 /*
332                         Parent fetch functions
333                 */
334                 parent.position_valid = false;
335                 parent.node.setContent(5);
336
337                 MapNode n;
338
339                 // Positions in the block should still be valid
340                 UASSERT(b.isValidPositionParent(v3s16(0,0,0)) == true);
341                 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
342                 n = b.getNodeParent(v3s16(0,MAP_BLOCKSIZE-1,0));
343                 UASSERT(n.getContent() == CONTENT_AIR);
344
345                 // ...but outside the block they should be invalid
346                 UASSERT(b.isValidPositionParent(v3s16(-121,2341,0)) == false);
347                 UASSERT(b.isValidPositionParent(v3s16(-1,0,0)) == false);
348                 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == false);
349
350                 {
351                         bool exception_thrown = false;
352                         try{
353                                 // This should throw an exception
354                                 MapNode n = b.getNodeParent(v3s16(0,0,-1));
355                         }
356                         catch(InvalidPositionException &e)
357                         {
358                                 exception_thrown = true;
359                         }
360                         UASSERT(exception_thrown);
361                 }
362
363                 parent.position_valid = true;
364                 // Now the positions outside should be valid
365                 UASSERT(b.isValidPositionParent(v3s16(-121,2341,0)) == true);
366                 UASSERT(b.isValidPositionParent(v3s16(-1,0,0)) == true);
367                 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == true);
368                 n = b.getNodeParent(v3s16(0,0,MAP_BLOCKSIZE));
369                 UASSERT(n.getContent() == 5);
370
371                 /*
372                         Set a node
373                 */
374                 v3s16 p(1,2,0);
375                 n.setContent(4);
376                 b.setNode(p, n);
377                 UASSERT(b.getNode(p).getContent() == 4);
378                 //TODO: Update to new system
379                 /*UASSERT(b.getNodeTile(p) == 4);
380                 UASSERT(b.getNodeTile(v3s16(-1,-1,0)) == 5);*/
381
382                 /*
383                         propagateSunlight()
384                 */
385                 // Set lighting of all nodes to 0
386                 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
387                         for(u16 y=0; y<MAP_BLOCKSIZE; y++){
388                                 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
389                                         MapNode n = b.getNode(v3s16(x,y,z));
390                                         n.setLight(LIGHTBANK_DAY, 0);
391                                         n.setLight(LIGHTBANK_NIGHT, 0);
392                                         b.setNode(v3s16(x,y,z), n);
393                                 }
394                         }
395                 }
396                 {
397                         /*
398                                 Check how the block handles being a lonely sky block
399                         */
400                         parent.position_valid = true;
401                         b.setIsUnderground(false);
402                         parent.node.setContent(CONTENT_AIR);
403                         parent.node.setLight(LIGHTBANK_DAY, LIGHT_SUN);
404                         parent.node.setLight(LIGHTBANK_NIGHT, 0);
405                         core::map<v3s16, bool> light_sources;
406                         // The bottom block is invalid, because we have a shadowing node
407                         UASSERT(b.propagateSunlight(light_sources) == false);
408                         UASSERT(b.getNode(v3s16(1,4,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
409                         UASSERT(b.getNode(v3s16(1,3,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
410                         UASSERT(b.getNode(v3s16(1,2,0)).getLight(LIGHTBANK_DAY) == 0);
411                         UASSERT(b.getNode(v3s16(1,1,0)).getLight(LIGHTBANK_DAY) == 0);
412                         UASSERT(b.getNode(v3s16(1,0,0)).getLight(LIGHTBANK_DAY) == 0);
413                         UASSERT(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
414                         UASSERT(b.getFaceLight2(1000, p, v3s16(0,1,0)) == LIGHT_SUN);
415                         UASSERT(b.getFaceLight2(1000, p, v3s16(0,-1,0)) == 0);
416                         UASSERT(b.getFaceLight2(0, p, v3s16(0,-1,0)) == 0);
417                         // According to MapBlock::getFaceLight,
418                         // The face on the z+ side should have double-diminished light
419                         //UASSERT(b.getFaceLight(p, v3s16(0,0,1)) == diminish_light(diminish_light(LIGHT_MAX)));
420                         // The face on the z+ side should have diminished light
421                         UASSERT(b.getFaceLight2(1000, p, v3s16(0,0,1)) == diminish_light(LIGHT_MAX));
422                 }
423                 /*
424                         Check how the block handles being in between blocks with some non-sunlight
425                         while being underground
426                 */
427                 {
428                         // Make neighbours to exist and set some non-sunlight to them
429                         parent.position_valid = true;
430                         b.setIsUnderground(true);
431                         parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
432                         core::map<v3s16, bool> light_sources;
433                         // The block below should be valid because there shouldn't be
434                         // sunlight in there either
435                         UASSERT(b.propagateSunlight(light_sources, true) == true);
436                         // Should not touch nodes that are not affected (that is, all of them)
437                         //UASSERT(b.getNode(v3s16(1,2,3)).getLight() == LIGHT_SUN);
438                         // Should set light of non-sunlighted blocks to 0.
439                         UASSERT(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == 0);
440                 }
441                 /*
442                         Set up a situation where:
443                         - There is only air in this block
444                         - There is a valid non-sunlighted block at the bottom, and
445                         - Invalid blocks elsewhere.
446                         - the block is not underground.
447
448                         This should result in bottom block invalidity
449                 */
450                 {
451                         b.setIsUnderground(false);
452                         // Clear block
453                         for(u16 z=0; z<MAP_BLOCKSIZE; z++){
454                                 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
455                                         for(u16 x=0; x<MAP_BLOCKSIZE; x++){
456                                                 MapNode n;
457                                                 n.setContent(CONTENT_AIR);
458                                                 n.setLight(LIGHTBANK_DAY, 0);
459                                                 b.setNode(v3s16(x,y,z), n);
460                                         }
461                                 }
462                         }
463                         // Make neighbours invalid
464                         parent.position_valid = false;
465                         // Add exceptions to the top of the bottom block
466                         for(u16 x=0; x<MAP_BLOCKSIZE; x++)
467                         for(u16 z=0; z<MAP_BLOCKSIZE; z++)
468                         {
469                                 parent.validity_exceptions.push_back(v3s16(MAP_BLOCKSIZE+x, MAP_BLOCKSIZE-1, MAP_BLOCKSIZE+z));
470                         }
471                         // Lighting value for the valid nodes
472                         parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
473                         core::map<v3s16, bool> light_sources;
474                         // Bottom block is not valid
475                         UASSERT(b.propagateSunlight(light_sources) == false);
476                 }
477         }
478 };
479
480 struct TestMapSector: public TestBase
481 {
482         class TC : public NodeContainer
483         {
484         public:
485
486                 MapNode node;
487                 bool position_valid;
488
489                 TC()
490                 {
491                         position_valid = true;
492                 }
493
494                 virtual bool isValidPosition(v3s16 p)
495                 {
496                         return position_valid;
497                 }
498
499                 virtual MapNode getNode(v3s16 p)
500                 {
501                         if(position_valid == false)
502                                 throw InvalidPositionException();
503                         return node;
504                 }
505
506                 virtual void setNode(v3s16 p, MapNode & n)
507                 {
508                         if(position_valid == false)
509                                 throw InvalidPositionException();
510                 };
511
512                 virtual u16 nodeContainerId() const
513                 {
514                         return 666;
515                 }
516         };
517
518         void Run()
519         {
520                 TC parent;
521                 parent.position_valid = false;
522
523                 // Create one with no heightmaps
524                 ServerMapSector sector(&parent, v2s16(1,1));
525
526                 UASSERT(sector.getBlockNoCreateNoEx(0) == 0);
527                 UASSERT(sector.getBlockNoCreateNoEx(1) == 0);
528
529                 MapBlock * bref = sector.createBlankBlock(-2);
530
531                 UASSERT(sector.getBlockNoCreateNoEx(0) == 0);
532                 UASSERT(sector.getBlockNoCreateNoEx(-2) == bref);
533
534                 //TODO: Check for AlreadyExistsException
535
536                 /*bool exception_thrown = false;
537                 try{
538                         sector.getBlock(0);
539                 }
540                 catch(InvalidPositionException &e){
541                         exception_thrown = true;
542                 }
543                 UASSERT(exception_thrown);*/
544
545         }
546 };
547 #endif