3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
28 content_t t_CONTENT_STONE;
29 content_t t_CONTENT_GRASS;
30 content_t t_CONTENT_TORCH;
31 content_t t_CONTENT_WATER;
32 content_t t_CONTENT_LAVA;
33 content_t t_CONTENT_BRICK;
35 ////////////////////////////////////////////////////////////////////////////////
41 class TestGameDef : public IGameDef {
46 IItemDefManager *getItemDefManager() { return m_itemdef; }
47 INodeDefManager *getNodeDefManager() { return m_nodedef; }
48 ICraftDefManager *getCraftDefManager() { return m_craftdef; }
49 ITextureSource *getTextureSource() { return m_texturesrc; }
50 IShaderSource *getShaderSource() { return m_shadersrc; }
51 ISoundManager *getSoundManager() { return m_soundmgr; }
52 MtEventManager *getEventManager() { return m_eventmgr; }
53 scene::ISceneManager *getSceneManager() { return m_scenemgr; }
54 IRollbackManager *getRollbackManager() { return m_rollbackmgr; }
55 EmergeManager *getEmergeManager() { return m_emergemgr; }
57 scene::IAnimatedMesh *getMesh(const std::string &filename) { return NULL; }
58 bool checkLocalPrivilege(const std::string &priv) { return false; }
59 u16 allocateUnknownNodeId(const std::string &name) { return 0; }
61 void defineSomeNodes();
64 IItemDefManager *m_itemdef;
65 INodeDefManager *m_nodedef;
66 ICraftDefManager *m_craftdef;
67 ITextureSource *m_texturesrc;
68 IShaderSource *m_shadersrc;
69 ISoundManager *m_soundmgr;
70 MtEventManager *m_eventmgr;
71 scene::ISceneManager *m_scenemgr;
72 IRollbackManager *m_rollbackmgr;
73 EmergeManager *m_emergemgr;
77 TestGameDef::TestGameDef()
79 m_itemdef = createItemDefManager();
80 m_nodedef = createNodeDefManager();
86 TestGameDef::~TestGameDef()
93 void TestGameDef::defineSomeNodes()
95 IWritableItemDefManager *idef = (IWritableItemDefManager *)m_itemdef;
96 IWritableNodeDefManager *ndef = (IWritableNodeDefManager *)m_nodedef;
98 ItemDefinition itemdef;
102 itemdef = ItemDefinition();
103 itemdef.type = ITEM_NODE;
104 itemdef.name = "default:stone";
105 itemdef.description = "Stone";
106 itemdef.groups["cracky"] = 3;
107 itemdef.inventory_image = "[inventorycube"
110 "{default_stone.png";
111 f = ContentFeatures();
112 f.name = itemdef.name;
113 for(int i = 0; i < 6; i++)
114 f.tiledef[i].name = "default_stone.png";
115 f.is_ground_content = true;
116 idef->registerItem(itemdef);
117 t_CONTENT_STONE = ndef->set(f.name, f);
120 itemdef = ItemDefinition();
121 itemdef.type = ITEM_NODE;
122 itemdef.name = "default:dirt_with_grass";
123 itemdef.description = "Dirt with grass";
124 itemdef.groups["crumbly"] = 3;
125 itemdef.inventory_image = "[inventorycube"
127 "{default_dirt.png&default_grass_side.png"
128 "{default_dirt.png&default_grass_side.png";
129 f = ContentFeatures();
130 f.name = itemdef.name;
131 f.tiledef[0].name = "default_grass.png";
132 f.tiledef[1].name = "default_dirt.png";
133 for(int i = 2; i < 6; i++)
134 f.tiledef[i].name = "default_dirt.png^default_grass_side.png";
135 f.is_ground_content = true;
136 idef->registerItem(itemdef);
137 t_CONTENT_GRASS = ndef->set(f.name, f);
139 //// Torch (minimal definition for lighting tests)
140 itemdef = ItemDefinition();
141 itemdef.type = ITEM_NODE;
142 itemdef.name = "default:torch";
143 f = ContentFeatures();
144 f.name = itemdef.name;
145 f.param_type = CPT_LIGHT;
146 f.light_propagates = true;
147 f.sunlight_propagates = true;
148 f.light_source = LIGHT_MAX-1;
149 idef->registerItem(itemdef);
150 t_CONTENT_TORCH = ndef->set(f.name, f);
153 itemdef = ItemDefinition();
154 itemdef.type = ITEM_NODE;
155 itemdef.name = "default:water";
156 itemdef.description = "Water";
157 itemdef.inventory_image = "[inventorycube"
160 "{default_water.png";
161 f = ContentFeatures();
162 f.name = itemdef.name;
164 f.liquid_type = LIQUID_SOURCE;
165 f.liquid_viscosity = 4;
166 f.is_ground_content = true;
167 f.groups["liquids"] = 3;
168 for(int i = 0; i < 6; i++)
169 f.tiledef[i].name = "default_water.png";
170 idef->registerItem(itemdef);
171 t_CONTENT_WATER = ndef->set(f.name, f);
174 itemdef = ItemDefinition();
175 itemdef.type = ITEM_NODE;
176 itemdef.name = "default:lava";
177 itemdef.description = "Lava";
178 itemdef.inventory_image = "[inventorycube"
182 f = ContentFeatures();
183 f.name = itemdef.name;
185 f.liquid_type = LIQUID_SOURCE;
186 f.liquid_viscosity = 7;
187 f.light_source = LIGHT_MAX-1;
188 f.is_ground_content = true;
189 f.groups["liquids"] = 3;
190 for(int i = 0; i < 6; i++)
191 f.tiledef[i].name = "default_lava.png";
192 idef->registerItem(itemdef);
193 t_CONTENT_LAVA = ndef->set(f.name, f);
197 itemdef = ItemDefinition();
198 itemdef.type = ITEM_NODE;
199 itemdef.name = "default:brick";
200 itemdef.description = "Brick";
201 itemdef.groups["cracky"] = 3;
202 itemdef.inventory_image = "[inventorycube"
205 "{default_brick.png";
206 f = ContentFeatures();
207 f.name = itemdef.name;
208 for(int i = 0; i < 6; i++)
209 f.tiledef[i].name = "default_brick.png";
210 f.is_ground_content = true;
211 idef->registerItem(itemdef);
212 t_CONTENT_BRICK = ndef->set(f.name, f);
221 DSTACK(__FUNCTION_NAME);
223 u32 t1 = porting::getTime(PRECISION_MILLI);
226 log_set_lev_silence(LMT_ERROR, true);
228 u32 num_modules_failed = 0;
229 u32 num_total_tests_failed = 0;
230 u32 num_total_tests_run = 0;
231 std::vector<TestBase *> &testmods = TestManager::getTestModules();
232 for (size_t i = 0; i != testmods.size(); i++) {
233 if (!testmods[i]->testModule(&gamedef))
234 num_modules_failed++;
236 num_total_tests_failed += testmods[i]->num_tests_failed;
237 num_total_tests_run += testmods[i]->num_tests_run;
240 u32 tdiff = porting::getTime(PRECISION_MILLI) - t1;
242 log_set_lev_silence(LMT_ERROR, false);
244 const char *overall_status = (num_modules_failed == 0) ? "PASSED" : "FAILED";
247 << "++++++++++++++++++++++++++++++++++++++++"
248 << "++++++++++++++++++++++++++++++++++++++++" << std::endl
249 << "Unit Test Results: " << overall_status << std::endl
250 << " " << num_modules_failed << " / " << testmods.size()
251 << " failed modules (" << num_total_tests_failed << " / "
252 << num_total_tests_run << " failed individual tests)." << std::endl
253 << " Testing took " << tdiff << "ms total." << std::endl
254 << "++++++++++++++++++++++++++++++++++++++++"
255 << "++++++++++++++++++++++++++++++++++++++++" << std::endl;
257 if (num_modules_failed)
265 bool TestBase::testModule(IGameDef *gamedef)
267 dstream << "======== Testing module " << getName() << std::endl;
268 u32 t1 = porting::getTime(PRECISION_MILLI);
273 u32 tdiff = porting::getTime(PRECISION_MILLI) - t1;
274 dstream << "======== Module " << getName() << " "
275 << (num_tests_failed ? "failed" : "passed") << " (" << num_tests_failed
276 << " failures / " << num_tests_run << " tests) - " << tdiff
277 << "ms" << std::endl;
279 if (!m_test_dir.empty())
280 fs::RecursiveDelete(m_test_dir);
282 return num_tests_failed == 0;
285 std::string TestBase::getTestTempDirectory()
287 if (!m_test_dir.empty())
291 snprintf(buf, sizeof(buf), "%08X", myrand());
293 m_test_dir = fs::TempPath() + DIR_DELIM "mttest_" + buf;
294 if (!fs::CreateDir(m_test_dir))
295 throw TestFailedException();
300 std::string TestBase::getTestTempFile()
303 snprintf(buf, sizeof(buf), "%08X", myrand());
305 return getTestTempDirectory() + DIR_DELIM + buf + ".tmp";
310 NOTE: These tests became non-working then NodeContainer was removed.
311 These should be redone, utilizing some kind of a virtual
312 interface for Map (IMap would be fine).
315 struct TestMapBlock: public TestBase
317 class TC : public NodeContainer
323 core::list<v3s16> validity_exceptions;
327 position_valid = true;
330 virtual bool isValidPosition(v3s16 p)
332 //return position_valid ^ (p==position_valid_exception);
333 bool exception = false;
334 for(core::list<v3s16>::Iterator i=validity_exceptions.begin();
335 i != validity_exceptions.end(); i++)
343 return exception ? !position_valid : position_valid;
346 virtual MapNode getNode(v3s16 p)
348 if(isValidPosition(p) == false)
349 throw InvalidPositionException();
353 virtual void setNode(v3s16 p, MapNode & n)
355 if(isValidPosition(p) == false)
356 throw InvalidPositionException();
359 virtual u16 nodeContainerId() const
369 MapBlock b(&parent, v3s16(1,1,1));
370 v3s16 relpos(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
372 UASSERT(b.getPosRelative() == relpos);
374 UASSERT(b.getBox().MinEdge.X == MAP_BLOCKSIZE);
375 UASSERT(b.getBox().MaxEdge.X == MAP_BLOCKSIZE*2-1);
376 UASSERT(b.getBox().MinEdge.Y == MAP_BLOCKSIZE);
377 UASSERT(b.getBox().MaxEdge.Y == MAP_BLOCKSIZE*2-1);
378 UASSERT(b.getBox().MinEdge.Z == MAP_BLOCKSIZE);
379 UASSERT(b.getBox().MaxEdge.Z == MAP_BLOCKSIZE*2-1);
381 UASSERT(b.isValidPosition(v3s16(0,0,0)) == true);
382 UASSERT(b.isValidPosition(v3s16(-1,0,0)) == false);
383 UASSERT(b.isValidPosition(v3s16(-1,-142,-2341)) == false);
384 UASSERT(b.isValidPosition(v3s16(-124,142,2341)) == false);
385 UASSERT(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
386 UASSERT(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE,MAP_BLOCKSIZE-1)) == false);
389 TODO: this method should probably be removed
390 if the block size isn't going to be set variable
392 /*UASSERT(b.getSizeNodes() == v3s16(MAP_BLOCKSIZE,
393 MAP_BLOCKSIZE, MAP_BLOCKSIZE));*/
395 // Changed flag should be initially set
396 UASSERT(b.getModified() == MOD_STATE_WRITE_NEEDED);
398 UASSERT(b.getModified() == MOD_STATE_CLEAN);
400 // All nodes should have been set to
401 // .d=CONTENT_IGNORE and .getLight() = 0
402 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
403 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
404 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
406 //UASSERT(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_AIR);
407 UASSERT(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_IGNORE);
408 UASSERT(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_DAY) == 0);
409 UASSERT(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_NIGHT) == 0);
413 MapNode n(CONTENT_AIR);
414 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
415 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
416 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
418 b.setNode(v3s16(x,y,z), n);
423 Parent fetch functions
425 parent.position_valid = false;
426 parent.node.setContent(5);
430 // Positions in the block should still be valid
431 UASSERT(b.isValidPositionParent(v3s16(0,0,0)) == true);
432 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
433 n = b.getNodeParent(v3s16(0,MAP_BLOCKSIZE-1,0));
434 UASSERT(n.getContent() == CONTENT_AIR);
436 // ...but outside the block they should be invalid
437 UASSERT(b.isValidPositionParent(v3s16(-121,2341,0)) == false);
438 UASSERT(b.isValidPositionParent(v3s16(-1,0,0)) == false);
439 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == false);
442 bool exception_thrown = false;
444 // This should throw an exception
445 MapNode n = b.getNodeParent(v3s16(0,0,-1));
447 catch(InvalidPositionException &e)
449 exception_thrown = true;
451 UASSERT(exception_thrown);
454 parent.position_valid = true;
455 // Now the positions outside should be valid
456 UASSERT(b.isValidPositionParent(v3s16(-121,2341,0)) == true);
457 UASSERT(b.isValidPositionParent(v3s16(-1,0,0)) == true);
458 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == true);
459 n = b.getNodeParent(v3s16(0,0,MAP_BLOCKSIZE));
460 UASSERT(n.getContent() == 5);
468 UASSERT(b.getNode(p).getContent() == 4);
469 //TODO: Update to new system
470 /*UASSERT(b.getNodeTile(p) == 4);
471 UASSERT(b.getNodeTile(v3s16(-1,-1,0)) == 5);*/
476 // Set lighting of all nodes to 0
477 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
478 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
479 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
480 MapNode n = b.getNode(v3s16(x,y,z));
481 n.setLight(LIGHTBANK_DAY, 0);
482 n.setLight(LIGHTBANK_NIGHT, 0);
483 b.setNode(v3s16(x,y,z), n);
489 Check how the block handles being a lonely sky block
491 parent.position_valid = true;
492 b.setIsUnderground(false);
493 parent.node.setContent(CONTENT_AIR);
494 parent.node.setLight(LIGHTBANK_DAY, LIGHT_SUN);
495 parent.node.setLight(LIGHTBANK_NIGHT, 0);
496 core::map<v3s16, bool> light_sources;
497 // The bottom block is invalid, because we have a shadowing node
498 UASSERT(b.propagateSunlight(light_sources) == false);
499 UASSERT(b.getNode(v3s16(1,4,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
500 UASSERT(b.getNode(v3s16(1,3,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
501 UASSERT(b.getNode(v3s16(1,2,0)).getLight(LIGHTBANK_DAY) == 0);
502 UASSERT(b.getNode(v3s16(1,1,0)).getLight(LIGHTBANK_DAY) == 0);
503 UASSERT(b.getNode(v3s16(1,0,0)).getLight(LIGHTBANK_DAY) == 0);
504 UASSERT(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
505 UASSERT(b.getFaceLight2(1000, p, v3s16(0,1,0)) == LIGHT_SUN);
506 UASSERT(b.getFaceLight2(1000, p, v3s16(0,-1,0)) == 0);
507 UASSERT(b.getFaceLight2(0, p, v3s16(0,-1,0)) == 0);
508 // According to MapBlock::getFaceLight,
509 // The face on the z+ side should have double-diminished light
510 //UASSERT(b.getFaceLight(p, v3s16(0,0,1)) == diminish_light(diminish_light(LIGHT_MAX)));
511 // The face on the z+ side should have diminished light
512 UASSERT(b.getFaceLight2(1000, p, v3s16(0,0,1)) == diminish_light(LIGHT_MAX));
515 Check how the block handles being in between blocks with some non-sunlight
516 while being underground
519 // Make neighbours to exist and set some non-sunlight to them
520 parent.position_valid = true;
521 b.setIsUnderground(true);
522 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
523 core::map<v3s16, bool> light_sources;
524 // The block below should be valid because there shouldn't be
525 // sunlight in there either
526 UASSERT(b.propagateSunlight(light_sources, true) == true);
527 // Should not touch nodes that are not affected (that is, all of them)
528 //UASSERT(b.getNode(v3s16(1,2,3)).getLight() == LIGHT_SUN);
529 // Should set light of non-sunlighted blocks to 0.
530 UASSERT(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == 0);
533 Set up a situation where:
534 - There is only air in this block
535 - There is a valid non-sunlighted block at the bottom, and
536 - Invalid blocks elsewhere.
537 - the block is not underground.
539 This should result in bottom block invalidity
542 b.setIsUnderground(false);
544 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
545 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
546 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
548 n.setContent(CONTENT_AIR);
549 n.setLight(LIGHTBANK_DAY, 0);
550 b.setNode(v3s16(x,y,z), n);
554 // Make neighbours invalid
555 parent.position_valid = false;
556 // Add exceptions to the top of the bottom block
557 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
558 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
560 parent.validity_exceptions.push_back(v3s16(MAP_BLOCKSIZE+x, MAP_BLOCKSIZE-1, MAP_BLOCKSIZE+z));
562 // Lighting value for the valid nodes
563 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
564 core::map<v3s16, bool> light_sources;
565 // Bottom block is not valid
566 UASSERT(b.propagateSunlight(light_sources) == false);
571 struct TestMapSector: public TestBase
573 class TC : public NodeContainer
582 position_valid = true;
585 virtual bool isValidPosition(v3s16 p)
587 return position_valid;
590 virtual MapNode getNode(v3s16 p)
592 if(position_valid == false)
593 throw InvalidPositionException();
597 virtual void setNode(v3s16 p, MapNode & n)
599 if(position_valid == false)
600 throw InvalidPositionException();
603 virtual u16 nodeContainerId() const
612 parent.position_valid = false;
614 // Create one with no heightmaps
615 ServerMapSector sector(&parent, v2s16(1,1));
617 UASSERT(sector.getBlockNoCreateNoEx(0) == 0);
618 UASSERT(sector.getBlockNoCreateNoEx(1) == 0);
620 MapBlock * bref = sector.createBlankBlock(-2);
622 UASSERT(sector.getBlockNoCreateNoEx(0) == 0);
623 UASSERT(sector.getBlockNoCreateNoEx(-2) == bref);
625 //TODO: Check for AlreadyExistsException
627 /*bool exception_thrown = false;
631 catch(InvalidPositionException &e){
632 exception_thrown = true;
634 UASSERT(exception_thrown);*/