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