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 const NodeDefManager *getNodeDefManager() { return m_nodedef; }
52 ICraftDefManager *getCraftDefManager() { return m_craftdef; }
53 ITextureSource *getTextureSource() { return m_texturesrc; }
54 IShaderSource *getShaderSource() { return m_shadersrc; }
55 ISoundManager *getSoundManager() { return m_soundmgr; }
56 scene::ISceneManager *getSceneManager() { return m_scenemgr; }
57 IRollbackManager *getRollbackManager() { return m_rollbackmgr; }
58 EmergeManager *getEmergeManager() { return m_emergemgr; }
59 ModMetadataDatabase *getModStorageDatabase() { return m_mod_storage_database; }
61 scene::IAnimatedMesh *getMesh(const std::string &filename) { return NULL; }
62 bool checkLocalPrivilege(const std::string &priv) { return false; }
63 u16 allocateUnknownNodeId(const std::string &name) { return 0; }
65 void defineSomeNodes();
67 virtual const std::vector<ModSpec> &getMods() const
69 static std::vector<ModSpec> testmodspec;
72 virtual const ModSpec* getModSpec(const std::string &modname) const { return NULL; }
73 virtual bool registerModStorage(ModMetadata *meta) { return true; }
74 virtual void unregisterModStorage(const std::string &name) {}
75 bool joinModChannel(const std::string &channel);
76 bool leaveModChannel(const std::string &channel);
77 bool sendModChannelMessage(const std::string &channel, const std::string &message);
78 ModChannel *getModChannel(const std::string &channel)
80 return m_modchannel_mgr->getModChannel(channel);
84 IItemDefManager *m_itemdef = nullptr;
85 const NodeDefManager *m_nodedef = nullptr;
86 ICraftDefManager *m_craftdef = nullptr;
87 ITextureSource *m_texturesrc = nullptr;
88 IShaderSource *m_shadersrc = nullptr;
89 ISoundManager *m_soundmgr = nullptr;
90 scene::ISceneManager *m_scenemgr = nullptr;
91 IRollbackManager *m_rollbackmgr = nullptr;
92 EmergeManager *m_emergemgr = nullptr;
93 ModMetadataDatabase *m_mod_storage_database = nullptr;
94 std::unique_ptr<ModChannelMgr> m_modchannel_mgr;
98 TestGameDef::TestGameDef() :
99 m_mod_storage_database(new Database_Dummy()),
100 m_modchannel_mgr(new ModChannelMgr())
102 m_itemdef = createItemDefManager();
103 m_nodedef = createNodeDefManager();
109 TestGameDef::~TestGameDef()
113 delete m_mod_storage_database;
117 void TestGameDef::defineSomeNodes()
119 IWritableItemDefManager *idef = (IWritableItemDefManager *)m_itemdef;
120 NodeDefManager *ndef = (NodeDefManager *)m_nodedef;
122 ItemDefinition itemdef;
126 itemdef = ItemDefinition();
127 itemdef.type = ITEM_NODE;
128 itemdef.name = "default:stone";
129 itemdef.description = "Stone";
130 itemdef.groups["cracky"] = 3;
131 itemdef.inventory_image = "[inventorycube"
134 "{default_stone.png";
135 f = ContentFeatures();
136 f.name = itemdef.name;
137 for (TileDef &tiledef : f.tiledef)
138 tiledef.name = "default_stone.png";
139 f.is_ground_content = true;
140 idef->registerItem(itemdef);
141 t_CONTENT_STONE = ndef->set(f.name, f);
144 itemdef = ItemDefinition();
145 itemdef.type = ITEM_NODE;
146 itemdef.name = "default:dirt_with_grass";
147 itemdef.description = "Dirt with grass";
148 itemdef.groups["crumbly"] = 3;
149 itemdef.inventory_image = "[inventorycube"
151 "{default_dirt.png&default_grass_side.png"
152 "{default_dirt.png&default_grass_side.png";
153 f = ContentFeatures();
154 f.name = itemdef.name;
155 f.tiledef[0].name = "default_grass.png";
156 f.tiledef[1].name = "default_dirt.png";
157 for(int i = 2; i < 6; i++)
158 f.tiledef[i].name = "default_dirt.png^default_grass_side.png";
159 f.is_ground_content = true;
160 idef->registerItem(itemdef);
161 t_CONTENT_GRASS = ndef->set(f.name, f);
163 //// Torch (minimal definition for lighting tests)
164 itemdef = ItemDefinition();
165 itemdef.type = ITEM_NODE;
166 itemdef.name = "default:torch";
167 f = ContentFeatures();
168 f.name = itemdef.name;
169 f.param_type = CPT_LIGHT;
170 f.light_propagates = true;
171 f.sunlight_propagates = true;
172 f.light_source = LIGHT_MAX-1;
173 idef->registerItem(itemdef);
174 t_CONTENT_TORCH = ndef->set(f.name, f);
177 itemdef = ItemDefinition();
178 itemdef.type = ITEM_NODE;
179 itemdef.name = "default:water";
180 itemdef.description = "Water";
181 itemdef.inventory_image = "[inventorycube"
184 "{default_water.png";
185 f = ContentFeatures();
186 f.name = itemdef.name;
187 f.alpha = ALPHAMODE_BLEND;
188 f.liquid_type = LIQUID_SOURCE;
189 f.liquid_viscosity = 4;
190 f.is_ground_content = true;
191 f.groups["liquids"] = 3;
192 for (TileDef &tiledef : f.tiledef)
193 tiledef.name = "default_water.png";
194 idef->registerItem(itemdef);
195 t_CONTENT_WATER = ndef->set(f.name, f);
198 itemdef = ItemDefinition();
199 itemdef.type = ITEM_NODE;
200 itemdef.name = "default:lava";
201 itemdef.description = "Lava";
202 itemdef.inventory_image = "[inventorycube"
206 f = ContentFeatures();
207 f.name = itemdef.name;
208 f.alpha = ALPHAMODE_OPAQUE;
209 f.liquid_type = LIQUID_SOURCE;
210 f.liquid_viscosity = 7;
211 f.light_source = LIGHT_MAX-1;
212 f.is_ground_content = true;
213 f.groups["liquids"] = 3;
214 for (TileDef &tiledef : f.tiledef)
215 tiledef.name = "default_lava.png";
216 idef->registerItem(itemdef);
217 t_CONTENT_LAVA = ndef->set(f.name, f);
221 itemdef = ItemDefinition();
222 itemdef.type = ITEM_NODE;
223 itemdef.name = "default:brick";
224 itemdef.description = "Brick";
225 itemdef.groups["cracky"] = 3;
226 itemdef.inventory_image = "[inventorycube"
229 "{default_brick.png";
230 f = ContentFeatures();
231 f.name = itemdef.name;
232 for (TileDef &tiledef : f.tiledef)
233 tiledef.name = "default_brick.png";
234 f.is_ground_content = true;
235 idef->registerItem(itemdef);
236 t_CONTENT_BRICK = ndef->set(f.name, f);
239 bool TestGameDef::joinModChannel(const std::string &channel)
241 return m_modchannel_mgr->joinChannel(channel, PEER_ID_SERVER);
244 bool TestGameDef::leaveModChannel(const std::string &channel)
246 return m_modchannel_mgr->leaveChannel(channel, PEER_ID_SERVER);
249 bool TestGameDef::sendModChannelMessage(const std::string &channel,
250 const std::string &message)
252 if (!m_modchannel_mgr->channelRegistered(channel))
264 u64 t1 = porting::getTimeMs();
267 g_logger.setLevelSilenced(LL_ERROR, true);
269 u32 num_modules_failed = 0;
270 u32 num_total_tests_failed = 0;
271 u32 num_total_tests_run = 0;
272 std::vector<TestBase *> &testmods = TestManager::getTestModules();
273 for (size_t i = 0; i != testmods.size(); i++) {
274 if (!testmods[i]->testModule(&gamedef))
275 num_modules_failed++;
277 num_total_tests_failed += testmods[i]->num_tests_failed;
278 num_total_tests_run += testmods[i]->num_tests_run;
281 u64 tdiff = porting::getTimeMs() - t1;
283 g_logger.setLevelSilenced(LL_ERROR, false);
285 const char *overall_status = (num_modules_failed == 0) ? "PASSED" : "FAILED";
288 << "++++++++++++++++++++++++++++++++++++++++"
289 << "++++++++++++++++++++++++++++++++++++++++" << std::endl
290 << "Unit Test Results: " << overall_status << std::endl
291 << " " << num_modules_failed << " / " << testmods.size()
292 << " failed modules (" << num_total_tests_failed << " / "
293 << num_total_tests_run << " failed individual tests)." << std::endl
294 << " Testing took " << tdiff << "ms total." << std::endl
295 << "++++++++++++++++++++++++++++++++++++++++"
296 << "++++++++++++++++++++++++++++++++++++++++" << std::endl;
298 return num_modules_failed;
305 bool TestBase::testModule(IGameDef *gamedef)
307 rawstream << "======== Testing module " << getName() << std::endl;
308 u64 t1 = porting::getTimeMs();
313 u64 tdiff = porting::getTimeMs() - t1;
314 rawstream << "======== Module " << getName() << " "
315 << (num_tests_failed ? "failed" : "passed") << " (" << num_tests_failed
316 << " failures / " << num_tests_run << " tests) - " << tdiff
317 << "ms" << std::endl;
319 if (!m_test_dir.empty())
320 fs::RecursiveDelete(m_test_dir);
322 return num_tests_failed == 0;
325 std::string TestBase::getTestTempDirectory()
327 if (!m_test_dir.empty())
331 porting::mt_snprintf(buf, sizeof(buf), "%08X", myrand());
333 m_test_dir = fs::TempPath() + DIR_DELIM "mttest_" + buf;
334 if (!fs::CreateDir(m_test_dir))
335 throw TestFailedException();
340 std::string TestBase::getTestTempFile()
343 porting::mt_snprintf(buf, sizeof(buf), "%08X", myrand());
345 return getTestTempDirectory() + DIR_DELIM + buf + ".tmp";
350 NOTE: These tests became non-working then NodeContainer was removed.
351 These should be redone, utilizing some kind of a virtual
352 interface for Map (IMap would be fine).
355 struct TestMapBlock: public TestBase
357 class TC : public NodeContainer
363 core::list<v3s16> validity_exceptions;
367 position_valid = true;
370 virtual bool isValidPosition(v3s16 p)
372 //return position_valid ^ (p==position_valid_exception);
373 bool exception = false;
374 for(core::list<v3s16>::Iterator i=validity_exceptions.begin();
375 i != validity_exceptions.end(); i++)
383 return exception ? !position_valid : position_valid;
386 virtual MapNode getNode(v3s16 p)
388 if(isValidPosition(p) == false)
389 throw InvalidPositionException();
393 virtual void setNode(v3s16 p, MapNode & n)
395 if(isValidPosition(p) == false)
396 throw InvalidPositionException();
399 virtual u16 nodeContainerId() const
409 MapBlock b(&parent, v3s16(1,1,1));
410 v3s16 relpos(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
412 UASSERT(b.getPosRelative() == relpos);
414 UASSERT(b.getBox().MinEdge.X == MAP_BLOCKSIZE);
415 UASSERT(b.getBox().MaxEdge.X == MAP_BLOCKSIZE*2-1);
416 UASSERT(b.getBox().MinEdge.Y == MAP_BLOCKSIZE);
417 UASSERT(b.getBox().MaxEdge.Y == MAP_BLOCKSIZE*2-1);
418 UASSERT(b.getBox().MinEdge.Z == MAP_BLOCKSIZE);
419 UASSERT(b.getBox().MaxEdge.Z == MAP_BLOCKSIZE*2-1);
421 UASSERT(b.isValidPosition(v3s16(0,0,0)) == true);
422 UASSERT(b.isValidPosition(v3s16(-1,0,0)) == false);
423 UASSERT(b.isValidPosition(v3s16(-1,-142,-2341)) == false);
424 UASSERT(b.isValidPosition(v3s16(-124,142,2341)) == false);
425 UASSERT(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
426 UASSERT(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE,MAP_BLOCKSIZE-1)) == false);
429 TODO: this method should probably be removed
430 if the block size isn't going to be set variable
432 /*UASSERT(b.getSizeNodes() == v3s16(MAP_BLOCKSIZE,
433 MAP_BLOCKSIZE, MAP_BLOCKSIZE));*/
435 // Changed flag should be initially set
436 UASSERT(b.getModified() == MOD_STATE_WRITE_NEEDED);
438 UASSERT(b.getModified() == MOD_STATE_CLEAN);
440 // All nodes should have been set to
441 // .d=CONTENT_IGNORE and .getLight() = 0
442 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
443 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
444 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
446 //UASSERT(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_AIR);
447 UASSERT(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_IGNORE);
448 UASSERT(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_DAY) == 0);
449 UASSERT(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_NIGHT) == 0);
453 MapNode n(CONTENT_AIR);
454 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
455 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
456 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
458 b.setNode(v3s16(x,y,z), n);
463 Parent fetch functions
465 parent.position_valid = false;
466 parent.node.setContent(5);
470 // Positions in the block should still be valid
471 UASSERT(b.isValidPositionParent(v3s16(0,0,0)) == true);
472 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
473 n = b.getNodeParent(v3s16(0,MAP_BLOCKSIZE-1,0));
474 UASSERT(n.getContent() == CONTENT_AIR);
476 // ...but outside the block they should be invalid
477 UASSERT(b.isValidPositionParent(v3s16(-121,2341,0)) == false);
478 UASSERT(b.isValidPositionParent(v3s16(-1,0,0)) == false);
479 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == false);
482 bool exception_thrown = false;
484 // This should throw an exception
485 MapNode n = b.getNodeParent(v3s16(0,0,-1));
487 catch(InvalidPositionException &e)
489 exception_thrown = true;
491 UASSERT(exception_thrown);
494 parent.position_valid = true;
495 // Now the positions outside should be valid
496 UASSERT(b.isValidPositionParent(v3s16(-121,2341,0)) == true);
497 UASSERT(b.isValidPositionParent(v3s16(-1,0,0)) == true);
498 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == true);
499 n = b.getNodeParent(v3s16(0,0,MAP_BLOCKSIZE));
500 UASSERT(n.getContent() == 5);
508 UASSERT(b.getNode(p).getContent() == 4);
509 //TODO: Update to new system
510 /*UASSERT(b.getNodeTile(p) == 4);
511 UASSERT(b.getNodeTile(v3s16(-1,-1,0)) == 5);*/
516 // Set lighting of all nodes to 0
517 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
518 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
519 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
520 MapNode n = b.getNode(v3s16(x,y,z));
521 n.setLight(LIGHTBANK_DAY, 0);
522 n.setLight(LIGHTBANK_NIGHT, 0);
523 b.setNode(v3s16(x,y,z), n);
529 Check how the block handles being a lonely sky block
531 parent.position_valid = true;
532 b.setIsUnderground(false);
533 parent.node.setContent(CONTENT_AIR);
534 parent.node.setLight(LIGHTBANK_DAY, LIGHT_SUN);
535 parent.node.setLight(LIGHTBANK_NIGHT, 0);
536 std::map<v3s16, bool> light_sources;
537 // The bottom block is invalid, because we have a shadowing node
538 UASSERT(b.propagateSunlight(light_sources) == false);
539 UASSERT(b.getNode(v3s16(1,4,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
540 UASSERT(b.getNode(v3s16(1,3,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
541 UASSERT(b.getNode(v3s16(1,2,0)).getLight(LIGHTBANK_DAY) == 0);
542 UASSERT(b.getNode(v3s16(1,1,0)).getLight(LIGHTBANK_DAY) == 0);
543 UASSERT(b.getNode(v3s16(1,0,0)).getLight(LIGHTBANK_DAY) == 0);
544 UASSERT(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
545 UASSERT(b.getFaceLight2(1000, p, v3s16(0,1,0)) == LIGHT_SUN);
546 UASSERT(b.getFaceLight2(1000, p, v3s16(0,-1,0)) == 0);
547 UASSERT(b.getFaceLight2(0, p, v3s16(0,-1,0)) == 0);
548 // According to MapBlock::getFaceLight,
549 // The face on the z+ side should have double-diminished light
550 //UASSERT(b.getFaceLight(p, v3s16(0,0,1)) == diminish_light(diminish_light(LIGHT_MAX)));
551 // The face on the z+ side should have diminished light
552 UASSERT(b.getFaceLight2(1000, p, v3s16(0,0,1)) == diminish_light(LIGHT_MAX));
555 Check how the block handles being in between blocks with some non-sunlight
556 while being underground
559 // Make neighbours to exist and set some non-sunlight to them
560 parent.position_valid = true;
561 b.setIsUnderground(true);
562 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
563 std::map<v3s16, bool> light_sources;
564 // The block below should be valid because there shouldn't be
565 // sunlight in there either
566 UASSERT(b.propagateSunlight(light_sources, true) == true);
567 // Should not touch nodes that are not affected (that is, all of them)
568 //UASSERT(b.getNode(v3s16(1,2,3)).getLight() == LIGHT_SUN);
569 // Should set light of non-sunlighted blocks to 0.
570 UASSERT(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == 0);
573 Set up a situation where:
574 - There is only air in this block
575 - There is a valid non-sunlighted block at the bottom, and
576 - Invalid blocks elsewhere.
577 - the block is not underground.
579 This should result in bottom block invalidity
582 b.setIsUnderground(false);
584 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
585 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
586 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
588 n.setContent(CONTENT_AIR);
589 n.setLight(LIGHTBANK_DAY, 0);
590 b.setNode(v3s16(x,y,z), n);
594 // Make neighbours invalid
595 parent.position_valid = false;
596 // Add exceptions to the top of the bottom block
597 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
598 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
600 parent.validity_exceptions.push_back(v3s16(MAP_BLOCKSIZE+x, MAP_BLOCKSIZE-1, MAP_BLOCKSIZE+z));
602 // Lighting value for the valid nodes
603 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
604 std::map<v3s16, bool> light_sources;
605 // Bottom block is not valid
606 UASSERT(b.propagateSunlight(light_sources) == false);
611 struct TestMapSector: public TestBase
613 class TC : public NodeContainer
622 position_valid = true;
625 virtual bool isValidPosition(v3s16 p)
627 return position_valid;
630 virtual MapNode getNode(v3s16 p)
632 if(position_valid == false)
633 throw InvalidPositionException();
637 virtual void setNode(v3s16 p, MapNode & n)
639 if(position_valid == false)
640 throw InvalidPositionException();
643 virtual u16 nodeContainerId() const
652 parent.position_valid = false;
654 // Create one with no heightmaps
655 ServerMapSector sector(&parent, v2s16(1,1));
657 UASSERT(sector.getBlockNoCreateNoEx(0) == nullptr);
658 UASSERT(sector.getBlockNoCreateNoEx(1) == nullptr);
660 MapBlock * bref = sector.createBlankBlock(-2);
662 UASSERT(sector.getBlockNoCreateNoEx(0) == nullptr);
663 UASSERT(sector.getBlockNoCreateNoEx(-2) == bref);
665 //TODO: Check for AlreadyExistsException
667 /*bool exception_thrown = false;
671 catch(InvalidPositionException &e){
672 exception_thrown = true;
674 UASSERT(exception_thrown);*/