]> git.lizzy.rs Git - minetest.git/blob - src/test.cpp
fine-tuning of map generator and server and stuff.
[minetest.git] / src / test.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "test.h"
21 #include "common_irrlicht.h"
22 #include "debug.h"
23 #include "map.h"
24 #include "player.h"
25 #include "main.h"
26 #include "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                 /*
272                         Water stuff
273                 */
274
275                 v.clear();
276
277                 const char *content =
278                         "#...######  "
279                         "#...##..##  "
280                         "#........ .."
281                         "############"
282
283                         "#...######  "
284                         "#...##..##  "
285                         "#........#  "
286                         "############"
287                 ;
288
289                 v3s16 size(12, 4, 2);
290                 VoxelArea area(v3s16(0,0,0), size-v3s16(1,1,1));
291                 
292                 const char *p = content;
293                 for(s16 z=0; z<size.Z; z++)
294                 for(s16 y=size.Y-1; y>=0; y--)
295                 for(s16 x=0; x<size.X; x++)
296                 {
297                         MapNode n;
298                         //n.pressure = size.Y - y;
299                         if(*p == '#')
300                                 n.d = CONTENT_STONE;
301                         else if(*p == '.')
302                                 n.d = CONTENT_WATER;
303                         else if(*p == ' ')
304                                 n.d = CONTENT_AIR;
305                         else
306                                 assert(0);
307                         v.setNode(v3s16(x,y,z), n);
308                         p++;
309                 }
310
311                 v.print(dstream, VOXELPRINT_WATERPRESSURE);
312                 
313                 core::map<v3s16, u8> active_nodes;
314                 v.updateAreaWaterPressure(area, active_nodes);
315
316                 v.print(dstream, VOXELPRINT_WATERPRESSURE);
317                 
318                 //s16 highest_y = -32768;
319                 /*
320                         NOTE: These are commented out because this behaviour is changed
321                               all the time
322                 */
323                 //assert(v.getWaterPressure(v3s16(7, 1, 1), highest_y, 0) == -1);
324                 //assert(highest_y == 3);
325                 /*assert(v.getWaterPressure(v3s16(7, 1, 1), highest_y, 0) == 3);
326                 //assert(highest_y == 3);*/
327                 
328                 active_nodes.clear();
329                 active_nodes[v3s16(9,1,0)] = 1;
330                 //v.flowWater(active_nodes, 0, true, 1000);
331                 v.flowWater(active_nodes, 0, false, 1000);
332                 
333                 dstream<<"Final result of flowWater:"<<std::endl;
334                 v.print(dstream, VOXELPRINT_WATERPRESSURE);
335                 
336                 //assert(0);
337         }
338 };
339
340 struct TestMapBlock
341 {
342         class TC : public NodeContainer
343         {
344         public:
345
346                 MapNode node;
347                 bool position_valid;
348                 core::list<v3s16> validity_exceptions;
349
350                 TC()
351                 {
352                         position_valid = true;
353                 }
354
355                 virtual bool isValidPosition(v3s16 p)
356                 {
357                         //return position_valid ^ (p==position_valid_exception);
358                         bool exception = false;
359                         for(core::list<v3s16>::Iterator i=validity_exceptions.begin();
360                                         i != validity_exceptions.end(); i++)
361                         {
362                                 if(p == *i)
363                                 {
364                                         exception = true;
365                                         break;
366                                 }
367                         }
368                         return exception ? !position_valid : position_valid;
369                 }
370
371                 virtual MapNode getNode(v3s16 p)
372                 {
373                         if(isValidPosition(p) == false)
374                                 throw InvalidPositionException();
375                         return node;
376                 }
377
378                 virtual void setNode(v3s16 p, MapNode & n)
379                 {
380                         if(isValidPosition(p) == false)
381                                 throw InvalidPositionException();
382                 };
383
384                 virtual u16 nodeContainerId() const
385                 {
386                         return 666;
387                 }
388         };
389
390         void Run()
391         {
392                 TC parent;
393                 
394                 MapBlock b(&parent, v3s16(1,1,1));
395                 v3s16 relpos(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
396
397                 assert(b.getPosRelative() == relpos);
398
399                 assert(b.getBox().MinEdge.X == MAP_BLOCKSIZE);
400                 assert(b.getBox().MaxEdge.X == MAP_BLOCKSIZE*2-1);
401                 assert(b.getBox().MinEdge.Y == MAP_BLOCKSIZE);
402                 assert(b.getBox().MaxEdge.Y == MAP_BLOCKSIZE*2-1);
403                 assert(b.getBox().MinEdge.Z == MAP_BLOCKSIZE);
404                 assert(b.getBox().MaxEdge.Z == MAP_BLOCKSIZE*2-1);
405                 
406                 assert(b.isValidPosition(v3s16(0,0,0)) == true);
407                 assert(b.isValidPosition(v3s16(-1,0,0)) == false);
408                 assert(b.isValidPosition(v3s16(-1,-142,-2341)) == false);
409                 assert(b.isValidPosition(v3s16(-124,142,2341)) == false);
410                 assert(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
411                 assert(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE,MAP_BLOCKSIZE-1)) == false);
412
413                 /*
414                         TODO: this method should probably be removed
415                         if the block size isn't going to be set variable
416                 */
417                 /*assert(b.getSizeNodes() == v3s16(MAP_BLOCKSIZE,
418                                 MAP_BLOCKSIZE, MAP_BLOCKSIZE));*/
419                 
420                 // Changed flag should be initially set
421                 assert(b.getChangedFlag() == true);
422                 b.resetChangedFlag();
423                 assert(b.getChangedFlag() == false);
424
425                 // All nodes should have been set to
426                 // .d=CONTENT_AIR and .getLight() = 0
427                 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
428                 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
429                 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
430                 {
431                         assert(b.getNode(v3s16(x,y,z)).d == CONTENT_AIR);
432                         assert(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_DAY) == 0);
433                         assert(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_NIGHT) == 0);
434                 }
435                 
436                 /*
437                         Parent fetch functions
438                 */
439                 parent.position_valid = false;
440                 parent.node.d = 5;
441
442                 MapNode n;
443                 
444                 // Positions in the block should still be valid
445                 assert(b.isValidPositionParent(v3s16(0,0,0)) == true);
446                 assert(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
447                 n = b.getNodeParent(v3s16(0,MAP_BLOCKSIZE-1,0));
448                 assert(n.d == CONTENT_AIR);
449
450                 // ...but outside the block they should be invalid
451                 assert(b.isValidPositionParent(v3s16(-121,2341,0)) == false);
452                 assert(b.isValidPositionParent(v3s16(-1,0,0)) == false);
453                 assert(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == false);
454                 
455                 {
456                         bool exception_thrown = false;
457                         try{
458                                 // This should throw an exception
459                                 MapNode n = b.getNodeParent(v3s16(0,0,-1));
460                         }
461                         catch(InvalidPositionException &e)
462                         {
463                                 exception_thrown = true;
464                         }
465                         assert(exception_thrown);
466                 }
467
468                 parent.position_valid = true;
469                 // Now the positions outside should be valid
470                 assert(b.isValidPositionParent(v3s16(-121,2341,0)) == true);
471                 assert(b.isValidPositionParent(v3s16(-1,0,0)) == true);
472                 assert(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == true);
473                 n = b.getNodeParent(v3s16(0,0,MAP_BLOCKSIZE));
474                 assert(n.d == 5);
475
476                 /*
477                         Set a node
478                 */
479                 v3s16 p(1,2,0);
480                 n.d = 4;
481                 b.setNode(p, n);
482                 assert(b.getNode(p).d == 4);
483                 //TODO: Update to new system
484                 /*assert(b.getNodeTile(p) == 4);
485                 assert(b.getNodeTile(v3s16(-1,-1,0)) == 5);*/
486                 
487                 /*
488                         propagateSunlight()
489                 */
490                 // Set lighting of all nodes to 0
491                 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
492                         for(u16 y=0; y<MAP_BLOCKSIZE; y++){
493                                 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
494                                         MapNode n = b.getNode(v3s16(x,y,z));
495                                         n.setLight(LIGHTBANK_DAY, 0);
496                                         n.setLight(LIGHTBANK_NIGHT, 0);
497                                         b.setNode(v3s16(x,y,z), n);
498                                 }
499                         }
500                 }
501                 {
502                         /*
503                                 Check how the block handles being a lonely sky block
504                         */
505                         parent.position_valid = true;
506                         b.setIsUnderground(false);
507                         parent.node.d = CONTENT_AIR;
508                         parent.node.setLight(LIGHTBANK_DAY, LIGHT_SUN);
509                         parent.node.setLight(LIGHTBANK_NIGHT, 0);
510                         core::map<v3s16, bool> light_sources;
511                         // The bottom block is invalid, because we have a shadowing node
512                         assert(b.propagateSunlight(light_sources) == false);
513                         assert(b.getNode(v3s16(1,4,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
514                         assert(b.getNode(v3s16(1,3,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
515                         assert(b.getNode(v3s16(1,2,0)).getLight(LIGHTBANK_DAY) == 0);
516                         assert(b.getNode(v3s16(1,1,0)).getLight(LIGHTBANK_DAY) == 0);
517                         assert(b.getNode(v3s16(1,0,0)).getLight(LIGHTBANK_DAY) == 0);
518                         assert(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
519                         assert(b.getFaceLight(1000, p, v3s16(0,1,0)) == LIGHT_SUN);
520                         assert(b.getFaceLight(1000, p, v3s16(0,-1,0)) == 0);
521                         assert(b.getFaceLight(0, p, v3s16(0,-1,0)) == 0);
522                         // According to MapBlock::getFaceLight,
523                         // The face on the z+ side should have double-diminished light
524                         //assert(b.getFaceLight(p, v3s16(0,0,1)) == diminish_light(diminish_light(LIGHT_MAX)));
525                         // The face on the z+ side should have diminished light
526                         assert(b.getFaceLight(1000, p, v3s16(0,0,1)) == diminish_light(LIGHT_MAX));
527                 }
528                 /*
529                         Check how the block handles being in between blocks with some non-sunlight
530                         while being underground
531                 */
532                 {
533                         // Make neighbours to exist and set some non-sunlight to them
534                         parent.position_valid = true;
535                         b.setIsUnderground(true);
536                         parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
537                         core::map<v3s16, bool> light_sources;
538                         // The block below should be valid because there shouldn't be
539                         // sunlight in there either
540                         assert(b.propagateSunlight(light_sources, true) == true);
541                         // Should not touch nodes that are not affected (that is, all of them)
542                         //assert(b.getNode(v3s16(1,2,3)).getLight() == LIGHT_SUN);
543                         // Should set light of non-sunlighted blocks to 0.
544                         assert(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == 0);
545                 }
546                 /*
547                         Set up a situation where:
548                         - There is only air in this block
549                         - There is a valid non-sunlighted block at the bottom, and
550                         - Invalid blocks elsewhere.
551                         - the block is not underground.
552
553                         This should result in bottom block invalidity
554                 */
555                 {
556                         b.setIsUnderground(false);
557                         // Clear block
558                         for(u16 z=0; z<MAP_BLOCKSIZE; z++){
559                                 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
560                                         for(u16 x=0; x<MAP_BLOCKSIZE; x++){
561                                                 MapNode n;
562                                                 n.d = CONTENT_AIR;
563                                                 n.setLight(LIGHTBANK_DAY, 0);
564                                                 b.setNode(v3s16(x,y,z), n);
565                                         }
566                                 }
567                         }
568                         // Make neighbours invalid
569                         parent.position_valid = false;
570                         // Add exceptions to the top of the bottom block
571                         for(u16 x=0; x<MAP_BLOCKSIZE; x++)
572                         for(u16 z=0; z<MAP_BLOCKSIZE; z++)
573                         {
574                                 parent.validity_exceptions.push_back(v3s16(MAP_BLOCKSIZE+x, MAP_BLOCKSIZE-1, MAP_BLOCKSIZE+z));
575                         }
576                         // Lighting value for the valid nodes
577                         parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
578                         core::map<v3s16, bool> light_sources;
579                         // Bottom block is not valid
580                         assert(b.propagateSunlight(light_sources) == false);
581                 }
582         }
583 };
584
585 struct TestMapSector
586 {
587         class TC : public NodeContainer
588         {
589         public:
590
591                 MapNode node;
592                 bool position_valid;
593
594                 TC()
595                 {
596                         position_valid = true;
597                 }
598
599                 virtual bool isValidPosition(v3s16 p)
600                 {
601                         return position_valid;
602                 }
603
604                 virtual MapNode getNode(v3s16 p)
605                 {
606                         if(position_valid == false)
607                                 throw InvalidPositionException();
608                         return node;
609                 }
610
611                 virtual void setNode(v3s16 p, MapNode & n)
612                 {
613                         if(position_valid == false)
614                                 throw InvalidPositionException();
615                 };
616                 
617                 virtual u16 nodeContainerId() const
618                 {
619                         return 666;
620                 }
621         };
622         
623         void Run()
624         {
625                 TC parent;
626                 parent.position_valid = false;
627                 
628                 // Create one with no heightmaps
629                 ServerMapSector sector(&parent, v2s16(1,1), 0);
630                 //ConstantGenerator *dummyheightmap = new ConstantGenerator();
631                 //sector->setHeightmap(dummyheightmap);
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 TestHeightmap
656 {
657         void TestSingleFixed()
658         {
659                 const s16 BS1 = 4;
660                 OneChildHeightmap hm1(BS1);
661                 
662                 // Test that it is filled with < GROUNDHEIGHT_VALID_MINVALUE
663                 for(s16 y=0; y<=BS1; y++){
664                         for(s16 x=0; x<=BS1; x++){
665                                 v2s16 p(x,y);
666                                 assert(hm1.m_child.getGroundHeight(p)
667                                         < GROUNDHEIGHT_VALID_MINVALUE);
668                         }
669                 }
670
671                 hm1.m_child.setGroundHeight(v2s16(1,0), 2.0);
672                 //hm1.m_child.print();
673                 assert(fabs(hm1.getGroundHeight(v2s16(1,0))-2.0)<0.001);
674                 hm1.setGroundHeight(v2s16(0,1), 3.0);
675                 assert(fabs(hm1.m_child.getGroundHeight(v2s16(0,1))-3.0)<0.001);
676                 
677                 // Fill with -1.0
678                 for(s16 y=0; y<=BS1; y++){
679                         for(s16 x=0; x<=BS1; x++){
680                                 v2s16 p(x,y);
681                                 hm1.m_child.setGroundHeight(p, -1.0);
682                         }
683                 }
684
685                 f32 corners[] = {0.0, 0.0, 1.0, 1.0};
686                 hm1.m_child.generateContinued(0.0, 0.0, corners);
687                 
688                 hm1.m_child.print();
689                 assert(fabs(hm1.m_child.getGroundHeight(v2s16(1,0))-0.2)<0.05);
690                 assert(fabs(hm1.m_child.getGroundHeight(v2s16(4,3))-0.7)<0.05);
691                 assert(fabs(hm1.m_child.getGroundHeight(v2s16(4,4))-1.0)<0.05);
692         }
693
694         void TestUnlimited()
695         {
696                 //g_heightmap_debugprint = true;
697                 const s16 BS1 = 4;
698                 /*UnlimitedHeightmap hm1(BS1,
699                                 new ConstantGenerator(0.0),
700                                 new ConstantGenerator(0.0),
701                                 new ConstantGenerator(5.0));*/
702                 PointAttributeDatabase padb;
703                 UnlimitedHeightmap hm1(BS1, &padb);
704                 // Go through it so it generates itself
705                 for(s16 y=0; y<=BS1; y++){
706                         for(s16 x=0; x<=BS1; x++){
707                                 v2s16 p(x,y);
708                                 hm1.getGroundHeight(p);
709                         }
710                 }
711                 // Print it
712                 dstream<<"UnlimitedHeightmap hm1:"<<std::endl;
713                 hm1.print();
714                 
715                 dstream<<"testing UnlimitedHeightmap set/get"<<std::endl;
716                 v2s16 p1(0,3);
717                 f32 v1(234.01);
718                 // Get first heightmap and try setGroundHeight
719                 FixedHeightmap * href = hm1.getHeightmap(v2s16(0,0));
720                 href->setGroundHeight(p1, v1);
721                 // Read from UnlimitedHeightmap
722                 assert(fabs(hm1.getGroundHeight(p1)-v1)<0.001);
723         }
724         
725         void Random()
726         {
727                 dstream<<"Running random code (get a human to check this)"<<std::endl;
728                 dstream<<"myrand() values: ";
729                 for(u16 i=0; i<5; i++)
730                         dstream<<(u16)myrand()<<" ";
731                 dstream<<std::endl;
732
733                 const s16 BS1 = 8;
734                 /*UnlimitedHeightmap hm1(BS1,
735                                 new ConstantGenerator(10.0),
736                                 new ConstantGenerator(0.3),
737                                 new ConstantGenerator(0.0));*/
738
739                 PointAttributeDatabase padb;
740
741                 padb.getList("hm_baseheight")->addPoint(v2s16(-BS1,0), Attribute(0));
742                 padb.getList("hm_randmax")->addPoint(v2s16(-BS1,0), Attribute(0));
743                 padb.getList("hm_randfactor")->addPoint(v2s16(-BS1,0), Attribute(0.0));
744
745                 padb.getList("hm_baseheight")->addPoint(v2s16(0,0), Attribute(-20));
746                 padb.getList("hm_randmax")->addPoint(v2s16(0,0), Attribute(0));
747                 padb.getList("hm_randfactor")->addPoint(v2s16(0,0), Attribute(0.5));
748
749                 padb.getList("hm_baseheight")->addPoint(v2s16(BS1*2,BS1), Attribute(0));
750                 padb.getList("hm_randmax")->addPoint(v2s16(BS1*2,BS1), Attribute(30));
751                 padb.getList("hm_randfactor")->addPoint(v2s16(BS1*2,BS1), Attribute(0.63));
752
753                 UnlimitedHeightmap hm1(BS1, &padb);
754
755                 // Force hm1 to generate a some heightmap
756                 hm1.getGroundHeight(v2s16(0,0));
757                 hm1.getGroundHeight(v2s16(0,BS1));
758                 /*hm1.getGroundHeight(v2s16(BS1,-1));
759                 hm1.getGroundHeight(v2s16(BS1-1,-1));*/
760                 hm1.print();
761
762                 // Get the (0,0) and (1,0) heightmaps
763                 /*FixedHeightmap * hr00 = hm1.getHeightmap(v2s16(0,0));
764                 FixedHeightmap * hr01 = hm1.getHeightmap(v2s16(1,0));
765                 f32 corners[] = {1.0, 1.0, 1.0, 1.0};
766                 hr00->generateContinued(0.0, 0.0, corners);
767                 hm1.print();*/
768
769                 //assert(0);
770         }
771
772         void Run()
773         {
774                 //srand(7); // Get constant random
775                 srand(time(0)); // Get better random
776
777                 TestSingleFixed();
778                 TestUnlimited();
779                 Random();
780         }
781 };
782
783 struct TestSocket
784 {
785         void Run()
786         {
787                 const int port = 30003;
788                 UDPSocket socket;
789                 socket.Bind(port);
790
791                 const char sendbuffer[] = "hello world!";
792                 socket.Send(Address(127,0,0,1,port), sendbuffer, sizeof(sendbuffer));
793
794                 sleep_ms(50);
795
796                 char rcvbuffer[256];
797                 memset(rcvbuffer, 0, sizeof(rcvbuffer));
798                 Address sender;
799                 for(;;)
800                 {
801                         int bytes_read = socket.Receive(sender, rcvbuffer, sizeof(rcvbuffer));
802                         if(bytes_read < 0)
803                                 break;
804                 }
805                 //FIXME: This fails on some systems
806                 assert(strncmp(sendbuffer, rcvbuffer, sizeof(sendbuffer))==0);
807                 assert(sender.getAddress() == Address(127,0,0,1, 0).getAddress());
808         }
809 };
810
811 struct TestConnection
812 {
813         void TestHelpers()
814         {
815                 /*
816                         Test helper functions
817                 */
818
819                 // Some constants for testing
820                 u32 proto_id = 0x12345678;
821                 u16 peer_id = 123;
822                 u8 channel = 2;
823                 SharedBuffer<u8> data1(1);
824                 data1[0] = 100;
825                 Address a(127,0,0,1, 10);
826                 u16 seqnum = 34352;
827
828                 con::BufferedPacket p1 = con::makePacket(a, data1,
829                                 proto_id, peer_id, channel);
830                 /*
831                         We should now have a packet with this data:
832                         Header:
833                                 [0] u32 protocol_id
834                                 [4] u16 sender_peer_id
835                                 [6] u8 channel
836                         Data:
837                                 [7] u8 data1[0]
838                 */
839                 assert(readU32(&p1.data[0]) == proto_id);
840                 assert(readU16(&p1.data[4]) == peer_id);
841                 assert(readU8(&p1.data[6]) == channel);
842                 assert(readU8(&p1.data[7]) == data1[0]);
843                 
844                 //dstream<<"initial data1[0]="<<((u32)data1[0]&0xff)<<std::endl;
845
846                 SharedBuffer<u8> p2 = con::makeReliablePacket(data1, seqnum);
847
848                 /*dstream<<"p2.getSize()="<<p2.getSize()<<", data1.getSize()="
849                                 <<data1.getSize()<<std::endl;
850                 dstream<<"readU8(&p2[3])="<<readU8(&p2[3])
851                                 <<" p2[3]="<<((u32)p2[3]&0xff)<<std::endl;
852                 dstream<<"data1[0]="<<((u32)data1[0]&0xff)<<std::endl;*/
853
854                 assert(p2.getSize() == 3 + data1.getSize());
855                 assert(readU8(&p2[0]) == TYPE_RELIABLE);
856                 assert(readU16(&p2[1]) == seqnum);
857                 assert(readU8(&p2[3]) == data1[0]);
858         }
859
860         struct Handler : public con::PeerHandler
861         {
862                 Handler(const char *a_name)
863                 {
864                         count = 0;
865                         last_id = 0;
866                         name = a_name;
867                 }
868                 void peerAdded(con::Peer *peer)
869                 {
870                         dstream<<"Handler("<<name<<")::peerAdded(): "
871                                         "id="<<peer->id<<std::endl;
872                         last_id = peer->id;
873                         count++;
874                 }
875                 void deletingPeer(con::Peer *peer, bool timeout)
876                 {
877                         dstream<<"Handler("<<name<<")::deletingPeer(): "
878                                         "id="<<peer->id
879                                         <<", timeout="<<timeout<<std::endl;
880                         last_id = peer->id;
881                         count--;
882                 }
883
884                 s32 count;
885                 u16 last_id;
886                 const char *name;
887         };
888
889         void Run()
890         {
891                 DSTACK("TestConnection::Run");
892
893                 TestHelpers();
894
895                 /*
896                         Test some real connections
897                 */
898                 u32 proto_id = 0xad26846a;
899
900                 Handler hand_server("server");
901                 Handler hand_client("client");
902                 
903                 dstream<<"** Creating server Connection"<<std::endl;
904                 con::Connection server(proto_id, 512, 5.0, &hand_server);
905                 server.Serve(30001);
906                 
907                 dstream<<"** Creating client Connection"<<std::endl;
908                 con::Connection client(proto_id, 512, 5.0, &hand_client);
909
910                 assert(hand_server.count == 0);
911                 assert(hand_client.count == 0);
912                 
913                 sleep_ms(50);
914                 
915                 Address server_address(127,0,0,1, 30001);
916                 dstream<<"** running client.Connect()"<<std::endl;
917                 client.Connect(server_address);
918
919                 sleep_ms(50);
920                 
921                 // Client should have added server now
922                 assert(hand_client.count == 1);
923                 assert(hand_client.last_id == 1);
924                 // But server should not have added client
925                 assert(hand_server.count == 0);
926
927                 try
928                 {
929                         u16 peer_id;
930                         u8 data[100];
931                         dstream<<"** running server.Receive()"<<std::endl;
932                         u32 size = server.Receive(peer_id, data, 100);
933                         dstream<<"** Server received: peer_id="<<peer_id
934                                         <<", size="<<size
935                                         <<std::endl;
936                 }
937                 catch(con::NoIncomingDataException &e)
938                 {
939                         // No actual data received, but the client has
940                         // probably been connected
941                 }
942                 
943                 // Client should be the same
944                 assert(hand_client.count == 1);
945                 assert(hand_client.last_id == 1);
946                 // Server should have the client
947                 assert(hand_server.count == 1);
948                 assert(hand_server.last_id == 2);
949                 
950                 //sleep_ms(50);
951
952                 while(client.Connected() == false)
953                 {
954                         try
955                         {
956                                 u16 peer_id;
957                                 u8 data[100];
958                                 dstream<<"** running client.Receive()"<<std::endl;
959                                 u32 size = client.Receive(peer_id, data, 100);
960                                 dstream<<"** Client received: peer_id="<<peer_id
961                                                 <<", size="<<size
962                                                 <<std::endl;
963                         }
964                         catch(con::NoIncomingDataException &e)
965                         {
966                         }
967                         sleep_ms(50);
968                 }
969
970                 sleep_ms(50);
971                 
972                 try
973                 {
974                         u16 peer_id;
975                         u8 data[100];
976                         dstream<<"** running server.Receive()"<<std::endl;
977                         u32 size = server.Receive(peer_id, data, 100);
978                         dstream<<"** Server received: peer_id="<<peer_id
979                                         <<", size="<<size
980                                         <<std::endl;
981                 }
982                 catch(con::NoIncomingDataException &e)
983                 {
984                 }
985
986                 {
987                         /*u8 data[] = "Hello World!";
988                         u32 datasize = sizeof(data);*/
989                         SharedBuffer<u8> data = SharedBufferFromString("Hello World!");
990
991                         dstream<<"** running client.Send()"<<std::endl;
992                         client.Send(PEER_ID_SERVER, 0, data, true);
993
994                         sleep_ms(50);
995
996                         u16 peer_id;
997                         u8 recvdata[100];
998                         dstream<<"** running server.Receive()"<<std::endl;
999                         u32 size = server.Receive(peer_id, recvdata, 100);
1000                         dstream<<"** Server received: peer_id="<<peer_id
1001                                         <<", size="<<size
1002                                         <<", data="<<*data
1003                                         <<std::endl;
1004                         assert(memcmp(*data, recvdata, data.getSize()) == 0);
1005                 }
1006                 
1007                 u16 peer_id_client = 2;
1008
1009                 {
1010                         /*
1011                                 Send consequent packets in different order
1012                         */
1013                         //u8 data1[] = "hello1";
1014                         //u8 data2[] = "hello2";
1015                         SharedBuffer<u8> data1 = SharedBufferFromString("hello1");
1016                         SharedBuffer<u8> data2 = SharedBufferFromString("Hello2");
1017
1018                         Address client_address =
1019                                         server.GetPeer(peer_id_client)->address;
1020                         
1021                         dstream<<"*** Sending packets in wrong order (2,1,2)"
1022                                         <<std::endl;
1023                         
1024                         u8 chn = 0;
1025                         con::Channel *ch = &server.GetPeer(peer_id_client)->channels[chn];
1026                         u16 sn = ch->next_outgoing_seqnum;
1027                         ch->next_outgoing_seqnum = sn+1;
1028                         server.Send(peer_id_client, chn, data2, true);
1029                         ch->next_outgoing_seqnum = sn;
1030                         server.Send(peer_id_client, chn, data1, true);
1031                         ch->next_outgoing_seqnum = sn+1;
1032                         server.Send(peer_id_client, chn, data2, true);
1033
1034                         sleep_ms(50);
1035
1036                         dstream<<"*** Receiving the packets"<<std::endl;
1037
1038                         u16 peer_id;
1039                         u8 recvdata[20];
1040                         u32 size;
1041
1042                         dstream<<"** running client.Receive()"<<std::endl;
1043                         peer_id = 132;
1044                         size = client.Receive(peer_id, recvdata, 20);
1045                         dstream<<"** Client received: peer_id="<<peer_id
1046                                         <<", size="<<size
1047                                         <<", data="<<recvdata
1048                                         <<std::endl;
1049                         assert(size == data1.getSize());
1050                         assert(memcmp(*data1, recvdata, data1.getSize()) == 0);
1051                         assert(peer_id == PEER_ID_SERVER);
1052                         
1053                         dstream<<"** running client.Receive()"<<std::endl;
1054                         peer_id = 132;
1055                         size = client.Receive(peer_id, recvdata, 20);
1056                         dstream<<"** Client received: peer_id="<<peer_id
1057                                         <<", size="<<size
1058                                         <<", data="<<recvdata
1059                                         <<std::endl;
1060                         assert(size == data2.getSize());
1061                         assert(memcmp(*data2, recvdata, data2.getSize()) == 0);
1062                         assert(peer_id == PEER_ID_SERVER);
1063                         
1064                         bool got_exception = false;
1065                         try
1066                         {
1067                                 dstream<<"** running client.Receive()"<<std::endl;
1068                                 peer_id = 132;
1069                                 size = client.Receive(peer_id, recvdata, 20);
1070                                 dstream<<"** Client received: peer_id="<<peer_id
1071                                                 <<", size="<<size
1072                                                 <<", data="<<recvdata
1073                                                 <<std::endl;
1074                         }
1075                         catch(con::NoIncomingDataException &e)
1076                         {
1077                                 dstream<<"** No incoming data for client"<<std::endl;
1078                                 got_exception = true;
1079                         }
1080                         assert(got_exception);
1081                 }
1082                 {
1083                         //u8 data1[1100];
1084                         SharedBuffer<u8> data1(1100);
1085                         for(u16 i=0; i<1100; i++){
1086                                 data1[i] = i/4;
1087                         }
1088
1089                         dstream<<"Sending data (size="<<1100<<"):";
1090                         for(int i=0; i<1100 && i<20; i++){
1091                                 if(i%2==0) DEBUGPRINT(" ");
1092                                 DEBUGPRINT("%.2X", ((int)((const char*)*data1)[i])&0xff);
1093                         }
1094                         if(1100>20)
1095                                 dstream<<"...";
1096                         dstream<<std::endl;
1097                         
1098                         server.Send(peer_id_client, 0, data1, true);
1099
1100                         sleep_ms(50);
1101                         
1102                         u8 recvdata[2000];
1103                         dstream<<"** running client.Receive()"<<std::endl;
1104                         u16 peer_id = 132;
1105                         u16 size = client.Receive(peer_id, recvdata, 2000);
1106                         dstream<<"** Client received: peer_id="<<peer_id
1107                                         <<", size="<<size
1108                                         <<std::endl;
1109
1110                         dstream<<"Received data (size="<<size<<"):";
1111                         for(int i=0; i<size && i<20; i++){
1112                                 if(i%2==0) DEBUGPRINT(" ");
1113                                 DEBUGPRINT("%.2X", ((int)((const char*)recvdata)[i])&0xff);
1114                         }
1115                         if(size>20)
1116                                 dstream<<"...";
1117                         dstream<<std::endl;
1118
1119                         assert(memcmp(*data1, recvdata, data1.getSize()) == 0);
1120                         assert(peer_id == PEER_ID_SERVER);
1121                 }
1122                 
1123                 // Check peer handlers
1124                 assert(hand_client.count == 1);
1125                 assert(hand_client.last_id == 1);
1126                 assert(hand_server.count == 1);
1127                 assert(hand_server.last_id == 2);
1128                 
1129                 //assert(0);
1130         }
1131 };
1132
1133 #define TEST(X)\
1134 {\
1135         X x;\
1136         dstream<<"Running " #X <<std::endl;\
1137         x.Run();\
1138 }
1139
1140 void run_tests()
1141 {
1142         DSTACK(__FUNCTION_NAME);
1143         dstream<<"run_tests() started"<<std::endl;
1144         TEST(TestUtilities);
1145         TEST(TestCompress);
1146         TEST(TestMapNode);
1147         TEST(TestVoxelManipulator);
1148         TEST(TestMapBlock);
1149         TEST(TestMapSector);
1150         TEST(TestHeightmap);
1151         if(INTERNET_SIMULATOR == false){
1152                 TEST(TestSocket);
1153                 dout_con<<"=== BEGIN RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
1154                 TEST(TestConnection);
1155                 dout_con<<"=== END RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
1156         }
1157         dstream<<"run_tests() passed"<<std::endl;
1158 }
1159