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