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 u64 t1 = porting::getTimeMs();
234 g_logger.setLevelSilenced(LL_ERROR, true);
236 u32 num_modules_failed = 0;
237 u32 num_total_tests_failed = 0;
238 u32 num_total_tests_run = 0;
239 std::vector<TestBase *> &testmods = TestManager::getTestModules();
240 for (size_t i = 0; i != testmods.size(); i++) {
241 if (!testmods[i]->testModule(&gamedef))
242 num_modules_failed++;
244 num_total_tests_failed += testmods[i]->num_tests_failed;
245 num_total_tests_run += testmods[i]->num_tests_run;
248 u64 tdiff = porting::getTimeMs() - t1;
250 g_logger.setLevelSilenced(LL_ERROR, false);
252 const char *overall_status = (num_modules_failed == 0) ? "PASSED" : "FAILED";
255 << "++++++++++++++++++++++++++++++++++++++++"
256 << "++++++++++++++++++++++++++++++++++++++++" << std::endl
257 << "Unit Test Results: " << overall_status << std::endl
258 << " " << num_modules_failed << " / " << testmods.size()
259 << " failed modules (" << num_total_tests_failed << " / "
260 << num_total_tests_run << " failed individual tests)." << std::endl
261 << " Testing took " << tdiff << "ms total." << std::endl
262 << "++++++++++++++++++++++++++++++++++++++++"
263 << "++++++++++++++++++++++++++++++++++++++++" << std::endl;
265 return num_modules_failed;
272 bool TestBase::testModule(IGameDef *gamedef)
274 rawstream << "======== Testing module " << getName() << std::endl;
275 u64 t1 = porting::getTimeMs();
280 u64 tdiff = porting::getTimeMs() - t1;
281 rawstream << "======== Module " << getName() << " "
282 << (num_tests_failed ? "failed" : "passed") << " (" << num_tests_failed
283 << " failures / " << num_tests_run << " tests) - " << tdiff
284 << "ms" << std::endl;
286 if (!m_test_dir.empty())
287 fs::RecursiveDelete(m_test_dir);
289 return num_tests_failed == 0;
292 std::string TestBase::getTestTempDirectory()
294 if (!m_test_dir.empty())
298 snprintf(buf, sizeof(buf), "%08X", myrand());
300 m_test_dir = fs::TempPath() + DIR_DELIM "mttest_" + buf;
301 if (!fs::CreateDir(m_test_dir))
302 throw TestFailedException();
307 std::string TestBase::getTestTempFile()
310 snprintf(buf, sizeof(buf), "%08X", myrand());
312 return getTestTempDirectory() + DIR_DELIM + buf + ".tmp";
317 NOTE: These tests became non-working then NodeContainer was removed.
318 These should be redone, utilizing some kind of a virtual
319 interface for Map (IMap would be fine).
322 struct TestMapBlock: public TestBase
324 class TC : public NodeContainer
330 core::list<v3s16> validity_exceptions;
334 position_valid = true;
337 virtual bool isValidPosition(v3s16 p)
339 //return position_valid ^ (p==position_valid_exception);
340 bool exception = false;
341 for(core::list<v3s16>::Iterator i=validity_exceptions.begin();
342 i != validity_exceptions.end(); i++)
350 return exception ? !position_valid : position_valid;
353 virtual MapNode getNode(v3s16 p)
355 if(isValidPosition(p) == false)
356 throw InvalidPositionException();
360 virtual void setNode(v3s16 p, MapNode & n)
362 if(isValidPosition(p) == false)
363 throw InvalidPositionException();
366 virtual u16 nodeContainerId() const
376 MapBlock b(&parent, v3s16(1,1,1));
377 v3s16 relpos(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
379 UASSERT(b.getPosRelative() == relpos);
381 UASSERT(b.getBox().MinEdge.X == MAP_BLOCKSIZE);
382 UASSERT(b.getBox().MaxEdge.X == MAP_BLOCKSIZE*2-1);
383 UASSERT(b.getBox().MinEdge.Y == MAP_BLOCKSIZE);
384 UASSERT(b.getBox().MaxEdge.Y == MAP_BLOCKSIZE*2-1);
385 UASSERT(b.getBox().MinEdge.Z == MAP_BLOCKSIZE);
386 UASSERT(b.getBox().MaxEdge.Z == MAP_BLOCKSIZE*2-1);
388 UASSERT(b.isValidPosition(v3s16(0,0,0)) == true);
389 UASSERT(b.isValidPosition(v3s16(-1,0,0)) == false);
390 UASSERT(b.isValidPosition(v3s16(-1,-142,-2341)) == false);
391 UASSERT(b.isValidPosition(v3s16(-124,142,2341)) == false);
392 UASSERT(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
393 UASSERT(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE,MAP_BLOCKSIZE-1)) == false);
396 TODO: this method should probably be removed
397 if the block size isn't going to be set variable
399 /*UASSERT(b.getSizeNodes() == v3s16(MAP_BLOCKSIZE,
400 MAP_BLOCKSIZE, MAP_BLOCKSIZE));*/
402 // Changed flag should be initially set
403 UASSERT(b.getModified() == MOD_STATE_WRITE_NEEDED);
405 UASSERT(b.getModified() == MOD_STATE_CLEAN);
407 // All nodes should have been set to
408 // .d=CONTENT_IGNORE and .getLight() = 0
409 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
410 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
411 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
413 //UASSERT(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_AIR);
414 UASSERT(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_IGNORE);
415 UASSERT(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_DAY) == 0);
416 UASSERT(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_NIGHT) == 0);
420 MapNode n(CONTENT_AIR);
421 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
422 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
423 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
425 b.setNode(v3s16(x,y,z), n);
430 Parent fetch functions
432 parent.position_valid = false;
433 parent.node.setContent(5);
437 // Positions in the block should still be valid
438 UASSERT(b.isValidPositionParent(v3s16(0,0,0)) == true);
439 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
440 n = b.getNodeParent(v3s16(0,MAP_BLOCKSIZE-1,0));
441 UASSERT(n.getContent() == CONTENT_AIR);
443 // ...but outside the block they should be invalid
444 UASSERT(b.isValidPositionParent(v3s16(-121,2341,0)) == false);
445 UASSERT(b.isValidPositionParent(v3s16(-1,0,0)) == false);
446 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == false);
449 bool exception_thrown = false;
451 // This should throw an exception
452 MapNode n = b.getNodeParent(v3s16(0,0,-1));
454 catch(InvalidPositionException &e)
456 exception_thrown = true;
458 UASSERT(exception_thrown);
461 parent.position_valid = true;
462 // Now the positions outside should be valid
463 UASSERT(b.isValidPositionParent(v3s16(-121,2341,0)) == true);
464 UASSERT(b.isValidPositionParent(v3s16(-1,0,0)) == true);
465 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == true);
466 n = b.getNodeParent(v3s16(0,0,MAP_BLOCKSIZE));
467 UASSERT(n.getContent() == 5);
475 UASSERT(b.getNode(p).getContent() == 4);
476 //TODO: Update to new system
477 /*UASSERT(b.getNodeTile(p) == 4);
478 UASSERT(b.getNodeTile(v3s16(-1,-1,0)) == 5);*/
483 // Set lighting of all nodes to 0
484 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
485 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
486 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
487 MapNode n = b.getNode(v3s16(x,y,z));
488 n.setLight(LIGHTBANK_DAY, 0);
489 n.setLight(LIGHTBANK_NIGHT, 0);
490 b.setNode(v3s16(x,y,z), n);
496 Check how the block handles being a lonely sky block
498 parent.position_valid = true;
499 b.setIsUnderground(false);
500 parent.node.setContent(CONTENT_AIR);
501 parent.node.setLight(LIGHTBANK_DAY, LIGHT_SUN);
502 parent.node.setLight(LIGHTBANK_NIGHT, 0);
503 core::map<v3s16, bool> light_sources;
504 // The bottom block is invalid, because we have a shadowing node
505 UASSERT(b.propagateSunlight(light_sources) == false);
506 UASSERT(b.getNode(v3s16(1,4,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
507 UASSERT(b.getNode(v3s16(1,3,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
508 UASSERT(b.getNode(v3s16(1,2,0)).getLight(LIGHTBANK_DAY) == 0);
509 UASSERT(b.getNode(v3s16(1,1,0)).getLight(LIGHTBANK_DAY) == 0);
510 UASSERT(b.getNode(v3s16(1,0,0)).getLight(LIGHTBANK_DAY) == 0);
511 UASSERT(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
512 UASSERT(b.getFaceLight2(1000, p, v3s16(0,1,0)) == LIGHT_SUN);
513 UASSERT(b.getFaceLight2(1000, p, v3s16(0,-1,0)) == 0);
514 UASSERT(b.getFaceLight2(0, p, v3s16(0,-1,0)) == 0);
515 // According to MapBlock::getFaceLight,
516 // The face on the z+ side should have double-diminished light
517 //UASSERT(b.getFaceLight(p, v3s16(0,0,1)) == diminish_light(diminish_light(LIGHT_MAX)));
518 // The face on the z+ side should have diminished light
519 UASSERT(b.getFaceLight2(1000, p, v3s16(0,0,1)) == diminish_light(LIGHT_MAX));
522 Check how the block handles being in between blocks with some non-sunlight
523 while being underground
526 // Make neighbours to exist and set some non-sunlight to them
527 parent.position_valid = true;
528 b.setIsUnderground(true);
529 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
530 core::map<v3s16, bool> light_sources;
531 // The block below should be valid because there shouldn't be
532 // sunlight in there either
533 UASSERT(b.propagateSunlight(light_sources, true) == true);
534 // Should not touch nodes that are not affected (that is, all of them)
535 //UASSERT(b.getNode(v3s16(1,2,3)).getLight() == LIGHT_SUN);
536 // Should set light of non-sunlighted blocks to 0.
537 UASSERT(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == 0);
540 Set up a situation where:
541 - There is only air in this block
542 - There is a valid non-sunlighted block at the bottom, and
543 - Invalid blocks elsewhere.
544 - the block is not underground.
546 This should result in bottom block invalidity
549 b.setIsUnderground(false);
551 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
552 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
553 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
555 n.setContent(CONTENT_AIR);
556 n.setLight(LIGHTBANK_DAY, 0);
557 b.setNode(v3s16(x,y,z), n);
561 // Make neighbours invalid
562 parent.position_valid = false;
563 // Add exceptions to the top of the bottom block
564 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
565 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
567 parent.validity_exceptions.push_back(v3s16(MAP_BLOCKSIZE+x, MAP_BLOCKSIZE-1, MAP_BLOCKSIZE+z));
569 // Lighting value for the valid nodes
570 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
571 core::map<v3s16, bool> light_sources;
572 // Bottom block is not valid
573 UASSERT(b.propagateSunlight(light_sources) == false);
578 struct TestMapSector: public TestBase
580 class TC : public NodeContainer
589 position_valid = true;
592 virtual bool isValidPosition(v3s16 p)
594 return position_valid;
597 virtual MapNode getNode(v3s16 p)
599 if(position_valid == false)
600 throw InvalidPositionException();
604 virtual void setNode(v3s16 p, MapNode & n)
606 if(position_valid == false)
607 throw InvalidPositionException();
610 virtual u16 nodeContainerId() const
619 parent.position_valid = false;
621 // Create one with no heightmaps
622 ServerMapSector sector(&parent, v2s16(1,1));
624 UASSERT(sector.getBlockNoCreateNoEx(0) == 0);
625 UASSERT(sector.getBlockNoCreateNoEx(1) == 0);
627 MapBlock * bref = sector.createBlankBlock(-2);
629 UASSERT(sector.getBlockNoCreateNoEx(0) == 0);
630 UASSERT(sector.getBlockNoCreateNoEx(-2) == bref);
632 //TODO: Check for AlreadyExistsException
634 /*bool exception_thrown = false;
638 catch(InvalidPositionException &e){
639 exception_thrown = true;
641 UASSERT(exception_thrown);*/