]> git.lizzy.rs Git - dragonfireclient.git/blob - src/test.cpp
end-of-day.
[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 "heightmap.h"
27 #include "socket.h"
28 #include "connection.h"
29 #include "utility.h"
30 #include "serialization.h"
31 #include "voxel.h"
32 #include <sstream>
33 #include "porting.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 = 0;
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.getFaceLight(1000, p, v3s16(0,1,0)) == LIGHT_SUN);
522                         assert(b.getFaceLight(1000, p, v3s16(0,-1,0)) == 0);
523                         assert(b.getFaceLight(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.getFaceLight(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), 0);
632                 //ConstantGenerator *dummyheightmap = new ConstantGenerator();
633                 //sector->setHeightmap(dummyheightmap);
634                 
635                 EXCEPTION_CHECK(InvalidPositionException, sector.getBlockNoCreate(0));
636                 EXCEPTION_CHECK(InvalidPositionException, sector.getBlockNoCreate(1));
637
638                 MapBlock * bref = sector.createBlankBlock(-2);
639                 
640                 EXCEPTION_CHECK(InvalidPositionException, sector.getBlockNoCreate(0));
641                 assert(sector.getBlockNoCreate(-2) == bref);
642                 
643                 //TODO: Check for AlreadyExistsException
644
645                 /*bool exception_thrown = false;
646                 try{
647                         sector.getBlock(0);
648                 }
649                 catch(InvalidPositionException &e){
650                         exception_thrown = true;
651                 }
652                 assert(exception_thrown);*/
653
654         }
655 };
656
657 struct TestHeightmap
658 {
659         void TestSingleFixed()
660         {
661                 const s16 BS1 = 4;
662                 OneChildHeightmap hm1(BS1);
663                 
664                 // Test that it is filled with < GROUNDHEIGHT_VALID_MINVALUE
665                 for(s16 y=0; y<=BS1; y++){
666                         for(s16 x=0; x<=BS1; x++){
667                                 v2s16 p(x,y);
668                                 assert(hm1.m_child.getGroundHeight(p)
669                                         < GROUNDHEIGHT_VALID_MINVALUE);
670                         }
671                 }
672
673                 hm1.m_child.setGroundHeight(v2s16(1,0), 2.0);
674                 //hm1.m_child.print();
675                 assert(fabs(hm1.getGroundHeight(v2s16(1,0))-2.0)<0.001);
676                 hm1.setGroundHeight(v2s16(0,1), 3.0);
677                 assert(fabs(hm1.m_child.getGroundHeight(v2s16(0,1))-3.0)<0.001);
678                 
679                 // Fill with -1.0
680                 for(s16 y=0; y<=BS1; y++){
681                         for(s16 x=0; x<=BS1; x++){
682                                 v2s16 p(x,y);
683                                 hm1.m_child.setGroundHeight(p, -1.0);
684                         }
685                 }
686
687                 f32 corners[] = {0.0, 0.0, 1.0, 1.0};
688                 hm1.m_child.generateContinued(0.0, 0.0, corners);
689                 
690                 hm1.m_child.print();
691                 assert(fabs(hm1.m_child.getGroundHeight(v2s16(1,0))-0.2)<0.05);
692                 assert(fabs(hm1.m_child.getGroundHeight(v2s16(4,3))-0.7)<0.05);
693                 assert(fabs(hm1.m_child.getGroundHeight(v2s16(4,4))-1.0)<0.05);
694         }
695
696         void TestUnlimited()
697         {
698                 //g_heightmap_debugprint = true;
699                 const s16 BS1 = 4;
700                 /*UnlimitedHeightmap hm1(BS1,
701                                 new ConstantGenerator(0.0),
702                                 new ConstantGenerator(0.0),
703                                 new ConstantGenerator(5.0));*/
704                 PointAttributeDatabase padb;
705                 UnlimitedHeightmap hm1(BS1, &padb);
706                 // Go through it so it generates itself
707                 for(s16 y=0; y<=BS1; y++){
708                         for(s16 x=0; x<=BS1; x++){
709                                 v2s16 p(x,y);
710                                 hm1.getGroundHeight(p);
711                         }
712                 }
713                 // Print it
714                 dstream<<"UnlimitedHeightmap hm1:"<<std::endl;
715                 hm1.print();
716                 
717                 dstream<<"testing UnlimitedHeightmap set/get"<<std::endl;
718                 v2s16 p1(0,3);
719                 f32 v1(234.01);
720                 // Get first heightmap and try setGroundHeight
721                 FixedHeightmap * href = hm1.getHeightmap(v2s16(0,0));
722                 href->setGroundHeight(p1, v1);
723                 // Read from UnlimitedHeightmap
724                 assert(fabs(hm1.getGroundHeight(p1)-v1)<0.001);
725         }
726         
727         void Random()
728         {
729                 dstream<<"Running random code (get a human to check this)"<<std::endl;
730                 dstream<<"myrand() values: ";
731                 for(u16 i=0; i<5; i++)
732                         dstream<<(u16)myrand()<<" ";
733                 dstream<<std::endl;
734
735                 const s16 BS1 = 8;
736                 /*UnlimitedHeightmap hm1(BS1,
737                                 new ConstantGenerator(10.0),
738                                 new ConstantGenerator(0.3),
739                                 new ConstantGenerator(0.0));*/
740
741                 PointAttributeDatabase padb;
742
743                 padb.getList("hm_baseheight")->addPoint(v2s16(-BS1,0), Attribute(0));
744                 padb.getList("hm_randmax")->addPoint(v2s16(-BS1,0), Attribute(0));
745                 padb.getList("hm_randfactor")->addPoint(v2s16(-BS1,0), Attribute(0.0));
746
747                 padb.getList("hm_baseheight")->addPoint(v2s16(0,0), Attribute(-20));
748                 padb.getList("hm_randmax")->addPoint(v2s16(0,0), Attribute(0));
749                 padb.getList("hm_randfactor")->addPoint(v2s16(0,0), Attribute(0.5));
750
751                 padb.getList("hm_baseheight")->addPoint(v2s16(BS1*2,BS1), Attribute(0));
752                 padb.getList("hm_randmax")->addPoint(v2s16(BS1*2,BS1), Attribute(30));
753                 padb.getList("hm_randfactor")->addPoint(v2s16(BS1*2,BS1), Attribute(0.63));
754
755                 UnlimitedHeightmap hm1(BS1, &padb);
756
757                 // Force hm1 to generate a some heightmap
758                 hm1.getGroundHeight(v2s16(0,0));
759                 hm1.getGroundHeight(v2s16(0,BS1));
760                 /*hm1.getGroundHeight(v2s16(BS1,-1));
761                 hm1.getGroundHeight(v2s16(BS1-1,-1));*/
762                 hm1.print();
763
764                 // Get the (0,0) and (1,0) heightmaps
765                 /*FixedHeightmap * hr00 = hm1.getHeightmap(v2s16(0,0));
766                 FixedHeightmap * hr01 = hm1.getHeightmap(v2s16(1,0));
767                 f32 corners[] = {1.0, 1.0, 1.0, 1.0};
768                 hr00->generateContinued(0.0, 0.0, corners);
769                 hm1.print();*/
770
771                 //assert(0);
772         }
773
774         void Run()
775         {
776                 //srand(7); // Get constant random
777                 srand(time(0)); // Get better random
778
779                 TestSingleFixed();
780                 TestUnlimited();
781                 Random();
782         }
783 };
784
785 struct TestSocket
786 {
787         void Run()
788         {
789                 const int port = 30003;
790                 UDPSocket socket;
791                 socket.Bind(port);
792
793                 const char sendbuffer[] = "hello world!";
794                 socket.Send(Address(127,0,0,1,port), sendbuffer, sizeof(sendbuffer));
795
796                 sleep_ms(50);
797
798                 char rcvbuffer[256];
799                 memset(rcvbuffer, 0, sizeof(rcvbuffer));
800                 Address sender;
801                 for(;;)
802                 {
803                         int bytes_read = socket.Receive(sender, rcvbuffer, sizeof(rcvbuffer));
804                         if(bytes_read < 0)
805                                 break;
806                 }
807                 //FIXME: This fails on some systems
808                 assert(strncmp(sendbuffer, rcvbuffer, sizeof(sendbuffer))==0);
809                 assert(sender.getAddress() == Address(127,0,0,1, 0).getAddress());
810         }
811 };
812
813 struct TestConnection
814 {
815         void TestHelpers()
816         {
817                 /*
818                         Test helper functions
819                 */
820
821                 // Some constants for testing
822                 u32 proto_id = 0x12345678;
823                 u16 peer_id = 123;
824                 u8 channel = 2;
825                 SharedBuffer<u8> data1(1);
826                 data1[0] = 100;
827                 Address a(127,0,0,1, 10);
828                 u16 seqnum = 34352;
829
830                 con::BufferedPacket p1 = con::makePacket(a, data1,
831                                 proto_id, peer_id, channel);
832                 /*
833                         We should now have a packet with this data:
834                         Header:
835                                 [0] u32 protocol_id
836                                 [4] u16 sender_peer_id
837                                 [6] u8 channel
838                         Data:
839                                 [7] u8 data1[0]
840                 */
841                 assert(readU32(&p1.data[0]) == proto_id);
842                 assert(readU16(&p1.data[4]) == peer_id);
843                 assert(readU8(&p1.data[6]) == channel);
844                 assert(readU8(&p1.data[7]) == data1[0]);
845                 
846                 //dstream<<"initial data1[0]="<<((u32)data1[0]&0xff)<<std::endl;
847
848                 SharedBuffer<u8> p2 = con::makeReliablePacket(data1, seqnum);
849
850                 /*dstream<<"p2.getSize()="<<p2.getSize()<<", data1.getSize()="
851                                 <<data1.getSize()<<std::endl;
852                 dstream<<"readU8(&p2[3])="<<readU8(&p2[3])
853                                 <<" p2[3]="<<((u32)p2[3]&0xff)<<std::endl;
854                 dstream<<"data1[0]="<<((u32)data1[0]&0xff)<<std::endl;*/
855
856                 assert(p2.getSize() == 3 + data1.getSize());
857                 assert(readU8(&p2[0]) == TYPE_RELIABLE);
858                 assert(readU16(&p2[1]) == seqnum);
859                 assert(readU8(&p2[3]) == data1[0]);
860         }
861
862         struct Handler : public con::PeerHandler
863         {
864                 Handler(const char *a_name)
865                 {
866                         count = 0;
867                         last_id = 0;
868                         name = a_name;
869                 }
870                 void peerAdded(con::Peer *peer)
871                 {
872                         dstream<<"Handler("<<name<<")::peerAdded(): "
873                                         "id="<<peer->id<<std::endl;
874                         last_id = peer->id;
875                         count++;
876                 }
877                 void deletingPeer(con::Peer *peer, bool timeout)
878                 {
879                         dstream<<"Handler("<<name<<")::deletingPeer(): "
880                                         "id="<<peer->id
881                                         <<", timeout="<<timeout<<std::endl;
882                         last_id = peer->id;
883                         count--;
884                 }
885
886                 s32 count;
887                 u16 last_id;
888                 const char *name;
889         };
890
891         void Run()
892         {
893                 DSTACK("TestConnection::Run");
894
895                 TestHelpers();
896
897                 /*
898                         Test some real connections
899                 */
900                 u32 proto_id = 0xad26846a;
901
902                 Handler hand_server("server");
903                 Handler hand_client("client");
904                 
905                 dstream<<"** Creating server Connection"<<std::endl;
906                 con::Connection server(proto_id, 512, 5.0, &hand_server);
907                 server.Serve(30001);
908                 
909                 dstream<<"** Creating client Connection"<<std::endl;
910                 con::Connection client(proto_id, 512, 5.0, &hand_client);
911
912                 assert(hand_server.count == 0);
913                 assert(hand_client.count == 0);
914                 
915                 sleep_ms(50);
916                 
917                 Address server_address(127,0,0,1, 30001);
918                 dstream<<"** running client.Connect()"<<std::endl;
919                 client.Connect(server_address);
920
921                 sleep_ms(50);
922                 
923                 // Client should have added server now
924                 assert(hand_client.count == 1);
925                 assert(hand_client.last_id == 1);
926                 // But server should not have added client
927                 assert(hand_server.count == 0);
928
929                 try
930                 {
931                         u16 peer_id;
932                         u8 data[100];
933                         dstream<<"** running server.Receive()"<<std::endl;
934                         u32 size = server.Receive(peer_id, data, 100);
935                         dstream<<"** Server received: peer_id="<<peer_id
936                                         <<", size="<<size
937                                         <<std::endl;
938                 }
939                 catch(con::NoIncomingDataException &e)
940                 {
941                         // No actual data received, but the client has
942                         // probably been connected
943                 }
944                 
945                 // Client should be the same
946                 assert(hand_client.count == 1);
947                 assert(hand_client.last_id == 1);
948                 // Server should have the client
949                 assert(hand_server.count == 1);
950                 assert(hand_server.last_id == 2);
951                 
952                 //sleep_ms(50);
953
954                 while(client.Connected() == false)
955                 {
956                         try
957                         {
958                                 u16 peer_id;
959                                 u8 data[100];
960                                 dstream<<"** running client.Receive()"<<std::endl;
961                                 u32 size = client.Receive(peer_id, data, 100);
962                                 dstream<<"** Client received: peer_id="<<peer_id
963                                                 <<", size="<<size
964                                                 <<std::endl;
965                         }
966                         catch(con::NoIncomingDataException &e)
967                         {
968                         }
969                         sleep_ms(50);
970                 }
971
972                 sleep_ms(50);
973                 
974                 try
975                 {
976                         u16 peer_id;
977                         u8 data[100];
978                         dstream<<"** running server.Receive()"<<std::endl;
979                         u32 size = server.Receive(peer_id, data, 100);
980                         dstream<<"** Server received: peer_id="<<peer_id
981                                         <<", size="<<size
982                                         <<std::endl;
983                 }
984                 catch(con::NoIncomingDataException &e)
985                 {
986                 }
987
988                 {
989                         /*u8 data[] = "Hello World!";
990                         u32 datasize = sizeof(data);*/
991                         SharedBuffer<u8> data = SharedBufferFromString("Hello World!");
992
993                         dstream<<"** running client.Send()"<<std::endl;
994                         client.Send(PEER_ID_SERVER, 0, data, true);
995
996                         sleep_ms(50);
997
998                         u16 peer_id;
999                         u8 recvdata[100];
1000                         dstream<<"** running server.Receive()"<<std::endl;
1001                         u32 size = server.Receive(peer_id, recvdata, 100);
1002                         dstream<<"** Server received: peer_id="<<peer_id
1003                                         <<", size="<<size
1004                                         <<", data="<<*data
1005                                         <<std::endl;
1006                         assert(memcmp(*data, recvdata, data.getSize()) == 0);
1007                 }
1008                 
1009                 u16 peer_id_client = 2;
1010
1011                 {
1012                         /*
1013                                 Send consequent packets in different order
1014                         */
1015                         //u8 data1[] = "hello1";
1016                         //u8 data2[] = "hello2";
1017                         SharedBuffer<u8> data1 = SharedBufferFromString("hello1");
1018                         SharedBuffer<u8> data2 = SharedBufferFromString("Hello2");
1019
1020                         Address client_address =
1021                                         server.GetPeer(peer_id_client)->address;
1022                         
1023                         dstream<<"*** Sending packets in wrong order (2,1,2)"
1024                                         <<std::endl;
1025                         
1026                         u8 chn = 0;
1027                         con::Channel *ch = &server.GetPeer(peer_id_client)->channels[chn];
1028                         u16 sn = ch->next_outgoing_seqnum;
1029                         ch->next_outgoing_seqnum = sn+1;
1030                         server.Send(peer_id_client, chn, data2, true);
1031                         ch->next_outgoing_seqnum = sn;
1032                         server.Send(peer_id_client, chn, data1, true);
1033                         ch->next_outgoing_seqnum = sn+1;
1034                         server.Send(peer_id_client, chn, data2, true);
1035
1036                         sleep_ms(50);
1037
1038                         dstream<<"*** Receiving the packets"<<std::endl;
1039
1040                         u16 peer_id;
1041                         u8 recvdata[20];
1042                         u32 size;
1043
1044                         dstream<<"** running client.Receive()"<<std::endl;
1045                         peer_id = 132;
1046                         size = client.Receive(peer_id, recvdata, 20);
1047                         dstream<<"** Client received: peer_id="<<peer_id
1048                                         <<", size="<<size
1049                                         <<", data="<<recvdata
1050                                         <<std::endl;
1051                         assert(size == data1.getSize());
1052                         assert(memcmp(*data1, recvdata, data1.getSize()) == 0);
1053                         assert(peer_id == PEER_ID_SERVER);
1054                         
1055                         dstream<<"** running client.Receive()"<<std::endl;
1056                         peer_id = 132;
1057                         size = client.Receive(peer_id, recvdata, 20);
1058                         dstream<<"** Client received: peer_id="<<peer_id
1059                                         <<", size="<<size
1060                                         <<", data="<<recvdata
1061                                         <<std::endl;
1062                         assert(size == data2.getSize());
1063                         assert(memcmp(*data2, recvdata, data2.getSize()) == 0);
1064                         assert(peer_id == PEER_ID_SERVER);
1065                         
1066                         bool got_exception = false;
1067                         try
1068                         {
1069                                 dstream<<"** running client.Receive()"<<std::endl;
1070                                 peer_id = 132;
1071                                 size = client.Receive(peer_id, recvdata, 20);
1072                                 dstream<<"** Client received: peer_id="<<peer_id
1073                                                 <<", size="<<size
1074                                                 <<", data="<<recvdata
1075                                                 <<std::endl;
1076                         }
1077                         catch(con::NoIncomingDataException &e)
1078                         {
1079                                 dstream<<"** No incoming data for client"<<std::endl;
1080                                 got_exception = true;
1081                         }
1082                         assert(got_exception);
1083                 }
1084                 {
1085                         //u8 data1[1100];
1086                         SharedBuffer<u8> data1(1100);
1087                         for(u16 i=0; i<1100; i++){
1088                                 data1[i] = i/4;
1089                         }
1090
1091                         dstream<<"Sending data (size="<<1100<<"):";
1092                         for(int i=0; i<1100 && i<20; i++){
1093                                 if(i%2==0) DEBUGPRINT(" ");
1094                                 DEBUGPRINT("%.2X", ((int)((const char*)*data1)[i])&0xff);
1095                         }
1096                         if(1100>20)
1097                                 dstream<<"...";
1098                         dstream<<std::endl;
1099                         
1100                         server.Send(peer_id_client, 0, data1, true);
1101
1102                         sleep_ms(50);
1103                         
1104                         u8 recvdata[2000];
1105                         dstream<<"** running client.Receive()"<<std::endl;
1106                         u16 peer_id = 132;
1107                         u16 size = client.Receive(peer_id, recvdata, 2000);
1108                         dstream<<"** Client received: peer_id="<<peer_id
1109                                         <<", size="<<size
1110                                         <<std::endl;
1111
1112                         dstream<<"Received data (size="<<size<<"):";
1113                         for(int i=0; i<size && i<20; i++){
1114                                 if(i%2==0) DEBUGPRINT(" ");
1115                                 DEBUGPRINT("%.2X", ((int)((const char*)recvdata)[i])&0xff);
1116                         }
1117                         if(size>20)
1118                                 dstream<<"...";
1119                         dstream<<std::endl;
1120
1121                         assert(memcmp(*data1, recvdata, data1.getSize()) == 0);
1122                         assert(peer_id == PEER_ID_SERVER);
1123                 }
1124                 
1125                 // Check peer handlers
1126                 assert(hand_client.count == 1);
1127                 assert(hand_client.last_id == 1);
1128                 assert(hand_server.count == 1);
1129                 assert(hand_server.last_id == 2);
1130                 
1131                 //assert(0);
1132         }
1133 };
1134
1135 #define TEST(X)\
1136 {\
1137         X x;\
1138         dstream<<"Running " #X <<std::endl;\
1139         x.Run();\
1140 }
1141
1142 void run_tests()
1143 {
1144         DSTACK(__FUNCTION_NAME);
1145         dstream<<"run_tests() started"<<std::endl;
1146         TEST(TestUtilities);
1147         TEST(TestCompress);
1148         TEST(TestMapNode);
1149         TEST(TestVoxelManipulator);
1150         TEST(TestMapBlock);
1151         TEST(TestMapSector);
1152         TEST(TestHeightmap);
1153         if(INTERNET_SIMULATOR == false){
1154                 TEST(TestSocket);
1155                 dout_con<<"=== BEGIN RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
1156                 TEST(TestConnection);
1157                 dout_con<<"=== END RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
1158         }
1159         dstream<<"run_tests() passed"<<std::endl;
1160 }
1161