150bddc567ff2fa8637d5724921fb03b5a6896bf
[oweals/minetest.git] / src / unittest / test.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #include "test.h"
21
22 #include "nodedef.h"
23 #include "itemdef.h"
24 #include "gamedef.h"
25 #include "modchannels.h"
26 #include "mods.h"
27 #include "util/numeric.h"
28
29 content_t t_CONTENT_STONE;
30 content_t t_CONTENT_GRASS;
31 content_t t_CONTENT_TORCH;
32 content_t t_CONTENT_WATER;
33 content_t t_CONTENT_LAVA;
34 content_t t_CONTENT_BRICK;
35
36 ////////////////////////////////////////////////////////////////////////////////
37
38 ////
39 //// TestGameDef
40 ////
41
42 class TestGameDef : public IGameDef {
43 public:
44         TestGameDef();
45         ~TestGameDef();
46
47         IItemDefManager *getItemDefManager() { return m_itemdef; }
48         const NodeDefManager *getNodeDefManager() { return m_nodedef; }
49         ICraftDefManager *getCraftDefManager() { return m_craftdef; }
50         ITextureSource *getTextureSource() { return m_texturesrc; }
51         IShaderSource *getShaderSource() { return m_shadersrc; }
52         ISoundManager *getSoundManager() { return m_soundmgr; }
53         MtEventManager *getEventManager() { return m_eventmgr; }
54         scene::ISceneManager *getSceneManager() { return m_scenemgr; }
55         IRollbackManager *getRollbackManager() { return m_rollbackmgr; }
56         EmergeManager *getEmergeManager() { return m_emergemgr; }
57
58         scene::IAnimatedMesh *getMesh(const std::string &filename) { return NULL; }
59         bool checkLocalPrivilege(const std::string &priv) { return false; }
60         u16 allocateUnknownNodeId(const std::string &name) { return 0; }
61
62         void defineSomeNodes();
63
64         virtual const std::vector<ModSpec> &getMods() const
65         {
66                 static std::vector<ModSpec> testmodspec;
67                 return testmodspec;
68         }
69         virtual const ModSpec* getModSpec(const std::string &modname) const { return NULL; }
70         virtual std::string getModStoragePath() const { return "."; }
71         virtual bool registerModStorage(ModMetadata *meta) { return true; }
72         virtual void unregisterModStorage(const std::string &name) {}
73         bool joinModChannel(const std::string &channel);
74         bool leaveModChannel(const std::string &channel);
75         bool sendModChannelMessage(const std::string &channel, const std::string &message);
76         ModChannel *getModChannel(const std::string &channel)
77         {
78                 return m_modchannel_mgr->getModChannel(channel);
79         }
80
81 private:
82         IItemDefManager *m_itemdef = nullptr;
83         const NodeDefManager *m_nodedef = nullptr;
84         ICraftDefManager *m_craftdef = nullptr;
85         ITextureSource *m_texturesrc = nullptr;
86         IShaderSource *m_shadersrc = nullptr;
87         ISoundManager *m_soundmgr = nullptr;
88         MtEventManager *m_eventmgr = nullptr;
89         scene::ISceneManager *m_scenemgr = nullptr;
90         IRollbackManager *m_rollbackmgr = nullptr;
91         EmergeManager *m_emergemgr = nullptr;
92         std::unique_ptr<ModChannelMgr> m_modchannel_mgr;
93 };
94
95
96 TestGameDef::TestGameDef() :
97         m_modchannel_mgr(new ModChannelMgr())
98 {
99         m_itemdef = createItemDefManager();
100         m_nodedef = createNodeDefManager();
101
102         defineSomeNodes();
103 }
104
105
106 TestGameDef::~TestGameDef()
107 {
108         delete m_itemdef;
109         delete m_nodedef;
110 }
111
112
113 void TestGameDef::defineSomeNodes()
114 {
115         IWritableItemDefManager *idef = (IWritableItemDefManager *)m_itemdef;
116         NodeDefManager *ndef = (NodeDefManager *)m_nodedef;
117
118         ItemDefinition itemdef;
119         ContentFeatures f;
120
121         //// Stone
122         itemdef = ItemDefinition();
123         itemdef.type = ITEM_NODE;
124         itemdef.name = "default:stone";
125         itemdef.description = "Stone";
126         itemdef.groups["cracky"] = 3;
127         itemdef.inventory_image = "[inventorycube"
128                 "{default_stone.png"
129                 "{default_stone.png"
130                 "{default_stone.png";
131         f = ContentFeatures();
132         f.name = itemdef.name;
133         for (TileDef &tiledef : f.tiledef)
134                 tiledef.name = "default_stone.png";
135         f.is_ground_content = true;
136         idef->registerItem(itemdef);
137         t_CONTENT_STONE = ndef->set(f.name, f);
138
139         //// Grass
140         itemdef = ItemDefinition();
141         itemdef.type = ITEM_NODE;
142         itemdef.name = "default:dirt_with_grass";
143         itemdef.description = "Dirt with grass";
144         itemdef.groups["crumbly"] = 3;
145         itemdef.inventory_image = "[inventorycube"
146                 "{default_grass.png"
147                 "{default_dirt.png&default_grass_side.png"
148                 "{default_dirt.png&default_grass_side.png";
149         f = ContentFeatures();
150         f.name = itemdef.name;
151         f.tiledef[0].name = "default_grass.png";
152         f.tiledef[1].name = "default_dirt.png";
153         for(int i = 2; i < 6; i++)
154                 f.tiledef[i].name = "default_dirt.png^default_grass_side.png";
155         f.is_ground_content = true;
156         idef->registerItem(itemdef);
157         t_CONTENT_GRASS = ndef->set(f.name, f);
158
159         //// Torch (minimal definition for lighting tests)
160         itemdef = ItemDefinition();
161         itemdef.type = ITEM_NODE;
162         itemdef.name = "default:torch";
163         f = ContentFeatures();
164         f.name = itemdef.name;
165         f.param_type = CPT_LIGHT;
166         f.light_propagates = true;
167         f.sunlight_propagates = true;
168         f.light_source = LIGHT_MAX-1;
169         idef->registerItem(itemdef);
170         t_CONTENT_TORCH = ndef->set(f.name, f);
171
172         //// Water
173         itemdef = ItemDefinition();
174         itemdef.type = ITEM_NODE;
175         itemdef.name = "default:water";
176         itemdef.description = "Water";
177         itemdef.inventory_image = "[inventorycube"
178                 "{default_water.png"
179                 "{default_water.png"
180                 "{default_water.png";
181         f = ContentFeatures();
182         f.name = itemdef.name;
183         f.alpha = 128;
184         f.liquid_type = LIQUID_SOURCE;
185         f.liquid_viscosity = 4;
186         f.is_ground_content = true;
187         f.groups["liquids"] = 3;
188         for (TileDef &tiledef : f.tiledef)
189                 tiledef.name = "default_water.png";
190         idef->registerItem(itemdef);
191         t_CONTENT_WATER = ndef->set(f.name, f);
192
193         //// Lava
194         itemdef = ItemDefinition();
195         itemdef.type = ITEM_NODE;
196         itemdef.name = "default:lava";
197         itemdef.description = "Lava";
198         itemdef.inventory_image = "[inventorycube"
199                 "{default_lava.png"
200                 "{default_lava.png"
201                 "{default_lava.png";
202         f = ContentFeatures();
203         f.name = itemdef.name;
204         f.alpha = 128;
205         f.liquid_type = LIQUID_SOURCE;
206         f.liquid_viscosity = 7;
207         f.light_source = LIGHT_MAX-1;
208         f.is_ground_content = true;
209         f.groups["liquids"] = 3;
210         for (TileDef &tiledef : f.tiledef)
211                 tiledef.name = "default_lava.png";
212         idef->registerItem(itemdef);
213         t_CONTENT_LAVA = ndef->set(f.name, f);
214
215
216         //// Brick
217         itemdef = ItemDefinition();
218         itemdef.type = ITEM_NODE;
219         itemdef.name = "default:brick";
220         itemdef.description = "Brick";
221         itemdef.groups["cracky"] = 3;
222         itemdef.inventory_image = "[inventorycube"
223                 "{default_brick.png"
224                 "{default_brick.png"
225                 "{default_brick.png";
226         f = ContentFeatures();
227         f.name = itemdef.name;
228         for (TileDef &tiledef : f.tiledef)
229                 tiledef.name = "default_brick.png";
230         f.is_ground_content = true;
231         idef->registerItem(itemdef);
232         t_CONTENT_BRICK = ndef->set(f.name, f);
233 }
234
235 bool TestGameDef::joinModChannel(const std::string &channel)
236 {
237         return m_modchannel_mgr->joinChannel(channel, PEER_ID_SERVER);
238 }
239
240 bool TestGameDef::leaveModChannel(const std::string &channel)
241 {
242         return m_modchannel_mgr->leaveChannel(channel, PEER_ID_SERVER);
243 }
244
245 bool TestGameDef::sendModChannelMessage(const std::string &channel,
246         const std::string &message)
247 {
248         if (!m_modchannel_mgr->channelRegistered(channel))
249                 return false;
250
251         return true;
252 }
253
254 ////
255 //// run_tests
256 ////
257
258 bool run_tests()
259 {
260         u64 t1 = porting::getTimeMs();
261         TestGameDef gamedef;
262
263         g_logger.setLevelSilenced(LL_ERROR, true);
264
265         u32 num_modules_failed     = 0;
266         u32 num_total_tests_failed = 0;
267         u32 num_total_tests_run    = 0;
268         std::vector<TestBase *> &testmods = TestManager::getTestModules();
269         for (size_t i = 0; i != testmods.size(); i++) {
270                 if (!testmods[i]->testModule(&gamedef))
271                         num_modules_failed++;
272
273                 num_total_tests_failed += testmods[i]->num_tests_failed;
274                 num_total_tests_run += testmods[i]->num_tests_run;
275         }
276
277         u64 tdiff = porting::getTimeMs() - t1;
278
279         g_logger.setLevelSilenced(LL_ERROR, false);
280
281         const char *overall_status = (num_modules_failed == 0) ? "PASSED" : "FAILED";
282
283         rawstream
284                 << "++++++++++++++++++++++++++++++++++++++++"
285                 << "++++++++++++++++++++++++++++++++++++++++" << std::endl
286                 << "Unit Test Results: " << overall_status << std::endl
287                 << "    " << num_modules_failed << " / " << testmods.size()
288                 << " failed modules (" << num_total_tests_failed << " / "
289                 << num_total_tests_run << " failed individual tests)." << std::endl
290                 << "    Testing took " << tdiff << "ms total." << std::endl
291                 << "++++++++++++++++++++++++++++++++++++++++"
292                 << "++++++++++++++++++++++++++++++++++++++++" << std::endl;
293
294         return num_modules_failed;
295 }
296
297 ////
298 //// TestBase
299 ////
300
301 bool TestBase::testModule(IGameDef *gamedef)
302 {
303         rawstream << "======== Testing module " << getName() << std::endl;
304         u64 t1 = porting::getTimeMs();
305
306
307         runTests(gamedef);
308
309         u64 tdiff = porting::getTimeMs() - t1;
310         rawstream << "======== Module " << getName() << " "
311                 << (num_tests_failed ? "failed" : "passed") << " (" << num_tests_failed
312                 << " failures / " << num_tests_run << " tests) - " << tdiff
313                 << "ms" << std::endl;
314
315         if (!m_test_dir.empty())
316                 fs::RecursiveDelete(m_test_dir);
317
318         return num_tests_failed == 0;
319 }
320
321 std::string TestBase::getTestTempDirectory()
322 {
323         if (!m_test_dir.empty())
324                 return m_test_dir;
325
326         char buf[32];
327         snprintf(buf, sizeof(buf), "%08X", myrand());
328
329         m_test_dir = fs::TempPath() + DIR_DELIM "mttest_" + buf;
330         if (!fs::CreateDir(m_test_dir))
331                 throw TestFailedException();
332
333         return m_test_dir;
334 }
335
336 std::string TestBase::getTestTempFile()
337 {
338         char buf[32];
339         snprintf(buf, sizeof(buf), "%08X", myrand());
340
341         return getTestTempDirectory() + DIR_DELIM + buf + ".tmp";
342 }
343
344
345 /*
346         NOTE: These tests became non-working then NodeContainer was removed.
347               These should be redone, utilizing some kind of a virtual
348                   interface for Map (IMap would be fine).
349 */
350 #if 0
351 struct TestMapBlock: public TestBase
352 {
353         class TC : public NodeContainer
354         {
355         public:
356
357                 MapNode node;
358                 bool position_valid;
359                 core::list<v3s16> validity_exceptions;
360
361                 TC()
362                 {
363                         position_valid = true;
364                 }
365
366                 virtual bool isValidPosition(v3s16 p)
367                 {
368                         //return position_valid ^ (p==position_valid_exception);
369                         bool exception = false;
370                         for(core::list<v3s16>::Iterator i=validity_exceptions.begin();
371                                         i != validity_exceptions.end(); i++)
372                         {
373                                 if(p == *i)
374                                 {
375                                         exception = true;
376                                         break;
377                                 }
378                         }
379                         return exception ? !position_valid : position_valid;
380                 }
381
382                 virtual MapNode getNode(v3s16 p)
383                 {
384                         if(isValidPosition(p) == false)
385                                 throw InvalidPositionException();
386                         return node;
387                 }
388
389                 virtual void setNode(v3s16 p, MapNode & n)
390                 {
391                         if(isValidPosition(p) == false)
392                                 throw InvalidPositionException();
393                 };
394
395                 virtual u16 nodeContainerId() const
396                 {
397                         return 666;
398                 }
399         };
400
401         void Run()
402         {
403                 TC parent;
404
405                 MapBlock b(&parent, v3s16(1,1,1));
406                 v3s16 relpos(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
407
408                 UASSERT(b.getPosRelative() == relpos);
409
410                 UASSERT(b.getBox().MinEdge.X == MAP_BLOCKSIZE);
411                 UASSERT(b.getBox().MaxEdge.X == MAP_BLOCKSIZE*2-1);
412                 UASSERT(b.getBox().MinEdge.Y == MAP_BLOCKSIZE);
413                 UASSERT(b.getBox().MaxEdge.Y == MAP_BLOCKSIZE*2-1);
414                 UASSERT(b.getBox().MinEdge.Z == MAP_BLOCKSIZE);
415                 UASSERT(b.getBox().MaxEdge.Z == MAP_BLOCKSIZE*2-1);
416
417                 UASSERT(b.isValidPosition(v3s16(0,0,0)) == true);
418                 UASSERT(b.isValidPosition(v3s16(-1,0,0)) == false);
419                 UASSERT(b.isValidPosition(v3s16(-1,-142,-2341)) == false);
420                 UASSERT(b.isValidPosition(v3s16(-124,142,2341)) == false);
421                 UASSERT(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
422                 UASSERT(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE,MAP_BLOCKSIZE-1)) == false);
423
424                 /*
425                         TODO: this method should probably be removed
426                         if the block size isn't going to be set variable
427                 */
428                 /*UASSERT(b.getSizeNodes() == v3s16(MAP_BLOCKSIZE,
429                                 MAP_BLOCKSIZE, MAP_BLOCKSIZE));*/
430
431                 // Changed flag should be initially set
432                 UASSERT(b.getModified() == MOD_STATE_WRITE_NEEDED);
433                 b.resetModified();
434                 UASSERT(b.getModified() == MOD_STATE_CLEAN);
435
436                 // All nodes should have been set to
437                 // .d=CONTENT_IGNORE and .getLight() = 0
438                 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
439                 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
440                 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
441                 {
442                         //UASSERT(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_AIR);
443                         UASSERT(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_IGNORE);
444                         UASSERT(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_DAY) == 0);
445                         UASSERT(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_NIGHT) == 0);
446                 }
447
448                 {
449                         MapNode n(CONTENT_AIR);
450                         for(u16 z=0; z<MAP_BLOCKSIZE; z++)
451                         for(u16 y=0; y<MAP_BLOCKSIZE; y++)
452                         for(u16 x=0; x<MAP_BLOCKSIZE; x++)
453                         {
454                                 b.setNode(v3s16(x,y,z), n);
455                         }
456                 }
457
458                 /*
459                         Parent fetch functions
460                 */
461                 parent.position_valid = false;
462                 parent.node.setContent(5);
463
464                 MapNode n;
465
466                 // Positions in the block should still be valid
467                 UASSERT(b.isValidPositionParent(v3s16(0,0,0)) == true);
468                 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
469                 n = b.getNodeParent(v3s16(0,MAP_BLOCKSIZE-1,0));
470                 UASSERT(n.getContent() == CONTENT_AIR);
471
472                 // ...but outside the block they should be invalid
473                 UASSERT(b.isValidPositionParent(v3s16(-121,2341,0)) == false);
474                 UASSERT(b.isValidPositionParent(v3s16(-1,0,0)) == false);
475                 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == false);
476
477                 {
478                         bool exception_thrown = false;
479                         try{
480                                 // This should throw an exception
481                                 MapNode n = b.getNodeParent(v3s16(0,0,-1));
482                         }
483                         catch(InvalidPositionException &e)
484                         {
485                                 exception_thrown = true;
486                         }
487                         UASSERT(exception_thrown);
488                 }
489
490                 parent.position_valid = true;
491                 // Now the positions outside should be valid
492                 UASSERT(b.isValidPositionParent(v3s16(-121,2341,0)) == true);
493                 UASSERT(b.isValidPositionParent(v3s16(-1,0,0)) == true);
494                 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == true);
495                 n = b.getNodeParent(v3s16(0,0,MAP_BLOCKSIZE));
496                 UASSERT(n.getContent() == 5);
497
498                 /*
499                         Set a node
500                 */
501                 v3s16 p(1,2,0);
502                 n.setContent(4);
503                 b.setNode(p, n);
504                 UASSERT(b.getNode(p).getContent() == 4);
505                 //TODO: Update to new system
506                 /*UASSERT(b.getNodeTile(p) == 4);
507                 UASSERT(b.getNodeTile(v3s16(-1,-1,0)) == 5);*/
508
509                 /*
510                         propagateSunlight()
511                 */
512                 // Set lighting of all nodes to 0
513                 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
514                         for(u16 y=0; y<MAP_BLOCKSIZE; y++){
515                                 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
516                                         MapNode n = b.getNode(v3s16(x,y,z));
517                                         n.setLight(LIGHTBANK_DAY, 0);
518                                         n.setLight(LIGHTBANK_NIGHT, 0);
519                                         b.setNode(v3s16(x,y,z), n);
520                                 }
521                         }
522                 }
523                 {
524                         /*
525                                 Check how the block handles being a lonely sky block
526                         */
527                         parent.position_valid = true;
528                         b.setIsUnderground(false);
529                         parent.node.setContent(CONTENT_AIR);
530                         parent.node.setLight(LIGHTBANK_DAY, LIGHT_SUN);
531                         parent.node.setLight(LIGHTBANK_NIGHT, 0);
532                         core::map<v3s16, bool> light_sources;
533                         // The bottom block is invalid, because we have a shadowing node
534                         UASSERT(b.propagateSunlight(light_sources) == false);
535                         UASSERT(b.getNode(v3s16(1,4,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
536                         UASSERT(b.getNode(v3s16(1,3,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
537                         UASSERT(b.getNode(v3s16(1,2,0)).getLight(LIGHTBANK_DAY) == 0);
538                         UASSERT(b.getNode(v3s16(1,1,0)).getLight(LIGHTBANK_DAY) == 0);
539                         UASSERT(b.getNode(v3s16(1,0,0)).getLight(LIGHTBANK_DAY) == 0);
540                         UASSERT(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
541                         UASSERT(b.getFaceLight2(1000, p, v3s16(0,1,0)) == LIGHT_SUN);
542                         UASSERT(b.getFaceLight2(1000, p, v3s16(0,-1,0)) == 0);
543                         UASSERT(b.getFaceLight2(0, p, v3s16(0,-1,0)) == 0);
544                         // According to MapBlock::getFaceLight,
545                         // The face on the z+ side should have double-diminished light
546                         //UASSERT(b.getFaceLight(p, v3s16(0,0,1)) == diminish_light(diminish_light(LIGHT_MAX)));
547                         // The face on the z+ side should have diminished light
548                         UASSERT(b.getFaceLight2(1000, p, v3s16(0,0,1)) == diminish_light(LIGHT_MAX));
549                 }
550                 /*
551                         Check how the block handles being in between blocks with some non-sunlight
552                         while being underground
553                 */
554                 {
555                         // Make neighbours to exist and set some non-sunlight to them
556                         parent.position_valid = true;
557                         b.setIsUnderground(true);
558                         parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
559                         core::map<v3s16, bool> light_sources;
560                         // The block below should be valid because there shouldn't be
561                         // sunlight in there either
562                         UASSERT(b.propagateSunlight(light_sources, true) == true);
563                         // Should not touch nodes that are not affected (that is, all of them)
564                         //UASSERT(b.getNode(v3s16(1,2,3)).getLight() == LIGHT_SUN);
565                         // Should set light of non-sunlighted blocks to 0.
566                         UASSERT(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == 0);
567                 }
568                 /*
569                         Set up a situation where:
570                         - There is only air in this block
571                         - There is a valid non-sunlighted block at the bottom, and
572                         - Invalid blocks elsewhere.
573                         - the block is not underground.
574
575                         This should result in bottom block invalidity
576                 */
577                 {
578                         b.setIsUnderground(false);
579                         // Clear block
580                         for(u16 z=0; z<MAP_BLOCKSIZE; z++){
581                                 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
582                                         for(u16 x=0; x<MAP_BLOCKSIZE; x++){
583                                                 MapNode n;
584                                                 n.setContent(CONTENT_AIR);
585                                                 n.setLight(LIGHTBANK_DAY, 0);
586                                                 b.setNode(v3s16(x,y,z), n);
587                                         }
588                                 }
589                         }
590                         // Make neighbours invalid
591                         parent.position_valid = false;
592                         // Add exceptions to the top of the bottom block
593                         for(u16 x=0; x<MAP_BLOCKSIZE; x++)
594                         for(u16 z=0; z<MAP_BLOCKSIZE; z++)
595                         {
596                                 parent.validity_exceptions.push_back(v3s16(MAP_BLOCKSIZE+x, MAP_BLOCKSIZE-1, MAP_BLOCKSIZE+z));
597                         }
598                         // Lighting value for the valid nodes
599                         parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
600                         core::map<v3s16, bool> light_sources;
601                         // Bottom block is not valid
602                         UASSERT(b.propagateSunlight(light_sources) == false);
603                 }
604         }
605 };
606
607 struct TestMapSector: public TestBase
608 {
609         class TC : public NodeContainer
610         {
611         public:
612
613                 MapNode node;
614                 bool position_valid;
615
616                 TC()
617                 {
618                         position_valid = true;
619                 }
620
621                 virtual bool isValidPosition(v3s16 p)
622                 {
623                         return position_valid;
624                 }
625
626                 virtual MapNode getNode(v3s16 p)
627                 {
628                         if(position_valid == false)
629                                 throw InvalidPositionException();
630                         return node;
631                 }
632
633                 virtual void setNode(v3s16 p, MapNode & n)
634                 {
635                         if(position_valid == false)
636                                 throw InvalidPositionException();
637                 };
638
639                 virtual u16 nodeContainerId() const
640                 {
641                         return 666;
642                 }
643         };
644
645         void Run()
646         {
647                 TC parent;
648                 parent.position_valid = false;
649
650                 // Create one with no heightmaps
651                 ServerMapSector sector(&parent, v2s16(1,1));
652
653                 UASSERT(sector.getBlockNoCreateNoEx(0) == 0);
654                 UASSERT(sector.getBlockNoCreateNoEx(1) == 0);
655
656                 MapBlock * bref = sector.createBlankBlock(-2);
657
658                 UASSERT(sector.getBlockNoCreateNoEx(0) == 0);
659                 UASSERT(sector.getBlockNoCreateNoEx(-2) == bref);
660
661                 //TODO: Check for AlreadyExistsException
662
663                 /*bool exception_thrown = false;
664                 try{
665                         sector.getBlock(0);
666                 }
667                 catch(InvalidPositionException &e){
668                         exception_thrown = true;
669                 }
670                 UASSERT(exception_thrown);*/
671
672         }
673 };
674 #endif