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();
62 virtual const std::vector<ModSpec> &getMods() const
64 static std::vector<ModSpec> testmodspec;
67 virtual const ModSpec* getModSpec(const std::string &modname) const { return NULL; }
68 virtual std::string getModStoragePath() const { return "."; }
69 virtual bool registerModStorage(ModMetadata *meta) { return true; }
70 virtual void unregisterModStorage(const std::string &name) {}
73 IItemDefManager *m_itemdef = nullptr;
74 INodeDefManager *m_nodedef = nullptr;
75 ICraftDefManager *m_craftdef = nullptr;
76 ITextureSource *m_texturesrc = nullptr;
77 IShaderSource *m_shadersrc = nullptr;
78 ISoundManager *m_soundmgr = nullptr;
79 MtEventManager *m_eventmgr = nullptr;
80 scene::ISceneManager *m_scenemgr = nullptr;
81 IRollbackManager *m_rollbackmgr = nullptr;
82 EmergeManager *m_emergemgr = nullptr;
86 TestGameDef::TestGameDef()
88 m_itemdef = createItemDefManager();
89 m_nodedef = createNodeDefManager();
95 TestGameDef::~TestGameDef()
102 void TestGameDef::defineSomeNodes()
104 IWritableItemDefManager *idef = (IWritableItemDefManager *)m_itemdef;
105 IWritableNodeDefManager *ndef = (IWritableNodeDefManager *)m_nodedef;
107 ItemDefinition itemdef;
111 itemdef = ItemDefinition();
112 itemdef.type = ITEM_NODE;
113 itemdef.name = "default:stone";
114 itemdef.description = "Stone";
115 itemdef.groups["cracky"] = 3;
116 itemdef.inventory_image = "[inventorycube"
119 "{default_stone.png";
120 f = ContentFeatures();
121 f.name = itemdef.name;
122 for(int i = 0; i < 6; i++)
123 f.tiledef[i].name = "default_stone.png";
124 f.is_ground_content = true;
125 idef->registerItem(itemdef);
126 t_CONTENT_STONE = ndef->set(f.name, f);
129 itemdef = ItemDefinition();
130 itemdef.type = ITEM_NODE;
131 itemdef.name = "default:dirt_with_grass";
132 itemdef.description = "Dirt with grass";
133 itemdef.groups["crumbly"] = 3;
134 itemdef.inventory_image = "[inventorycube"
136 "{default_dirt.png&default_grass_side.png"
137 "{default_dirt.png&default_grass_side.png";
138 f = ContentFeatures();
139 f.name = itemdef.name;
140 f.tiledef[0].name = "default_grass.png";
141 f.tiledef[1].name = "default_dirt.png";
142 for(int i = 2; i < 6; i++)
143 f.tiledef[i].name = "default_dirt.png^default_grass_side.png";
144 f.is_ground_content = true;
145 idef->registerItem(itemdef);
146 t_CONTENT_GRASS = ndef->set(f.name, f);
148 //// Torch (minimal definition for lighting tests)
149 itemdef = ItemDefinition();
150 itemdef.type = ITEM_NODE;
151 itemdef.name = "default:torch";
152 f = ContentFeatures();
153 f.name = itemdef.name;
154 f.param_type = CPT_LIGHT;
155 f.light_propagates = true;
156 f.sunlight_propagates = true;
157 f.light_source = LIGHT_MAX-1;
158 idef->registerItem(itemdef);
159 t_CONTENT_TORCH = ndef->set(f.name, f);
162 itemdef = ItemDefinition();
163 itemdef.type = ITEM_NODE;
164 itemdef.name = "default:water";
165 itemdef.description = "Water";
166 itemdef.inventory_image = "[inventorycube"
169 "{default_water.png";
170 f = ContentFeatures();
171 f.name = itemdef.name;
173 f.liquid_type = LIQUID_SOURCE;
174 f.liquid_viscosity = 4;
175 f.is_ground_content = true;
176 f.groups["liquids"] = 3;
177 for(int i = 0; i < 6; i++)
178 f.tiledef[i].name = "default_water.png";
179 idef->registerItem(itemdef);
180 t_CONTENT_WATER = ndef->set(f.name, f);
183 itemdef = ItemDefinition();
184 itemdef.type = ITEM_NODE;
185 itemdef.name = "default:lava";
186 itemdef.description = "Lava";
187 itemdef.inventory_image = "[inventorycube"
191 f = ContentFeatures();
192 f.name = itemdef.name;
194 f.liquid_type = LIQUID_SOURCE;
195 f.liquid_viscosity = 7;
196 f.light_source = LIGHT_MAX-1;
197 f.is_ground_content = true;
198 f.groups["liquids"] = 3;
199 for(int i = 0; i < 6; i++)
200 f.tiledef[i].name = "default_lava.png";
201 idef->registerItem(itemdef);
202 t_CONTENT_LAVA = ndef->set(f.name, f);
206 itemdef = ItemDefinition();
207 itemdef.type = ITEM_NODE;
208 itemdef.name = "default:brick";
209 itemdef.description = "Brick";
210 itemdef.groups["cracky"] = 3;
211 itemdef.inventory_image = "[inventorycube"
214 "{default_brick.png";
215 f = ContentFeatures();
216 f.name = itemdef.name;
217 for(int i = 0; i < 6; i++)
218 f.tiledef[i].name = "default_brick.png";
219 f.is_ground_content = true;
220 idef->registerItem(itemdef);
221 t_CONTENT_BRICK = ndef->set(f.name, f);
230 DSTACK(FUNCTION_NAME);
232 u64 t1 = porting::getTimeMs();
235 g_logger.setLevelSilenced(LL_ERROR, true);
237 u32 num_modules_failed = 0;
238 u32 num_total_tests_failed = 0;
239 u32 num_total_tests_run = 0;
240 std::vector<TestBase *> &testmods = TestManager::getTestModules();
241 for (size_t i = 0; i != testmods.size(); i++) {
242 if (!testmods[i]->testModule(&gamedef))
243 num_modules_failed++;
245 num_total_tests_failed += testmods[i]->num_tests_failed;
246 num_total_tests_run += testmods[i]->num_tests_run;
249 u64 tdiff = porting::getTimeMs() - t1;
251 g_logger.setLevelSilenced(LL_ERROR, false);
253 const char *overall_status = (num_modules_failed == 0) ? "PASSED" : "FAILED";
256 << "++++++++++++++++++++++++++++++++++++++++"
257 << "++++++++++++++++++++++++++++++++++++++++" << std::endl
258 << "Unit Test Results: " << overall_status << std::endl
259 << " " << num_modules_failed << " / " << testmods.size()
260 << " failed modules (" << num_total_tests_failed << " / "
261 << num_total_tests_run << " failed individual tests)." << std::endl
262 << " Testing took " << tdiff << "ms total." << std::endl
263 << "++++++++++++++++++++++++++++++++++++++++"
264 << "++++++++++++++++++++++++++++++++++++++++" << std::endl;
266 return num_modules_failed;
273 bool TestBase::testModule(IGameDef *gamedef)
275 rawstream << "======== Testing module " << getName() << std::endl;
276 u64 t1 = porting::getTimeMs();
281 u64 tdiff = porting::getTimeMs() - t1;
282 rawstream << "======== Module " << getName() << " "
283 << (num_tests_failed ? "failed" : "passed") << " (" << num_tests_failed
284 << " failures / " << num_tests_run << " tests) - " << tdiff
285 << "ms" << std::endl;
287 if (!m_test_dir.empty())
288 fs::RecursiveDelete(m_test_dir);
290 return num_tests_failed == 0;
293 std::string TestBase::getTestTempDirectory()
295 if (!m_test_dir.empty())
299 snprintf(buf, sizeof(buf), "%08X", myrand());
301 m_test_dir = fs::TempPath() + DIR_DELIM "mttest_" + buf;
302 if (!fs::CreateDir(m_test_dir))
303 throw TestFailedException();
308 std::string TestBase::getTestTempFile()
311 snprintf(buf, sizeof(buf), "%08X", myrand());
313 return getTestTempDirectory() + DIR_DELIM + buf + ".tmp";
318 NOTE: These tests became non-working then NodeContainer was removed.
319 These should be redone, utilizing some kind of a virtual
320 interface for Map (IMap would be fine).
323 struct TestMapBlock: public TestBase
325 class TC : public NodeContainer
331 core::list<v3s16> validity_exceptions;
335 position_valid = true;
338 virtual bool isValidPosition(v3s16 p)
340 //return position_valid ^ (p==position_valid_exception);
341 bool exception = false;
342 for(core::list<v3s16>::Iterator i=validity_exceptions.begin();
343 i != validity_exceptions.end(); i++)
351 return exception ? !position_valid : position_valid;
354 virtual MapNode getNode(v3s16 p)
356 if(isValidPosition(p) == false)
357 throw InvalidPositionException();
361 virtual void setNode(v3s16 p, MapNode & n)
363 if(isValidPosition(p) == false)
364 throw InvalidPositionException();
367 virtual u16 nodeContainerId() const
377 MapBlock b(&parent, v3s16(1,1,1));
378 v3s16 relpos(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
380 UASSERT(b.getPosRelative() == relpos);
382 UASSERT(b.getBox().MinEdge.X == MAP_BLOCKSIZE);
383 UASSERT(b.getBox().MaxEdge.X == MAP_BLOCKSIZE*2-1);
384 UASSERT(b.getBox().MinEdge.Y == MAP_BLOCKSIZE);
385 UASSERT(b.getBox().MaxEdge.Y == MAP_BLOCKSIZE*2-1);
386 UASSERT(b.getBox().MinEdge.Z == MAP_BLOCKSIZE);
387 UASSERT(b.getBox().MaxEdge.Z == MAP_BLOCKSIZE*2-1);
389 UASSERT(b.isValidPosition(v3s16(0,0,0)) == true);
390 UASSERT(b.isValidPosition(v3s16(-1,0,0)) == false);
391 UASSERT(b.isValidPosition(v3s16(-1,-142,-2341)) == false);
392 UASSERT(b.isValidPosition(v3s16(-124,142,2341)) == false);
393 UASSERT(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
394 UASSERT(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE,MAP_BLOCKSIZE-1)) == false);
397 TODO: this method should probably be removed
398 if the block size isn't going to be set variable
400 /*UASSERT(b.getSizeNodes() == v3s16(MAP_BLOCKSIZE,
401 MAP_BLOCKSIZE, MAP_BLOCKSIZE));*/
403 // Changed flag should be initially set
404 UASSERT(b.getModified() == MOD_STATE_WRITE_NEEDED);
406 UASSERT(b.getModified() == MOD_STATE_CLEAN);
408 // All nodes should have been set to
409 // .d=CONTENT_IGNORE and .getLight() = 0
410 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
411 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
412 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
414 //UASSERT(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_AIR);
415 UASSERT(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_IGNORE);
416 UASSERT(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_DAY) == 0);
417 UASSERT(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_NIGHT) == 0);
421 MapNode n(CONTENT_AIR);
422 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
423 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
424 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
426 b.setNode(v3s16(x,y,z), n);
431 Parent fetch functions
433 parent.position_valid = false;
434 parent.node.setContent(5);
438 // Positions in the block should still be valid
439 UASSERT(b.isValidPositionParent(v3s16(0,0,0)) == true);
440 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
441 n = b.getNodeParent(v3s16(0,MAP_BLOCKSIZE-1,0));
442 UASSERT(n.getContent() == CONTENT_AIR);
444 // ...but outside the block they should be invalid
445 UASSERT(b.isValidPositionParent(v3s16(-121,2341,0)) == false);
446 UASSERT(b.isValidPositionParent(v3s16(-1,0,0)) == false);
447 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == false);
450 bool exception_thrown = false;
452 // This should throw an exception
453 MapNode n = b.getNodeParent(v3s16(0,0,-1));
455 catch(InvalidPositionException &e)
457 exception_thrown = true;
459 UASSERT(exception_thrown);
462 parent.position_valid = true;
463 // Now the positions outside should be valid
464 UASSERT(b.isValidPositionParent(v3s16(-121,2341,0)) == true);
465 UASSERT(b.isValidPositionParent(v3s16(-1,0,0)) == true);
466 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == true);
467 n = b.getNodeParent(v3s16(0,0,MAP_BLOCKSIZE));
468 UASSERT(n.getContent() == 5);
476 UASSERT(b.getNode(p).getContent() == 4);
477 //TODO: Update to new system
478 /*UASSERT(b.getNodeTile(p) == 4);
479 UASSERT(b.getNodeTile(v3s16(-1,-1,0)) == 5);*/
484 // Set lighting of all nodes to 0
485 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
486 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
487 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
488 MapNode n = b.getNode(v3s16(x,y,z));
489 n.setLight(LIGHTBANK_DAY, 0);
490 n.setLight(LIGHTBANK_NIGHT, 0);
491 b.setNode(v3s16(x,y,z), n);
497 Check how the block handles being a lonely sky block
499 parent.position_valid = true;
500 b.setIsUnderground(false);
501 parent.node.setContent(CONTENT_AIR);
502 parent.node.setLight(LIGHTBANK_DAY, LIGHT_SUN);
503 parent.node.setLight(LIGHTBANK_NIGHT, 0);
504 core::map<v3s16, bool> light_sources;
505 // The bottom block is invalid, because we have a shadowing node
506 UASSERT(b.propagateSunlight(light_sources) == false);
507 UASSERT(b.getNode(v3s16(1,4,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
508 UASSERT(b.getNode(v3s16(1,3,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
509 UASSERT(b.getNode(v3s16(1,2,0)).getLight(LIGHTBANK_DAY) == 0);
510 UASSERT(b.getNode(v3s16(1,1,0)).getLight(LIGHTBANK_DAY) == 0);
511 UASSERT(b.getNode(v3s16(1,0,0)).getLight(LIGHTBANK_DAY) == 0);
512 UASSERT(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
513 UASSERT(b.getFaceLight2(1000, p, v3s16(0,1,0)) == LIGHT_SUN);
514 UASSERT(b.getFaceLight2(1000, p, v3s16(0,-1,0)) == 0);
515 UASSERT(b.getFaceLight2(0, p, v3s16(0,-1,0)) == 0);
516 // According to MapBlock::getFaceLight,
517 // The face on the z+ side should have double-diminished light
518 //UASSERT(b.getFaceLight(p, v3s16(0,0,1)) == diminish_light(diminish_light(LIGHT_MAX)));
519 // The face on the z+ side should have diminished light
520 UASSERT(b.getFaceLight2(1000, p, v3s16(0,0,1)) == diminish_light(LIGHT_MAX));
523 Check how the block handles being in between blocks with some non-sunlight
524 while being underground
527 // Make neighbours to exist and set some non-sunlight to them
528 parent.position_valid = true;
529 b.setIsUnderground(true);
530 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
531 core::map<v3s16, bool> light_sources;
532 // The block below should be valid because there shouldn't be
533 // sunlight in there either
534 UASSERT(b.propagateSunlight(light_sources, true) == true);
535 // Should not touch nodes that are not affected (that is, all of them)
536 //UASSERT(b.getNode(v3s16(1,2,3)).getLight() == LIGHT_SUN);
537 // Should set light of non-sunlighted blocks to 0.
538 UASSERT(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == 0);
541 Set up a situation where:
542 - There is only air in this block
543 - There is a valid non-sunlighted block at the bottom, and
544 - Invalid blocks elsewhere.
545 - the block is not underground.
547 This should result in bottom block invalidity
550 b.setIsUnderground(false);
552 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
553 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
554 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
556 n.setContent(CONTENT_AIR);
557 n.setLight(LIGHTBANK_DAY, 0);
558 b.setNode(v3s16(x,y,z), n);
562 // Make neighbours invalid
563 parent.position_valid = false;
564 // Add exceptions to the top of the bottom block
565 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
566 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
568 parent.validity_exceptions.push_back(v3s16(MAP_BLOCKSIZE+x, MAP_BLOCKSIZE-1, MAP_BLOCKSIZE+z));
570 // Lighting value for the valid nodes
571 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
572 core::map<v3s16, bool> light_sources;
573 // Bottom block is not valid
574 UASSERT(b.propagateSunlight(light_sources) == false);
579 struct TestMapSector: public TestBase
581 class TC : public NodeContainer
590 position_valid = true;
593 virtual bool isValidPosition(v3s16 p)
595 return position_valid;
598 virtual MapNode getNode(v3s16 p)
600 if(position_valid == false)
601 throw InvalidPositionException();
605 virtual void setNode(v3s16 p, MapNode & n)
607 if(position_valid == false)
608 throw InvalidPositionException();
611 virtual u16 nodeContainerId() const
620 parent.position_valid = false;
622 // Create one with no heightmaps
623 ServerMapSector sector(&parent, v2s16(1,1));
625 UASSERT(sector.getBlockNoCreateNoEx(0) == 0);
626 UASSERT(sector.getBlockNoCreateNoEx(1) == 0);
628 MapBlock * bref = sector.createBlankBlock(-2);
630 UASSERT(sector.getBlockNoCreateNoEx(0) == 0);
631 UASSERT(sector.getBlockNoCreateNoEx(-2) == bref);
633 //TODO: Check for AlreadyExistsException
635 /*bool exception_thrown = false;
639 catch(InvalidPositionException &e){
640 exception_thrown = true;
642 UASSERT(exception_thrown);*/