Initial sqlite3 maps.
[oweals/minetest.git] / src / test.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010 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 General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
14
15 You should have received a copy of the GNU 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 #include "common_irrlicht.h"
22 #include "debug.h"
23 #include "map.h"
24 #include "player.h"
25 #include "main.h"
26 #include "socket.h"
27 #include "connection.h"
28 #include "utility.h"
29 #include "serialization.h"
30 #include "voxel.h"
31 #include <sstream>
32 #include "porting.h"
33 #include "content_mapnode.h"
34 #include "mapsector.h"
35
36 /*
37         Asserts that the exception occurs
38 */
39 #define EXCEPTION_CHECK(EType, code)\
40 {\
41         bool exception_thrown = false;\
42         try{ code; }\
43         catch(EType &e) { exception_thrown = true; }\
44         assert(exception_thrown);\
45 }
46
47 struct TestUtilities
48 {
49         void Run()
50         {
51                 /*dstream<<"wrapDegrees(100.0) = "<<wrapDegrees(100.0)<<std::endl;
52                 dstream<<"wrapDegrees(720.5) = "<<wrapDegrees(720.5)<<std::endl;
53                 dstream<<"wrapDegrees(-0.5) = "<<wrapDegrees(-0.5)<<std::endl;*/
54                 assert(fabs(wrapDegrees(100.0) - 100.0) < 0.001);
55                 assert(fabs(wrapDegrees(720.5) - 0.5) < 0.001);
56                 assert(fabs(wrapDegrees(-0.5) - (-0.5)) < 0.001);
57                 assert(fabs(wrapDegrees(-365.5) - (-5.5)) < 0.001);
58                 assert(lowercase("Foo bAR") == "foo bar");
59                 assert(is_yes("YeS") == true);
60                 assert(is_yes("") == false);
61                 assert(is_yes("FAlse") == false);
62         }
63 };
64
65 struct TestSettings
66 {
67         void Run()
68         {
69                 Settings s;
70                 // Test reading of settings
71                 s.parseConfigLine("leet = 1337");
72                 s.parseConfigLine("leetleet = 13371337");
73                 s.parseConfigLine("leetleet_neg = -13371337");
74                 s.parseConfigLine("floaty_thing = 1.1");
75                 s.parseConfigLine("stringy_thing = asd /( ¤%&(/\" BLÖÄRP");
76                 s.parseConfigLine("coord = (1, 2, 4.5)");
77                 assert(s.getS32("leet") == 1337);
78                 assert(s.getS16("leetleet") == 32767);
79                 assert(s.getS16("leetleet_neg") == -32768);
80                 // Not sure if 1.1 is an exact value as a float, but doesn't matter
81                 assert(fabs(s.getFloat("floaty_thing") - 1.1) < 0.001);
82                 assert(s.get("stringy_thing") == "asd /( ¤%&(/\" BLÖÄRP");
83                 assert(fabs(s.getV3F("coord").X - 1.0) < 0.001);
84                 assert(fabs(s.getV3F("coord").Y - 2.0) < 0.001);
85                 assert(fabs(s.getV3F("coord").Z - 4.5) < 0.001);
86                 // Test the setting of settings too
87                 s.setFloat("floaty_thing_2", 1.2);
88                 s.setV3F("coord2", v3f(1, 2, 3.3));
89                 assert(s.get("floaty_thing_2").substr(0,3) == "1.2");
90                 assert(fabs(s.getFloat("floaty_thing_2") - 1.2) < 0.001);
91                 assert(fabs(s.getV3F("coord2").X - 1.0) < 0.001);
92                 assert(fabs(s.getV3F("coord2").Y - 2.0) < 0.001);
93                 assert(fabs(s.getV3F("coord2").Z - 3.3) < 0.001);
94         }
95 };
96                 
97 struct TestCompress
98 {
99         void Run()
100         {
101                 { // ver 0
102
103                 SharedBuffer<u8> fromdata(4);
104                 fromdata[0]=1;
105                 fromdata[1]=5;
106                 fromdata[2]=5;
107                 fromdata[3]=1;
108                 
109                 std::ostringstream os(std::ios_base::binary);
110                 compress(fromdata, os, 0);
111
112                 std::string str_out = os.str();
113                 
114                 dstream<<"str_out.size()="<<str_out.size()<<std::endl;
115                 dstream<<"TestCompress: 1,5,5,1 -> ";
116                 for(u32 i=0; i<str_out.size(); i++)
117                 {
118                         dstream<<(u32)str_out[i]<<",";
119                 }
120                 dstream<<std::endl;
121
122                 assert(str_out.size() == 10);
123
124                 assert(str_out[0] == 0);
125                 assert(str_out[1] == 0);
126                 assert(str_out[2] == 0);
127                 assert(str_out[3] == 4);
128                 assert(str_out[4] == 0);
129                 assert(str_out[5] == 1);
130                 assert(str_out[6] == 1);
131                 assert(str_out[7] == 5);
132                 assert(str_out[8] == 0);
133                 assert(str_out[9] == 1);
134
135                 std::istringstream is(str_out, std::ios_base::binary);
136                 std::ostringstream os2(std::ios_base::binary);
137
138                 decompress(is, os2, 0);
139                 std::string str_out2 = os2.str();
140
141                 dstream<<"decompress: ";
142                 for(u32 i=0; i<str_out2.size(); i++)
143                 {
144                         dstream<<(u32)str_out2[i]<<",";
145                 }
146                 dstream<<std::endl;
147
148                 assert(str_out2.size() == fromdata.getSize());
149
150                 for(u32 i=0; i<str_out2.size(); i++)
151                 {
152                         assert(str_out2[i] == fromdata[i]);
153                 }
154
155                 }
156
157                 { // ver HIGHEST
158
159                 SharedBuffer<u8> fromdata(4);
160                 fromdata[0]=1;
161                 fromdata[1]=5;
162                 fromdata[2]=5;
163                 fromdata[3]=1;
164                 
165                 std::ostringstream os(std::ios_base::binary);
166                 compress(fromdata, os, SER_FMT_VER_HIGHEST);
167
168                 std::string str_out = os.str();
169                 
170                 dstream<<"str_out.size()="<<str_out.size()<<std::endl;
171                 dstream<<"TestCompress: 1,5,5,1 -> ";
172                 for(u32 i=0; i<str_out.size(); i++)
173                 {
174                         dstream<<(u32)str_out[i]<<",";
175                 }
176                 dstream<<std::endl;
177
178                 /*assert(str_out.size() == 10);
179
180                 assert(str_out[0] == 0);
181                 assert(str_out[1] == 0);
182                 assert(str_out[2] == 0);
183                 assert(str_out[3] == 4);
184                 assert(str_out[4] == 0);
185                 assert(str_out[5] == 1);
186                 assert(str_out[6] == 1);
187                 assert(str_out[7] == 5);
188                 assert(str_out[8] == 0);
189                 assert(str_out[9] == 1);*/
190
191                 std::istringstream is(str_out, std::ios_base::binary);
192                 std::ostringstream os2(std::ios_base::binary);
193
194                 decompress(is, os2, SER_FMT_VER_HIGHEST);
195                 std::string str_out2 = os2.str();
196
197                 dstream<<"decompress: ";
198                 for(u32 i=0; i<str_out2.size(); i++)
199                 {
200                         dstream<<(u32)str_out2[i]<<",";
201                 }
202                 dstream<<std::endl;
203
204                 assert(str_out2.size() == fromdata.getSize());
205
206                 for(u32 i=0; i<str_out2.size(); i++)
207                 {
208                         assert(str_out2[i] == fromdata[i]);
209                 }
210
211                 }
212         }
213 };
214
215 struct TestMapNode
216 {
217         void Run()
218         {
219                 MapNode n;
220
221                 // Default values
222                 assert(n.getContent() == CONTENT_AIR);
223                 assert(n.getLight(LIGHTBANK_DAY) == 0);
224                 assert(n.getLight(LIGHTBANK_NIGHT) == 0);
225                 
226                 // Transparency
227                 n.setContent(CONTENT_AIR);
228                 assert(n.light_propagates() == true);
229                 n.setContent(CONTENT_STONE);
230                 assert(n.light_propagates() == false);
231         }
232 };
233
234 struct TestVoxelManipulator
235 {
236         void Run()
237         {
238                 /*
239                         VoxelArea
240                 */
241
242                 VoxelArea a(v3s16(-1,-1,-1), v3s16(1,1,1));
243                 assert(a.index(0,0,0) == 1*3*3 + 1*3 + 1);
244                 assert(a.index(-1,-1,-1) == 0);
245                 
246                 VoxelArea c(v3s16(-2,-2,-2), v3s16(2,2,2));
247                 // An area that is 1 bigger in x+ and z-
248                 VoxelArea d(v3s16(-2,-2,-3), v3s16(3,2,2));
249                 
250                 core::list<VoxelArea> aa;
251                 d.diff(c, aa);
252                 
253                 // Correct results
254                 core::array<VoxelArea> results;
255                 results.push_back(VoxelArea(v3s16(-2,-2,-3),v3s16(3,2,-3)));
256                 results.push_back(VoxelArea(v3s16(3,-2,-2),v3s16(3,2,2)));
257
258                 assert(aa.size() == results.size());
259                 
260                 dstream<<"Result of diff:"<<std::endl;
261                 for(core::list<VoxelArea>::Iterator
262                                 i = aa.begin(); i != aa.end(); i++)
263                 {
264                         i->print(dstream);
265                         dstream<<std::endl;
266                         
267                         s32 j = results.linear_search(*i);
268                         assert(j != -1);
269                         results.erase(j, 1);
270                 }
271
272
273                 /*
274                         VoxelManipulator
275                 */
276                 
277                 VoxelManipulator v;
278
279                 v.print(dstream);
280
281                 dstream<<"*** Setting (-1,0,-1)=2 ***"<<std::endl;
282                 
283                 v.setNodeNoRef(v3s16(-1,0,-1), MapNode(2));
284
285                 v.print(dstream);
286
287                 assert(v.getNode(v3s16(-1,0,-1)).getContent() == 2);
288
289                 dstream<<"*** Reading from inexistent (0,0,-1) ***"<<std::endl;
290
291                 EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,0,-1)));
292
293                 v.print(dstream);
294
295                 dstream<<"*** Adding area ***"<<std::endl;
296
297                 v.addArea(a);
298                 
299                 v.print(dstream);
300
301                 assert(v.getNode(v3s16(-1,0,-1)).getContent() == 2);
302                 EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,1,1)));
303
304 #if 0
305                 /*
306                         Water stuff
307                 */
308
309                 v.clear();
310
311                 const char *content =
312                         "#...######  "
313                         "#...##..##  "
314                         "#........ .."
315                         "############"
316
317                         "#...######  "
318                         "#...##..##  "
319                         "#........#  "
320                         "############"
321                 ;
322
323                 v3s16 size(12, 4, 2);
324                 VoxelArea area(v3s16(0,0,0), size-v3s16(1,1,1));
325                 
326                 const char *p = content;
327                 for(s16 z=0; z<size.Z; z++)
328                 for(s16 y=size.Y-1; y>=0; y--)
329                 for(s16 x=0; x<size.X; x++)
330                 {
331                         MapNode n;
332                         //n.pressure = size.Y - y;
333                         if(*p == '#')
334                                 n.setContent(CONTENT_STONE);
335                         else if(*p == '.')
336                                 n.setContent(CONTENT_WATER);
337                         else if(*p == ' ')
338                                 n.setContent(CONTENT_AIR);
339                         else
340                                 assert(0);
341                         v.setNode(v3s16(x,y,z), n);
342                         p++;
343                 }
344
345                 v.print(dstream, VOXELPRINT_WATERPRESSURE);
346                 
347                 core::map<v3s16, u8> active_nodes;
348                 v.updateAreaWaterPressure(area, active_nodes);
349
350                 v.print(dstream, VOXELPRINT_WATERPRESSURE);
351                 
352                 //s16 highest_y = -32768;
353                 /*
354                         NOTE: These are commented out because this behaviour is changed
355                               all the time
356                 */
357                 //assert(v.getWaterPressure(v3s16(7, 1, 1), highest_y, 0) == -1);
358                 //assert(highest_y == 3);
359                 /*assert(v.getWaterPressure(v3s16(7, 1, 1), highest_y, 0) == 3);
360                 //assert(highest_y == 3);*/
361                 
362                 active_nodes.clear();
363                 active_nodes[v3s16(9,1,0)] = 1;
364                 //v.flowWater(active_nodes, 0, true, 1000);
365                 v.flowWater(active_nodes, 0, false, 1000);
366                 
367                 dstream<<"Final result of flowWater:"<<std::endl;
368                 v.print(dstream, VOXELPRINT_WATERPRESSURE);
369 #endif
370                 
371                 //assert(0);
372         }
373 };
374
375 /*
376         NOTE: These tests became non-working then NodeContainer was removed.
377               These should be redone, utilizing some kind of a virtual
378                   interface for Map (IMap would be fine).
379 */
380 #if 0
381 struct TestMapBlock
382 {
383         class TC : public NodeContainer
384         {
385         public:
386
387                 MapNode node;
388                 bool position_valid;
389                 core::list<v3s16> validity_exceptions;
390
391                 TC()
392                 {
393                         position_valid = true;
394                 }
395
396                 virtual bool isValidPosition(v3s16 p)
397                 {
398                         //return position_valid ^ (p==position_valid_exception);
399                         bool exception = false;
400                         for(core::list<v3s16>::Iterator i=validity_exceptions.begin();
401                                         i != validity_exceptions.end(); i++)
402                         {
403                                 if(p == *i)
404                                 {
405                                         exception = true;
406                                         break;
407                                 }
408                         }
409                         return exception ? !position_valid : position_valid;
410                 }
411
412                 virtual MapNode getNode(v3s16 p)
413                 {
414                         if(isValidPosition(p) == false)
415                                 throw InvalidPositionException();
416                         return node;
417                 }
418
419                 virtual void setNode(v3s16 p, MapNode & n)
420                 {
421                         if(isValidPosition(p) == false)
422                                 throw InvalidPositionException();
423                 };
424
425                 virtual u16 nodeContainerId() const
426                 {
427                         return 666;
428                 }
429         };
430
431         void Run()
432         {
433                 TC parent;
434                 
435                 MapBlock b(&parent, v3s16(1,1,1));
436                 v3s16 relpos(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
437
438                 assert(b.getPosRelative() == relpos);
439
440                 assert(b.getBox().MinEdge.X == MAP_BLOCKSIZE);
441                 assert(b.getBox().MaxEdge.X == MAP_BLOCKSIZE*2-1);
442                 assert(b.getBox().MinEdge.Y == MAP_BLOCKSIZE);
443                 assert(b.getBox().MaxEdge.Y == MAP_BLOCKSIZE*2-1);
444                 assert(b.getBox().MinEdge.Z == MAP_BLOCKSIZE);
445                 assert(b.getBox().MaxEdge.Z == MAP_BLOCKSIZE*2-1);
446                 
447                 assert(b.isValidPosition(v3s16(0,0,0)) == true);
448                 assert(b.isValidPosition(v3s16(-1,0,0)) == false);
449                 assert(b.isValidPosition(v3s16(-1,-142,-2341)) == false);
450                 assert(b.isValidPosition(v3s16(-124,142,2341)) == false);
451                 assert(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
452                 assert(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE,MAP_BLOCKSIZE-1)) == false);
453
454                 /*
455                         TODO: this method should probably be removed
456                         if the block size isn't going to be set variable
457                 */
458                 /*assert(b.getSizeNodes() == v3s16(MAP_BLOCKSIZE,
459                                 MAP_BLOCKSIZE, MAP_BLOCKSIZE));*/
460                 
461                 // Changed flag should be initially set
462                 assert(b.getChangedFlag() == true);
463                 b.resetChangedFlag();
464                 assert(b.getChangedFlag() == false);
465
466                 // All nodes should have been set to
467                 // .d=CONTENT_IGNORE and .getLight() = 0
468                 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
469                 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
470                 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
471                 {
472                         //assert(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_AIR);
473                         assert(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_IGNORE);
474                         assert(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_DAY) == 0);
475                         assert(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_NIGHT) == 0);
476                 }
477                 
478                 {
479                         MapNode n(CONTENT_AIR);
480                         for(u16 z=0; z<MAP_BLOCKSIZE; z++)
481                         for(u16 y=0; y<MAP_BLOCKSIZE; y++)
482                         for(u16 x=0; x<MAP_BLOCKSIZE; x++)
483                         {
484                                 b.setNode(v3s16(x,y,z), n);
485                         }
486                 }
487                         
488                 /*
489                         Parent fetch functions
490                 */
491                 parent.position_valid = false;
492                 parent.node.setContent(5);
493
494                 MapNode n;
495                 
496                 // Positions in the block should still be valid
497                 assert(b.isValidPositionParent(v3s16(0,0,0)) == true);
498                 assert(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
499                 n = b.getNodeParent(v3s16(0,MAP_BLOCKSIZE-1,0));
500                 assert(n.getContent() == CONTENT_AIR);
501
502                 // ...but outside the block they should be invalid
503                 assert(b.isValidPositionParent(v3s16(-121,2341,0)) == false);
504                 assert(b.isValidPositionParent(v3s16(-1,0,0)) == false);
505                 assert(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == false);
506                 
507                 {
508                         bool exception_thrown = false;
509                         try{
510                                 // This should throw an exception
511                                 MapNode n = b.getNodeParent(v3s16(0,0,-1));
512                         }
513                         catch(InvalidPositionException &e)
514                         {
515                                 exception_thrown = true;
516                         }
517                         assert(exception_thrown);
518                 }
519
520                 parent.position_valid = true;
521                 // Now the positions outside should be valid
522                 assert(b.isValidPositionParent(v3s16(-121,2341,0)) == true);
523                 assert(b.isValidPositionParent(v3s16(-1,0,0)) == true);
524                 assert(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == true);
525                 n = b.getNodeParent(v3s16(0,0,MAP_BLOCKSIZE));
526                 assert(n.getContent() == 5);
527
528                 /*
529                         Set a node
530                 */
531                 v3s16 p(1,2,0);
532                 n.setContent(4);
533                 b.setNode(p, n);
534                 assert(b.getNode(p).getContent() == 4);
535                 //TODO: Update to new system
536                 /*assert(b.getNodeTile(p) == 4);
537                 assert(b.getNodeTile(v3s16(-1,-1,0)) == 5);*/
538                 
539                 /*
540                         propagateSunlight()
541                 */
542                 // Set lighting of all nodes to 0
543                 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
544                         for(u16 y=0; y<MAP_BLOCKSIZE; y++){
545                                 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
546                                         MapNode n = b.getNode(v3s16(x,y,z));
547                                         n.setLight(LIGHTBANK_DAY, 0);
548                                         n.setLight(LIGHTBANK_NIGHT, 0);
549                                         b.setNode(v3s16(x,y,z), n);
550                                 }
551                         }
552                 }
553                 {
554                         /*
555                                 Check how the block handles being a lonely sky block
556                         */
557                         parent.position_valid = true;
558                         b.setIsUnderground(false);
559                         parent.node.setContent(CONTENT_AIR);
560                         parent.node.setLight(LIGHTBANK_DAY, LIGHT_SUN);
561                         parent.node.setLight(LIGHTBANK_NIGHT, 0);
562                         core::map<v3s16, bool> light_sources;
563                         // The bottom block is invalid, because we have a shadowing node
564                         assert(b.propagateSunlight(light_sources) == false);
565                         assert(b.getNode(v3s16(1,4,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
566                         assert(b.getNode(v3s16(1,3,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
567                         assert(b.getNode(v3s16(1,2,0)).getLight(LIGHTBANK_DAY) == 0);
568                         assert(b.getNode(v3s16(1,1,0)).getLight(LIGHTBANK_DAY) == 0);
569                         assert(b.getNode(v3s16(1,0,0)).getLight(LIGHTBANK_DAY) == 0);
570                         assert(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
571                         assert(b.getFaceLight2(1000, p, v3s16(0,1,0)) == LIGHT_SUN);
572                         assert(b.getFaceLight2(1000, p, v3s16(0,-1,0)) == 0);
573                         assert(b.getFaceLight2(0, p, v3s16(0,-1,0)) == 0);
574                         // According to MapBlock::getFaceLight,
575                         // The face on the z+ side should have double-diminished light
576                         //assert(b.getFaceLight(p, v3s16(0,0,1)) == diminish_light(diminish_light(LIGHT_MAX)));
577                         // The face on the z+ side should have diminished light
578                         assert(b.getFaceLight2(1000, p, v3s16(0,0,1)) == diminish_light(LIGHT_MAX));
579                 }
580                 /*
581                         Check how the block handles being in between blocks with some non-sunlight
582                         while being underground
583                 */
584                 {
585                         // Make neighbours to exist and set some non-sunlight to them
586                         parent.position_valid = true;
587                         b.setIsUnderground(true);
588                         parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
589                         core::map<v3s16, bool> light_sources;
590                         // The block below should be valid because there shouldn't be
591                         // sunlight in there either
592                         assert(b.propagateSunlight(light_sources, true) == true);
593                         // Should not touch nodes that are not affected (that is, all of them)
594                         //assert(b.getNode(v3s16(1,2,3)).getLight() == LIGHT_SUN);
595                         // Should set light of non-sunlighted blocks to 0.
596                         assert(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == 0);
597                 }
598                 /*
599                         Set up a situation where:
600                         - There is only air in this block
601                         - There is a valid non-sunlighted block at the bottom, and
602                         - Invalid blocks elsewhere.
603                         - the block is not underground.
604
605                         This should result in bottom block invalidity
606                 */
607                 {
608                         b.setIsUnderground(false);
609                         // Clear block
610                         for(u16 z=0; z<MAP_BLOCKSIZE; z++){
611                                 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
612                                         for(u16 x=0; x<MAP_BLOCKSIZE; x++){
613                                                 MapNode n;
614                                                 n.setContent(CONTENT_AIR);
615                                                 n.setLight(LIGHTBANK_DAY, 0);
616                                                 b.setNode(v3s16(x,y,z), n);
617                                         }
618                                 }
619                         }
620                         // Make neighbours invalid
621                         parent.position_valid = false;
622                         // Add exceptions to the top of the bottom block
623                         for(u16 x=0; x<MAP_BLOCKSIZE; x++)
624                         for(u16 z=0; z<MAP_BLOCKSIZE; z++)
625                         {
626                                 parent.validity_exceptions.push_back(v3s16(MAP_BLOCKSIZE+x, MAP_BLOCKSIZE-1, MAP_BLOCKSIZE+z));
627                         }
628                         // Lighting value for the valid nodes
629                         parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
630                         core::map<v3s16, bool> light_sources;
631                         // Bottom block is not valid
632                         assert(b.propagateSunlight(light_sources) == false);
633                 }
634         }
635 };
636
637 struct TestMapSector
638 {
639         class TC : public NodeContainer
640         {
641         public:
642
643                 MapNode node;
644                 bool position_valid;
645
646                 TC()
647                 {
648                         position_valid = true;
649                 }
650
651                 virtual bool isValidPosition(v3s16 p)
652                 {
653                         return position_valid;
654                 }
655
656                 virtual MapNode getNode(v3s16 p)
657                 {
658                         if(position_valid == false)
659                                 throw InvalidPositionException();
660                         return node;
661                 }
662
663                 virtual void setNode(v3s16 p, MapNode & n)
664                 {
665                         if(position_valid == false)
666                                 throw InvalidPositionException();
667                 };
668                 
669                 virtual u16 nodeContainerId() const
670                 {
671                         return 666;
672                 }
673         };
674         
675         void Run()
676         {
677                 TC parent;
678                 parent.position_valid = false;
679                 
680                 // Create one with no heightmaps
681                 ServerMapSector sector(&parent, v2s16(1,1));
682                 
683                 assert(sector.getBlockNoCreateNoEx(0) == 0);
684                 assert(sector.getBlockNoCreateNoEx(1) == 0);
685
686                 MapBlock * bref = sector.createBlankBlock(-2);
687                 
688                 assert(sector.getBlockNoCreateNoEx(0) == 0);
689                 assert(sector.getBlockNoCreateNoEx(-2) == bref);
690                 
691                 //TODO: Check for AlreadyExistsException
692
693                 /*bool exception_thrown = false;
694                 try{
695                         sector.getBlock(0);
696                 }
697                 catch(InvalidPositionException &e){
698                         exception_thrown = true;
699                 }
700                 assert(exception_thrown);*/
701
702         }
703 };
704 #endif
705
706 struct TestSocket
707 {
708         void Run()
709         {
710                 const int port = 30003;
711                 UDPSocket socket;
712                 socket.Bind(port);
713
714                 const char sendbuffer[] = "hello world!";
715                 socket.Send(Address(127,0,0,1,port), sendbuffer, sizeof(sendbuffer));
716
717                 sleep_ms(50);
718
719                 char rcvbuffer[256];
720                 memset(rcvbuffer, 0, sizeof(rcvbuffer));
721                 Address sender;
722                 for(;;)
723                 {
724                         int bytes_read = socket.Receive(sender, rcvbuffer, sizeof(rcvbuffer));
725                         if(bytes_read < 0)
726                                 break;
727                 }
728                 //FIXME: This fails on some systems
729                 assert(strncmp(sendbuffer, rcvbuffer, sizeof(sendbuffer))==0);
730                 assert(sender.getAddress() == Address(127,0,0,1, 0).getAddress());
731         }
732 };
733
734 struct TestConnection
735 {
736         void TestHelpers()
737         {
738                 /*
739                         Test helper functions
740                 */
741
742                 // Some constants for testing
743                 u32 proto_id = 0x12345678;
744                 u16 peer_id = 123;
745                 u8 channel = 2;
746                 SharedBuffer<u8> data1(1);
747                 data1[0] = 100;
748                 Address a(127,0,0,1, 10);
749                 u16 seqnum = 34352;
750
751                 con::BufferedPacket p1 = con::makePacket(a, data1,
752                                 proto_id, peer_id, channel);
753                 /*
754                         We should now have a packet with this data:
755                         Header:
756                                 [0] u32 protocol_id
757                                 [4] u16 sender_peer_id
758                                 [6] u8 channel
759                         Data:
760                                 [7] u8 data1[0]
761                 */
762                 assert(readU32(&p1.data[0]) == proto_id);
763                 assert(readU16(&p1.data[4]) == peer_id);
764                 assert(readU8(&p1.data[6]) == channel);
765                 assert(readU8(&p1.data[7]) == data1[0]);
766                 
767                 //dstream<<"initial data1[0]="<<((u32)data1[0]&0xff)<<std::endl;
768
769                 SharedBuffer<u8> p2 = con::makeReliablePacket(data1, seqnum);
770
771                 /*dstream<<"p2.getSize()="<<p2.getSize()<<", data1.getSize()="
772                                 <<data1.getSize()<<std::endl;
773                 dstream<<"readU8(&p2[3])="<<readU8(&p2[3])
774                                 <<" p2[3]="<<((u32)p2[3]&0xff)<<std::endl;
775                 dstream<<"data1[0]="<<((u32)data1[0]&0xff)<<std::endl;*/
776
777                 assert(p2.getSize() == 3 + data1.getSize());
778                 assert(readU8(&p2[0]) == TYPE_RELIABLE);
779                 assert(readU16(&p2[1]) == seqnum);
780                 assert(readU8(&p2[3]) == data1[0]);
781         }
782
783         struct Handler : public con::PeerHandler
784         {
785                 Handler(const char *a_name)
786                 {
787                         count = 0;
788                         last_id = 0;
789                         name = a_name;
790                 }
791                 void peerAdded(con::Peer *peer)
792                 {
793                         dstream<<"Handler("<<name<<")::peerAdded(): "
794                                         "id="<<peer->id<<std::endl;
795                         last_id = peer->id;
796                         count++;
797                 }
798                 void deletingPeer(con::Peer *peer, bool timeout)
799                 {
800                         dstream<<"Handler("<<name<<")::deletingPeer(): "
801                                         "id="<<peer->id
802                                         <<", timeout="<<timeout<<std::endl;
803                         last_id = peer->id;
804                         count--;
805                 }
806
807                 s32 count;
808                 u16 last_id;
809                 const char *name;
810         };
811
812         void Run()
813         {
814                 DSTACK("TestConnection::Run");
815
816                 TestHelpers();
817
818                 /*
819                         Test some real connections
820                 */
821                 u32 proto_id = 0xad26846a;
822
823                 Handler hand_server("server");
824                 Handler hand_client("client");
825                 
826                 dstream<<"** Creating server Connection"<<std::endl;
827                 con::Connection server(proto_id, 512, 5.0, &hand_server);
828                 server.Serve(30001);
829                 
830                 dstream<<"** Creating client Connection"<<std::endl;
831                 con::Connection client(proto_id, 512, 5.0, &hand_client);
832
833                 assert(hand_server.count == 0);
834                 assert(hand_client.count == 0);
835                 
836                 sleep_ms(50);
837                 
838                 Address server_address(127,0,0,1, 30001);
839                 dstream<<"** running client.Connect()"<<std::endl;
840                 client.Connect(server_address);
841
842                 sleep_ms(50);
843                 
844                 // Client should have added server now
845                 assert(hand_client.count == 1);
846                 assert(hand_client.last_id == 1);
847                 // But server should not have added client
848                 assert(hand_server.count == 0);
849
850                 try
851                 {
852                         u16 peer_id;
853                         u8 data[100];
854                         dstream<<"** running server.Receive()"<<std::endl;
855                         u32 size = server.Receive(peer_id, data, 100);
856                         dstream<<"** Server received: peer_id="<<peer_id
857                                         <<", size="<<size
858                                         <<std::endl;
859                 }
860                 catch(con::NoIncomingDataException &e)
861                 {
862                         // No actual data received, but the client has
863                         // probably been connected
864                 }
865                 
866                 // Client should be the same
867                 assert(hand_client.count == 1);
868                 assert(hand_client.last_id == 1);
869                 // Server should have the client
870                 assert(hand_server.count == 1);
871                 assert(hand_server.last_id == 2);
872                 
873                 //sleep_ms(50);
874
875                 while(client.Connected() == false)
876                 {
877                         try
878                         {
879                                 u16 peer_id;
880                                 u8 data[100];
881                                 dstream<<"** running client.Receive()"<<std::endl;
882                                 u32 size = client.Receive(peer_id, data, 100);
883                                 dstream<<"** Client received: peer_id="<<peer_id
884                                                 <<", size="<<size
885                                                 <<std::endl;
886                         }
887                         catch(con::NoIncomingDataException &e)
888                         {
889                         }
890                         sleep_ms(50);
891                 }
892
893                 sleep_ms(50);
894                 
895                 try
896                 {
897                         u16 peer_id;
898                         u8 data[100];
899                         dstream<<"** running server.Receive()"<<std::endl;
900                         u32 size = server.Receive(peer_id, data, 100);
901                         dstream<<"** Server received: peer_id="<<peer_id
902                                         <<", size="<<size
903                                         <<std::endl;
904                 }
905                 catch(con::NoIncomingDataException &e)
906                 {
907                 }
908
909                 {
910                         /*u8 data[] = "Hello World!";
911                         u32 datasize = sizeof(data);*/
912                         SharedBuffer<u8> data = SharedBufferFromString("Hello World!");
913
914                         dstream<<"** running client.Send()"<<std::endl;
915                         client.Send(PEER_ID_SERVER, 0, data, true);
916
917                         sleep_ms(50);
918
919                         u16 peer_id;
920                         u8 recvdata[100];
921                         dstream<<"** running server.Receive()"<<std::endl;
922                         u32 size = server.Receive(peer_id, recvdata, 100);
923                         dstream<<"** Server received: peer_id="<<peer_id
924                                         <<", size="<<size
925                                         <<", data="<<*data
926                                         <<std::endl;
927                         assert(memcmp(*data, recvdata, data.getSize()) == 0);
928                 }
929                 
930                 u16 peer_id_client = 2;
931
932                 {
933                         /*
934                                 Send consequent packets in different order
935                         */
936                         //u8 data1[] = "hello1";
937                         //u8 data2[] = "hello2";
938                         SharedBuffer<u8> data1 = SharedBufferFromString("hello1");
939                         SharedBuffer<u8> data2 = SharedBufferFromString("Hello2");
940
941                         Address client_address =
942                                         server.GetPeer(peer_id_client)->address;
943                         
944                         dstream<<"*** Sending packets in wrong order (2,1,2)"
945                                         <<std::endl;
946                         
947                         u8 chn = 0;
948                         con::Channel *ch = &server.GetPeer(peer_id_client)->channels[chn];
949                         u16 sn = ch->next_outgoing_seqnum;
950                         ch->next_outgoing_seqnum = sn+1;
951                         server.Send(peer_id_client, chn, data2, true);
952                         ch->next_outgoing_seqnum = sn;
953                         server.Send(peer_id_client, chn, data1, true);
954                         ch->next_outgoing_seqnum = sn+1;
955                         server.Send(peer_id_client, chn, data2, true);
956
957                         sleep_ms(50);
958
959                         dstream<<"*** Receiving the packets"<<std::endl;
960
961                         u16 peer_id;
962                         u8 recvdata[20];
963                         u32 size;
964
965                         dstream<<"** running client.Receive()"<<std::endl;
966                         peer_id = 132;
967                         size = client.Receive(peer_id, recvdata, 20);
968                         dstream<<"** Client received: peer_id="<<peer_id
969                                         <<", size="<<size
970                                         <<", data="<<recvdata
971                                         <<std::endl;
972                         assert(size == data1.getSize());
973                         assert(memcmp(*data1, recvdata, data1.getSize()) == 0);
974                         assert(peer_id == PEER_ID_SERVER);
975                         
976                         dstream<<"** running client.Receive()"<<std::endl;
977                         peer_id = 132;
978                         size = client.Receive(peer_id, recvdata, 20);
979                         dstream<<"** Client received: peer_id="<<peer_id
980                                         <<", size="<<size
981                                         <<", data="<<recvdata
982                                         <<std::endl;
983                         assert(size == data2.getSize());
984                         assert(memcmp(*data2, recvdata, data2.getSize()) == 0);
985                         assert(peer_id == PEER_ID_SERVER);
986                         
987                         bool got_exception = false;
988                         try
989                         {
990                                 dstream<<"** running client.Receive()"<<std::endl;
991                                 peer_id = 132;
992                                 size = client.Receive(peer_id, recvdata, 20);
993                                 dstream<<"** Client received: peer_id="<<peer_id
994                                                 <<", size="<<size
995                                                 <<", data="<<recvdata
996                                                 <<std::endl;
997                         }
998                         catch(con::NoIncomingDataException &e)
999                         {
1000                                 dstream<<"** No incoming data for client"<<std::endl;
1001                                 got_exception = true;
1002                         }
1003                         assert(got_exception);
1004                 }
1005                 {
1006                         const int datasize = 30000;
1007                         SharedBuffer<u8> data1(datasize);
1008                         for(u16 i=0; i<datasize; i++){
1009                                 data1[i] = i/4;
1010                         }
1011
1012                         dstream<<"Sending data (size="<<datasize<<"):";
1013                         for(int i=0; i<datasize && i<20; i++){
1014                                 if(i%2==0) DEBUGPRINT(" ");
1015                                 DEBUGPRINT("%.2X", ((int)((const char*)*data1)[i])&0xff);
1016                         }
1017                         if(datasize>20)
1018                                 dstream<<"...";
1019                         dstream<<std::endl;
1020                         
1021                         server.Send(peer_id_client, 0, data1, true);
1022
1023                         sleep_ms(50);
1024                         
1025                         u8 recvdata[datasize + 1000];
1026                         dstream<<"** running client.Receive()"<<std::endl;
1027                         u16 peer_id = 132;
1028                         u16 size = client.Receive(peer_id, recvdata, datasize + 1000);
1029                         dstream<<"** Client received: peer_id="<<peer_id
1030                                         <<", size="<<size
1031                                         <<std::endl;
1032
1033                         dstream<<"Received data (size="<<size<<"):";
1034                         for(int i=0; i<size && i<20; i++){
1035                                 if(i%2==0) DEBUGPRINT(" ");
1036                                 DEBUGPRINT("%.2X", ((int)((const char*)recvdata)[i])&0xff);
1037                         }
1038                         if(size>20)
1039                                 dstream<<"...";
1040                         dstream<<std::endl;
1041
1042                         assert(memcmp(*data1, recvdata, data1.getSize()) == 0);
1043                         assert(peer_id == PEER_ID_SERVER);
1044                 }
1045                 
1046                 // Check peer handlers
1047                 assert(hand_client.count == 1);
1048                 assert(hand_client.last_id == 1);
1049                 assert(hand_server.count == 1);
1050                 assert(hand_server.last_id == 2);
1051                 
1052                 //assert(0);
1053         }
1054 };
1055
1056 #define TEST(X)\
1057 {\
1058         X x;\
1059         dstream<<"Running " #X <<std::endl;\
1060         x.Run();\
1061 }
1062
1063 void run_tests()
1064 {
1065         DSTACK(__FUNCTION_NAME);
1066         dstream<<"run_tests() started"<<std::endl;
1067         TEST(TestUtilities);
1068         TEST(TestSettings);
1069         TEST(TestCompress);
1070         TEST(TestMapNode);
1071         TEST(TestVoxelManipulator);
1072         //TEST(TestMapBlock);
1073         //TEST(TestMapSector);
1074         if(INTERNET_SIMULATOR == false){
1075                 TEST(TestSocket);
1076                 dout_con<<"=== BEGIN RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
1077                 TEST(TestConnection);
1078                 dout_con<<"=== END RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
1079         }
1080         dstream<<"run_tests() passed"<<std::endl;
1081 }
1082