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.
22 #include "client/sound.h"
26 #include "modchannels.h"
27 #include "content/mods.h"
28 #include "util/numeric.h"
31 content_t t_CONTENT_STONE;
32 content_t t_CONTENT_GRASS;
33 content_t t_CONTENT_TORCH;
34 content_t t_CONTENT_WATER;
35 content_t t_CONTENT_LAVA;
36 content_t t_CONTENT_BRICK;
38 ////////////////////////////////////////////////////////////////////////////////
44 class TestGameDef : public IGameDef {
49 IItemDefManager *getItemDefManager() { return m_itemdef; }
50 const NodeDefManager *getNodeDefManager() { return m_nodedef; }
51 ICraftDefManager *getCraftDefManager() { return m_craftdef; }
52 ITextureSource *getTextureSource() { return m_texturesrc; }
53 IShaderSource *getShaderSource() { return m_shadersrc; }
54 ISoundManager *getSoundManager() { return m_soundmgr; }
55 scene::ISceneManager *getSceneManager() { return m_scenemgr; }
56 IRollbackManager *getRollbackManager() { return m_rollbackmgr; }
57 EmergeManager *getEmergeManager() { return m_emergemgr; }
59 scene::IAnimatedMesh *getMesh(const std::string &filename) { return NULL; }
60 bool checkLocalPrivilege(const std::string &priv) { return false; }
61 u16 allocateUnknownNodeId(const std::string &name) { return 0; }
63 void defineSomeNodes();
65 virtual const std::vector<ModSpec> &getMods() const
67 static std::vector<ModSpec> testmodspec;
70 virtual const ModSpec* getModSpec(const std::string &modname) const { return NULL; }
71 virtual std::string getModStoragePath() const { return "."; }
72 virtual bool registerModStorage(ModMetadata *meta) { return true; }
73 virtual void unregisterModStorage(const std::string &name) {}
74 bool joinModChannel(const std::string &channel);
75 bool leaveModChannel(const std::string &channel);
76 bool sendModChannelMessage(const std::string &channel, const std::string &message);
77 ModChannel *getModChannel(const std::string &channel)
79 return m_modchannel_mgr->getModChannel(channel);
83 IItemDefManager *m_itemdef = nullptr;
84 const NodeDefManager *m_nodedef = nullptr;
85 ICraftDefManager *m_craftdef = nullptr;
86 ITextureSource *m_texturesrc = nullptr;
87 IShaderSource *m_shadersrc = nullptr;
88 ISoundManager *m_soundmgr = nullptr;
89 scene::ISceneManager *m_scenemgr = nullptr;
90 IRollbackManager *m_rollbackmgr = nullptr;
91 EmergeManager *m_emergemgr = nullptr;
92 std::unique_ptr<ModChannelMgr> m_modchannel_mgr;
96 TestGameDef::TestGameDef() :
97 m_modchannel_mgr(new ModChannelMgr())
99 m_itemdef = createItemDefManager();
100 m_nodedef = createNodeDefManager();
106 TestGameDef::~TestGameDef()
113 void TestGameDef::defineSomeNodes()
115 IWritableItemDefManager *idef = (IWritableItemDefManager *)m_itemdef;
116 NodeDefManager *ndef = (NodeDefManager *)m_nodedef;
118 ItemDefinition itemdef;
122 itemdef = ItemDefinition();
123 itemdef.type = ITEM_NODE;
124 itemdef.name = "default:stone";
125 itemdef.description = "Stone";
126 itemdef.groups["cracky"] = 3;
127 itemdef.inventory_image = "[inventorycube"
130 "{default_stone.png";
131 f = ContentFeatures();
132 f.name = itemdef.name;
133 for (TileDef &tiledef : f.tiledef)
134 tiledef.name = "default_stone.png";
135 f.is_ground_content = true;
136 idef->registerItem(itemdef);
137 t_CONTENT_STONE = ndef->set(f.name, f);
140 itemdef = ItemDefinition();
141 itemdef.type = ITEM_NODE;
142 itemdef.name = "default:dirt_with_grass";
143 itemdef.description = "Dirt with grass";
144 itemdef.groups["crumbly"] = 3;
145 itemdef.inventory_image = "[inventorycube"
147 "{default_dirt.png&default_grass_side.png"
148 "{default_dirt.png&default_grass_side.png";
149 f = ContentFeatures();
150 f.name = itemdef.name;
151 f.tiledef[0].name = "default_grass.png";
152 f.tiledef[1].name = "default_dirt.png";
153 for(int i = 2; i < 6; i++)
154 f.tiledef[i].name = "default_dirt.png^default_grass_side.png";
155 f.is_ground_content = true;
156 idef->registerItem(itemdef);
157 t_CONTENT_GRASS = ndef->set(f.name, f);
159 //// Torch (minimal definition for lighting tests)
160 itemdef = ItemDefinition();
161 itemdef.type = ITEM_NODE;
162 itemdef.name = "default:torch";
163 f = ContentFeatures();
164 f.name = itemdef.name;
165 f.param_type = CPT_LIGHT;
166 f.light_propagates = true;
167 f.sunlight_propagates = true;
168 f.light_source = LIGHT_MAX-1;
169 idef->registerItem(itemdef);
170 t_CONTENT_TORCH = ndef->set(f.name, f);
173 itemdef = ItemDefinition();
174 itemdef.type = ITEM_NODE;
175 itemdef.name = "default:water";
176 itemdef.description = "Water";
177 itemdef.inventory_image = "[inventorycube"
180 "{default_water.png";
181 f = ContentFeatures();
182 f.name = itemdef.name;
184 f.liquid_type = LIQUID_SOURCE;
185 f.liquid_viscosity = 4;
186 f.is_ground_content = true;
187 f.groups["liquids"] = 3;
188 for (TileDef &tiledef : f.tiledef)
189 tiledef.name = "default_water.png";
190 idef->registerItem(itemdef);
191 t_CONTENT_WATER = ndef->set(f.name, f);
194 itemdef = ItemDefinition();
195 itemdef.type = ITEM_NODE;
196 itemdef.name = "default:lava";
197 itemdef.description = "Lava";
198 itemdef.inventory_image = "[inventorycube"
202 f = ContentFeatures();
203 f.name = itemdef.name;
205 f.liquid_type = LIQUID_SOURCE;
206 f.liquid_viscosity = 7;
207 f.light_source = LIGHT_MAX-1;
208 f.is_ground_content = true;
209 f.groups["liquids"] = 3;
210 for (TileDef &tiledef : f.tiledef)
211 tiledef.name = "default_lava.png";
212 idef->registerItem(itemdef);
213 t_CONTENT_LAVA = ndef->set(f.name, f);
217 itemdef = ItemDefinition();
218 itemdef.type = ITEM_NODE;
219 itemdef.name = "default:brick";
220 itemdef.description = "Brick";
221 itemdef.groups["cracky"] = 3;
222 itemdef.inventory_image = "[inventorycube"
225 "{default_brick.png";
226 f = ContentFeatures();
227 f.name = itemdef.name;
228 for (TileDef &tiledef : f.tiledef)
229 tiledef.name = "default_brick.png";
230 f.is_ground_content = true;
231 idef->registerItem(itemdef);
232 t_CONTENT_BRICK = ndef->set(f.name, f);
235 bool TestGameDef::joinModChannel(const std::string &channel)
237 return m_modchannel_mgr->joinChannel(channel, PEER_ID_SERVER);
240 bool TestGameDef::leaveModChannel(const std::string &channel)
242 return m_modchannel_mgr->leaveChannel(channel, PEER_ID_SERVER);
245 bool TestGameDef::sendModChannelMessage(const std::string &channel,
246 const std::string &message)
248 if (!m_modchannel_mgr->channelRegistered(channel))
260 u64 t1 = porting::getTimeMs();
263 g_logger.setLevelSilenced(LL_ERROR, true);
265 u32 num_modules_failed = 0;
266 u32 num_total_tests_failed = 0;
267 u32 num_total_tests_run = 0;
268 std::vector<TestBase *> &testmods = TestManager::getTestModules();
269 for (size_t i = 0; i != testmods.size(); i++) {
270 if (!testmods[i]->testModule(&gamedef))
271 num_modules_failed++;
273 num_total_tests_failed += testmods[i]->num_tests_failed;
274 num_total_tests_run += testmods[i]->num_tests_run;
277 u64 tdiff = porting::getTimeMs() - t1;
279 g_logger.setLevelSilenced(LL_ERROR, false);
281 const char *overall_status = (num_modules_failed == 0) ? "PASSED" : "FAILED";
284 << "++++++++++++++++++++++++++++++++++++++++"
285 << "++++++++++++++++++++++++++++++++++++++++" << std::endl
286 << "Unit Test Results: " << overall_status << std::endl
287 << " " << num_modules_failed << " / " << testmods.size()
288 << " failed modules (" << num_total_tests_failed << " / "
289 << num_total_tests_run << " failed individual tests)." << std::endl
290 << " Testing took " << tdiff << "ms total." << std::endl
291 << "++++++++++++++++++++++++++++++++++++++++"
292 << "++++++++++++++++++++++++++++++++++++++++" << std::endl;
294 return num_modules_failed;
301 bool TestBase::testModule(IGameDef *gamedef)
303 rawstream << "======== Testing module " << getName() << std::endl;
304 u64 t1 = porting::getTimeMs();
309 u64 tdiff = porting::getTimeMs() - t1;
310 rawstream << "======== Module " << getName() << " "
311 << (num_tests_failed ? "failed" : "passed") << " (" << num_tests_failed
312 << " failures / " << num_tests_run << " tests) - " << tdiff
313 << "ms" << std::endl;
315 if (!m_test_dir.empty())
316 fs::RecursiveDelete(m_test_dir);
318 return num_tests_failed == 0;
321 std::string TestBase::getTestTempDirectory()
323 if (!m_test_dir.empty())
327 porting::mt_snprintf(buf, sizeof(buf), "%08X", myrand());
329 m_test_dir = fs::TempPath() + DIR_DELIM "mttest_" + buf;
330 if (!fs::CreateDir(m_test_dir))
331 throw TestFailedException();
336 std::string TestBase::getTestTempFile()
339 porting::mt_snprintf(buf, sizeof(buf), "%08X", myrand());
341 return getTestTempDirectory() + DIR_DELIM + buf + ".tmp";
346 NOTE: These tests became non-working then NodeContainer was removed.
347 These should be redone, utilizing some kind of a virtual
348 interface for Map (IMap would be fine).
351 struct TestMapBlock: public TestBase
353 class TC : public NodeContainer
359 core::list<v3s16> validity_exceptions;
363 position_valid = true;
366 virtual bool isValidPosition(v3s16 p)
368 //return position_valid ^ (p==position_valid_exception);
369 bool exception = false;
370 for(core::list<v3s16>::Iterator i=validity_exceptions.begin();
371 i != validity_exceptions.end(); i++)
379 return exception ? !position_valid : position_valid;
382 virtual MapNode getNode(v3s16 p)
384 if(isValidPosition(p) == false)
385 throw InvalidPositionException();
389 virtual void setNode(v3s16 p, MapNode & n)
391 if(isValidPosition(p) == false)
392 throw InvalidPositionException();
395 virtual u16 nodeContainerId() const
405 MapBlock b(&parent, v3s16(1,1,1));
406 v3s16 relpos(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
408 UASSERT(b.getPosRelative() == relpos);
410 UASSERT(b.getBox().MinEdge.X == MAP_BLOCKSIZE);
411 UASSERT(b.getBox().MaxEdge.X == MAP_BLOCKSIZE*2-1);
412 UASSERT(b.getBox().MinEdge.Y == MAP_BLOCKSIZE);
413 UASSERT(b.getBox().MaxEdge.Y == MAP_BLOCKSIZE*2-1);
414 UASSERT(b.getBox().MinEdge.Z == MAP_BLOCKSIZE);
415 UASSERT(b.getBox().MaxEdge.Z == MAP_BLOCKSIZE*2-1);
417 UASSERT(b.isValidPosition(v3s16(0,0,0)) == true);
418 UASSERT(b.isValidPosition(v3s16(-1,0,0)) == false);
419 UASSERT(b.isValidPosition(v3s16(-1,-142,-2341)) == false);
420 UASSERT(b.isValidPosition(v3s16(-124,142,2341)) == false);
421 UASSERT(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
422 UASSERT(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE,MAP_BLOCKSIZE-1)) == false);
425 TODO: this method should probably be removed
426 if the block size isn't going to be set variable
428 /*UASSERT(b.getSizeNodes() == v3s16(MAP_BLOCKSIZE,
429 MAP_BLOCKSIZE, MAP_BLOCKSIZE));*/
431 // Changed flag should be initially set
432 UASSERT(b.getModified() == MOD_STATE_WRITE_NEEDED);
434 UASSERT(b.getModified() == MOD_STATE_CLEAN);
436 // All nodes should have been set to
437 // .d=CONTENT_IGNORE and .getLight() = 0
438 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
439 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
440 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
442 //UASSERT(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_AIR);
443 UASSERT(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_IGNORE);
444 UASSERT(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_DAY) == 0);
445 UASSERT(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_NIGHT) == 0);
449 MapNode n(CONTENT_AIR);
450 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
451 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
452 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
454 b.setNode(v3s16(x,y,z), n);
459 Parent fetch functions
461 parent.position_valid = false;
462 parent.node.setContent(5);
466 // Positions in the block should still be valid
467 UASSERT(b.isValidPositionParent(v3s16(0,0,0)) == true);
468 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
469 n = b.getNodeParent(v3s16(0,MAP_BLOCKSIZE-1,0));
470 UASSERT(n.getContent() == CONTENT_AIR);
472 // ...but outside the block they should be invalid
473 UASSERT(b.isValidPositionParent(v3s16(-121,2341,0)) == false);
474 UASSERT(b.isValidPositionParent(v3s16(-1,0,0)) == false);
475 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == false);
478 bool exception_thrown = false;
480 // This should throw an exception
481 MapNode n = b.getNodeParent(v3s16(0,0,-1));
483 catch(InvalidPositionException &e)
485 exception_thrown = true;
487 UASSERT(exception_thrown);
490 parent.position_valid = true;
491 // Now the positions outside should be valid
492 UASSERT(b.isValidPositionParent(v3s16(-121,2341,0)) == true);
493 UASSERT(b.isValidPositionParent(v3s16(-1,0,0)) == true);
494 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == true);
495 n = b.getNodeParent(v3s16(0,0,MAP_BLOCKSIZE));
496 UASSERT(n.getContent() == 5);
504 UASSERT(b.getNode(p).getContent() == 4);
505 //TODO: Update to new system
506 /*UASSERT(b.getNodeTile(p) == 4);
507 UASSERT(b.getNodeTile(v3s16(-1,-1,0)) == 5);*/
512 // Set lighting of all nodes to 0
513 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
514 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
515 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
516 MapNode n = b.getNode(v3s16(x,y,z));
517 n.setLight(LIGHTBANK_DAY, 0);
518 n.setLight(LIGHTBANK_NIGHT, 0);
519 b.setNode(v3s16(x,y,z), n);
525 Check how the block handles being a lonely sky block
527 parent.position_valid = true;
528 b.setIsUnderground(false);
529 parent.node.setContent(CONTENT_AIR);
530 parent.node.setLight(LIGHTBANK_DAY, LIGHT_SUN);
531 parent.node.setLight(LIGHTBANK_NIGHT, 0);
532 core::map<v3s16, bool> light_sources;
533 // The bottom block is invalid, because we have a shadowing node
534 UASSERT(b.propagateSunlight(light_sources) == false);
535 UASSERT(b.getNode(v3s16(1,4,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
536 UASSERT(b.getNode(v3s16(1,3,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
537 UASSERT(b.getNode(v3s16(1,2,0)).getLight(LIGHTBANK_DAY) == 0);
538 UASSERT(b.getNode(v3s16(1,1,0)).getLight(LIGHTBANK_DAY) == 0);
539 UASSERT(b.getNode(v3s16(1,0,0)).getLight(LIGHTBANK_DAY) == 0);
540 UASSERT(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
541 UASSERT(b.getFaceLight2(1000, p, v3s16(0,1,0)) == LIGHT_SUN);
542 UASSERT(b.getFaceLight2(1000, p, v3s16(0,-1,0)) == 0);
543 UASSERT(b.getFaceLight2(0, p, v3s16(0,-1,0)) == 0);
544 // According to MapBlock::getFaceLight,
545 // The face on the z+ side should have double-diminished light
546 //UASSERT(b.getFaceLight(p, v3s16(0,0,1)) == diminish_light(diminish_light(LIGHT_MAX)));
547 // The face on the z+ side should have diminished light
548 UASSERT(b.getFaceLight2(1000, p, v3s16(0,0,1)) == diminish_light(LIGHT_MAX));
551 Check how the block handles being in between blocks with some non-sunlight
552 while being underground
555 // Make neighbours to exist and set some non-sunlight to them
556 parent.position_valid = true;
557 b.setIsUnderground(true);
558 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
559 core::map<v3s16, bool> light_sources;
560 // The block below should be valid because there shouldn't be
561 // sunlight in there either
562 UASSERT(b.propagateSunlight(light_sources, true) == true);
563 // Should not touch nodes that are not affected (that is, all of them)
564 //UASSERT(b.getNode(v3s16(1,2,3)).getLight() == LIGHT_SUN);
565 // Should set light of non-sunlighted blocks to 0.
566 UASSERT(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == 0);
569 Set up a situation where:
570 - There is only air in this block
571 - There is a valid non-sunlighted block at the bottom, and
572 - Invalid blocks elsewhere.
573 - the block is not underground.
575 This should result in bottom block invalidity
578 b.setIsUnderground(false);
580 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
581 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
582 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
584 n.setContent(CONTENT_AIR);
585 n.setLight(LIGHTBANK_DAY, 0);
586 b.setNode(v3s16(x,y,z), n);
590 // Make neighbours invalid
591 parent.position_valid = false;
592 // Add exceptions to the top of the bottom block
593 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
594 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
596 parent.validity_exceptions.push_back(v3s16(MAP_BLOCKSIZE+x, MAP_BLOCKSIZE-1, MAP_BLOCKSIZE+z));
598 // Lighting value for the valid nodes
599 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
600 core::map<v3s16, bool> light_sources;
601 // Bottom block is not valid
602 UASSERT(b.propagateSunlight(light_sources) == false);
607 struct TestMapSector: public TestBase
609 class TC : public NodeContainer
618 position_valid = true;
621 virtual bool isValidPosition(v3s16 p)
623 return position_valid;
626 virtual MapNode getNode(v3s16 p)
628 if(position_valid == false)
629 throw InvalidPositionException();
633 virtual void setNode(v3s16 p, MapNode & n)
635 if(position_valid == false)
636 throw InvalidPositionException();
639 virtual u16 nodeContainerId() const
648 parent.position_valid = false;
650 // Create one with no heightmaps
651 ServerMapSector sector(&parent, v2s16(1,1));
653 UASSERT(sector.getBlockNoCreateNoEx(0) == nullptr);
654 UASSERT(sector.getBlockNoCreateNoEx(1) == nullptr);
656 MapBlock * bref = sector.createBlankBlock(-2);
658 UASSERT(sector.getBlockNoCreateNoEx(0) == nullptr);
659 UASSERT(sector.getBlockNoCreateNoEx(-2) == bref);
661 //TODO: Check for AlreadyExistsException
663 /*bool exception_thrown = false;
667 catch(InvalidPositionException &e){
668 exception_thrown = true;
670 UASSERT(exception_thrown);*/