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.
26 #include "util/numeric.h"
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();
63 virtual const std::vector<ModSpec> &getMods() const
65 static std::vector<ModSpec> testmodspec;
68 virtual const ModSpec* getModSpec(const std::string &modname) const { return NULL; }
69 virtual std::string getModStoragePath() const { return "."; }
70 virtual bool registerModStorage(ModMetadata *meta) { return true; }
71 virtual void unregisterModStorage(const std::string &name) {}
74 IItemDefManager *m_itemdef = nullptr;
75 INodeDefManager *m_nodedef = nullptr;
76 ICraftDefManager *m_craftdef = nullptr;
77 ITextureSource *m_texturesrc = nullptr;
78 IShaderSource *m_shadersrc = nullptr;
79 ISoundManager *m_soundmgr = nullptr;
80 MtEventManager *m_eventmgr = nullptr;
81 scene::ISceneManager *m_scenemgr = nullptr;
82 IRollbackManager *m_rollbackmgr = nullptr;
83 EmergeManager *m_emergemgr = nullptr;
87 TestGameDef::TestGameDef()
89 m_itemdef = createItemDefManager();
90 m_nodedef = createNodeDefManager();
96 TestGameDef::~TestGameDef()
103 void TestGameDef::defineSomeNodes()
105 IWritableItemDefManager *idef = (IWritableItemDefManager *)m_itemdef;
106 IWritableNodeDefManager *ndef = (IWritableNodeDefManager *)m_nodedef;
108 ItemDefinition itemdef;
112 itemdef = ItemDefinition();
113 itemdef.type = ITEM_NODE;
114 itemdef.name = "default:stone";
115 itemdef.description = "Stone";
116 itemdef.groups["cracky"] = 3;
117 itemdef.inventory_image = "[inventorycube"
120 "{default_stone.png";
121 f = ContentFeatures();
122 f.name = itemdef.name;
123 for (TileDef &tiledef : f.tiledef)
124 tiledef.name = "default_stone.png";
125 f.is_ground_content = true;
126 idef->registerItem(itemdef);
127 t_CONTENT_STONE = ndef->set(f.name, f);
130 itemdef = ItemDefinition();
131 itemdef.type = ITEM_NODE;
132 itemdef.name = "default:dirt_with_grass";
133 itemdef.description = "Dirt with grass";
134 itemdef.groups["crumbly"] = 3;
135 itemdef.inventory_image = "[inventorycube"
137 "{default_dirt.png&default_grass_side.png"
138 "{default_dirt.png&default_grass_side.png";
139 f = ContentFeatures();
140 f.name = itemdef.name;
141 f.tiledef[0].name = "default_grass.png";
142 f.tiledef[1].name = "default_dirt.png";
143 for(int i = 2; i < 6; i++)
144 f.tiledef[i].name = "default_dirt.png^default_grass_side.png";
145 f.is_ground_content = true;
146 idef->registerItem(itemdef);
147 t_CONTENT_GRASS = ndef->set(f.name, f);
149 //// Torch (minimal definition for lighting tests)
150 itemdef = ItemDefinition();
151 itemdef.type = ITEM_NODE;
152 itemdef.name = "default:torch";
153 f = ContentFeatures();
154 f.name = itemdef.name;
155 f.param_type = CPT_LIGHT;
156 f.light_propagates = true;
157 f.sunlight_propagates = true;
158 f.light_source = LIGHT_MAX-1;
159 idef->registerItem(itemdef);
160 t_CONTENT_TORCH = ndef->set(f.name, f);
163 itemdef = ItemDefinition();
164 itemdef.type = ITEM_NODE;
165 itemdef.name = "default:water";
166 itemdef.description = "Water";
167 itemdef.inventory_image = "[inventorycube"
170 "{default_water.png";
171 f = ContentFeatures();
172 f.name = itemdef.name;
174 f.liquid_type = LIQUID_SOURCE;
175 f.liquid_viscosity = 4;
176 f.is_ground_content = true;
177 f.groups["liquids"] = 3;
178 for (TileDef &tiledef : f.tiledef)
179 tiledef.name = "default_water.png";
180 idef->registerItem(itemdef);
181 t_CONTENT_WATER = ndef->set(f.name, f);
184 itemdef = ItemDefinition();
185 itemdef.type = ITEM_NODE;
186 itemdef.name = "default:lava";
187 itemdef.description = "Lava";
188 itemdef.inventory_image = "[inventorycube"
192 f = ContentFeatures();
193 f.name = itemdef.name;
195 f.liquid_type = LIQUID_SOURCE;
196 f.liquid_viscosity = 7;
197 f.light_source = LIGHT_MAX-1;
198 f.is_ground_content = true;
199 f.groups["liquids"] = 3;
200 for (TileDef &tiledef : f.tiledef)
201 tiledef.name = "default_lava.png";
202 idef->registerItem(itemdef);
203 t_CONTENT_LAVA = ndef->set(f.name, f);
207 itemdef = ItemDefinition();
208 itemdef.type = ITEM_NODE;
209 itemdef.name = "default:brick";
210 itemdef.description = "Brick";
211 itemdef.groups["cracky"] = 3;
212 itemdef.inventory_image = "[inventorycube"
215 "{default_brick.png";
216 f = ContentFeatures();
217 f.name = itemdef.name;
218 for (TileDef &tiledef : f.tiledef)
219 tiledef.name = "default_brick.png";
220 f.is_ground_content = true;
221 idef->registerItem(itemdef);
222 t_CONTENT_BRICK = ndef->set(f.name, f);
231 DSTACK(FUNCTION_NAME);
233 u64 t1 = porting::getTimeMs();
236 g_logger.setLevelSilenced(LL_ERROR, true);
238 u32 num_modules_failed = 0;
239 u32 num_total_tests_failed = 0;
240 u32 num_total_tests_run = 0;
241 std::vector<TestBase *> &testmods = TestManager::getTestModules();
242 for (size_t i = 0; i != testmods.size(); i++) {
243 if (!testmods[i]->testModule(&gamedef))
244 num_modules_failed++;
246 num_total_tests_failed += testmods[i]->num_tests_failed;
247 num_total_tests_run += testmods[i]->num_tests_run;
250 u64 tdiff = porting::getTimeMs() - t1;
252 g_logger.setLevelSilenced(LL_ERROR, false);
254 const char *overall_status = (num_modules_failed == 0) ? "PASSED" : "FAILED";
257 << "++++++++++++++++++++++++++++++++++++++++"
258 << "++++++++++++++++++++++++++++++++++++++++" << std::endl
259 << "Unit Test Results: " << overall_status << std::endl
260 << " " << num_modules_failed << " / " << testmods.size()
261 << " failed modules (" << num_total_tests_failed << " / "
262 << num_total_tests_run << " failed individual tests)." << std::endl
263 << " Testing took " << tdiff << "ms total." << std::endl
264 << "++++++++++++++++++++++++++++++++++++++++"
265 << "++++++++++++++++++++++++++++++++++++++++" << std::endl;
267 return num_modules_failed;
274 bool TestBase::testModule(IGameDef *gamedef)
276 rawstream << "======== Testing module " << getName() << std::endl;
277 u64 t1 = porting::getTimeMs();
282 u64 tdiff = porting::getTimeMs() - t1;
283 rawstream << "======== Module " << getName() << " "
284 << (num_tests_failed ? "failed" : "passed") << " (" << num_tests_failed
285 << " failures / " << num_tests_run << " tests) - " << tdiff
286 << "ms" << std::endl;
288 if (!m_test_dir.empty())
289 fs::RecursiveDelete(m_test_dir);
291 return num_tests_failed == 0;
294 std::string TestBase::getTestTempDirectory()
296 if (!m_test_dir.empty())
300 snprintf(buf, sizeof(buf), "%08X", myrand());
302 m_test_dir = fs::TempPath() + DIR_DELIM "mttest_" + buf;
303 if (!fs::CreateDir(m_test_dir))
304 throw TestFailedException();
309 std::string TestBase::getTestTempFile()
312 snprintf(buf, sizeof(buf), "%08X", myrand());
314 return getTestTempDirectory() + DIR_DELIM + buf + ".tmp";
319 NOTE: These tests became non-working then NodeContainer was removed.
320 These should be redone, utilizing some kind of a virtual
321 interface for Map (IMap would be fine).
324 struct TestMapBlock: public TestBase
326 class TC : public NodeContainer
332 core::list<v3s16> validity_exceptions;
336 position_valid = true;
339 virtual bool isValidPosition(v3s16 p)
341 //return position_valid ^ (p==position_valid_exception);
342 bool exception = false;
343 for(core::list<v3s16>::Iterator i=validity_exceptions.begin();
344 i != validity_exceptions.end(); i++)
352 return exception ? !position_valid : position_valid;
355 virtual MapNode getNode(v3s16 p)
357 if(isValidPosition(p) == false)
358 throw InvalidPositionException();
362 virtual void setNode(v3s16 p, MapNode & n)
364 if(isValidPosition(p) == false)
365 throw InvalidPositionException();
368 virtual u16 nodeContainerId() const
378 MapBlock b(&parent, v3s16(1,1,1));
379 v3s16 relpos(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
381 UASSERT(b.getPosRelative() == relpos);
383 UASSERT(b.getBox().MinEdge.X == MAP_BLOCKSIZE);
384 UASSERT(b.getBox().MaxEdge.X == MAP_BLOCKSIZE*2-1);
385 UASSERT(b.getBox().MinEdge.Y == MAP_BLOCKSIZE);
386 UASSERT(b.getBox().MaxEdge.Y == MAP_BLOCKSIZE*2-1);
387 UASSERT(b.getBox().MinEdge.Z == MAP_BLOCKSIZE);
388 UASSERT(b.getBox().MaxEdge.Z == MAP_BLOCKSIZE*2-1);
390 UASSERT(b.isValidPosition(v3s16(0,0,0)) == true);
391 UASSERT(b.isValidPosition(v3s16(-1,0,0)) == false);
392 UASSERT(b.isValidPosition(v3s16(-1,-142,-2341)) == false);
393 UASSERT(b.isValidPosition(v3s16(-124,142,2341)) == false);
394 UASSERT(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
395 UASSERT(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE,MAP_BLOCKSIZE-1)) == false);
398 TODO: this method should probably be removed
399 if the block size isn't going to be set variable
401 /*UASSERT(b.getSizeNodes() == v3s16(MAP_BLOCKSIZE,
402 MAP_BLOCKSIZE, MAP_BLOCKSIZE));*/
404 // Changed flag should be initially set
405 UASSERT(b.getModified() == MOD_STATE_WRITE_NEEDED);
407 UASSERT(b.getModified() == MOD_STATE_CLEAN);
409 // All nodes should have been set to
410 // .d=CONTENT_IGNORE and .getLight() = 0
411 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
412 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
413 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
415 //UASSERT(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_AIR);
416 UASSERT(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_IGNORE);
417 UASSERT(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_DAY) == 0);
418 UASSERT(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_NIGHT) == 0);
422 MapNode n(CONTENT_AIR);
423 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
424 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
425 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
427 b.setNode(v3s16(x,y,z), n);
432 Parent fetch functions
434 parent.position_valid = false;
435 parent.node.setContent(5);
439 // Positions in the block should still be valid
440 UASSERT(b.isValidPositionParent(v3s16(0,0,0)) == true);
441 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
442 n = b.getNodeParent(v3s16(0,MAP_BLOCKSIZE-1,0));
443 UASSERT(n.getContent() == CONTENT_AIR);
445 // ...but outside the block they should be invalid
446 UASSERT(b.isValidPositionParent(v3s16(-121,2341,0)) == false);
447 UASSERT(b.isValidPositionParent(v3s16(-1,0,0)) == false);
448 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == false);
451 bool exception_thrown = false;
453 // This should throw an exception
454 MapNode n = b.getNodeParent(v3s16(0,0,-1));
456 catch(InvalidPositionException &e)
458 exception_thrown = true;
460 UASSERT(exception_thrown);
463 parent.position_valid = true;
464 // Now the positions outside should be valid
465 UASSERT(b.isValidPositionParent(v3s16(-121,2341,0)) == true);
466 UASSERT(b.isValidPositionParent(v3s16(-1,0,0)) == true);
467 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == true);
468 n = b.getNodeParent(v3s16(0,0,MAP_BLOCKSIZE));
469 UASSERT(n.getContent() == 5);
477 UASSERT(b.getNode(p).getContent() == 4);
478 //TODO: Update to new system
479 /*UASSERT(b.getNodeTile(p) == 4);
480 UASSERT(b.getNodeTile(v3s16(-1,-1,0)) == 5);*/
485 // Set lighting of all nodes to 0
486 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
487 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
488 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
489 MapNode n = b.getNode(v3s16(x,y,z));
490 n.setLight(LIGHTBANK_DAY, 0);
491 n.setLight(LIGHTBANK_NIGHT, 0);
492 b.setNode(v3s16(x,y,z), n);
498 Check how the block handles being a lonely sky block
500 parent.position_valid = true;
501 b.setIsUnderground(false);
502 parent.node.setContent(CONTENT_AIR);
503 parent.node.setLight(LIGHTBANK_DAY, LIGHT_SUN);
504 parent.node.setLight(LIGHTBANK_NIGHT, 0);
505 core::map<v3s16, bool> light_sources;
506 // The bottom block is invalid, because we have a shadowing node
507 UASSERT(b.propagateSunlight(light_sources) == false);
508 UASSERT(b.getNode(v3s16(1,4,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
509 UASSERT(b.getNode(v3s16(1,3,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
510 UASSERT(b.getNode(v3s16(1,2,0)).getLight(LIGHTBANK_DAY) == 0);
511 UASSERT(b.getNode(v3s16(1,1,0)).getLight(LIGHTBANK_DAY) == 0);
512 UASSERT(b.getNode(v3s16(1,0,0)).getLight(LIGHTBANK_DAY) == 0);
513 UASSERT(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
514 UASSERT(b.getFaceLight2(1000, p, v3s16(0,1,0)) == LIGHT_SUN);
515 UASSERT(b.getFaceLight2(1000, p, v3s16(0,-1,0)) == 0);
516 UASSERT(b.getFaceLight2(0, p, v3s16(0,-1,0)) == 0);
517 // According to MapBlock::getFaceLight,
518 // The face on the z+ side should have double-diminished light
519 //UASSERT(b.getFaceLight(p, v3s16(0,0,1)) == diminish_light(diminish_light(LIGHT_MAX)));
520 // The face on the z+ side should have diminished light
521 UASSERT(b.getFaceLight2(1000, p, v3s16(0,0,1)) == diminish_light(LIGHT_MAX));
524 Check how the block handles being in between blocks with some non-sunlight
525 while being underground
528 // Make neighbours to exist and set some non-sunlight to them
529 parent.position_valid = true;
530 b.setIsUnderground(true);
531 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
532 core::map<v3s16, bool> light_sources;
533 // The block below should be valid because there shouldn't be
534 // sunlight in there either
535 UASSERT(b.propagateSunlight(light_sources, true) == true);
536 // Should not touch nodes that are not affected (that is, all of them)
537 //UASSERT(b.getNode(v3s16(1,2,3)).getLight() == LIGHT_SUN);
538 // Should set light of non-sunlighted blocks to 0.
539 UASSERT(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == 0);
542 Set up a situation where:
543 - There is only air in this block
544 - There is a valid non-sunlighted block at the bottom, and
545 - Invalid blocks elsewhere.
546 - the block is not underground.
548 This should result in bottom block invalidity
551 b.setIsUnderground(false);
553 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
554 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
555 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
557 n.setContent(CONTENT_AIR);
558 n.setLight(LIGHTBANK_DAY, 0);
559 b.setNode(v3s16(x,y,z), n);
563 // Make neighbours invalid
564 parent.position_valid = false;
565 // Add exceptions to the top of the bottom block
566 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
567 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
569 parent.validity_exceptions.push_back(v3s16(MAP_BLOCKSIZE+x, MAP_BLOCKSIZE-1, MAP_BLOCKSIZE+z));
571 // Lighting value for the valid nodes
572 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
573 core::map<v3s16, bool> light_sources;
574 // Bottom block is not valid
575 UASSERT(b.propagateSunlight(light_sources) == false);
580 struct TestMapSector: public TestBase
582 class TC : public NodeContainer
591 position_valid = true;
594 virtual bool isValidPosition(v3s16 p)
596 return position_valid;
599 virtual MapNode getNode(v3s16 p)
601 if(position_valid == false)
602 throw InvalidPositionException();
606 virtual void setNode(v3s16 p, MapNode & n)
608 if(position_valid == false)
609 throw InvalidPositionException();
612 virtual u16 nodeContainerId() const
621 parent.position_valid = false;
623 // Create one with no heightmaps
624 ServerMapSector sector(&parent, v2s16(1,1));
626 UASSERT(sector.getBlockNoCreateNoEx(0) == 0);
627 UASSERT(sector.getBlockNoCreateNoEx(1) == 0);
629 MapBlock * bref = sector.createBlankBlock(-2);
631 UASSERT(sector.getBlockNoCreateNoEx(0) == 0);
632 UASSERT(sector.getBlockNoCreateNoEx(-2) == bref);
634 //TODO: Check for AlreadyExistsException
636 /*bool exception_thrown = false;
640 catch(InvalidPositionException &e){
641 exception_thrown = true;
643 UASSERT(exception_thrown);*/