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.
28 content_t CONTENT_STONE;
29 content_t CONTENT_GRASS;
30 content_t CONTENT_TORCH;
32 ////////////////////////////////////////////////////////////////////////////////
38 class TestGameDef : public IGameDef {
43 IItemDefManager *getItemDefManager() { return m_itemdef; }
44 INodeDefManager *getNodeDefManager() { return m_nodedef; }
45 ICraftDefManager *getCraftDefManager() { return m_craftdef; }
46 ITextureSource *getTextureSource() { return m_texturesrc; }
47 IShaderSource *getShaderSource() { return m_shadersrc; }
48 ISoundManager *getSoundManager() { return m_soundmgr; }
49 MtEventManager *getEventManager() { return m_eventmgr; }
50 scene::ISceneManager *getSceneManager() { return m_scenemgr; }
51 IRollbackManager *getRollbackManager() { return m_rollbackmgr; }
52 EmergeManager *getEmergeManager() { return m_emergemgr; }
54 scene::IAnimatedMesh *getMesh(const std::string &filename) { return NULL; }
55 bool checkLocalPrivilege(const std::string &priv) { return false; }
56 u16 allocateUnknownNodeId(const std::string &name) { return 0; }
58 void defineSomeNodes();
61 IItemDefManager *m_itemdef;
62 INodeDefManager *m_nodedef;
63 ICraftDefManager *m_craftdef;
64 ITextureSource *m_texturesrc;
65 IShaderSource *m_shadersrc;
66 ISoundManager *m_soundmgr;
67 MtEventManager *m_eventmgr;
68 scene::ISceneManager *m_scenemgr;
69 IRollbackManager *m_rollbackmgr;
70 EmergeManager *m_emergemgr;
74 TestGameDef::TestGameDef()
76 m_itemdef = createItemDefManager();
77 m_nodedef = createNodeDefManager();
83 TestGameDef::~TestGameDef()
90 void TestGameDef::defineSomeNodes()
92 IWritableItemDefManager *idef = (IWritableItemDefManager *)m_itemdef;
93 IWritableNodeDefManager *ndef = (IWritableNodeDefManager *)m_nodedef;
95 ItemDefinition itemdef;
99 itemdef = ItemDefinition();
100 itemdef.type = ITEM_NODE;
101 itemdef.name = "default:stone";
102 itemdef.description = "Stone";
103 itemdef.groups["cracky"] = 3;
104 itemdef.inventory_image = "[inventorycube"
107 "{default_stone.png";
108 f = ContentFeatures();
109 f.name = itemdef.name;
110 for(int i = 0; i < 6; i++)
111 f.tiledef[i].name = "default_stone.png";
112 f.is_ground_content = true;
113 idef->registerItem(itemdef);
114 CONTENT_STONE = ndef->set(f.name, f);
117 itemdef = ItemDefinition();
118 itemdef.type = ITEM_NODE;
119 itemdef.name = "default:dirt_with_grass";
120 itemdef.description = "Dirt with grass";
121 itemdef.groups["crumbly"] = 3;
122 itemdef.inventory_image = "[inventorycube"
124 "{default_dirt.png&default_grass_side.png"
125 "{default_dirt.png&default_grass_side.png";
126 f = ContentFeatures();
127 f.name = itemdef.name;
128 f.tiledef[0].name = "default_grass.png";
129 f.tiledef[1].name = "default_dirt.png";
130 for(int i = 2; i < 6; i++)
131 f.tiledef[i].name = "default_dirt.png^default_grass_side.png";
132 f.is_ground_content = true;
133 idef->registerItem(itemdef);
134 CONTENT_GRASS = ndef->set(f.name, f);
136 //// Torch (minimal definition for lighting tests)
137 itemdef = ItemDefinition();
138 itemdef.type = ITEM_NODE;
139 itemdef.name = "default:torch";
140 f = ContentFeatures();
141 f.name = itemdef.name;
142 f.param_type = CPT_LIGHT;
143 f.light_propagates = true;
144 f.sunlight_propagates = true;
145 f.light_source = LIGHT_MAX-1;
146 idef->registerItem(itemdef);
147 CONTENT_TORCH = ndef->set(f.name, f);
156 DSTACK(__FUNCTION_NAME);
158 u32 t1 = porting::getTime(PRECISION_MILLI);
161 log_set_lev_silence(LMT_ERROR, true);
163 u32 num_modules_failed = 0;
164 u32 num_total_tests_failed = 0;
165 u32 num_total_tests_run = 0;
166 std::vector<TestBase *> &testmods = TestManager::getTestModules();
167 for (size_t i = 0; i != testmods.size(); i++) {
168 if (!testmods[i]->testModule(&gamedef))
169 num_modules_failed++;
171 num_total_tests_failed += testmods[i]->num_tests_failed;
172 num_total_tests_run += testmods[i]->num_tests_run;
175 u32 tdiff = porting::getTime(PRECISION_MILLI) - t1;
177 log_set_lev_silence(LMT_ERROR, false);
179 const char *overall_status = (num_modules_failed == 0) ? "PASSED" : "FAILED";
182 << "++++++++++++++++++++++++++++++++++++++++"
183 << "++++++++++++++++++++++++++++++++++++++++" << std::endl
184 << "Unit Test Results: " << overall_status << std::endl
185 << " " << num_modules_failed << " / " << testmods.size()
186 << " failed modules (" << num_total_tests_failed << " / "
187 << num_total_tests_run << " failed individual tests)." << std::endl
188 << " Testing took " << tdiff << "ms total." << std::endl
189 << "++++++++++++++++++++++++++++++++++++++++"
190 << "++++++++++++++++++++++++++++++++++++++++" << std::endl;
192 if (num_modules_failed)
200 bool TestBase::testModule(IGameDef *gamedef)
202 dstream << "======== Testing module " << getName() << std::endl;
203 u32 t1 = porting::getTime(PRECISION_MILLI);
208 u32 tdiff = porting::getTime(PRECISION_MILLI) - t1;
209 dstream << "======== Module " << getName() << " "
210 << (num_tests_failed ? "failed" : "passed") << " (" << num_tests_failed
211 << " failures / " << num_tests_run << " tests) - " << tdiff
212 << "ms" << std::endl;
214 return num_tests_failed == 0;
219 NOTE: These tests became non-working then NodeContainer was removed.
220 These should be redone, utilizing some kind of a virtual
221 interface for Map (IMap would be fine).
224 struct TestMapBlock: public TestBase
226 class TC : public NodeContainer
232 core::list<v3s16> validity_exceptions;
236 position_valid = true;
239 virtual bool isValidPosition(v3s16 p)
241 //return position_valid ^ (p==position_valid_exception);
242 bool exception = false;
243 for(core::list<v3s16>::Iterator i=validity_exceptions.begin();
244 i != validity_exceptions.end(); i++)
252 return exception ? !position_valid : position_valid;
255 virtual MapNode getNode(v3s16 p)
257 if(isValidPosition(p) == false)
258 throw InvalidPositionException();
262 virtual void setNode(v3s16 p, MapNode & n)
264 if(isValidPosition(p) == false)
265 throw InvalidPositionException();
268 virtual u16 nodeContainerId() const
278 MapBlock b(&parent, v3s16(1,1,1));
279 v3s16 relpos(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
281 UASSERT(b.getPosRelative() == relpos);
283 UASSERT(b.getBox().MinEdge.X == MAP_BLOCKSIZE);
284 UASSERT(b.getBox().MaxEdge.X == MAP_BLOCKSIZE*2-1);
285 UASSERT(b.getBox().MinEdge.Y == MAP_BLOCKSIZE);
286 UASSERT(b.getBox().MaxEdge.Y == MAP_BLOCKSIZE*2-1);
287 UASSERT(b.getBox().MinEdge.Z == MAP_BLOCKSIZE);
288 UASSERT(b.getBox().MaxEdge.Z == MAP_BLOCKSIZE*2-1);
290 UASSERT(b.isValidPosition(v3s16(0,0,0)) == true);
291 UASSERT(b.isValidPosition(v3s16(-1,0,0)) == false);
292 UASSERT(b.isValidPosition(v3s16(-1,-142,-2341)) == false);
293 UASSERT(b.isValidPosition(v3s16(-124,142,2341)) == false);
294 UASSERT(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
295 UASSERT(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE,MAP_BLOCKSIZE-1)) == false);
298 TODO: this method should probably be removed
299 if the block size isn't going to be set variable
301 /*UASSERT(b.getSizeNodes() == v3s16(MAP_BLOCKSIZE,
302 MAP_BLOCKSIZE, MAP_BLOCKSIZE));*/
304 // Changed flag should be initially set
305 UASSERT(b.getModified() == MOD_STATE_WRITE_NEEDED);
307 UASSERT(b.getModified() == MOD_STATE_CLEAN);
309 // All nodes should have been set to
310 // .d=CONTENT_IGNORE and .getLight() = 0
311 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
312 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
313 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
315 //UASSERT(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_AIR);
316 UASSERT(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_IGNORE);
317 UASSERT(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_DAY) == 0);
318 UASSERT(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_NIGHT) == 0);
322 MapNode n(CONTENT_AIR);
323 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
324 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
325 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
327 b.setNode(v3s16(x,y,z), n);
332 Parent fetch functions
334 parent.position_valid = false;
335 parent.node.setContent(5);
339 // Positions in the block should still be valid
340 UASSERT(b.isValidPositionParent(v3s16(0,0,0)) == true);
341 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
342 n = b.getNodeParent(v3s16(0,MAP_BLOCKSIZE-1,0));
343 UASSERT(n.getContent() == CONTENT_AIR);
345 // ...but outside the block they should be invalid
346 UASSERT(b.isValidPositionParent(v3s16(-121,2341,0)) == false);
347 UASSERT(b.isValidPositionParent(v3s16(-1,0,0)) == false);
348 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == false);
351 bool exception_thrown = false;
353 // This should throw an exception
354 MapNode n = b.getNodeParent(v3s16(0,0,-1));
356 catch(InvalidPositionException &e)
358 exception_thrown = true;
360 UASSERT(exception_thrown);
363 parent.position_valid = true;
364 // Now the positions outside should be valid
365 UASSERT(b.isValidPositionParent(v3s16(-121,2341,0)) == true);
366 UASSERT(b.isValidPositionParent(v3s16(-1,0,0)) == true);
367 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == true);
368 n = b.getNodeParent(v3s16(0,0,MAP_BLOCKSIZE));
369 UASSERT(n.getContent() == 5);
377 UASSERT(b.getNode(p).getContent() == 4);
378 //TODO: Update to new system
379 /*UASSERT(b.getNodeTile(p) == 4);
380 UASSERT(b.getNodeTile(v3s16(-1,-1,0)) == 5);*/
385 // Set lighting of all nodes to 0
386 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
387 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
388 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
389 MapNode n = b.getNode(v3s16(x,y,z));
390 n.setLight(LIGHTBANK_DAY, 0);
391 n.setLight(LIGHTBANK_NIGHT, 0);
392 b.setNode(v3s16(x,y,z), n);
398 Check how the block handles being a lonely sky block
400 parent.position_valid = true;
401 b.setIsUnderground(false);
402 parent.node.setContent(CONTENT_AIR);
403 parent.node.setLight(LIGHTBANK_DAY, LIGHT_SUN);
404 parent.node.setLight(LIGHTBANK_NIGHT, 0);
405 core::map<v3s16, bool> light_sources;
406 // The bottom block is invalid, because we have a shadowing node
407 UASSERT(b.propagateSunlight(light_sources) == false);
408 UASSERT(b.getNode(v3s16(1,4,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
409 UASSERT(b.getNode(v3s16(1,3,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
410 UASSERT(b.getNode(v3s16(1,2,0)).getLight(LIGHTBANK_DAY) == 0);
411 UASSERT(b.getNode(v3s16(1,1,0)).getLight(LIGHTBANK_DAY) == 0);
412 UASSERT(b.getNode(v3s16(1,0,0)).getLight(LIGHTBANK_DAY) == 0);
413 UASSERT(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
414 UASSERT(b.getFaceLight2(1000, p, v3s16(0,1,0)) == LIGHT_SUN);
415 UASSERT(b.getFaceLight2(1000, p, v3s16(0,-1,0)) == 0);
416 UASSERT(b.getFaceLight2(0, p, v3s16(0,-1,0)) == 0);
417 // According to MapBlock::getFaceLight,
418 // The face on the z+ side should have double-diminished light
419 //UASSERT(b.getFaceLight(p, v3s16(0,0,1)) == diminish_light(diminish_light(LIGHT_MAX)));
420 // The face on the z+ side should have diminished light
421 UASSERT(b.getFaceLight2(1000, p, v3s16(0,0,1)) == diminish_light(LIGHT_MAX));
424 Check how the block handles being in between blocks with some non-sunlight
425 while being underground
428 // Make neighbours to exist and set some non-sunlight to them
429 parent.position_valid = true;
430 b.setIsUnderground(true);
431 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
432 core::map<v3s16, bool> light_sources;
433 // The block below should be valid because there shouldn't be
434 // sunlight in there either
435 UASSERT(b.propagateSunlight(light_sources, true) == true);
436 // Should not touch nodes that are not affected (that is, all of them)
437 //UASSERT(b.getNode(v3s16(1,2,3)).getLight() == LIGHT_SUN);
438 // Should set light of non-sunlighted blocks to 0.
439 UASSERT(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == 0);
442 Set up a situation where:
443 - There is only air in this block
444 - There is a valid non-sunlighted block at the bottom, and
445 - Invalid blocks elsewhere.
446 - the block is not underground.
448 This should result in bottom block invalidity
451 b.setIsUnderground(false);
453 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
454 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
455 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
457 n.setContent(CONTENT_AIR);
458 n.setLight(LIGHTBANK_DAY, 0);
459 b.setNode(v3s16(x,y,z), n);
463 // Make neighbours invalid
464 parent.position_valid = false;
465 // Add exceptions to the top of the bottom block
466 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
467 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
469 parent.validity_exceptions.push_back(v3s16(MAP_BLOCKSIZE+x, MAP_BLOCKSIZE-1, MAP_BLOCKSIZE+z));
471 // Lighting value for the valid nodes
472 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
473 core::map<v3s16, bool> light_sources;
474 // Bottom block is not valid
475 UASSERT(b.propagateSunlight(light_sources) == false);
480 struct TestMapSector: public TestBase
482 class TC : public NodeContainer
491 position_valid = true;
494 virtual bool isValidPosition(v3s16 p)
496 return position_valid;
499 virtual MapNode getNode(v3s16 p)
501 if(position_valid == false)
502 throw InvalidPositionException();
506 virtual void setNode(v3s16 p, MapNode & n)
508 if(position_valid == false)
509 throw InvalidPositionException();
512 virtual u16 nodeContainerId() const
521 parent.position_valid = false;
523 // Create one with no heightmaps
524 ServerMapSector sector(&parent, v2s16(1,1));
526 UASSERT(sector.getBlockNoCreateNoEx(0) == 0);
527 UASSERT(sector.getBlockNoCreateNoEx(1) == 0);
529 MapBlock * bref = sector.createBlankBlock(-2);
531 UASSERT(sector.getBlockNoCreateNoEx(0) == 0);
532 UASSERT(sector.getBlockNoCreateNoEx(-2) == bref);
534 //TODO: Check for AlreadyExistsException
536 /*bool exception_thrown = false;
540 catch(InvalidPositionException &e){
541 exception_thrown = true;
543 UASSERT(exception_thrown);*/