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 IWritableItemDefManager *getWritableItemDefManager() { return m_itemdef; }
51 const NodeDefManager *getNodeDefManager() { return m_nodedef; }
52 NodeDefManager *getWritableNodeDefManager() { return m_nodedef; }
53 ICraftDefManager *getCraftDefManager() { return m_craftdef; }
54 ITextureSource *getTextureSource() { return m_texturesrc; }
55 IShaderSource *getShaderSource() { return m_shadersrc; }
56 ISoundManager *getSoundManager() { return m_soundmgr; }
57 scene::ISceneManager *getSceneManager() { return m_scenemgr; }
58 IRollbackManager *getRollbackManager() { return m_rollbackmgr; }
59 EmergeManager *getEmergeManager() { return m_emergemgr; }
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 std::string getModStoragePath() const { return "."; }
74 virtual bool registerModStorage(ModMetadata *meta) { return true; }
75 virtual void unregisterModStorage(const std::string &name) {}
76 bool joinModChannel(const std::string &channel);
77 bool leaveModChannel(const std::string &channel);
78 bool sendModChannelMessage(const std::string &channel, const std::string &message);
79 ModChannel *getModChannel(const std::string &channel)
81 return m_modchannel_mgr->getModChannel(channel);
85 IWritableItemDefManager *m_itemdef = nullptr;
86 NodeDefManager *m_nodedef = nullptr;
87 ICraftDefManager *m_craftdef = nullptr;
88 ITextureSource *m_texturesrc = nullptr;
89 IShaderSource *m_shadersrc = nullptr;
90 ISoundManager *m_soundmgr = nullptr;
91 scene::ISceneManager *m_scenemgr = nullptr;
92 IRollbackManager *m_rollbackmgr = nullptr;
93 EmergeManager *m_emergemgr = nullptr;
94 std::unique_ptr<ModChannelMgr> m_modchannel_mgr;
98 TestGameDef::TestGameDef() :
99 m_modchannel_mgr(new ModChannelMgr())
101 m_itemdef = createItemDefManager();
102 m_nodedef = createNodeDefManager();
108 TestGameDef::~TestGameDef()
115 void TestGameDef::defineSomeNodes()
117 IWritableItemDefManager *idef = (IWritableItemDefManager *)m_itemdef;
118 NodeDefManager *ndef = (NodeDefManager *)m_nodedef;
120 ItemDefinition itemdef;
124 itemdef = ItemDefinition();
125 itemdef.type = ITEM_NODE;
126 itemdef.name = "default:stone";
127 itemdef.description = "Stone";
128 itemdef.groups["cracky"] = 3;
129 itemdef.inventory_image = "[inventorycube"
132 "{default_stone.png";
133 f = ContentFeatures();
134 f.name = itemdef.name;
135 for (TileDef &tiledef : f.tiledef)
136 tiledef.name = "default_stone.png";
137 f.is_ground_content = true;
138 idef->registerItem(itemdef);
139 t_CONTENT_STONE = ndef->set(f.name, f);
142 itemdef = ItemDefinition();
143 itemdef.type = ITEM_NODE;
144 itemdef.name = "default:dirt_with_grass";
145 itemdef.description = "Dirt with grass";
146 itemdef.groups["crumbly"] = 3;
147 itemdef.inventory_image = "[inventorycube"
149 "{default_dirt.png&default_grass_side.png"
150 "{default_dirt.png&default_grass_side.png";
151 f = ContentFeatures();
152 f.name = itemdef.name;
153 f.tiledef[0].name = "default_grass.png";
154 f.tiledef[1].name = "default_dirt.png";
155 for(int i = 2; i < 6; i++)
156 f.tiledef[i].name = "default_dirt.png^default_grass_side.png";
157 f.is_ground_content = true;
158 idef->registerItem(itemdef);
159 t_CONTENT_GRASS = ndef->set(f.name, f);
161 //// Torch (minimal definition for lighting tests)
162 itemdef = ItemDefinition();
163 itemdef.type = ITEM_NODE;
164 itemdef.name = "default:torch";
165 f = ContentFeatures();
166 f.name = itemdef.name;
167 f.param_type = CPT_LIGHT;
168 f.light_propagates = true;
169 f.sunlight_propagates = true;
170 f.light_source = LIGHT_MAX-1;
171 idef->registerItem(itemdef);
172 t_CONTENT_TORCH = ndef->set(f.name, f);
175 itemdef = ItemDefinition();
176 itemdef.type = ITEM_NODE;
177 itemdef.name = "default:water";
178 itemdef.description = "Water";
179 itemdef.inventory_image = "[inventorycube"
182 "{default_water.png";
183 f = ContentFeatures();
184 f.name = itemdef.name;
186 f.liquid_type = LIQUID_SOURCE;
187 f.liquid_viscosity = 4;
188 f.is_ground_content = true;
189 f.groups["liquids"] = 3;
190 for (TileDef &tiledef : f.tiledef)
191 tiledef.name = "default_water.png";
192 idef->registerItem(itemdef);
193 t_CONTENT_WATER = ndef->set(f.name, f);
196 itemdef = ItemDefinition();
197 itemdef.type = ITEM_NODE;
198 itemdef.name = "default:lava";
199 itemdef.description = "Lava";
200 itemdef.inventory_image = "[inventorycube"
204 f = ContentFeatures();
205 f.name = itemdef.name;
207 f.liquid_type = LIQUID_SOURCE;
208 f.liquid_viscosity = 7;
209 f.light_source = LIGHT_MAX-1;
210 f.is_ground_content = true;
211 f.groups["liquids"] = 3;
212 for (TileDef &tiledef : f.tiledef)
213 tiledef.name = "default_lava.png";
214 idef->registerItem(itemdef);
215 t_CONTENT_LAVA = ndef->set(f.name, f);
219 itemdef = ItemDefinition();
220 itemdef.type = ITEM_NODE;
221 itemdef.name = "default:brick";
222 itemdef.description = "Brick";
223 itemdef.groups["cracky"] = 3;
224 itemdef.inventory_image = "[inventorycube"
227 "{default_brick.png";
228 f = ContentFeatures();
229 f.name = itemdef.name;
230 for (TileDef &tiledef : f.tiledef)
231 tiledef.name = "default_brick.png";
232 f.is_ground_content = true;
233 idef->registerItem(itemdef);
234 t_CONTENT_BRICK = ndef->set(f.name, f);
237 bool TestGameDef::joinModChannel(const std::string &channel)
239 return m_modchannel_mgr->joinChannel(channel, PEER_ID_SERVER);
242 bool TestGameDef::leaveModChannel(const std::string &channel)
244 return m_modchannel_mgr->leaveChannel(channel, PEER_ID_SERVER);
247 bool TestGameDef::sendModChannelMessage(const std::string &channel,
248 const std::string &message)
250 if (!m_modchannel_mgr->channelRegistered(channel))
262 u64 t1 = porting::getTimeMs();
265 g_logger.setLevelSilenced(LL_ERROR, true);
267 u32 num_modules_failed = 0;
268 u32 num_total_tests_failed = 0;
269 u32 num_total_tests_run = 0;
270 std::vector<TestBase *> &testmods = TestManager::getTestModules();
271 for (size_t i = 0; i != testmods.size(); i++) {
272 if (!testmods[i]->testModule(&gamedef))
273 num_modules_failed++;
275 num_total_tests_failed += testmods[i]->num_tests_failed;
276 num_total_tests_run += testmods[i]->num_tests_run;
279 u64 tdiff = porting::getTimeMs() - t1;
281 g_logger.setLevelSilenced(LL_ERROR, false);
283 const char *overall_status = (num_modules_failed == 0) ? "PASSED" : "FAILED";
286 << "++++++++++++++++++++++++++++++++++++++++"
287 << "++++++++++++++++++++++++++++++++++++++++" << std::endl
288 << "Unit Test Results: " << overall_status << std::endl
289 << " " << num_modules_failed << " / " << testmods.size()
290 << " failed modules (" << num_total_tests_failed << " / "
291 << num_total_tests_run << " failed individual tests)." << std::endl
292 << " Testing took " << tdiff << "ms total." << std::endl
293 << "++++++++++++++++++++++++++++++++++++++++"
294 << "++++++++++++++++++++++++++++++++++++++++" << std::endl;
296 return num_modules_failed;
303 bool TestBase::testModule(IGameDef *gamedef)
305 rawstream << "======== Testing module " << getName() << std::endl;
306 u64 t1 = porting::getTimeMs();
311 u64 tdiff = porting::getTimeMs() - t1;
312 rawstream << "======== Module " << getName() << " "
313 << (num_tests_failed ? "failed" : "passed") << " (" << num_tests_failed
314 << " failures / " << num_tests_run << " tests) - " << tdiff
315 << "ms" << std::endl;
317 if (!m_test_dir.empty())
318 fs::RecursiveDelete(m_test_dir);
320 return num_tests_failed == 0;
323 std::string TestBase::getTestTempDirectory()
325 if (!m_test_dir.empty())
329 porting::mt_snprintf(buf, sizeof(buf), "%08X", myrand());
331 m_test_dir = fs::TempPath() + DIR_DELIM "mttest_" + buf;
332 if (!fs::CreateDir(m_test_dir))
333 throw TestFailedException();
338 std::string TestBase::getTestTempFile()
341 porting::mt_snprintf(buf, sizeof(buf), "%08X", myrand());
343 return getTestTempDirectory() + DIR_DELIM + buf + ".tmp";
348 NOTE: These tests became non-working then NodeContainer was removed.
349 These should be redone, utilizing some kind of a virtual
350 interface for Map (IMap would be fine).
353 struct TestMapBlock: public TestBase
355 class TC : public NodeContainer
361 core::list<v3s16> validity_exceptions;
365 position_valid = true;
368 virtual bool isValidPosition(v3s16 p)
370 //return position_valid ^ (p==position_valid_exception);
371 bool exception = false;
372 for(core::list<v3s16>::Iterator i=validity_exceptions.begin();
373 i != validity_exceptions.end(); i++)
381 return exception ? !position_valid : position_valid;
384 virtual MapNode getNode(v3s16 p)
386 if(isValidPosition(p) == false)
387 throw InvalidPositionException();
391 virtual void setNode(v3s16 p, MapNode & n)
393 if(isValidPosition(p) == false)
394 throw InvalidPositionException();
397 virtual u16 nodeContainerId() const
407 MapBlock b(&parent, v3s16(1,1,1));
408 v3s16 relpos(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
410 UASSERT(b.getPosRelative() == relpos);
412 UASSERT(b.getBox().MinEdge.X == MAP_BLOCKSIZE);
413 UASSERT(b.getBox().MaxEdge.X == MAP_BLOCKSIZE*2-1);
414 UASSERT(b.getBox().MinEdge.Y == MAP_BLOCKSIZE);
415 UASSERT(b.getBox().MaxEdge.Y == MAP_BLOCKSIZE*2-1);
416 UASSERT(b.getBox().MinEdge.Z == MAP_BLOCKSIZE);
417 UASSERT(b.getBox().MaxEdge.Z == MAP_BLOCKSIZE*2-1);
419 UASSERT(b.isValidPosition(v3s16(0,0,0)) == true);
420 UASSERT(b.isValidPosition(v3s16(-1,0,0)) == false);
421 UASSERT(b.isValidPosition(v3s16(-1,-142,-2341)) == false);
422 UASSERT(b.isValidPosition(v3s16(-124,142,2341)) == false);
423 UASSERT(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
424 UASSERT(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE,MAP_BLOCKSIZE-1)) == false);
427 TODO: this method should probably be removed
428 if the block size isn't going to be set variable
430 /*UASSERT(b.getSizeNodes() == v3s16(MAP_BLOCKSIZE,
431 MAP_BLOCKSIZE, MAP_BLOCKSIZE));*/
433 // Changed flag should be initially set
434 UASSERT(b.getModified() == MOD_STATE_WRITE_NEEDED);
436 UASSERT(b.getModified() == MOD_STATE_CLEAN);
438 // All nodes should have been set to
439 // .d=CONTENT_IGNORE and .getLight() = 0
440 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
441 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
442 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
444 //UASSERT(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_AIR);
445 UASSERT(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_IGNORE);
446 UASSERT(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_DAY) == 0);
447 UASSERT(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_NIGHT) == 0);
451 MapNode n(CONTENT_AIR);
452 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
453 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
454 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
456 b.setNode(v3s16(x,y,z), n);
461 Parent fetch functions
463 parent.position_valid = false;
464 parent.node.setContent(5);
468 // Positions in the block should still be valid
469 UASSERT(b.isValidPositionParent(v3s16(0,0,0)) == true);
470 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
471 n = b.getNodeParent(v3s16(0,MAP_BLOCKSIZE-1,0));
472 UASSERT(n.getContent() == CONTENT_AIR);
474 // ...but outside the block they should be invalid
475 UASSERT(b.isValidPositionParent(v3s16(-121,2341,0)) == false);
476 UASSERT(b.isValidPositionParent(v3s16(-1,0,0)) == false);
477 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == false);
480 bool exception_thrown = false;
482 // This should throw an exception
483 MapNode n = b.getNodeParent(v3s16(0,0,-1));
485 catch(InvalidPositionException &e)
487 exception_thrown = true;
489 UASSERT(exception_thrown);
492 parent.position_valid = true;
493 // Now the positions outside should be valid
494 UASSERT(b.isValidPositionParent(v3s16(-121,2341,0)) == true);
495 UASSERT(b.isValidPositionParent(v3s16(-1,0,0)) == true);
496 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == true);
497 n = b.getNodeParent(v3s16(0,0,MAP_BLOCKSIZE));
498 UASSERT(n.getContent() == 5);
506 UASSERT(b.getNode(p).getContent() == 4);
507 //TODO: Update to new system
508 /*UASSERT(b.getNodeTile(p) == 4);
509 UASSERT(b.getNodeTile(v3s16(-1,-1,0)) == 5);*/
514 // Set lighting of all nodes to 0
515 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
516 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
517 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
518 MapNode n = b.getNode(v3s16(x,y,z));
519 n.setLight(LIGHTBANK_DAY, 0);
520 n.setLight(LIGHTBANK_NIGHT, 0);
521 b.setNode(v3s16(x,y,z), n);
527 Check how the block handles being a lonely sky block
529 parent.position_valid = true;
530 b.setIsUnderground(false);
531 parent.node.setContent(CONTENT_AIR);
532 parent.node.setLight(LIGHTBANK_DAY, LIGHT_SUN);
533 parent.node.setLight(LIGHTBANK_NIGHT, 0);
534 core::map<v3s16, bool> light_sources;
535 // The bottom block is invalid, because we have a shadowing node
536 UASSERT(b.propagateSunlight(light_sources) == false);
537 UASSERT(b.getNode(v3s16(1,4,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
538 UASSERT(b.getNode(v3s16(1,3,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
539 UASSERT(b.getNode(v3s16(1,2,0)).getLight(LIGHTBANK_DAY) == 0);
540 UASSERT(b.getNode(v3s16(1,1,0)).getLight(LIGHTBANK_DAY) == 0);
541 UASSERT(b.getNode(v3s16(1,0,0)).getLight(LIGHTBANK_DAY) == 0);
542 UASSERT(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
543 UASSERT(b.getFaceLight2(1000, p, v3s16(0,1,0)) == LIGHT_SUN);
544 UASSERT(b.getFaceLight2(1000, p, v3s16(0,-1,0)) == 0);
545 UASSERT(b.getFaceLight2(0, p, v3s16(0,-1,0)) == 0);
546 // According to MapBlock::getFaceLight,
547 // The face on the z+ side should have double-diminished light
548 //UASSERT(b.getFaceLight(p, v3s16(0,0,1)) == diminish_light(diminish_light(LIGHT_MAX)));
549 // The face on the z+ side should have diminished light
550 UASSERT(b.getFaceLight2(1000, p, v3s16(0,0,1)) == diminish_light(LIGHT_MAX));
553 Check how the block handles being in between blocks with some non-sunlight
554 while being underground
557 // Make neighbours to exist and set some non-sunlight to them
558 parent.position_valid = true;
559 b.setIsUnderground(true);
560 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
561 core::map<v3s16, bool> light_sources;
562 // The block below should be valid because there shouldn't be
563 // sunlight in there either
564 UASSERT(b.propagateSunlight(light_sources, true) == true);
565 // Should not touch nodes that are not affected (that is, all of them)
566 //UASSERT(b.getNode(v3s16(1,2,3)).getLight() == LIGHT_SUN);
567 // Should set light of non-sunlighted blocks to 0.
568 UASSERT(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == 0);
571 Set up a situation where:
572 - There is only air in this block
573 - There is a valid non-sunlighted block at the bottom, and
574 - Invalid blocks elsewhere.
575 - the block is not underground.
577 This should result in bottom block invalidity
580 b.setIsUnderground(false);
582 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
583 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
584 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
586 n.setContent(CONTENT_AIR);
587 n.setLight(LIGHTBANK_DAY, 0);
588 b.setNode(v3s16(x,y,z), n);
592 // Make neighbours invalid
593 parent.position_valid = false;
594 // Add exceptions to the top of the bottom block
595 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
596 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
598 parent.validity_exceptions.push_back(v3s16(MAP_BLOCKSIZE+x, MAP_BLOCKSIZE-1, MAP_BLOCKSIZE+z));
600 // Lighting value for the valid nodes
601 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
602 core::map<v3s16, bool> light_sources;
603 // Bottom block is not valid
604 UASSERT(b.propagateSunlight(light_sources) == false);
609 struct TestMapSector: public TestBase
611 class TC : public NodeContainer
620 position_valid = true;
623 virtual bool isValidPosition(v3s16 p)
625 return position_valid;
628 virtual MapNode getNode(v3s16 p)
630 if(position_valid == false)
631 throw InvalidPositionException();
635 virtual void setNode(v3s16 p, MapNode & n)
637 if(position_valid == false)
638 throw InvalidPositionException();
641 virtual u16 nodeContainerId() const
650 parent.position_valid = false;
652 // Create one with no heightmaps
653 ServerMapSector sector(&parent, v2s16(1,1));
655 UASSERT(sector.getBlockNoCreateNoEx(0) == nullptr);
656 UASSERT(sector.getBlockNoCreateNoEx(1) == nullptr);
658 MapBlock * bref = sector.createBlankBlock(-2);
660 UASSERT(sector.getBlockNoCreateNoEx(0) == nullptr);
661 UASSERT(sector.getBlockNoCreateNoEx(-2) == bref);
663 //TODO: Check for AlreadyExistsException
665 /*bool exception_thrown = false;
669 catch(InvalidPositionException &e){
670 exception_thrown = true;
672 UASSERT(exception_thrown);*/