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 "database/database-dummy.h"
29 #include "util/numeric.h"
32 content_t t_CONTENT_STONE;
33 content_t t_CONTENT_GRASS;
34 content_t t_CONTENT_TORCH;
35 content_t t_CONTENT_WATER;
36 content_t t_CONTENT_LAVA;
37 content_t t_CONTENT_BRICK;
39 ////////////////////////////////////////////////////////////////////////////////
45 class TestGameDef : public IGameDef {
50 IItemDefManager *getItemDefManager() { return m_itemdef; }
51 IWritableItemDefManager *getWritableItemDefManager() { return m_itemdef; }
52 const NodeDefManager *getNodeDefManager() { return m_nodedef; }
53 NodeDefManager *getWritableNodeDefManager() { return m_nodedef; }
54 ICraftDefManager *getCraftDefManager() { return m_craftdef; }
55 ITextureSource *getTextureSource() { return m_texturesrc; }
56 IShaderSource *getShaderSource() { return m_shadersrc; }
57 ISoundManager *getSoundManager() { return m_soundmgr; }
58 scene::ISceneManager *getSceneManager() { return m_scenemgr; }
59 IRollbackManager *getRollbackManager() { return m_rollbackmgr; }
60 EmergeManager *getEmergeManager() { return m_emergemgr; }
61 ModMetadataDatabase *getModStorageDatabase() { return m_mod_storage_database; }
63 scene::IAnimatedMesh *getMesh(const std::string &filename) { return NULL; }
64 bool checkLocalPrivilege(const std::string &priv) { return false; }
65 u16 allocateUnknownNodeId(const std::string &name) { return 0; }
67 void defineSomeNodes();
69 virtual const std::vector<ModSpec> &getMods() const
71 static std::vector<ModSpec> testmodspec;
74 virtual const ModSpec* getModSpec(const std::string &modname) const { return NULL; }
75 virtual bool registerModStorage(ModMetadata *meta) { return true; }
76 virtual void unregisterModStorage(const std::string &name) {}
77 bool joinModChannel(const std::string &channel);
78 bool leaveModChannel(const std::string &channel);
79 bool sendModChannelMessage(const std::string &channel, const std::string &message);
80 ModChannel *getModChannel(const std::string &channel)
82 return m_modchannel_mgr->getModChannel(channel);
86 IWritableItemDefManager *m_itemdef = nullptr;
87 NodeDefManager *m_nodedef = nullptr;
88 ICraftDefManager *m_craftdef = nullptr;
89 ITextureSource *m_texturesrc = nullptr;
90 IShaderSource *m_shadersrc = nullptr;
91 ISoundManager *m_soundmgr = nullptr;
92 scene::ISceneManager *m_scenemgr = nullptr;
93 IRollbackManager *m_rollbackmgr = nullptr;
94 EmergeManager *m_emergemgr = nullptr;
95 ModMetadataDatabase *m_mod_storage_database = nullptr;
96 std::unique_ptr<ModChannelMgr> m_modchannel_mgr;
100 TestGameDef::TestGameDef() :
101 m_mod_storage_database(new Database_Dummy()),
102 m_modchannel_mgr(new ModChannelMgr())
104 m_itemdef = createItemDefManager();
105 m_nodedef = createNodeDefManager();
111 TestGameDef::~TestGameDef()
115 delete m_mod_storage_database;
119 void TestGameDef::defineSomeNodes()
121 IWritableItemDefManager *idef = (IWritableItemDefManager *)m_itemdef;
122 NodeDefManager *ndef = (NodeDefManager *)m_nodedef;
124 ItemDefinition itemdef;
128 itemdef = ItemDefinition();
129 itemdef.type = ITEM_NODE;
130 itemdef.name = "default:stone";
131 itemdef.description = "Stone";
132 itemdef.groups["cracky"] = 3;
133 itemdef.inventory_image = "[inventorycube"
136 "{default_stone.png";
137 f = ContentFeatures();
138 f.name = itemdef.name;
139 for (TileDef &tiledef : f.tiledef)
140 tiledef.name = "default_stone.png";
141 f.is_ground_content = true;
142 idef->registerItem(itemdef);
143 t_CONTENT_STONE = ndef->set(f.name, f);
146 itemdef = ItemDefinition();
147 itemdef.type = ITEM_NODE;
148 itemdef.name = "default:dirt_with_grass";
149 itemdef.description = "Dirt with grass";
150 itemdef.groups["crumbly"] = 3;
151 itemdef.inventory_image = "[inventorycube"
153 "{default_dirt.png&default_grass_side.png"
154 "{default_dirt.png&default_grass_side.png";
155 f = ContentFeatures();
156 f.name = itemdef.name;
157 f.tiledef[0].name = "default_grass.png";
158 f.tiledef[1].name = "default_dirt.png";
159 for(int i = 2; i < 6; i++)
160 f.tiledef[i].name = "default_dirt.png^default_grass_side.png";
161 f.is_ground_content = true;
162 idef->registerItem(itemdef);
163 t_CONTENT_GRASS = ndef->set(f.name, f);
165 //// Torch (minimal definition for lighting tests)
166 itemdef = ItemDefinition();
167 itemdef.type = ITEM_NODE;
168 itemdef.name = "default:torch";
169 f = ContentFeatures();
170 f.name = itemdef.name;
171 f.param_type = CPT_LIGHT;
172 f.light_propagates = true;
173 f.sunlight_propagates = true;
174 f.light_source = LIGHT_MAX-1;
175 idef->registerItem(itemdef);
176 t_CONTENT_TORCH = ndef->set(f.name, f);
179 itemdef = ItemDefinition();
180 itemdef.type = ITEM_NODE;
181 itemdef.name = "default:water";
182 itemdef.description = "Water";
183 itemdef.inventory_image = "[inventorycube"
186 "{default_water.png";
187 f = ContentFeatures();
188 f.name = itemdef.name;
189 f.alpha = ALPHAMODE_BLEND;
190 f.liquid_type = LIQUID_SOURCE;
191 f.liquid_viscosity = 4;
192 f.is_ground_content = true;
193 f.groups["liquids"] = 3;
194 for (TileDef &tiledef : f.tiledef)
195 tiledef.name = "default_water.png";
196 idef->registerItem(itemdef);
197 t_CONTENT_WATER = ndef->set(f.name, f);
200 itemdef = ItemDefinition();
201 itemdef.type = ITEM_NODE;
202 itemdef.name = "default:lava";
203 itemdef.description = "Lava";
204 itemdef.inventory_image = "[inventorycube"
208 f = ContentFeatures();
209 f.name = itemdef.name;
210 f.alpha = ALPHAMODE_OPAQUE;
211 f.liquid_type = LIQUID_SOURCE;
212 f.liquid_viscosity = 7;
213 f.light_source = LIGHT_MAX-1;
214 f.is_ground_content = true;
215 f.groups["liquids"] = 3;
216 for (TileDef &tiledef : f.tiledef)
217 tiledef.name = "default_lava.png";
218 idef->registerItem(itemdef);
219 t_CONTENT_LAVA = ndef->set(f.name, f);
223 itemdef = ItemDefinition();
224 itemdef.type = ITEM_NODE;
225 itemdef.name = "default:brick";
226 itemdef.description = "Brick";
227 itemdef.groups["cracky"] = 3;
228 itemdef.inventory_image = "[inventorycube"
231 "{default_brick.png";
232 f = ContentFeatures();
233 f.name = itemdef.name;
234 for (TileDef &tiledef : f.tiledef)
235 tiledef.name = "default_brick.png";
236 f.is_ground_content = true;
237 idef->registerItem(itemdef);
238 t_CONTENT_BRICK = ndef->set(f.name, f);
241 bool TestGameDef::joinModChannel(const std::string &channel)
243 return m_modchannel_mgr->joinChannel(channel, PEER_ID_SERVER);
246 bool TestGameDef::leaveModChannel(const std::string &channel)
248 return m_modchannel_mgr->leaveChannel(channel, PEER_ID_SERVER);
251 bool TestGameDef::sendModChannelMessage(const std::string &channel,
252 const std::string &message)
254 if (!m_modchannel_mgr->channelRegistered(channel))
266 u64 t1 = porting::getTimeMs();
269 g_logger.setLevelSilenced(LL_ERROR, true);
271 u32 num_modules_failed = 0;
272 u32 num_total_tests_failed = 0;
273 u32 num_total_tests_run = 0;
274 std::vector<TestBase *> &testmods = TestManager::getTestModules();
275 for (size_t i = 0; i != testmods.size(); i++) {
276 if (!testmods[i]->testModule(&gamedef))
277 num_modules_failed++;
279 num_total_tests_failed += testmods[i]->num_tests_failed;
280 num_total_tests_run += testmods[i]->num_tests_run;
283 u64 tdiff = porting::getTimeMs() - t1;
285 g_logger.setLevelSilenced(LL_ERROR, false);
287 const char *overall_status = (num_modules_failed == 0) ? "PASSED" : "FAILED";
290 << "++++++++++++++++++++++++++++++++++++++++"
291 << "++++++++++++++++++++++++++++++++++++++++" << std::endl
292 << "Unit Test Results: " << overall_status << std::endl
293 << " " << num_modules_failed << " / " << testmods.size()
294 << " failed modules (" << num_total_tests_failed << " / "
295 << num_total_tests_run << " failed individual tests)." << std::endl
296 << " Testing took " << tdiff << "ms total." << std::endl
297 << "++++++++++++++++++++++++++++++++++++++++"
298 << "++++++++++++++++++++++++++++++++++++++++" << std::endl;
300 return num_modules_failed;
307 bool TestBase::testModule(IGameDef *gamedef)
309 rawstream << "======== Testing module " << getName() << std::endl;
310 u64 t1 = porting::getTimeMs();
315 u64 tdiff = porting::getTimeMs() - t1;
316 rawstream << "======== Module " << getName() << " "
317 << (num_tests_failed ? "failed" : "passed") << " (" << num_tests_failed
318 << " failures / " << num_tests_run << " tests) - " << tdiff
319 << "ms" << std::endl;
321 if (!m_test_dir.empty())
322 fs::RecursiveDelete(m_test_dir);
324 return num_tests_failed == 0;
327 std::string TestBase::getTestTempDirectory()
329 if (!m_test_dir.empty())
333 porting::mt_snprintf(buf, sizeof(buf), "%08X", myrand());
335 m_test_dir = fs::TempPath() + DIR_DELIM "mttest_" + buf;
336 if (!fs::CreateDir(m_test_dir))
337 throw TestFailedException();
342 std::string TestBase::getTestTempFile()
345 porting::mt_snprintf(buf, sizeof(buf), "%08X", myrand());
347 return getTestTempDirectory() + DIR_DELIM + buf + ".tmp";
352 NOTE: These tests became non-working then NodeContainer was removed.
353 These should be redone, utilizing some kind of a virtual
354 interface for Map (IMap would be fine).
357 struct TestMapBlock: public TestBase
359 class TC : public NodeContainer
365 core::list<v3s16> validity_exceptions;
369 position_valid = true;
372 virtual bool isValidPosition(v3s16 p)
374 //return position_valid ^ (p==position_valid_exception);
375 bool exception = false;
376 for(core::list<v3s16>::Iterator i=validity_exceptions.begin();
377 i != validity_exceptions.end(); i++)
385 return exception ? !position_valid : position_valid;
388 virtual MapNode getNode(v3s16 p)
390 if(isValidPosition(p) == false)
391 throw InvalidPositionException();
395 virtual void setNode(v3s16 p, MapNode & n)
397 if(isValidPosition(p) == false)
398 throw InvalidPositionException();
401 virtual u16 nodeContainerId() const
411 MapBlock b(&parent, v3s16(1,1,1));
412 v3s16 relpos(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
414 UASSERT(b.getPosRelative() == relpos);
416 UASSERT(b.getBox().MinEdge.X == MAP_BLOCKSIZE);
417 UASSERT(b.getBox().MaxEdge.X == MAP_BLOCKSIZE*2-1);
418 UASSERT(b.getBox().MinEdge.Y == MAP_BLOCKSIZE);
419 UASSERT(b.getBox().MaxEdge.Y == MAP_BLOCKSIZE*2-1);
420 UASSERT(b.getBox().MinEdge.Z == MAP_BLOCKSIZE);
421 UASSERT(b.getBox().MaxEdge.Z == MAP_BLOCKSIZE*2-1);
423 UASSERT(b.isValidPosition(v3s16(0,0,0)) == true);
424 UASSERT(b.isValidPosition(v3s16(-1,0,0)) == false);
425 UASSERT(b.isValidPosition(v3s16(-1,-142,-2341)) == false);
426 UASSERT(b.isValidPosition(v3s16(-124,142,2341)) == false);
427 UASSERT(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
428 UASSERT(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE,MAP_BLOCKSIZE-1)) == false);
431 TODO: this method should probably be removed
432 if the block size isn't going to be set variable
434 /*UASSERT(b.getSizeNodes() == v3s16(MAP_BLOCKSIZE,
435 MAP_BLOCKSIZE, MAP_BLOCKSIZE));*/
437 // Changed flag should be initially set
438 UASSERT(b.getModified() == MOD_STATE_WRITE_NEEDED);
440 UASSERT(b.getModified() == MOD_STATE_CLEAN);
442 // All nodes should have been set to
443 // .d=CONTENT_IGNORE and .getLight() = 0
444 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
445 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
446 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
448 //UASSERT(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_AIR);
449 UASSERT(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_IGNORE);
450 UASSERT(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_DAY) == 0);
451 UASSERT(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_NIGHT) == 0);
455 MapNode n(CONTENT_AIR);
456 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
457 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
458 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
460 b.setNode(v3s16(x,y,z), n);
465 Parent fetch functions
467 parent.position_valid = false;
468 parent.node.setContent(5);
472 // Positions in the block should still be valid
473 UASSERT(b.isValidPositionParent(v3s16(0,0,0)) == true);
474 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
475 n = b.getNodeParent(v3s16(0,MAP_BLOCKSIZE-1,0));
476 UASSERT(n.getContent() == CONTENT_AIR);
478 // ...but outside the block they should be invalid
479 UASSERT(b.isValidPositionParent(v3s16(-121,2341,0)) == false);
480 UASSERT(b.isValidPositionParent(v3s16(-1,0,0)) == false);
481 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == false);
484 bool exception_thrown = false;
486 // This should throw an exception
487 MapNode n = b.getNodeParent(v3s16(0,0,-1));
489 catch(InvalidPositionException &e)
491 exception_thrown = true;
493 UASSERT(exception_thrown);
496 parent.position_valid = true;
497 // Now the positions outside should be valid
498 UASSERT(b.isValidPositionParent(v3s16(-121,2341,0)) == true);
499 UASSERT(b.isValidPositionParent(v3s16(-1,0,0)) == true);
500 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == true);
501 n = b.getNodeParent(v3s16(0,0,MAP_BLOCKSIZE));
502 UASSERT(n.getContent() == 5);
510 UASSERT(b.getNode(p).getContent() == 4);
511 //TODO: Update to new system
512 /*UASSERT(b.getNodeTile(p) == 4);
513 UASSERT(b.getNodeTile(v3s16(-1,-1,0)) == 5);*/
518 // Set lighting of all nodes to 0
519 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
520 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
521 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
522 MapNode n = b.getNode(v3s16(x,y,z));
523 n.setLight(LIGHTBANK_DAY, 0);
524 n.setLight(LIGHTBANK_NIGHT, 0);
525 b.setNode(v3s16(x,y,z), n);
531 Check how the block handles being a lonely sky block
533 parent.position_valid = true;
534 b.setIsUnderground(false);
535 parent.node.setContent(CONTENT_AIR);
536 parent.node.setLight(LIGHTBANK_DAY, LIGHT_SUN);
537 parent.node.setLight(LIGHTBANK_NIGHT, 0);
538 core::map<v3s16, bool> light_sources;
539 // The bottom block is invalid, because we have a shadowing node
540 UASSERT(b.propagateSunlight(light_sources) == false);
541 UASSERT(b.getNode(v3s16(1,4,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
542 UASSERT(b.getNode(v3s16(1,3,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
543 UASSERT(b.getNode(v3s16(1,2,0)).getLight(LIGHTBANK_DAY) == 0);
544 UASSERT(b.getNode(v3s16(1,1,0)).getLight(LIGHTBANK_DAY) == 0);
545 UASSERT(b.getNode(v3s16(1,0,0)).getLight(LIGHTBANK_DAY) == 0);
546 UASSERT(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
547 UASSERT(b.getFaceLight2(1000, p, v3s16(0,1,0)) == LIGHT_SUN);
548 UASSERT(b.getFaceLight2(1000, p, v3s16(0,-1,0)) == 0);
549 UASSERT(b.getFaceLight2(0, p, v3s16(0,-1,0)) == 0);
550 // According to MapBlock::getFaceLight,
551 // The face on the z+ side should have double-diminished light
552 //UASSERT(b.getFaceLight(p, v3s16(0,0,1)) == diminish_light(diminish_light(LIGHT_MAX)));
553 // The face on the z+ side should have diminished light
554 UASSERT(b.getFaceLight2(1000, p, v3s16(0,0,1)) == diminish_light(LIGHT_MAX));
557 Check how the block handles being in between blocks with some non-sunlight
558 while being underground
561 // Make neighbours to exist and set some non-sunlight to them
562 parent.position_valid = true;
563 b.setIsUnderground(true);
564 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
565 core::map<v3s16, bool> light_sources;
566 // The block below should be valid because there shouldn't be
567 // sunlight in there either
568 UASSERT(b.propagateSunlight(light_sources, true) == true);
569 // Should not touch nodes that are not affected (that is, all of them)
570 //UASSERT(b.getNode(v3s16(1,2,3)).getLight() == LIGHT_SUN);
571 // Should set light of non-sunlighted blocks to 0.
572 UASSERT(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == 0);
575 Set up a situation where:
576 - There is only air in this block
577 - There is a valid non-sunlighted block at the bottom, and
578 - Invalid blocks elsewhere.
579 - the block is not underground.
581 This should result in bottom block invalidity
584 b.setIsUnderground(false);
586 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
587 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
588 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
590 n.setContent(CONTENT_AIR);
591 n.setLight(LIGHTBANK_DAY, 0);
592 b.setNode(v3s16(x,y,z), n);
596 // Make neighbours invalid
597 parent.position_valid = false;
598 // Add exceptions to the top of the bottom block
599 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
600 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
602 parent.validity_exceptions.push_back(v3s16(MAP_BLOCKSIZE+x, MAP_BLOCKSIZE-1, MAP_BLOCKSIZE+z));
604 // Lighting value for the valid nodes
605 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
606 core::map<v3s16, bool> light_sources;
607 // Bottom block is not valid
608 UASSERT(b.propagateSunlight(light_sources) == false);
613 struct TestMapSector: public TestBase
615 class TC : public NodeContainer
624 position_valid = true;
627 virtual bool isValidPosition(v3s16 p)
629 return position_valid;
632 virtual MapNode getNode(v3s16 p)
634 if(position_valid == false)
635 throw InvalidPositionException();
639 virtual void setNode(v3s16 p, MapNode & n)
641 if(position_valid == false)
642 throw InvalidPositionException();
645 virtual u16 nodeContainerId() const
654 parent.position_valid = false;
656 // Create one with no heightmaps
657 ServerMapSector sector(&parent, v2s16(1,1));
659 UASSERT(sector.getBlockNoCreateNoEx(0) == nullptr);
660 UASSERT(sector.getBlockNoCreateNoEx(1) == nullptr);
662 MapBlock * bref = sector.createBlankBlock(-2);
664 UASSERT(sector.getBlockNoCreateNoEx(0) == nullptr);
665 UASSERT(sector.getBlockNoCreateNoEx(-2) == bref);
667 //TODO: Check for AlreadyExistsException
669 /*bool exception_thrown = false;
673 catch(InvalidPositionException &e){
674 exception_thrown = true;
676 UASSERT(exception_thrown);*/