]> git.lizzy.rs Git - dragonfireclient.git/blob - src/test.cpp
made screen go slightly blue when underwater
[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 /*
344         NOTE: These tests became non-working then NodeContainer was removed.
345               These should be redone, utilizing some kind of a virtual
346                   interface for Map (IMap would be fine).
347 */
348 #if 0
349 struct TestMapBlock
350 {
351         class TC : public NodeContainer
352         {
353         public:
354
355                 MapNode node;
356                 bool position_valid;
357                 core::list<v3s16> validity_exceptions;
358
359                 TC()
360                 {
361                         position_valid = true;
362                 }
363
364                 virtual bool isValidPosition(v3s16 p)
365                 {
366                         //return position_valid ^ (p==position_valid_exception);
367                         bool exception = false;
368                         for(core::list<v3s16>::Iterator i=validity_exceptions.begin();
369                                         i != validity_exceptions.end(); i++)
370                         {
371                                 if(p == *i)
372                                 {
373                                         exception = true;
374                                         break;
375                                 }
376                         }
377                         return exception ? !position_valid : position_valid;
378                 }
379
380                 virtual MapNode getNode(v3s16 p)
381                 {
382                         if(isValidPosition(p) == false)
383                                 throw InvalidPositionException();
384                         return node;
385                 }
386
387                 virtual void setNode(v3s16 p, MapNode & n)
388                 {
389                         if(isValidPosition(p) == false)
390                                 throw InvalidPositionException();
391                 };
392
393                 virtual u16 nodeContainerId() const
394                 {
395                         return 666;
396                 }
397         };
398
399         void Run()
400         {
401                 TC parent;
402                 
403                 MapBlock b(&parent, v3s16(1,1,1));
404                 v3s16 relpos(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
405
406                 assert(b.getPosRelative() == relpos);
407
408                 assert(b.getBox().MinEdge.X == MAP_BLOCKSIZE);
409                 assert(b.getBox().MaxEdge.X == MAP_BLOCKSIZE*2-1);
410                 assert(b.getBox().MinEdge.Y == MAP_BLOCKSIZE);
411                 assert(b.getBox().MaxEdge.Y == MAP_BLOCKSIZE*2-1);
412                 assert(b.getBox().MinEdge.Z == MAP_BLOCKSIZE);
413                 assert(b.getBox().MaxEdge.Z == MAP_BLOCKSIZE*2-1);
414                 
415                 assert(b.isValidPosition(v3s16(0,0,0)) == true);
416                 assert(b.isValidPosition(v3s16(-1,0,0)) == false);
417                 assert(b.isValidPosition(v3s16(-1,-142,-2341)) == false);
418                 assert(b.isValidPosition(v3s16(-124,142,2341)) == false);
419                 assert(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
420                 assert(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE,MAP_BLOCKSIZE-1)) == false);
421
422                 /*
423                         TODO: this method should probably be removed
424                         if the block size isn't going to be set variable
425                 */
426                 /*assert(b.getSizeNodes() == v3s16(MAP_BLOCKSIZE,
427                                 MAP_BLOCKSIZE, MAP_BLOCKSIZE));*/
428                 
429                 // Changed flag should be initially set
430                 assert(b.getChangedFlag() == true);
431                 b.resetChangedFlag();
432                 assert(b.getChangedFlag() == false);
433
434                 // All nodes should have been set to
435                 // .d=CONTENT_IGNORE and .getLight() = 0
436                 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
437                 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
438                 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
439                 {
440                         //assert(b.getNode(v3s16(x,y,z)).d == CONTENT_AIR);
441                         assert(b.getNode(v3s16(x,y,z)).d == CONTENT_IGNORE);
442                         assert(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_DAY) == 0);
443                         assert(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_NIGHT) == 0);
444                 }
445                 
446                 {
447                         MapNode n(CONTENT_AIR);
448                         for(u16 z=0; z<MAP_BLOCKSIZE; z++)
449                         for(u16 y=0; y<MAP_BLOCKSIZE; y++)
450                         for(u16 x=0; x<MAP_BLOCKSIZE; x++)
451                         {
452                                 b.setNode(v3s16(x,y,z), n);
453                         }
454                 }
455                         
456                 /*
457                         Parent fetch functions
458                 */
459                 parent.position_valid = false;
460                 parent.node.d = 5;
461
462                 MapNode n;
463                 
464                 // Positions in the block should still be valid
465                 assert(b.isValidPositionParent(v3s16(0,0,0)) == true);
466                 assert(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
467                 n = b.getNodeParent(v3s16(0,MAP_BLOCKSIZE-1,0));
468                 assert(n.d == CONTENT_AIR);
469
470                 // ...but outside the block they should be invalid
471                 assert(b.isValidPositionParent(v3s16(-121,2341,0)) == false);
472                 assert(b.isValidPositionParent(v3s16(-1,0,0)) == false);
473                 assert(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == false);
474                 
475                 {
476                         bool exception_thrown = false;
477                         try{
478                                 // This should throw an exception
479                                 MapNode n = b.getNodeParent(v3s16(0,0,-1));
480                         }
481                         catch(InvalidPositionException &e)
482                         {
483                                 exception_thrown = true;
484                         }
485                         assert(exception_thrown);
486                 }
487
488                 parent.position_valid = true;
489                 // Now the positions outside should be valid
490                 assert(b.isValidPositionParent(v3s16(-121,2341,0)) == true);
491                 assert(b.isValidPositionParent(v3s16(-1,0,0)) == true);
492                 assert(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == true);
493                 n = b.getNodeParent(v3s16(0,0,MAP_BLOCKSIZE));
494                 assert(n.d == 5);
495
496                 /*
497                         Set a node
498                 */
499                 v3s16 p(1,2,0);
500                 n.d = 4;
501                 b.setNode(p, n);
502                 assert(b.getNode(p).d == 4);
503                 //TODO: Update to new system
504                 /*assert(b.getNodeTile(p) == 4);
505                 assert(b.getNodeTile(v3s16(-1,-1,0)) == 5);*/
506                 
507                 /*
508                         propagateSunlight()
509                 */
510                 // Set lighting of all nodes to 0
511                 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
512                         for(u16 y=0; y<MAP_BLOCKSIZE; y++){
513                                 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
514                                         MapNode n = b.getNode(v3s16(x,y,z));
515                                         n.setLight(LIGHTBANK_DAY, 0);
516                                         n.setLight(LIGHTBANK_NIGHT, 0);
517                                         b.setNode(v3s16(x,y,z), n);
518                                 }
519                         }
520                 }
521                 {
522                         /*
523                                 Check how the block handles being a lonely sky block
524                         */
525                         parent.position_valid = true;
526                         b.setIsUnderground(false);
527                         parent.node.d = CONTENT_AIR;
528                         parent.node.setLight(LIGHTBANK_DAY, LIGHT_SUN);
529                         parent.node.setLight(LIGHTBANK_NIGHT, 0);
530                         core::map<v3s16, bool> light_sources;
531                         // The bottom block is invalid, because we have a shadowing node
532                         assert(b.propagateSunlight(light_sources) == false);
533                         assert(b.getNode(v3s16(1,4,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
534                         assert(b.getNode(v3s16(1,3,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
535                         assert(b.getNode(v3s16(1,2,0)).getLight(LIGHTBANK_DAY) == 0);
536                         assert(b.getNode(v3s16(1,1,0)).getLight(LIGHTBANK_DAY) == 0);
537                         assert(b.getNode(v3s16(1,0,0)).getLight(LIGHTBANK_DAY) == 0);
538                         assert(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
539                         assert(b.getFaceLight2(1000, p, v3s16(0,1,0)) == LIGHT_SUN);
540                         assert(b.getFaceLight2(1000, p, v3s16(0,-1,0)) == 0);
541                         assert(b.getFaceLight2(0, p, v3s16(0,-1,0)) == 0);
542                         // According to MapBlock::getFaceLight,
543                         // The face on the z+ side should have double-diminished light
544                         //assert(b.getFaceLight(p, v3s16(0,0,1)) == diminish_light(diminish_light(LIGHT_MAX)));
545                         // The face on the z+ side should have diminished light
546                         assert(b.getFaceLight2(1000, p, v3s16(0,0,1)) == diminish_light(LIGHT_MAX));
547                 }
548                 /*
549                         Check how the block handles being in between blocks with some non-sunlight
550                         while being underground
551                 */
552                 {
553                         // Make neighbours to exist and set some non-sunlight to them
554                         parent.position_valid = true;
555                         b.setIsUnderground(true);
556                         parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
557                         core::map<v3s16, bool> light_sources;
558                         // The block below should be valid because there shouldn't be
559                         // sunlight in there either
560                         assert(b.propagateSunlight(light_sources, true) == true);
561                         // Should not touch nodes that are not affected (that is, all of them)
562                         //assert(b.getNode(v3s16(1,2,3)).getLight() == LIGHT_SUN);
563                         // Should set light of non-sunlighted blocks to 0.
564                         assert(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == 0);
565                 }
566                 /*
567                         Set up a situation where:
568                         - There is only air in this block
569                         - There is a valid non-sunlighted block at the bottom, and
570                         - Invalid blocks elsewhere.
571                         - the block is not underground.
572
573                         This should result in bottom block invalidity
574                 */
575                 {
576                         b.setIsUnderground(false);
577                         // Clear block
578                         for(u16 z=0; z<MAP_BLOCKSIZE; z++){
579                                 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
580                                         for(u16 x=0; x<MAP_BLOCKSIZE; x++){
581                                                 MapNode n;
582                                                 n.d = CONTENT_AIR;
583                                                 n.setLight(LIGHTBANK_DAY, 0);
584                                                 b.setNode(v3s16(x,y,z), n);
585                                         }
586                                 }
587                         }
588                         // Make neighbours invalid
589                         parent.position_valid = false;
590                         // Add exceptions to the top of the bottom block
591                         for(u16 x=0; x<MAP_BLOCKSIZE; x++)
592                         for(u16 z=0; z<MAP_BLOCKSIZE; z++)
593                         {
594                                 parent.validity_exceptions.push_back(v3s16(MAP_BLOCKSIZE+x, MAP_BLOCKSIZE-1, MAP_BLOCKSIZE+z));
595                         }
596                         // Lighting value for the valid nodes
597                         parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
598                         core::map<v3s16, bool> light_sources;
599                         // Bottom block is not valid
600                         assert(b.propagateSunlight(light_sources) == false);
601                 }
602         }
603 };
604
605 struct TestMapSector
606 {
607         class TC : public NodeContainer
608         {
609         public:
610
611                 MapNode node;
612                 bool position_valid;
613
614                 TC()
615                 {
616                         position_valid = true;
617                 }
618
619                 virtual bool isValidPosition(v3s16 p)
620                 {
621                         return position_valid;
622                 }
623
624                 virtual MapNode getNode(v3s16 p)
625                 {
626                         if(position_valid == false)
627                                 throw InvalidPositionException();
628                         return node;
629                 }
630
631                 virtual void setNode(v3s16 p, MapNode & n)
632                 {
633                         if(position_valid == false)
634                                 throw InvalidPositionException();
635                 };
636                 
637                 virtual u16 nodeContainerId() const
638                 {
639                         return 666;
640                 }
641         };
642         
643         void Run()
644         {
645                 TC parent;
646                 parent.position_valid = false;
647                 
648                 // Create one with no heightmaps
649                 ServerMapSector sector(&parent, v2s16(1,1));
650                 
651                 assert(sector.getBlockNoCreateNoEx(0) == 0);
652                 assert(sector.getBlockNoCreateNoEx(1) == 0);
653
654                 MapBlock * bref = sector.createBlankBlock(-2);
655                 
656                 assert(sector.getBlockNoCreateNoEx(0) == 0);
657                 assert(sector.getBlockNoCreateNoEx(-2) == bref);
658                 
659                 //TODO: Check for AlreadyExistsException
660
661                 /*bool exception_thrown = false;
662                 try{
663                         sector.getBlock(0);
664                 }
665                 catch(InvalidPositionException &e){
666                         exception_thrown = true;
667                 }
668                 assert(exception_thrown);*/
669
670         }
671 };
672 #endif
673
674 struct TestSocket
675 {
676         void Run()
677         {
678                 const int port = 30003;
679                 UDPSocket socket;
680                 socket.Bind(port);
681
682                 const char sendbuffer[] = "hello world!";
683                 socket.Send(Address(127,0,0,1,port), sendbuffer, sizeof(sendbuffer));
684
685                 sleep_ms(50);
686
687                 char rcvbuffer[256];
688                 memset(rcvbuffer, 0, sizeof(rcvbuffer));
689                 Address sender;
690                 for(;;)
691                 {
692                         int bytes_read = socket.Receive(sender, rcvbuffer, sizeof(rcvbuffer));
693                         if(bytes_read < 0)
694                                 break;
695                 }
696                 //FIXME: This fails on some systems
697                 assert(strncmp(sendbuffer, rcvbuffer, sizeof(sendbuffer))==0);
698                 assert(sender.getAddress() == Address(127,0,0,1, 0).getAddress());
699         }
700 };
701
702 struct TestConnection
703 {
704         void TestHelpers()
705         {
706                 /*
707                         Test helper functions
708                 */
709
710                 // Some constants for testing
711                 u32 proto_id = 0x12345678;
712                 u16 peer_id = 123;
713                 u8 channel = 2;
714                 SharedBuffer<u8> data1(1);
715                 data1[0] = 100;
716                 Address a(127,0,0,1, 10);
717                 u16 seqnum = 34352;
718
719                 con::BufferedPacket p1 = con::makePacket(a, data1,
720                                 proto_id, peer_id, channel);
721                 /*
722                         We should now have a packet with this data:
723                         Header:
724                                 [0] u32 protocol_id
725                                 [4] u16 sender_peer_id
726                                 [6] u8 channel
727                         Data:
728                                 [7] u8 data1[0]
729                 */
730                 assert(readU32(&p1.data[0]) == proto_id);
731                 assert(readU16(&p1.data[4]) == peer_id);
732                 assert(readU8(&p1.data[6]) == channel);
733                 assert(readU8(&p1.data[7]) == data1[0]);
734                 
735                 //dstream<<"initial data1[0]="<<((u32)data1[0]&0xff)<<std::endl;
736
737                 SharedBuffer<u8> p2 = con::makeReliablePacket(data1, seqnum);
738
739                 /*dstream<<"p2.getSize()="<<p2.getSize()<<", data1.getSize()="
740                                 <<data1.getSize()<<std::endl;
741                 dstream<<"readU8(&p2[3])="<<readU8(&p2[3])
742                                 <<" p2[3]="<<((u32)p2[3]&0xff)<<std::endl;
743                 dstream<<"data1[0]="<<((u32)data1[0]&0xff)<<std::endl;*/
744
745                 assert(p2.getSize() == 3 + data1.getSize());
746                 assert(readU8(&p2[0]) == TYPE_RELIABLE);
747                 assert(readU16(&p2[1]) == seqnum);
748                 assert(readU8(&p2[3]) == data1[0]);
749         }
750
751         struct Handler : public con::PeerHandler
752         {
753                 Handler(const char *a_name)
754                 {
755                         count = 0;
756                         last_id = 0;
757                         name = a_name;
758                 }
759                 void peerAdded(con::Peer *peer)
760                 {
761                         dstream<<"Handler("<<name<<")::peerAdded(): "
762                                         "id="<<peer->id<<std::endl;
763                         last_id = peer->id;
764                         count++;
765                 }
766                 void deletingPeer(con::Peer *peer, bool timeout)
767                 {
768                         dstream<<"Handler("<<name<<")::deletingPeer(): "
769                                         "id="<<peer->id
770                                         <<", timeout="<<timeout<<std::endl;
771                         last_id = peer->id;
772                         count--;
773                 }
774
775                 s32 count;
776                 u16 last_id;
777                 const char *name;
778         };
779
780         void Run()
781         {
782                 DSTACK("TestConnection::Run");
783
784                 TestHelpers();
785
786                 /*
787                         Test some real connections
788                 */
789                 u32 proto_id = 0xad26846a;
790
791                 Handler hand_server("server");
792                 Handler hand_client("client");
793                 
794                 dstream<<"** Creating server Connection"<<std::endl;
795                 con::Connection server(proto_id, 512, 5.0, &hand_server);
796                 server.Serve(30001);
797                 
798                 dstream<<"** Creating client Connection"<<std::endl;
799                 con::Connection client(proto_id, 512, 5.0, &hand_client);
800
801                 assert(hand_server.count == 0);
802                 assert(hand_client.count == 0);
803                 
804                 sleep_ms(50);
805                 
806                 Address server_address(127,0,0,1, 30001);
807                 dstream<<"** running client.Connect()"<<std::endl;
808                 client.Connect(server_address);
809
810                 sleep_ms(50);
811                 
812                 // Client should have added server now
813                 assert(hand_client.count == 1);
814                 assert(hand_client.last_id == 1);
815                 // But server should not have added client
816                 assert(hand_server.count == 0);
817
818                 try
819                 {
820                         u16 peer_id;
821                         u8 data[100];
822                         dstream<<"** running server.Receive()"<<std::endl;
823                         u32 size = server.Receive(peer_id, data, 100);
824                         dstream<<"** Server received: peer_id="<<peer_id
825                                         <<", size="<<size
826                                         <<std::endl;
827                 }
828                 catch(con::NoIncomingDataException &e)
829                 {
830                         // No actual data received, but the client has
831                         // probably been connected
832                 }
833                 
834                 // Client should be the same
835                 assert(hand_client.count == 1);
836                 assert(hand_client.last_id == 1);
837                 // Server should have the client
838                 assert(hand_server.count == 1);
839                 assert(hand_server.last_id == 2);
840                 
841                 //sleep_ms(50);
842
843                 while(client.Connected() == false)
844                 {
845                         try
846                         {
847                                 u16 peer_id;
848                                 u8 data[100];
849                                 dstream<<"** running client.Receive()"<<std::endl;
850                                 u32 size = client.Receive(peer_id, data, 100);
851                                 dstream<<"** Client received: peer_id="<<peer_id
852                                                 <<", size="<<size
853                                                 <<std::endl;
854                         }
855                         catch(con::NoIncomingDataException &e)
856                         {
857                         }
858                         sleep_ms(50);
859                 }
860
861                 sleep_ms(50);
862                 
863                 try
864                 {
865                         u16 peer_id;
866                         u8 data[100];
867                         dstream<<"** running server.Receive()"<<std::endl;
868                         u32 size = server.Receive(peer_id, data, 100);
869                         dstream<<"** Server received: peer_id="<<peer_id
870                                         <<", size="<<size
871                                         <<std::endl;
872                 }
873                 catch(con::NoIncomingDataException &e)
874                 {
875                 }
876
877                 {
878                         /*u8 data[] = "Hello World!";
879                         u32 datasize = sizeof(data);*/
880                         SharedBuffer<u8> data = SharedBufferFromString("Hello World!");
881
882                         dstream<<"** running client.Send()"<<std::endl;
883                         client.Send(PEER_ID_SERVER, 0, data, true);
884
885                         sleep_ms(50);
886
887                         u16 peer_id;
888                         u8 recvdata[100];
889                         dstream<<"** running server.Receive()"<<std::endl;
890                         u32 size = server.Receive(peer_id, recvdata, 100);
891                         dstream<<"** Server received: peer_id="<<peer_id
892                                         <<", size="<<size
893                                         <<", data="<<*data
894                                         <<std::endl;
895                         assert(memcmp(*data, recvdata, data.getSize()) == 0);
896                 }
897                 
898                 u16 peer_id_client = 2;
899
900                 {
901                         /*
902                                 Send consequent packets in different order
903                         */
904                         //u8 data1[] = "hello1";
905                         //u8 data2[] = "hello2";
906                         SharedBuffer<u8> data1 = SharedBufferFromString("hello1");
907                         SharedBuffer<u8> data2 = SharedBufferFromString("Hello2");
908
909                         Address client_address =
910                                         server.GetPeer(peer_id_client)->address;
911                         
912                         dstream<<"*** Sending packets in wrong order (2,1,2)"
913                                         <<std::endl;
914                         
915                         u8 chn = 0;
916                         con::Channel *ch = &server.GetPeer(peer_id_client)->channels[chn];
917                         u16 sn = ch->next_outgoing_seqnum;
918                         ch->next_outgoing_seqnum = sn+1;
919                         server.Send(peer_id_client, chn, data2, true);
920                         ch->next_outgoing_seqnum = sn;
921                         server.Send(peer_id_client, chn, data1, true);
922                         ch->next_outgoing_seqnum = sn+1;
923                         server.Send(peer_id_client, chn, data2, true);
924
925                         sleep_ms(50);
926
927                         dstream<<"*** Receiving the packets"<<std::endl;
928
929                         u16 peer_id;
930                         u8 recvdata[20];
931                         u32 size;
932
933                         dstream<<"** running client.Receive()"<<std::endl;
934                         peer_id = 132;
935                         size = client.Receive(peer_id, recvdata, 20);
936                         dstream<<"** Client received: peer_id="<<peer_id
937                                         <<", size="<<size
938                                         <<", data="<<recvdata
939                                         <<std::endl;
940                         assert(size == data1.getSize());
941                         assert(memcmp(*data1, recvdata, data1.getSize()) == 0);
942                         assert(peer_id == PEER_ID_SERVER);
943                         
944                         dstream<<"** running client.Receive()"<<std::endl;
945                         peer_id = 132;
946                         size = client.Receive(peer_id, recvdata, 20);
947                         dstream<<"** Client received: peer_id="<<peer_id
948                                         <<", size="<<size
949                                         <<", data="<<recvdata
950                                         <<std::endl;
951                         assert(size == data2.getSize());
952                         assert(memcmp(*data2, recvdata, data2.getSize()) == 0);
953                         assert(peer_id == PEER_ID_SERVER);
954                         
955                         bool got_exception = false;
956                         try
957                         {
958                                 dstream<<"** running client.Receive()"<<std::endl;
959                                 peer_id = 132;
960                                 size = client.Receive(peer_id, recvdata, 20);
961                                 dstream<<"** Client received: peer_id="<<peer_id
962                                                 <<", size="<<size
963                                                 <<", data="<<recvdata
964                                                 <<std::endl;
965                         }
966                         catch(con::NoIncomingDataException &e)
967                         {
968                                 dstream<<"** No incoming data for client"<<std::endl;
969                                 got_exception = true;
970                         }
971                         assert(got_exception);
972                 }
973                 {
974                         const int datasize = 30000;
975                         SharedBuffer<u8> data1(datasize);
976                         for(u16 i=0; i<datasize; i++){
977                                 data1[i] = i/4;
978                         }
979
980                         dstream<<"Sending data (size="<<datasize<<"):";
981                         for(int i=0; i<datasize && i<20; i++){
982                                 if(i%2==0) DEBUGPRINT(" ");
983                                 DEBUGPRINT("%.2X", ((int)((const char*)*data1)[i])&0xff);
984                         }
985                         if(datasize>20)
986                                 dstream<<"...";
987                         dstream<<std::endl;
988                         
989                         server.Send(peer_id_client, 0, data1, true);
990
991                         sleep_ms(50);
992                         
993                         u8 recvdata[datasize + 1000];
994                         dstream<<"** running client.Receive()"<<std::endl;
995                         u16 peer_id = 132;
996                         u16 size = client.Receive(peer_id, recvdata, datasize + 1000);
997                         dstream<<"** Client received: peer_id="<<peer_id
998                                         <<", size="<<size
999                                         <<std::endl;
1000
1001                         dstream<<"Received data (size="<<size<<"):";
1002                         for(int i=0; i<size && i<20; i++){
1003                                 if(i%2==0) DEBUGPRINT(" ");
1004                                 DEBUGPRINT("%.2X", ((int)((const char*)recvdata)[i])&0xff);
1005                         }
1006                         if(size>20)
1007                                 dstream<<"...";
1008                         dstream<<std::endl;
1009
1010                         assert(memcmp(*data1, recvdata, data1.getSize()) == 0);
1011                         assert(peer_id == PEER_ID_SERVER);
1012                 }
1013                 
1014                 // Check peer handlers
1015                 assert(hand_client.count == 1);
1016                 assert(hand_client.last_id == 1);
1017                 assert(hand_server.count == 1);
1018                 assert(hand_server.last_id == 2);
1019                 
1020                 //assert(0);
1021         }
1022 };
1023
1024 #define TEST(X)\
1025 {\
1026         X x;\
1027         dstream<<"Running " #X <<std::endl;\
1028         x.Run();\
1029 }
1030
1031 void run_tests()
1032 {
1033         DSTACK(__FUNCTION_NAME);
1034         dstream<<"run_tests() started"<<std::endl;
1035         TEST(TestUtilities);
1036         TEST(TestCompress);
1037         TEST(TestMapNode);
1038         TEST(TestVoxelManipulator);
1039         //TEST(TestMapBlock);
1040         //TEST(TestMapSector);
1041         if(INTERNET_SIMULATOR == false){
1042                 TEST(TestSocket);
1043                 dout_con<<"=== BEGIN RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
1044                 TEST(TestConnection);
1045                 dout_con<<"=== END RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
1046         }
1047         dstream<<"run_tests() passed"<<std::endl;
1048 }
1049