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"
28 #include "util/numeric.h"
30 content_t t_CONTENT_STONE;
31 content_t t_CONTENT_GRASS;
32 content_t t_CONTENT_TORCH;
33 content_t t_CONTENT_WATER;
34 content_t t_CONTENT_LAVA;
35 content_t t_CONTENT_BRICK;
37 ////////////////////////////////////////////////////////////////////////////////
43 class TestGameDef : public IGameDef {
48 IItemDefManager *getItemDefManager() { return m_itemdef; }
49 const NodeDefManager *getNodeDefManager() { return m_nodedef; }
50 ICraftDefManager *getCraftDefManager() { return m_craftdef; }
51 ITextureSource *getTextureSource() { return m_texturesrc; }
52 IShaderSource *getShaderSource() { return m_shadersrc; }
53 ISoundManager *getSoundManager() { return m_soundmgr; }
54 MtEventManager *getEventManager() { return m_eventmgr; }
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 MtEventManager *m_eventmgr = nullptr;
90 scene::ISceneManager *m_scenemgr = nullptr;
91 IRollbackManager *m_rollbackmgr = nullptr;
92 EmergeManager *m_emergemgr = nullptr;
93 std::unique_ptr<ModChannelMgr> m_modchannel_mgr;
97 TestGameDef::TestGameDef() :
98 m_modchannel_mgr(new ModChannelMgr())
100 m_itemdef = createItemDefManager();
101 m_nodedef = createNodeDefManager();
107 TestGameDef::~TestGameDef()
114 void TestGameDef::defineSomeNodes()
116 IWritableItemDefManager *idef = (IWritableItemDefManager *)m_itemdef;
117 NodeDefManager *ndef = (NodeDefManager *)m_nodedef;
119 ItemDefinition itemdef;
123 itemdef = ItemDefinition();
124 itemdef.type = ITEM_NODE;
125 itemdef.name = "default:stone";
126 itemdef.description = "Stone";
127 itemdef.groups["cracky"] = 3;
128 itemdef.inventory_image = "[inventorycube"
131 "{default_stone.png";
132 f = ContentFeatures();
133 f.name = itemdef.name;
134 for (TileDef &tiledef : f.tiledef)
135 tiledef.name = "default_stone.png";
136 f.is_ground_content = true;
137 idef->registerItem(itemdef);
138 t_CONTENT_STONE = ndef->set(f.name, f);
141 itemdef = ItemDefinition();
142 itemdef.type = ITEM_NODE;
143 itemdef.name = "default:dirt_with_grass";
144 itemdef.description = "Dirt with grass";
145 itemdef.groups["crumbly"] = 3;
146 itemdef.inventory_image = "[inventorycube"
148 "{default_dirt.png&default_grass_side.png"
149 "{default_dirt.png&default_grass_side.png";
150 f = ContentFeatures();
151 f.name = itemdef.name;
152 f.tiledef[0].name = "default_grass.png";
153 f.tiledef[1].name = "default_dirt.png";
154 for(int i = 2; i < 6; i++)
155 f.tiledef[i].name = "default_dirt.png^default_grass_side.png";
156 f.is_ground_content = true;
157 idef->registerItem(itemdef);
158 t_CONTENT_GRASS = ndef->set(f.name, f);
160 //// Torch (minimal definition for lighting tests)
161 itemdef = ItemDefinition();
162 itemdef.type = ITEM_NODE;
163 itemdef.name = "default:torch";
164 f = ContentFeatures();
165 f.name = itemdef.name;
166 f.param_type = CPT_LIGHT;
167 f.light_propagates = true;
168 f.sunlight_propagates = true;
169 f.light_source = LIGHT_MAX-1;
170 idef->registerItem(itemdef);
171 t_CONTENT_TORCH = ndef->set(f.name, f);
174 itemdef = ItemDefinition();
175 itemdef.type = ITEM_NODE;
176 itemdef.name = "default:water";
177 itemdef.description = "Water";
178 itemdef.inventory_image = "[inventorycube"
181 "{default_water.png";
182 f = ContentFeatures();
183 f.name = itemdef.name;
185 f.liquid_type = LIQUID_SOURCE;
186 f.liquid_viscosity = 4;
187 f.is_ground_content = true;
188 f.groups["liquids"] = 3;
189 for (TileDef &tiledef : f.tiledef)
190 tiledef.name = "default_water.png";
191 idef->registerItem(itemdef);
192 t_CONTENT_WATER = ndef->set(f.name, f);
195 itemdef = ItemDefinition();
196 itemdef.type = ITEM_NODE;
197 itemdef.name = "default:lava";
198 itemdef.description = "Lava";
199 itemdef.inventory_image = "[inventorycube"
203 f = ContentFeatures();
204 f.name = itemdef.name;
206 f.liquid_type = LIQUID_SOURCE;
207 f.liquid_viscosity = 7;
208 f.light_source = LIGHT_MAX-1;
209 f.is_ground_content = true;
210 f.groups["liquids"] = 3;
211 for (TileDef &tiledef : f.tiledef)
212 tiledef.name = "default_lava.png";
213 idef->registerItem(itemdef);
214 t_CONTENT_LAVA = ndef->set(f.name, f);
218 itemdef = ItemDefinition();
219 itemdef.type = ITEM_NODE;
220 itemdef.name = "default:brick";
221 itemdef.description = "Brick";
222 itemdef.groups["cracky"] = 3;
223 itemdef.inventory_image = "[inventorycube"
226 "{default_brick.png";
227 f = ContentFeatures();
228 f.name = itemdef.name;
229 for (TileDef &tiledef : f.tiledef)
230 tiledef.name = "default_brick.png";
231 f.is_ground_content = true;
232 idef->registerItem(itemdef);
233 t_CONTENT_BRICK = ndef->set(f.name, f);
236 bool TestGameDef::joinModChannel(const std::string &channel)
238 return m_modchannel_mgr->joinChannel(channel, PEER_ID_SERVER);
241 bool TestGameDef::leaveModChannel(const std::string &channel)
243 return m_modchannel_mgr->leaveChannel(channel, PEER_ID_SERVER);
246 bool TestGameDef::sendModChannelMessage(const std::string &channel,
247 const std::string &message)
249 if (!m_modchannel_mgr->channelRegistered(channel))
261 u64 t1 = porting::getTimeMs();
264 g_logger.setLevelSilenced(LL_ERROR, true);
266 u32 num_modules_failed = 0;
267 u32 num_total_tests_failed = 0;
268 u32 num_total_tests_run = 0;
269 std::vector<TestBase *> &testmods = TestManager::getTestModules();
270 for (size_t i = 0; i != testmods.size(); i++) {
271 if (!testmods[i]->testModule(&gamedef))
272 num_modules_failed++;
274 num_total_tests_failed += testmods[i]->num_tests_failed;
275 num_total_tests_run += testmods[i]->num_tests_run;
278 u64 tdiff = porting::getTimeMs() - t1;
280 g_logger.setLevelSilenced(LL_ERROR, false);
282 const char *overall_status = (num_modules_failed == 0) ? "PASSED" : "FAILED";
285 << "++++++++++++++++++++++++++++++++++++++++"
286 << "++++++++++++++++++++++++++++++++++++++++" << std::endl
287 << "Unit Test Results: " << overall_status << std::endl
288 << " " << num_modules_failed << " / " << testmods.size()
289 << " failed modules (" << num_total_tests_failed << " / "
290 << num_total_tests_run << " failed individual tests)." << std::endl
291 << " Testing took " << tdiff << "ms total." << std::endl
292 << "++++++++++++++++++++++++++++++++++++++++"
293 << "++++++++++++++++++++++++++++++++++++++++" << std::endl;
295 return num_modules_failed;
302 bool TestBase::testModule(IGameDef *gamedef)
304 rawstream << "======== Testing module " << getName() << std::endl;
305 u64 t1 = porting::getTimeMs();
310 u64 tdiff = porting::getTimeMs() - t1;
311 rawstream << "======== Module " << getName() << " "
312 << (num_tests_failed ? "failed" : "passed") << " (" << num_tests_failed
313 << " failures / " << num_tests_run << " tests) - " << tdiff
314 << "ms" << std::endl;
316 if (!m_test_dir.empty())
317 fs::RecursiveDelete(m_test_dir);
319 return num_tests_failed == 0;
322 std::string TestBase::getTestTempDirectory()
324 if (!m_test_dir.empty())
328 snprintf(buf, sizeof(buf), "%08X", myrand());
330 m_test_dir = fs::TempPath() + DIR_DELIM "mttest_" + buf;
331 if (!fs::CreateDir(m_test_dir))
332 throw TestFailedException();
337 std::string TestBase::getTestTempFile()
340 snprintf(buf, sizeof(buf), "%08X", myrand());
342 return getTestTempDirectory() + DIR_DELIM + buf + ".tmp";
347 NOTE: These tests became non-working then NodeContainer was removed.
348 These should be redone, utilizing some kind of a virtual
349 interface for Map (IMap would be fine).
352 struct TestMapBlock: public TestBase
354 class TC : public NodeContainer
360 core::list<v3s16> validity_exceptions;
364 position_valid = true;
367 virtual bool isValidPosition(v3s16 p)
369 //return position_valid ^ (p==position_valid_exception);
370 bool exception = false;
371 for(core::list<v3s16>::Iterator i=validity_exceptions.begin();
372 i != validity_exceptions.end(); i++)
380 return exception ? !position_valid : position_valid;
383 virtual MapNode getNode(v3s16 p)
385 if(isValidPosition(p) == false)
386 throw InvalidPositionException();
390 virtual void setNode(v3s16 p, MapNode & n)
392 if(isValidPosition(p) == false)
393 throw InvalidPositionException();
396 virtual u16 nodeContainerId() const
406 MapBlock b(&parent, v3s16(1,1,1));
407 v3s16 relpos(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
409 UASSERT(b.getPosRelative() == relpos);
411 UASSERT(b.getBox().MinEdge.X == MAP_BLOCKSIZE);
412 UASSERT(b.getBox().MaxEdge.X == MAP_BLOCKSIZE*2-1);
413 UASSERT(b.getBox().MinEdge.Y == MAP_BLOCKSIZE);
414 UASSERT(b.getBox().MaxEdge.Y == MAP_BLOCKSIZE*2-1);
415 UASSERT(b.getBox().MinEdge.Z == MAP_BLOCKSIZE);
416 UASSERT(b.getBox().MaxEdge.Z == MAP_BLOCKSIZE*2-1);
418 UASSERT(b.isValidPosition(v3s16(0,0,0)) == true);
419 UASSERT(b.isValidPosition(v3s16(-1,0,0)) == false);
420 UASSERT(b.isValidPosition(v3s16(-1,-142,-2341)) == false);
421 UASSERT(b.isValidPosition(v3s16(-124,142,2341)) == false);
422 UASSERT(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
423 UASSERT(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE,MAP_BLOCKSIZE-1)) == false);
426 TODO: this method should probably be removed
427 if the block size isn't going to be set variable
429 /*UASSERT(b.getSizeNodes() == v3s16(MAP_BLOCKSIZE,
430 MAP_BLOCKSIZE, MAP_BLOCKSIZE));*/
432 // Changed flag should be initially set
433 UASSERT(b.getModified() == MOD_STATE_WRITE_NEEDED);
435 UASSERT(b.getModified() == MOD_STATE_CLEAN);
437 // All nodes should have been set to
438 // .d=CONTENT_IGNORE and .getLight() = 0
439 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
440 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
441 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
443 //UASSERT(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_AIR);
444 UASSERT(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_IGNORE);
445 UASSERT(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_DAY) == 0);
446 UASSERT(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_NIGHT) == 0);
450 MapNode n(CONTENT_AIR);
451 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
452 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
453 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
455 b.setNode(v3s16(x,y,z), n);
460 Parent fetch functions
462 parent.position_valid = false;
463 parent.node.setContent(5);
467 // Positions in the block should still be valid
468 UASSERT(b.isValidPositionParent(v3s16(0,0,0)) == true);
469 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
470 n = b.getNodeParent(v3s16(0,MAP_BLOCKSIZE-1,0));
471 UASSERT(n.getContent() == CONTENT_AIR);
473 // ...but outside the block they should be invalid
474 UASSERT(b.isValidPositionParent(v3s16(-121,2341,0)) == false);
475 UASSERT(b.isValidPositionParent(v3s16(-1,0,0)) == false);
476 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == false);
479 bool exception_thrown = false;
481 // This should throw an exception
482 MapNode n = b.getNodeParent(v3s16(0,0,-1));
484 catch(InvalidPositionException &e)
486 exception_thrown = true;
488 UASSERT(exception_thrown);
491 parent.position_valid = true;
492 // Now the positions outside should be valid
493 UASSERT(b.isValidPositionParent(v3s16(-121,2341,0)) == true);
494 UASSERT(b.isValidPositionParent(v3s16(-1,0,0)) == true);
495 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == true);
496 n = b.getNodeParent(v3s16(0,0,MAP_BLOCKSIZE));
497 UASSERT(n.getContent() == 5);
505 UASSERT(b.getNode(p).getContent() == 4);
506 //TODO: Update to new system
507 /*UASSERT(b.getNodeTile(p) == 4);
508 UASSERT(b.getNodeTile(v3s16(-1,-1,0)) == 5);*/
513 // Set lighting of all nodes to 0
514 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
515 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
516 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
517 MapNode n = b.getNode(v3s16(x,y,z));
518 n.setLight(LIGHTBANK_DAY, 0);
519 n.setLight(LIGHTBANK_NIGHT, 0);
520 b.setNode(v3s16(x,y,z), n);
526 Check how the block handles being a lonely sky block
528 parent.position_valid = true;
529 b.setIsUnderground(false);
530 parent.node.setContent(CONTENT_AIR);
531 parent.node.setLight(LIGHTBANK_DAY, LIGHT_SUN);
532 parent.node.setLight(LIGHTBANK_NIGHT, 0);
533 core::map<v3s16, bool> light_sources;
534 // The bottom block is invalid, because we have a shadowing node
535 UASSERT(b.propagateSunlight(light_sources) == false);
536 UASSERT(b.getNode(v3s16(1,4,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
537 UASSERT(b.getNode(v3s16(1,3,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
538 UASSERT(b.getNode(v3s16(1,2,0)).getLight(LIGHTBANK_DAY) == 0);
539 UASSERT(b.getNode(v3s16(1,1,0)).getLight(LIGHTBANK_DAY) == 0);
540 UASSERT(b.getNode(v3s16(1,0,0)).getLight(LIGHTBANK_DAY) == 0);
541 UASSERT(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
542 UASSERT(b.getFaceLight2(1000, p, v3s16(0,1,0)) == LIGHT_SUN);
543 UASSERT(b.getFaceLight2(1000, p, v3s16(0,-1,0)) == 0);
544 UASSERT(b.getFaceLight2(0, p, v3s16(0,-1,0)) == 0);
545 // According to MapBlock::getFaceLight,
546 // The face on the z+ side should have double-diminished light
547 //UASSERT(b.getFaceLight(p, v3s16(0,0,1)) == diminish_light(diminish_light(LIGHT_MAX)));
548 // The face on the z+ side should have diminished light
549 UASSERT(b.getFaceLight2(1000, p, v3s16(0,0,1)) == diminish_light(LIGHT_MAX));
552 Check how the block handles being in between blocks with some non-sunlight
553 while being underground
556 // Make neighbours to exist and set some non-sunlight to them
557 parent.position_valid = true;
558 b.setIsUnderground(true);
559 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
560 core::map<v3s16, bool> light_sources;
561 // The block below should be valid because there shouldn't be
562 // sunlight in there either
563 UASSERT(b.propagateSunlight(light_sources, true) == true);
564 // Should not touch nodes that are not affected (that is, all of them)
565 //UASSERT(b.getNode(v3s16(1,2,3)).getLight() == LIGHT_SUN);
566 // Should set light of non-sunlighted blocks to 0.
567 UASSERT(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == 0);
570 Set up a situation where:
571 - There is only air in this block
572 - There is a valid non-sunlighted block at the bottom, and
573 - Invalid blocks elsewhere.
574 - the block is not underground.
576 This should result in bottom block invalidity
579 b.setIsUnderground(false);
581 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
582 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
583 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
585 n.setContent(CONTENT_AIR);
586 n.setLight(LIGHTBANK_DAY, 0);
587 b.setNode(v3s16(x,y,z), n);
591 // Make neighbours invalid
592 parent.position_valid = false;
593 // Add exceptions to the top of the bottom block
594 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
595 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
597 parent.validity_exceptions.push_back(v3s16(MAP_BLOCKSIZE+x, MAP_BLOCKSIZE-1, MAP_BLOCKSIZE+z));
599 // Lighting value for the valid nodes
600 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
601 core::map<v3s16, bool> light_sources;
602 // Bottom block is not valid
603 UASSERT(b.propagateSunlight(light_sources) == false);
608 struct TestMapSector: public TestBase
610 class TC : public NodeContainer
619 position_valid = true;
622 virtual bool isValidPosition(v3s16 p)
624 return position_valid;
627 virtual MapNode getNode(v3s16 p)
629 if(position_valid == false)
630 throw InvalidPositionException();
634 virtual void setNode(v3s16 p, MapNode & n)
636 if(position_valid == false)
637 throw InvalidPositionException();
640 virtual u16 nodeContainerId() const
649 parent.position_valid = false;
651 // Create one with no heightmaps
652 ServerMapSector sector(&parent, v2s16(1,1));
654 UASSERT(sector.getBlockNoCreateNoEx(0) == 0);
655 UASSERT(sector.getBlockNoCreateNoEx(1) == 0);
657 MapBlock * bref = sector.createBlankBlock(-2);
659 UASSERT(sector.getBlockNoCreateNoEx(0) == 0);
660 UASSERT(sector.getBlockNoCreateNoEx(-2) == bref);
662 //TODO: Check for AlreadyExistsException
664 /*bool exception_thrown = false;
668 catch(InvalidPositionException &e){
669 exception_thrown = true;
671 UASSERT(exception_thrown);*/