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.
27 content_t t_CONTENT_STONE;
28 content_t t_CONTENT_GRASS;
29 content_t t_CONTENT_TORCH;
30 content_t t_CONTENT_WATER;
31 content_t t_CONTENT_LAVA;
32 content_t t_CONTENT_BRICK;
34 ////////////////////////////////////////////////////////////////////////////////
40 class TestGameDef : public IGameDef {
45 IItemDefManager *getItemDefManager() { return m_itemdef; }
46 INodeDefManager *getNodeDefManager() { return m_nodedef; }
47 ICraftDefManager *getCraftDefManager() { return m_craftdef; }
48 ITextureSource *getTextureSource() { return m_texturesrc; }
49 IShaderSource *getShaderSource() { return m_shadersrc; }
50 ISoundManager *getSoundManager() { return m_soundmgr; }
51 MtEventManager *getEventManager() { return m_eventmgr; }
52 scene::ISceneManager *getSceneManager() { return m_scenemgr; }
53 IRollbackManager *getRollbackManager() { return m_rollbackmgr; }
54 EmergeManager *getEmergeManager() { return m_emergemgr; }
56 scene::IAnimatedMesh *getMesh(const std::string &filename) { return NULL; }
57 bool checkLocalPrivilege(const std::string &priv) { return false; }
58 u16 allocateUnknownNodeId(const std::string &name) { return 0; }
60 void defineSomeNodes();
63 IItemDefManager *m_itemdef;
64 INodeDefManager *m_nodedef;
65 ICraftDefManager *m_craftdef;
66 ITextureSource *m_texturesrc;
67 IShaderSource *m_shadersrc;
68 ISoundManager *m_soundmgr;
69 MtEventManager *m_eventmgr;
70 scene::ISceneManager *m_scenemgr;
71 IRollbackManager *m_rollbackmgr;
72 EmergeManager *m_emergemgr;
76 TestGameDef::TestGameDef()
78 m_itemdef = createItemDefManager();
79 m_nodedef = createNodeDefManager();
85 TestGameDef::~TestGameDef()
92 void TestGameDef::defineSomeNodes()
94 IWritableItemDefManager *idef = (IWritableItemDefManager *)m_itemdef;
95 IWritableNodeDefManager *ndef = (IWritableNodeDefManager *)m_nodedef;
97 ItemDefinition itemdef;
101 itemdef = ItemDefinition();
102 itemdef.type = ITEM_NODE;
103 itemdef.name = "default:stone";
104 itemdef.description = "Stone";
105 itemdef.groups["cracky"] = 3;
106 itemdef.inventory_image = "[inventorycube"
109 "{default_stone.png";
110 f = ContentFeatures();
111 f.name = itemdef.name;
112 for(int i = 0; i < 6; i++)
113 f.tiledef[i].name = "default_stone.png";
114 f.is_ground_content = true;
115 idef->registerItem(itemdef);
116 t_CONTENT_STONE = ndef->set(f.name, f);
119 itemdef = ItemDefinition();
120 itemdef.type = ITEM_NODE;
121 itemdef.name = "default:dirt_with_grass";
122 itemdef.description = "Dirt with grass";
123 itemdef.groups["crumbly"] = 3;
124 itemdef.inventory_image = "[inventorycube"
126 "{default_dirt.png&default_grass_side.png"
127 "{default_dirt.png&default_grass_side.png";
128 f = ContentFeatures();
129 f.name = itemdef.name;
130 f.tiledef[0].name = "default_grass.png";
131 f.tiledef[1].name = "default_dirt.png";
132 for(int i = 2; i < 6; i++)
133 f.tiledef[i].name = "default_dirt.png^default_grass_side.png";
134 f.is_ground_content = true;
135 idef->registerItem(itemdef);
136 t_CONTENT_GRASS = ndef->set(f.name, f);
138 //// Torch (minimal definition for lighting tests)
139 itemdef = ItemDefinition();
140 itemdef.type = ITEM_NODE;
141 itemdef.name = "default:torch";
142 f = ContentFeatures();
143 f.name = itemdef.name;
144 f.param_type = CPT_LIGHT;
145 f.light_propagates = true;
146 f.sunlight_propagates = true;
147 f.light_source = LIGHT_MAX-1;
148 idef->registerItem(itemdef);
149 t_CONTENT_TORCH = ndef->set(f.name, f);
152 itemdef = ItemDefinition();
153 itemdef.type = ITEM_NODE;
154 itemdef.name = "default:water";
155 itemdef.description = "Water";
156 itemdef.inventory_image = "[inventorycube"
159 "{default_water.png";
160 f = ContentFeatures();
161 f.name = itemdef.name;
163 f.liquid_type = LIQUID_SOURCE;
164 f.liquid_viscosity = 4;
165 f.is_ground_content = true;
166 f.groups["liquids"] = 3;
167 for(int i = 0; i < 6; i++)
168 f.tiledef[i].name = "default_water.png";
169 idef->registerItem(itemdef);
170 t_CONTENT_WATER = ndef->set(f.name, f);
173 itemdef = ItemDefinition();
174 itemdef.type = ITEM_NODE;
175 itemdef.name = "default:lava";
176 itemdef.description = "Lava";
177 itemdef.inventory_image = "[inventorycube"
181 f = ContentFeatures();
182 f.name = itemdef.name;
184 f.liquid_type = LIQUID_SOURCE;
185 f.liquid_viscosity = 7;
186 f.light_source = LIGHT_MAX-1;
187 f.is_ground_content = true;
188 f.groups["liquids"] = 3;
189 for(int i = 0; i < 6; i++)
190 f.tiledef[i].name = "default_lava.png";
191 idef->registerItem(itemdef);
192 t_CONTENT_LAVA = ndef->set(f.name, f);
196 itemdef = ItemDefinition();
197 itemdef.type = ITEM_NODE;
198 itemdef.name = "default:brick";
199 itemdef.description = "Brick";
200 itemdef.groups["cracky"] = 3;
201 itemdef.inventory_image = "[inventorycube"
204 "{default_brick.png";
205 f = ContentFeatures();
206 f.name = itemdef.name;
207 for(int i = 0; i < 6; i++)
208 f.tiledef[i].name = "default_brick.png";
209 f.is_ground_content = true;
210 idef->registerItem(itemdef);
211 t_CONTENT_BRICK = ndef->set(f.name, f);
220 DSTACK(FUNCTION_NAME);
222 u32 t1 = porting::getTime(PRECISION_MILLI);
225 g_logger.setLevelSilenced(LL_ERROR, true);
227 u32 num_modules_failed = 0;
228 u32 num_total_tests_failed = 0;
229 u32 num_total_tests_run = 0;
230 std::vector<TestBase *> &testmods = TestManager::getTestModules();
231 for (size_t i = 0; i != testmods.size(); i++) {
232 if (!testmods[i]->testModule(&gamedef))
233 num_modules_failed++;
235 num_total_tests_failed += testmods[i]->num_tests_failed;
236 num_total_tests_run += testmods[i]->num_tests_run;
239 u32 tdiff = porting::getTime(PRECISION_MILLI) - t1;
241 g_logger.setLevelSilenced(LL_ERROR, false);
243 const char *overall_status = (num_modules_failed == 0) ? "PASSED" : "FAILED";
246 << "++++++++++++++++++++++++++++++++++++++++"
247 << "++++++++++++++++++++++++++++++++++++++++" << std::endl
248 << "Unit Test Results: " << overall_status << std::endl
249 << " " << num_modules_failed << " / " << testmods.size()
250 << " failed modules (" << num_total_tests_failed << " / "
251 << num_total_tests_run << " failed individual tests)." << std::endl
252 << " Testing took " << tdiff << "ms total." << std::endl
253 << "++++++++++++++++++++++++++++++++++++++++"
254 << "++++++++++++++++++++++++++++++++++++++++" << std::endl;
256 return num_modules_failed;
263 bool TestBase::testModule(IGameDef *gamedef)
265 rawstream << "======== Testing module " << getName() << std::endl;
266 u32 t1 = porting::getTime(PRECISION_MILLI);
271 u32 tdiff = porting::getTime(PRECISION_MILLI) - t1;
272 rawstream << "======== Module " << getName() << " "
273 << (num_tests_failed ? "failed" : "passed") << " (" << num_tests_failed
274 << " failures / " << num_tests_run << " tests) - " << tdiff
275 << "ms" << std::endl;
277 if (!m_test_dir.empty())
278 fs::RecursiveDelete(m_test_dir);
280 return num_tests_failed == 0;
283 std::string TestBase::getTestTempDirectory()
285 if (!m_test_dir.empty())
289 snprintf(buf, sizeof(buf), "%08X", myrand());
291 m_test_dir = fs::TempPath() + DIR_DELIM "mttest_" + buf;
292 if (!fs::CreateDir(m_test_dir))
293 throw TestFailedException();
298 std::string TestBase::getTestTempFile()
301 snprintf(buf, sizeof(buf), "%08X", myrand());
303 return getTestTempDirectory() + DIR_DELIM + buf + ".tmp";
308 NOTE: These tests became non-working then NodeContainer was removed.
309 These should be redone, utilizing some kind of a virtual
310 interface for Map (IMap would be fine).
313 struct TestMapBlock: public TestBase
315 class TC : public NodeContainer
321 core::list<v3s16> validity_exceptions;
325 position_valid = true;
328 virtual bool isValidPosition(v3s16 p)
330 //return position_valid ^ (p==position_valid_exception);
331 bool exception = false;
332 for(core::list<v3s16>::Iterator i=validity_exceptions.begin();
333 i != validity_exceptions.end(); i++)
341 return exception ? !position_valid : position_valid;
344 virtual MapNode getNode(v3s16 p)
346 if(isValidPosition(p) == false)
347 throw InvalidPositionException();
351 virtual void setNode(v3s16 p, MapNode & n)
353 if(isValidPosition(p) == false)
354 throw InvalidPositionException();
357 virtual u16 nodeContainerId() const
367 MapBlock b(&parent, v3s16(1,1,1));
368 v3s16 relpos(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
370 UASSERT(b.getPosRelative() == relpos);
372 UASSERT(b.getBox().MinEdge.X == MAP_BLOCKSIZE);
373 UASSERT(b.getBox().MaxEdge.X == MAP_BLOCKSIZE*2-1);
374 UASSERT(b.getBox().MinEdge.Y == MAP_BLOCKSIZE);
375 UASSERT(b.getBox().MaxEdge.Y == MAP_BLOCKSIZE*2-1);
376 UASSERT(b.getBox().MinEdge.Z == MAP_BLOCKSIZE);
377 UASSERT(b.getBox().MaxEdge.Z == MAP_BLOCKSIZE*2-1);
379 UASSERT(b.isValidPosition(v3s16(0,0,0)) == true);
380 UASSERT(b.isValidPosition(v3s16(-1,0,0)) == false);
381 UASSERT(b.isValidPosition(v3s16(-1,-142,-2341)) == false);
382 UASSERT(b.isValidPosition(v3s16(-124,142,2341)) == false);
383 UASSERT(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
384 UASSERT(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE,MAP_BLOCKSIZE-1)) == false);
387 TODO: this method should probably be removed
388 if the block size isn't going to be set variable
390 /*UASSERT(b.getSizeNodes() == v3s16(MAP_BLOCKSIZE,
391 MAP_BLOCKSIZE, MAP_BLOCKSIZE));*/
393 // Changed flag should be initially set
394 UASSERT(b.getModified() == MOD_STATE_WRITE_NEEDED);
396 UASSERT(b.getModified() == MOD_STATE_CLEAN);
398 // All nodes should have been set to
399 // .d=CONTENT_IGNORE and .getLight() = 0
400 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
401 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
402 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
404 //UASSERT(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_AIR);
405 UASSERT(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_IGNORE);
406 UASSERT(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_DAY) == 0);
407 UASSERT(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_NIGHT) == 0);
411 MapNode n(CONTENT_AIR);
412 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
413 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
414 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
416 b.setNode(v3s16(x,y,z), n);
421 Parent fetch functions
423 parent.position_valid = false;
424 parent.node.setContent(5);
428 // Positions in the block should still be valid
429 UASSERT(b.isValidPositionParent(v3s16(0,0,0)) == true);
430 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
431 n = b.getNodeParent(v3s16(0,MAP_BLOCKSIZE-1,0));
432 UASSERT(n.getContent() == CONTENT_AIR);
434 // ...but outside the block they should be invalid
435 UASSERT(b.isValidPositionParent(v3s16(-121,2341,0)) == false);
436 UASSERT(b.isValidPositionParent(v3s16(-1,0,0)) == false);
437 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == false);
440 bool exception_thrown = false;
442 // This should throw an exception
443 MapNode n = b.getNodeParent(v3s16(0,0,-1));
445 catch(InvalidPositionException &e)
447 exception_thrown = true;
449 UASSERT(exception_thrown);
452 parent.position_valid = true;
453 // Now the positions outside should be valid
454 UASSERT(b.isValidPositionParent(v3s16(-121,2341,0)) == true);
455 UASSERT(b.isValidPositionParent(v3s16(-1,0,0)) == true);
456 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == true);
457 n = b.getNodeParent(v3s16(0,0,MAP_BLOCKSIZE));
458 UASSERT(n.getContent() == 5);
466 UASSERT(b.getNode(p).getContent() == 4);
467 //TODO: Update to new system
468 /*UASSERT(b.getNodeTile(p) == 4);
469 UASSERT(b.getNodeTile(v3s16(-1,-1,0)) == 5);*/
474 // Set lighting of all nodes to 0
475 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
476 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
477 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
478 MapNode n = b.getNode(v3s16(x,y,z));
479 n.setLight(LIGHTBANK_DAY, 0);
480 n.setLight(LIGHTBANK_NIGHT, 0);
481 b.setNode(v3s16(x,y,z), n);
487 Check how the block handles being a lonely sky block
489 parent.position_valid = true;
490 b.setIsUnderground(false);
491 parent.node.setContent(CONTENT_AIR);
492 parent.node.setLight(LIGHTBANK_DAY, LIGHT_SUN);
493 parent.node.setLight(LIGHTBANK_NIGHT, 0);
494 core::map<v3s16, bool> light_sources;
495 // The bottom block is invalid, because we have a shadowing node
496 UASSERT(b.propagateSunlight(light_sources) == false);
497 UASSERT(b.getNode(v3s16(1,4,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
498 UASSERT(b.getNode(v3s16(1,3,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
499 UASSERT(b.getNode(v3s16(1,2,0)).getLight(LIGHTBANK_DAY) == 0);
500 UASSERT(b.getNode(v3s16(1,1,0)).getLight(LIGHTBANK_DAY) == 0);
501 UASSERT(b.getNode(v3s16(1,0,0)).getLight(LIGHTBANK_DAY) == 0);
502 UASSERT(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
503 UASSERT(b.getFaceLight2(1000, p, v3s16(0,1,0)) == LIGHT_SUN);
504 UASSERT(b.getFaceLight2(1000, p, v3s16(0,-1,0)) == 0);
505 UASSERT(b.getFaceLight2(0, p, v3s16(0,-1,0)) == 0);
506 // According to MapBlock::getFaceLight,
507 // The face on the z+ side should have double-diminished light
508 //UASSERT(b.getFaceLight(p, v3s16(0,0,1)) == diminish_light(diminish_light(LIGHT_MAX)));
509 // The face on the z+ side should have diminished light
510 UASSERT(b.getFaceLight2(1000, p, v3s16(0,0,1)) == diminish_light(LIGHT_MAX));
513 Check how the block handles being in between blocks with some non-sunlight
514 while being underground
517 // Make neighbours to exist and set some non-sunlight to them
518 parent.position_valid = true;
519 b.setIsUnderground(true);
520 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
521 core::map<v3s16, bool> light_sources;
522 // The block below should be valid because there shouldn't be
523 // sunlight in there either
524 UASSERT(b.propagateSunlight(light_sources, true) == true);
525 // Should not touch nodes that are not affected (that is, all of them)
526 //UASSERT(b.getNode(v3s16(1,2,3)).getLight() == LIGHT_SUN);
527 // Should set light of non-sunlighted blocks to 0.
528 UASSERT(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == 0);
531 Set up a situation where:
532 - There is only air in this block
533 - There is a valid non-sunlighted block at the bottom, and
534 - Invalid blocks elsewhere.
535 - the block is not underground.
537 This should result in bottom block invalidity
540 b.setIsUnderground(false);
542 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
543 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
544 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
546 n.setContent(CONTENT_AIR);
547 n.setLight(LIGHTBANK_DAY, 0);
548 b.setNode(v3s16(x,y,z), n);
552 // Make neighbours invalid
553 parent.position_valid = false;
554 // Add exceptions to the top of the bottom block
555 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
556 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
558 parent.validity_exceptions.push_back(v3s16(MAP_BLOCKSIZE+x, MAP_BLOCKSIZE-1, MAP_BLOCKSIZE+z));
560 // Lighting value for the valid nodes
561 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
562 core::map<v3s16, bool> light_sources;
563 // Bottom block is not valid
564 UASSERT(b.propagateSunlight(light_sources) == false);
569 struct TestMapSector: public TestBase
571 class TC : public NodeContainer
580 position_valid = true;
583 virtual bool isValidPosition(v3s16 p)
585 return position_valid;
588 virtual MapNode getNode(v3s16 p)
590 if(position_valid == false)
591 throw InvalidPositionException();
595 virtual void setNode(v3s16 p, MapNode & n)
597 if(position_valid == false)
598 throw InvalidPositionException();
601 virtual u16 nodeContainerId() const
610 parent.position_valid = false;
612 // Create one with no heightmaps
613 ServerMapSector sector(&parent, v2s16(1,1));
615 UASSERT(sector.getBlockNoCreateNoEx(0) == 0);
616 UASSERT(sector.getBlockNoCreateNoEx(1) == 0);
618 MapBlock * bref = sector.createBlankBlock(-2);
620 UASSERT(sector.getBlockNoCreateNoEx(0) == 0);
621 UASSERT(sector.getBlockNoCreateNoEx(-2) == bref);
623 //TODO: Check for AlreadyExistsException
625 /*bool exception_thrown = false;
629 catch(InvalidPositionException &e){
630 exception_thrown = true;
632 UASSERT(exception_thrown);*/