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