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