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.
27 content_t t_CONTENT_STONE;
28 content_t t_CONTENT_GRASS;
29 content_t t_CONTENT_TORCH;
30 content_t t_CONTENT_WATER;
31 content_t t_CONTENT_LAVA;
32 content_t t_CONTENT_BRICK;
34 ////////////////////////////////////////////////////////////////////////////////
40 class TestGameDef : public IGameDef {
45 IItemDefManager *getItemDefManager() { return m_itemdef; }
46 INodeDefManager *getNodeDefManager() { return m_nodedef; }
47 ICraftDefManager *getCraftDefManager() { return m_craftdef; }
48 ITextureSource *getTextureSource() { return m_texturesrc; }
49 IShaderSource *getShaderSource() { return m_shadersrc; }
50 ISoundManager *getSoundManager() { return m_soundmgr; }
51 MtEventManager *getEventManager() { return m_eventmgr; }
52 scene::ISceneManager *getSceneManager() { return m_scenemgr; }
53 IRollbackManager *getRollbackManager() { return m_rollbackmgr; }
54 EmergeManager *getEmergeManager() { return m_emergemgr; }
56 scene::IAnimatedMesh *getMesh(const std::string &filename) { return NULL; }
57 bool checkLocalPrivilege(const std::string &priv) { return false; }
58 u16 allocateUnknownNodeId(const std::string &name) { return 0; }
60 void defineSomeNodes();
63 IItemDefManager *m_itemdef;
64 INodeDefManager *m_nodedef;
65 ICraftDefManager *m_craftdef;
66 ITextureSource *m_texturesrc;
67 IShaderSource *m_shadersrc;
68 ISoundManager *m_soundmgr;
69 MtEventManager *m_eventmgr;
70 scene::ISceneManager *m_scenemgr;
71 IRollbackManager *m_rollbackmgr;
72 EmergeManager *m_emergemgr;
76 TestGameDef::TestGameDef()
78 m_itemdef = createItemDefManager();
79 m_nodedef = createNodeDefManager();
85 TestGameDef::~TestGameDef()
92 void TestGameDef::defineSomeNodes()
94 IWritableItemDefManager *idef = (IWritableItemDefManager *)m_itemdef;
95 IWritableNodeDefManager *ndef = (IWritableNodeDefManager *)m_nodedef;
97 ItemDefinition itemdef;
101 itemdef = ItemDefinition();
102 itemdef.type = ITEM_NODE;
103 itemdef.name = "default:stone";
104 itemdef.description = "Stone";
105 itemdef.groups["cracky"] = 3;
106 itemdef.inventory_image = "[inventorycube"
109 "{default_stone.png";
110 f = ContentFeatures();
111 f.name = itemdef.name;
112 for(int i = 0; i < 6; i++)
113 f.tiledef[i].name = "default_stone.png";
114 f.is_ground_content = true;
115 idef->registerItem(itemdef);
116 t_CONTENT_STONE = ndef->set(f.name, f);
119 itemdef = ItemDefinition();
120 itemdef.type = ITEM_NODE;
121 itemdef.name = "default:dirt_with_grass";
122 itemdef.description = "Dirt with grass";
123 itemdef.groups["crumbly"] = 3;
124 itemdef.inventory_image = "[inventorycube"
126 "{default_dirt.png&default_grass_side.png"
127 "{default_dirt.png&default_grass_side.png";
128 f = ContentFeatures();
129 f.name = itemdef.name;
130 f.tiledef[0].name = "default_grass.png";
131 f.tiledef[1].name = "default_dirt.png";
132 for(int i = 2; i < 6; i++)
133 f.tiledef[i].name = "default_dirt.png^default_grass_side.png";
134 f.is_ground_content = true;
135 idef->registerItem(itemdef);
136 t_CONTENT_GRASS = ndef->set(f.name, f);
138 //// Torch (minimal definition for lighting tests)
139 itemdef = ItemDefinition();
140 itemdef.type = ITEM_NODE;
141 itemdef.name = "default:torch";
142 f = ContentFeatures();
143 f.name = itemdef.name;
144 f.param_type = CPT_LIGHT;
145 f.light_propagates = true;
146 f.sunlight_propagates = true;
147 f.light_source = LIGHT_MAX-1;
148 idef->registerItem(itemdef);
149 t_CONTENT_TORCH = ndef->set(f.name, f);
152 itemdef = ItemDefinition();
153 itemdef.type = ITEM_NODE;
154 itemdef.name = "default:water";
155 itemdef.description = "Water";
156 itemdef.inventory_image = "[inventorycube"
159 "{default_water.png";
160 f = ContentFeatures();
161 f.name = itemdef.name;
163 f.liquid_type = LIQUID_SOURCE;
164 f.liquid_viscosity = 4;
165 f.is_ground_content = true;
166 f.groups["liquids"] = 3;
167 for(int i = 0; i < 6; i++)
168 f.tiledef[i].name = "default_water.png";
169 idef->registerItem(itemdef);
170 t_CONTENT_WATER = ndef->set(f.name, f);
173 itemdef = ItemDefinition();
174 itemdef.type = ITEM_NODE;
175 itemdef.name = "default:lava";
176 itemdef.description = "Lava";
177 itemdef.inventory_image = "[inventorycube"
181 f = ContentFeatures();
182 f.name = itemdef.name;
184 f.liquid_type = LIQUID_SOURCE;
185 f.liquid_viscosity = 7;
186 f.light_source = LIGHT_MAX-1;
187 f.is_ground_content = true;
188 f.groups["liquids"] = 3;
189 for(int i = 0; i < 6; i++)
190 f.tiledef[i].name = "default_lava.png";
191 idef->registerItem(itemdef);
192 t_CONTENT_LAVA = ndef->set(f.name, f);
196 itemdef = ItemDefinition();
197 itemdef.type = ITEM_NODE;
198 itemdef.name = "default:brick";
199 itemdef.description = "Brick";
200 itemdef.groups["cracky"] = 3;
201 itemdef.inventory_image = "[inventorycube"
204 "{default_brick.png";
205 f = ContentFeatures();
206 f.name = itemdef.name;
207 for(int i = 0; i < 6; i++)
208 f.tiledef[i].name = "default_brick.png";
209 f.is_ground_content = true;
210 idef->registerItem(itemdef);
211 t_CONTENT_BRICK = ndef->set(f.name, f);
220 DSTACK(FUNCTION_NAME);
222 u32 t1 = porting::getTime(PRECISION_MILLI);
225 g_logger.setLevelSilenced(LL_ERROR, true);
227 u32 num_modules_failed = 0;
228 u32 num_total_tests_failed = 0;
229 u32 num_total_tests_run = 0;
230 std::vector<TestBase *> &testmods = TestManager::getTestModules();
231 for (size_t i = 0; i != testmods.size(); i++) {
232 if (!testmods[i]->testModule(&gamedef))
233 num_modules_failed++;
235 num_total_tests_failed += testmods[i]->num_tests_failed;
236 num_total_tests_run += testmods[i]->num_tests_run;
239 u32 tdiff = porting::getTime(PRECISION_MILLI) - t1;
241 g_logger.setLevelSilenced(LL_ERROR, false);
243 const char *overall_status = (num_modules_failed == 0) ? "PASSED" : "FAILED";
246 << "++++++++++++++++++++++++++++++++++++++++"
247 << "++++++++++++++++++++++++++++++++++++++++" << std::endl
248 << "Unit Test Results: " << overall_status << std::endl
249 << " " << num_modules_failed << " / " << testmods.size()
250 << " failed modules (" << num_total_tests_failed << " / "
251 << num_total_tests_run << " failed individual tests)." << std::endl
252 << " Testing took " << tdiff << "ms total." << std::endl
253 << "++++++++++++++++++++++++++++++++++++++++"
254 << "++++++++++++++++++++++++++++++++++++++++" << std::endl;
256 if (num_modules_failed)
264 bool TestBase::testModule(IGameDef *gamedef)
266 rawstream << "======== Testing module " << getName() << std::endl;
267 u32 t1 = porting::getTime(PRECISION_MILLI);
272 u32 tdiff = porting::getTime(PRECISION_MILLI) - t1;
273 rawstream << "======== Module " << getName() << " "
274 << (num_tests_failed ? "failed" : "passed") << " (" << num_tests_failed
275 << " failures / " << num_tests_run << " tests) - " << tdiff
276 << "ms" << std::endl;
278 if (!m_test_dir.empty())
279 fs::RecursiveDelete(m_test_dir);
281 return num_tests_failed == 0;
284 std::string TestBase::getTestTempDirectory()
286 if (!m_test_dir.empty())
290 snprintf(buf, sizeof(buf), "%08X", myrand());
292 m_test_dir = fs::TempPath() + DIR_DELIM "mttest_" + buf;
293 if (!fs::CreateDir(m_test_dir))
294 throw TestFailedException();
299 std::string TestBase::getTestTempFile()
302 snprintf(buf, sizeof(buf), "%08X", myrand());
304 return getTestTempDirectory() + DIR_DELIM + buf + ".tmp";
309 NOTE: These tests became non-working then NodeContainer was removed.
310 These should be redone, utilizing some kind of a virtual
311 interface for Map (IMap would be fine).
314 struct TestMapBlock: public TestBase
316 class TC : public NodeContainer
322 core::list<v3s16> validity_exceptions;
326 position_valid = true;
329 virtual bool isValidPosition(v3s16 p)
331 //return position_valid ^ (p==position_valid_exception);
332 bool exception = false;
333 for(core::list<v3s16>::Iterator i=validity_exceptions.begin();
334 i != validity_exceptions.end(); i++)
342 return exception ? !position_valid : position_valid;
345 virtual MapNode getNode(v3s16 p)
347 if(isValidPosition(p) == false)
348 throw InvalidPositionException();
352 virtual void setNode(v3s16 p, MapNode & n)
354 if(isValidPosition(p) == false)
355 throw InvalidPositionException();
358 virtual u16 nodeContainerId() const
368 MapBlock b(&parent, v3s16(1,1,1));
369 v3s16 relpos(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
371 UASSERT(b.getPosRelative() == relpos);
373 UASSERT(b.getBox().MinEdge.X == MAP_BLOCKSIZE);
374 UASSERT(b.getBox().MaxEdge.X == MAP_BLOCKSIZE*2-1);
375 UASSERT(b.getBox().MinEdge.Y == MAP_BLOCKSIZE);
376 UASSERT(b.getBox().MaxEdge.Y == MAP_BLOCKSIZE*2-1);
377 UASSERT(b.getBox().MinEdge.Z == MAP_BLOCKSIZE);
378 UASSERT(b.getBox().MaxEdge.Z == MAP_BLOCKSIZE*2-1);
380 UASSERT(b.isValidPosition(v3s16(0,0,0)) == true);
381 UASSERT(b.isValidPosition(v3s16(-1,0,0)) == false);
382 UASSERT(b.isValidPosition(v3s16(-1,-142,-2341)) == false);
383 UASSERT(b.isValidPosition(v3s16(-124,142,2341)) == false);
384 UASSERT(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
385 UASSERT(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE,MAP_BLOCKSIZE-1)) == false);
388 TODO: this method should probably be removed
389 if the block size isn't going to be set variable
391 /*UASSERT(b.getSizeNodes() == v3s16(MAP_BLOCKSIZE,
392 MAP_BLOCKSIZE, MAP_BLOCKSIZE));*/
394 // Changed flag should be initially set
395 UASSERT(b.getModified() == MOD_STATE_WRITE_NEEDED);
397 UASSERT(b.getModified() == MOD_STATE_CLEAN);
399 // All nodes should have been set to
400 // .d=CONTENT_IGNORE and .getLight() = 0
401 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
402 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
403 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
405 //UASSERT(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_AIR);
406 UASSERT(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_IGNORE);
407 UASSERT(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_DAY) == 0);
408 UASSERT(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_NIGHT) == 0);
412 MapNode n(CONTENT_AIR);
413 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
414 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
415 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
417 b.setNode(v3s16(x,y,z), n);
422 Parent fetch functions
424 parent.position_valid = false;
425 parent.node.setContent(5);
429 // Positions in the block should still be valid
430 UASSERT(b.isValidPositionParent(v3s16(0,0,0)) == true);
431 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
432 n = b.getNodeParent(v3s16(0,MAP_BLOCKSIZE-1,0));
433 UASSERT(n.getContent() == CONTENT_AIR);
435 // ...but outside the block they should be invalid
436 UASSERT(b.isValidPositionParent(v3s16(-121,2341,0)) == false);
437 UASSERT(b.isValidPositionParent(v3s16(-1,0,0)) == false);
438 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == false);
441 bool exception_thrown = false;
443 // This should throw an exception
444 MapNode n = b.getNodeParent(v3s16(0,0,-1));
446 catch(InvalidPositionException &e)
448 exception_thrown = true;
450 UASSERT(exception_thrown);
453 parent.position_valid = true;
454 // Now the positions outside should be valid
455 UASSERT(b.isValidPositionParent(v3s16(-121,2341,0)) == true);
456 UASSERT(b.isValidPositionParent(v3s16(-1,0,0)) == true);
457 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == true);
458 n = b.getNodeParent(v3s16(0,0,MAP_BLOCKSIZE));
459 UASSERT(n.getContent() == 5);
467 UASSERT(b.getNode(p).getContent() == 4);
468 //TODO: Update to new system
469 /*UASSERT(b.getNodeTile(p) == 4);
470 UASSERT(b.getNodeTile(v3s16(-1,-1,0)) == 5);*/
475 // Set lighting of all nodes to 0
476 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
477 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
478 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
479 MapNode n = b.getNode(v3s16(x,y,z));
480 n.setLight(LIGHTBANK_DAY, 0);
481 n.setLight(LIGHTBANK_NIGHT, 0);
482 b.setNode(v3s16(x,y,z), n);
488 Check how the block handles being a lonely sky block
490 parent.position_valid = true;
491 b.setIsUnderground(false);
492 parent.node.setContent(CONTENT_AIR);
493 parent.node.setLight(LIGHTBANK_DAY, LIGHT_SUN);
494 parent.node.setLight(LIGHTBANK_NIGHT, 0);
495 core::map<v3s16, bool> light_sources;
496 // The bottom block is invalid, because we have a shadowing node
497 UASSERT(b.propagateSunlight(light_sources) == false);
498 UASSERT(b.getNode(v3s16(1,4,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
499 UASSERT(b.getNode(v3s16(1,3,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
500 UASSERT(b.getNode(v3s16(1,2,0)).getLight(LIGHTBANK_DAY) == 0);
501 UASSERT(b.getNode(v3s16(1,1,0)).getLight(LIGHTBANK_DAY) == 0);
502 UASSERT(b.getNode(v3s16(1,0,0)).getLight(LIGHTBANK_DAY) == 0);
503 UASSERT(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
504 UASSERT(b.getFaceLight2(1000, p, v3s16(0,1,0)) == LIGHT_SUN);
505 UASSERT(b.getFaceLight2(1000, p, v3s16(0,-1,0)) == 0);
506 UASSERT(b.getFaceLight2(0, p, v3s16(0,-1,0)) == 0);
507 // According to MapBlock::getFaceLight,
508 // The face on the z+ side should have double-diminished light
509 //UASSERT(b.getFaceLight(p, v3s16(0,0,1)) == diminish_light(diminish_light(LIGHT_MAX)));
510 // The face on the z+ side should have diminished light
511 UASSERT(b.getFaceLight2(1000, p, v3s16(0,0,1)) == diminish_light(LIGHT_MAX));
514 Check how the block handles being in between blocks with some non-sunlight
515 while being underground
518 // Make neighbours to exist and set some non-sunlight to them
519 parent.position_valid = true;
520 b.setIsUnderground(true);
521 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
522 core::map<v3s16, bool> light_sources;
523 // The block below should be valid because there shouldn't be
524 // sunlight in there either
525 UASSERT(b.propagateSunlight(light_sources, true) == true);
526 // Should not touch nodes that are not affected (that is, all of them)
527 //UASSERT(b.getNode(v3s16(1,2,3)).getLight() == LIGHT_SUN);
528 // Should set light of non-sunlighted blocks to 0.
529 UASSERT(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == 0);
532 Set up a situation where:
533 - There is only air in this block
534 - There is a valid non-sunlighted block at the bottom, and
535 - Invalid blocks elsewhere.
536 - the block is not underground.
538 This should result in bottom block invalidity
541 b.setIsUnderground(false);
543 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
544 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
545 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
547 n.setContent(CONTENT_AIR);
548 n.setLight(LIGHTBANK_DAY, 0);
549 b.setNode(v3s16(x,y,z), n);
553 // Make neighbours invalid
554 parent.position_valid = false;
555 // Add exceptions to the top of the bottom block
556 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
557 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
559 parent.validity_exceptions.push_back(v3s16(MAP_BLOCKSIZE+x, MAP_BLOCKSIZE-1, MAP_BLOCKSIZE+z));
561 // Lighting value for the valid nodes
562 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
563 core::map<v3s16, bool> light_sources;
564 // Bottom block is not valid
565 UASSERT(b.propagateSunlight(light_sources) == false);
570 struct TestMapSector: public TestBase
572 class TC : public NodeContainer
581 position_valid = true;
584 virtual bool isValidPosition(v3s16 p)
586 return position_valid;
589 virtual MapNode getNode(v3s16 p)
591 if(position_valid == false)
592 throw InvalidPositionException();
596 virtual void setNode(v3s16 p, MapNode & n)
598 if(position_valid == false)
599 throw InvalidPositionException();
602 virtual u16 nodeContainerId() const
611 parent.position_valid = false;
613 // Create one with no heightmaps
614 ServerMapSector sector(&parent, v2s16(1,1));
616 UASSERT(sector.getBlockNoCreateNoEx(0) == 0);
617 UASSERT(sector.getBlockNoCreateNoEx(1) == 0);
619 MapBlock * bref = sector.createBlankBlock(-2);
621 UASSERT(sector.getBlockNoCreateNoEx(0) == 0);
622 UASSERT(sector.getBlockNoCreateNoEx(-2) == bref);
624 //TODO: Check for AlreadyExistsException
626 /*bool exception_thrown = false;
630 catch(InvalidPositionException &e){
631 exception_thrown = true;
633 UASSERT(exception_thrown);*/