]> git.lizzy.rs Git - minetest-m13.git/blob - src/test.cpp
Add Minetest-M13 source
[minetest-m13.git] / src / test.cpp
1 /*
2 Minetest-m13
3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "test.h"
21 #include "common_irrlicht.h"
22 #include "debug.h"
23 #include "map.h"
24 #include "player.h"
25 #include "main.h"
26 #include "socket.h"
27 #include "connection.h"
28 #include "utility.h"
29 #include "serialization.h"
30 #include "voxel.h"
31 #include <sstream>
32 #include "porting.h"
33 #include "content_mapnode.h"
34 #include "nodedef.h"
35 #include "mapsector.h"
36 #include "settings.h"
37 #include "log.h"
38
39 /*
40         Asserts that the exception occurs
41 */
42 #define EXCEPTION_CHECK(EType, code)\
43 {\
44         bool exception_thrown = false;\
45         try{ code; }\
46         catch(EType &e) { exception_thrown = true; }\
47         assert(exception_thrown);\
48 }
49
50 /*
51         A few item and node definitions for those tests that need them
52 */
53
54 #define CONTENT_STONE 0
55 #define CONTENT_GRASS 0x800
56
57 void define_some_nodes(IWritableItemDefManager *idef, IWritableNodeDefManager *ndef)
58 {
59         content_t i;
60         ItemDefinition itemdef;
61         ContentFeatures f;
62
63         /*
64                 Stone
65         */
66         i = CONTENT_STONE;
67         itemdef = ItemDefinition();
68         itemdef.type = ITEM_NODE;
69         itemdef.name = "default:stone";
70         itemdef.description = "Stone";
71         itemdef.inventory_image = "[inventorycube"
72                 "{default_stone.png"
73                 "{default_stone.png"
74                 "{default_stone.png";
75         f = ContentFeatures();
76         f.name = itemdef.name;
77         for(int i = 0; i < 6; i++)
78                 f.tname_tiles[i] = "default_stone.png";
79         f.is_ground_content = true;
80         f.material.diggability = DIGGABLE_NORMAL;
81         f.material.weight = 5.0;
82         f.material.crackiness = 1.0;
83         f.material.crumbliness = -0.1;
84         f.material.cuttability = -0.2;
85         idef->registerItem(itemdef);
86         ndef->set(i, f);
87
88         /*
89                 Grass
90         */
91         i = CONTENT_GRASS;
92         itemdef = ItemDefinition();
93         itemdef.type = ITEM_NODE;
94         itemdef.name = "default:dirt_with_grass";
95         itemdef.description = "Dirt with grass";
96         itemdef.inventory_image = "[inventorycube"
97                 "{default_grass.png"
98                 "{default_dirt.png&default_grass_side.png"
99                 "{default_dirt.png&default_grass_side.png";
100         f = ContentFeatures();
101         f.name = itemdef.name;
102         f.tname_tiles[0] = "default_grass.png";
103         f.tname_tiles[1] = "default_dirt.png";
104         for(int i = 2; i < 6; i++)
105                 f.tname_tiles[i] = "default_dirt.png^default_grass_side.png";
106         f.is_ground_content = true;
107         f.material.diggability = DIGGABLE_NORMAL;
108         f.material.weight = 1.2;
109         f.material.crackiness = 0.0;
110         f.material.crumbliness = 1.2;
111         f.material.cuttability = -0.4;
112         idef->registerItem(itemdef);
113         ndef->set(i, f);
114 }
115
116 struct TestUtilities
117 {
118         void Run()
119         {
120                 /*infostream<<"wrapDegrees(100.0) = "<<wrapDegrees(100.0)<<std::endl;
121                 infostream<<"wrapDegrees(720.5) = "<<wrapDegrees(720.5)<<std::endl;
122                 infostream<<"wrapDegrees(-0.5) = "<<wrapDegrees(-0.5)<<std::endl;*/
123                 assert(fabs(wrapDegrees(100.0) - 100.0) < 0.001);
124                 assert(fabs(wrapDegrees(720.5) - 0.5) < 0.001);
125                 assert(fabs(wrapDegrees(-0.5) - (-0.5)) < 0.001);
126                 assert(fabs(wrapDegrees(-365.5) - (-5.5)) < 0.001);
127                 assert(lowercase("Foo bAR") == "foo bar");
128                 assert(is_yes("YeS") == true);
129                 assert(is_yes("") == false);
130                 assert(is_yes("FAlse") == false);
131         }
132 };
133
134 struct TestSettings
135 {
136         void Run()
137         {
138                 Settings s;
139                 // Test reading of settings
140                 s.parseConfigLine("leet = 1337");
141                 s.parseConfigLine("leetleet = 13371337");
142                 s.parseConfigLine("leetleet_neg = -13371337");
143                 s.parseConfigLine("floaty_thing = 1.1");
144                 s.parseConfigLine("stringy_thing = asd /( ¤%&(/\" BLÖÄRP");
145                 s.parseConfigLine("coord = (1, 2, 4.5)");
146                 assert(s.getS32("leet") == 1337);
147                 assert(s.getS16("leetleet") == 32767);
148                 assert(s.getS16("leetleet_neg") == -32768);
149                 // Not sure if 1.1 is an exact value as a float, but doesn't matter
150                 assert(fabs(s.getFloat("floaty_thing") - 1.1) < 0.001);
151                 assert(s.get("stringy_thing") == "asd /( ¤%&(/\" BLÖÄRP");
152                 assert(fabs(s.getV3F("coord").X - 1.0) < 0.001);
153                 assert(fabs(s.getV3F("coord").Y - 2.0) < 0.001);
154                 assert(fabs(s.getV3F("coord").Z - 4.5) < 0.001);
155                 // Test the setting of settings too
156                 s.setFloat("floaty_thing_2", 1.2);
157                 s.setV3F("coord2", v3f(1, 2, 3.3));
158                 assert(s.get("floaty_thing_2").substr(0,3) == "1.2");
159                 assert(fabs(s.getFloat("floaty_thing_2") - 1.2) < 0.001);
160                 assert(fabs(s.getV3F("coord2").X - 1.0) < 0.001);
161                 assert(fabs(s.getV3F("coord2").Y - 2.0) < 0.001);
162                 assert(fabs(s.getV3F("coord2").Z - 3.3) < 0.001);
163         }
164 };
165
166 struct TestSerialization
167 {
168         // To be used like this:
169         //   mkstr("Some\0string\0with\0embedded\0nuls")
170         // since std::string("...") doesn't work as expected in that case.
171         template<size_t N> std::string mkstr(const char (&s)[N])
172         {
173                 return std::string(s, N - 1);
174         }
175
176         void Run()
177         {
178                 // Tests some serialization primitives
179
180                 assert(serializeString("") == mkstr("\0\0"));
181                 assert(serializeWideString(L"") == mkstr("\0\0"));
182                 assert(serializeLongString("") == mkstr("\0\0\0\0"));
183                 assert(serializeJsonString("") == "\"\"");
184                 
185                 std::string teststring = "Hello world!";
186                 assert(serializeString(teststring) ==
187                         mkstr("\0\14Hello world!"));
188                 assert(serializeWideString(narrow_to_wide(teststring)) ==
189                         mkstr("\0\14\0H\0e\0l\0l\0o\0 \0w\0o\0r\0l\0d\0!"));
190                 assert(serializeLongString(teststring) ==
191                         mkstr("\0\0\0\14Hello world!"));
192                 assert(serializeJsonString(teststring) ==
193                         "\"Hello world!\"");
194
195                 std::string teststring2;
196                 std::wstring teststring2_w;
197                 std::string teststring2_w_encoded;
198                 {
199                         std::ostringstream tmp_os;
200                         std::wostringstream tmp_os_w;
201                         std::ostringstream tmp_os_w_encoded;
202                         for(int i = 0; i < 256; i++)
203                         {
204                                 tmp_os<<(char)i;
205                                 tmp_os_w<<(wchar_t)i;
206                                 tmp_os_w_encoded<<(char)0<<(char)i;
207                         }
208                         teststring2 = tmp_os.str();
209                         teststring2_w = tmp_os_w.str();
210                         teststring2_w_encoded = tmp_os_w_encoded.str();
211                 }
212                 assert(serializeString(teststring2) ==
213                         mkstr("\1\0") + teststring2);
214                 assert(serializeWideString(teststring2_w) ==
215                         mkstr("\1\0") + teststring2_w_encoded);
216                 assert(serializeLongString(teststring2) ==
217                         mkstr("\0\0\1\0") + teststring2);
218                 // MSVC fails when directly using "\\\\"
219                 std::string backslash = "\\";
220                 assert(serializeJsonString(teststring2) ==
221                         mkstr("\"") +
222                         "\\u0000\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007" +
223                         "\\b\\t\\n\\u000b\\f\\r\\u000e\\u000f" +
224                         "\\u0010\\u0011\\u0012\\u0013\\u0014\\u0015\\u0016\\u0017" +
225                         "\\u0018\\u0019\\u001a\\u001b\\u001c\\u001d\\u001e\\u001f" +
226                         " !\\\"" + teststring2.substr(0x23, 0x2f-0x23) +
227                         "\\/" + teststring2.substr(0x30, 0x5c-0x30) +
228                         backslash + backslash + teststring2.substr(0x5d, 0x7f-0x5d) + "\\u007f" +
229                         "\\u0080\\u0081\\u0082\\u0083\\u0084\\u0085\\u0086\\u0087" +
230                         "\\u0088\\u0089\\u008a\\u008b\\u008c\\u008d\\u008e\\u008f" +
231                         "\\u0090\\u0091\\u0092\\u0093\\u0094\\u0095\\u0096\\u0097" +
232                         "\\u0098\\u0099\\u009a\\u009b\\u009c\\u009d\\u009e\\u009f" +
233                         "\\u00a0\\u00a1\\u00a2\\u00a3\\u00a4\\u00a5\\u00a6\\u00a7" +
234                         "\\u00a8\\u00a9\\u00aa\\u00ab\\u00ac\\u00ad\\u00ae\\u00af" +
235                         "\\u00b0\\u00b1\\u00b2\\u00b3\\u00b4\\u00b5\\u00b6\\u00b7" +
236                         "\\u00b8\\u00b9\\u00ba\\u00bb\\u00bc\\u00bd\\u00be\\u00bf" +
237                         "\\u00c0\\u00c1\\u00c2\\u00c3\\u00c4\\u00c5\\u00c6\\u00c7" +
238                         "\\u00c8\\u00c9\\u00ca\\u00cb\\u00cc\\u00cd\\u00ce\\u00cf" +
239                         "\\u00d0\\u00d1\\u00d2\\u00d3\\u00d4\\u00d5\\u00d6\\u00d7" +
240                         "\\u00d8\\u00d9\\u00da\\u00db\\u00dc\\u00dd\\u00de\\u00df" +
241                         "\\u00e0\\u00e1\\u00e2\\u00e3\\u00e4\\u00e5\\u00e6\\u00e7" +
242                         "\\u00e8\\u00e9\\u00ea\\u00eb\\u00ec\\u00ed\\u00ee\\u00ef" +
243                         "\\u00f0\\u00f1\\u00f2\\u00f3\\u00f4\\u00f5\\u00f6\\u00f7" +
244                         "\\u00f8\\u00f9\\u00fa\\u00fb\\u00fc\\u00fd\\u00fe\\u00ff" +
245                         "\"");
246
247                 {
248                         std::istringstream is(serializeString(teststring2), std::ios::binary);
249                         assert(deSerializeString(is) == teststring2);
250                         assert(!is.eof());
251                         is.get();
252                         assert(is.eof());
253                 }
254                 {
255                         std::istringstream is(serializeWideString(teststring2_w), std::ios::binary);
256                         assert(deSerializeWideString(is) == teststring2_w);
257                         assert(!is.eof());
258                         is.get();
259                         assert(is.eof());
260                 }
261                 {
262                         std::istringstream is(serializeLongString(teststring2), std::ios::binary);
263                         assert(deSerializeLongString(is) == teststring2);
264                         assert(!is.eof());
265                         is.get();
266                         assert(is.eof());
267                 }
268                 {
269                         std::istringstream is(serializeJsonString(teststring2), std::ios::binary);
270                         //dstream<<serializeJsonString(deSerializeJsonString(is));
271                         assert(deSerializeJsonString(is) == teststring2);
272                         assert(!is.eof());
273                         is.get();
274                         assert(is.eof());
275                 }
276         }
277 };
278
279 struct TestCompress
280 {
281         void Run()
282         {
283                 { // ver 0
284
285                 SharedBuffer<u8> fromdata(4);
286                 fromdata[0]=1;
287                 fromdata[1]=5;
288                 fromdata[2]=5;
289                 fromdata[3]=1;
290                 
291                 std::ostringstream os(std::ios_base::binary);
292                 compress(fromdata, os, 0);
293
294                 std::string str_out = os.str();
295                 
296                 infostream<<"str_out.size()="<<str_out.size()<<std::endl;
297                 infostream<<"TestCompress: 1,5,5,1 -> ";
298                 for(u32 i=0; i<str_out.size(); i++)
299                 {
300                         infostream<<(u32)str_out[i]<<",";
301                 }
302                 infostream<<std::endl;
303
304                 assert(str_out.size() == 10);
305
306                 assert(str_out[0] == 0);
307                 assert(str_out[1] == 0);
308                 assert(str_out[2] == 0);
309                 assert(str_out[3] == 4);
310                 assert(str_out[4] == 0);
311                 assert(str_out[5] == 1);
312                 assert(str_out[6] == 1);
313                 assert(str_out[7] == 5);
314                 assert(str_out[8] == 0);
315                 assert(str_out[9] == 1);
316
317                 std::istringstream is(str_out, std::ios_base::binary);
318                 std::ostringstream os2(std::ios_base::binary);
319
320                 decompress(is, os2, 0);
321                 std::string str_out2 = os2.str();
322
323                 infostream<<"decompress: ";
324                 for(u32 i=0; i<str_out2.size(); i++)
325                 {
326                         infostream<<(u32)str_out2[i]<<",";
327                 }
328                 infostream<<std::endl;
329
330                 assert(str_out2.size() == fromdata.getSize());
331
332                 for(u32 i=0; i<str_out2.size(); i++)
333                 {
334                         assert(str_out2[i] == fromdata[i]);
335                 }
336
337                 }
338
339                 { // ver HIGHEST
340
341                 SharedBuffer<u8> fromdata(4);
342                 fromdata[0]=1;
343                 fromdata[1]=5;
344                 fromdata[2]=5;
345                 fromdata[3]=1;
346                 
347                 std::ostringstream os(std::ios_base::binary);
348                 compress(fromdata, os, SER_FMT_VER_HIGHEST);
349
350                 std::string str_out = os.str();
351                 
352                 infostream<<"str_out.size()="<<str_out.size()<<std::endl;
353                 infostream<<"TestCompress: 1,5,5,1 -> ";
354                 for(u32 i=0; i<str_out.size(); i++)
355                 {
356                         infostream<<(u32)str_out[i]<<",";
357                 }
358                 infostream<<std::endl;
359
360                 /*assert(str_out.size() == 10);
361
362                 assert(str_out[0] == 0);
363                 assert(str_out[1] == 0);
364                 assert(str_out[2] == 0);
365                 assert(str_out[3] == 4);
366                 assert(str_out[4] == 0);
367                 assert(str_out[5] == 1);
368                 assert(str_out[6] == 1);
369                 assert(str_out[7] == 5);
370                 assert(str_out[8] == 0);
371                 assert(str_out[9] == 1);*/
372
373                 std::istringstream is(str_out, std::ios_base::binary);
374                 std::ostringstream os2(std::ios_base::binary);
375
376                 decompress(is, os2, SER_FMT_VER_HIGHEST);
377                 std::string str_out2 = os2.str();
378
379                 infostream<<"decompress: ";
380                 for(u32 i=0; i<str_out2.size(); i++)
381                 {
382                         infostream<<(u32)str_out2[i]<<",";
383                 }
384                 infostream<<std::endl;
385
386                 assert(str_out2.size() == fromdata.getSize());
387
388                 for(u32 i=0; i<str_out2.size(); i++)
389                 {
390                         assert(str_out2[i] == fromdata[i]);
391                 }
392
393                 }
394         }
395 };
396
397 struct TestMapNode
398 {
399         void Run(INodeDefManager *nodedef)
400         {
401                 MapNode n;
402
403                 // Default values
404                 assert(n.getContent() == CONTENT_AIR);
405                 assert(n.getLight(LIGHTBANK_DAY, nodedef) == 0);
406                 assert(n.getLight(LIGHTBANK_NIGHT, nodedef) == 0);
407                 
408                 // Transparency
409                 n.setContent(CONTENT_AIR);
410                 assert(nodedef->get(n).light_propagates == true);
411                 n.setContent(LEGN(nodedef, "CONTENT_STONE"));
412                 assert(nodedef->get(n).light_propagates == false);
413         }
414 };
415
416 struct TestVoxelManipulator
417 {
418         void Run(INodeDefManager *nodedef)
419         {
420                 /*
421                         VoxelArea
422                 */
423
424                 VoxelArea a(v3s16(-1,-1,-1), v3s16(1,1,1));
425                 assert(a.index(0,0,0) == 1*3*3 + 1*3 + 1);
426                 assert(a.index(-1,-1,-1) == 0);
427                 
428                 VoxelArea c(v3s16(-2,-2,-2), v3s16(2,2,2));
429                 // An area that is 1 bigger in x+ and z-
430                 VoxelArea d(v3s16(-2,-2,-3), v3s16(3,2,2));
431                 
432                 core::list<VoxelArea> aa;
433                 d.diff(c, aa);
434                 
435                 // Correct results
436                 core::array<VoxelArea> results;
437                 results.push_back(VoxelArea(v3s16(-2,-2,-3),v3s16(3,2,-3)));
438                 results.push_back(VoxelArea(v3s16(3,-2,-2),v3s16(3,2,2)));
439
440                 assert(aa.size() == results.size());
441                 
442                 infostream<<"Result of diff:"<<std::endl;
443                 for(core::list<VoxelArea>::Iterator
444                                 i = aa.begin(); i != aa.end(); i++)
445                 {
446                         i->print(infostream);
447                         infostream<<std::endl;
448                         
449                         s32 j = results.linear_search(*i);
450                         assert(j != -1);
451                         results.erase(j, 1);
452                 }
453
454
455                 /*
456                         VoxelManipulator
457                 */
458                 
459                 VoxelManipulator v;
460
461                 v.print(infostream, nodedef);
462
463                 infostream<<"*** Setting (-1,0,-1)=2 ***"<<std::endl;
464                 
465                 v.setNodeNoRef(v3s16(-1,0,-1), MapNode(CONTENT_GRASS));
466
467                 v.print(infostream, nodedef);
468
469                 assert(v.getNode(v3s16(-1,0,-1)).getContent() == CONTENT_GRASS);
470
471                 infostream<<"*** Reading from inexistent (0,0,-1) ***"<<std::endl;
472
473                 EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,0,-1)));
474
475                 v.print(infostream, nodedef);
476
477                 infostream<<"*** Adding area ***"<<std::endl;
478
479                 v.addArea(a);
480                 
481                 v.print(infostream, nodedef);
482
483                 assert(v.getNode(v3s16(-1,0,-1)).getContent() == CONTENT_GRASS);
484                 EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,1,1)));
485         }
486 };
487
488 /*
489         NOTE: These tests became non-working then NodeContainer was removed.
490               These should be redone, utilizing some kind of a virtual
491                   interface for Map (IMap would be fine).
492 */
493 #if 0
494 struct TestMapBlock
495 {
496         class TC : public NodeContainer
497         {
498         public:
499
500                 MapNode node;
501                 bool position_valid;
502                 core::list<v3s16> validity_exceptions;
503
504                 TC()
505                 {
506                         position_valid = true;
507                 }
508
509                 virtual bool isValidPosition(v3s16 p)
510                 {
511                         //return position_valid ^ (p==position_valid_exception);
512                         bool exception = false;
513                         for(core::list<v3s16>::Iterator i=validity_exceptions.begin();
514                                         i != validity_exceptions.end(); i++)
515                         {
516                                 if(p == *i)
517                                 {
518                                         exception = true;
519                                         break;
520                                 }
521                         }
522                         return exception ? !position_valid : position_valid;
523                 }
524
525                 virtual MapNode getNode(v3s16 p)
526                 {
527                         if(isValidPosition(p) == false)
528                                 throw InvalidPositionException();
529                         return node;
530                 }
531
532                 virtual void setNode(v3s16 p, MapNode & n)
533                 {
534                         if(isValidPosition(p) == false)
535                                 throw InvalidPositionException();
536                 };
537
538                 virtual u16 nodeContainerId() const
539                 {
540                         return 666;
541                 }
542         };
543
544         void Run()
545         {
546                 TC parent;
547                 
548                 MapBlock b(&parent, v3s16(1,1,1));
549                 v3s16 relpos(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
550
551                 assert(b.getPosRelative() == relpos);
552
553                 assert(b.getBox().MinEdge.X == MAP_BLOCKSIZE);
554                 assert(b.getBox().MaxEdge.X == MAP_BLOCKSIZE*2-1);
555                 assert(b.getBox().MinEdge.Y == MAP_BLOCKSIZE);
556                 assert(b.getBox().MaxEdge.Y == MAP_BLOCKSIZE*2-1);
557                 assert(b.getBox().MinEdge.Z == MAP_BLOCKSIZE);
558                 assert(b.getBox().MaxEdge.Z == MAP_BLOCKSIZE*2-1);
559                 
560                 assert(b.isValidPosition(v3s16(0,0,0)) == true);
561                 assert(b.isValidPosition(v3s16(-1,0,0)) == false);
562                 assert(b.isValidPosition(v3s16(-1,-142,-2341)) == false);
563                 assert(b.isValidPosition(v3s16(-124,142,2341)) == false);
564                 assert(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
565                 assert(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE,MAP_BLOCKSIZE-1)) == false);
566
567                 /*
568                         TODO: this method should probably be removed
569                         if the block size isn't going to be set variable
570                 */
571                 /*assert(b.getSizeNodes() == v3s16(MAP_BLOCKSIZE,
572                                 MAP_BLOCKSIZE, MAP_BLOCKSIZE));*/
573                 
574                 // Changed flag should be initially set
575                 assert(b.getModified() == MOD_STATE_WRITE_NEEDED);
576                 b.resetModified();
577                 assert(b.getModified() == MOD_STATE_CLEAN);
578
579                 // All nodes should have been set to
580                 // .d=CONTENT_IGNORE and .getLight() = 0
581                 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
582                 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
583                 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
584                 {
585                         //assert(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_AIR);
586                         assert(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_IGNORE);
587                         assert(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_DAY) == 0);
588                         assert(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_NIGHT) == 0);
589                 }
590                 
591                 {
592                         MapNode n(CONTENT_AIR);
593                         for(u16 z=0; z<MAP_BLOCKSIZE; z++)
594                         for(u16 y=0; y<MAP_BLOCKSIZE; y++)
595                         for(u16 x=0; x<MAP_BLOCKSIZE; x++)
596                         {
597                                 b.setNode(v3s16(x,y,z), n);
598                         }
599                 }
600                         
601                 /*
602                         Parent fetch functions
603                 */
604                 parent.position_valid = false;
605                 parent.node.setContent(5);
606
607                 MapNode n;
608                 
609                 // Positions in the block should still be valid
610                 assert(b.isValidPositionParent(v3s16(0,0,0)) == true);
611                 assert(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
612                 n = b.getNodeParent(v3s16(0,MAP_BLOCKSIZE-1,0));
613                 assert(n.getContent() == CONTENT_AIR);
614
615                 // ...but outside the block they should be invalid
616                 assert(b.isValidPositionParent(v3s16(-121,2341,0)) == false);
617                 assert(b.isValidPositionParent(v3s16(-1,0,0)) == false);
618                 assert(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == false);
619                 
620                 {
621                         bool exception_thrown = false;
622                         try{
623                                 // This should throw an exception
624                                 MapNode n = b.getNodeParent(v3s16(0,0,-1));
625                         }
626                         catch(InvalidPositionException &e)
627                         {
628                                 exception_thrown = true;
629                         }
630                         assert(exception_thrown);
631                 }
632
633                 parent.position_valid = true;
634                 // Now the positions outside should be valid
635                 assert(b.isValidPositionParent(v3s16(-121,2341,0)) == true);
636                 assert(b.isValidPositionParent(v3s16(-1,0,0)) == true);
637                 assert(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == true);
638                 n = b.getNodeParent(v3s16(0,0,MAP_BLOCKSIZE));
639                 assert(n.getContent() == 5);
640
641                 /*
642                         Set a node
643                 */
644                 v3s16 p(1,2,0);
645                 n.setContent(4);
646                 b.setNode(p, n);
647                 assert(b.getNode(p).getContent() == 4);
648                 //TODO: Update to new system
649                 /*assert(b.getNodeTile(p) == 4);
650                 assert(b.getNodeTile(v3s16(-1,-1,0)) == 5);*/
651                 
652                 /*
653                         propagateSunlight()
654                 */
655                 // Set lighting of all nodes to 0
656                 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
657                         for(u16 y=0; y<MAP_BLOCKSIZE; y++){
658                                 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
659                                         MapNode n = b.getNode(v3s16(x,y,z));
660                                         n.setLight(LIGHTBANK_DAY, 0);
661                                         n.setLight(LIGHTBANK_NIGHT, 0);
662                                         b.setNode(v3s16(x,y,z), n);
663                                 }
664                         }
665                 }
666                 {
667                         /*
668                                 Check how the block handles being a lonely sky block
669                         */
670                         parent.position_valid = true;
671                         b.setIsUnderground(false);
672                         parent.node.setContent(CONTENT_AIR);
673                         parent.node.setLight(LIGHTBANK_DAY, LIGHT_SUN);
674                         parent.node.setLight(LIGHTBANK_NIGHT, 0);
675                         core::map<v3s16, bool> light_sources;
676                         // The bottom block is invalid, because we have a shadowing node
677                         assert(b.propagateSunlight(light_sources) == false);
678                         assert(b.getNode(v3s16(1,4,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
679                         assert(b.getNode(v3s16(1,3,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
680                         assert(b.getNode(v3s16(1,2,0)).getLight(LIGHTBANK_DAY) == 0);
681                         assert(b.getNode(v3s16(1,1,0)).getLight(LIGHTBANK_DAY) == 0);
682                         assert(b.getNode(v3s16(1,0,0)).getLight(LIGHTBANK_DAY) == 0);
683                         assert(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
684                         assert(b.getFaceLight2(1000, p, v3s16(0,1,0)) == LIGHT_SUN);
685                         assert(b.getFaceLight2(1000, p, v3s16(0,-1,0)) == 0);
686                         assert(b.getFaceLight2(0, p, v3s16(0,-1,0)) == 0);
687                         // According to MapBlock::getFaceLight,
688                         // The face on the z+ side should have double-diminished light
689                         //assert(b.getFaceLight(p, v3s16(0,0,1)) == diminish_light(diminish_light(LIGHT_MAX)));
690                         // The face on the z+ side should have diminished light
691                         assert(b.getFaceLight2(1000, p, v3s16(0,0,1)) == diminish_light(LIGHT_MAX));
692                 }
693                 /*
694                         Check how the block handles being in between blocks with some non-sunlight
695                         while being underground
696                 */
697                 {
698                         // Make neighbours to exist and set some non-sunlight to them
699                         parent.position_valid = true;
700                         b.setIsUnderground(true);
701                         parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
702                         core::map<v3s16, bool> light_sources;
703                         // The block below should be valid because there shouldn't be
704                         // sunlight in there either
705                         assert(b.propagateSunlight(light_sources, true) == true);
706                         // Should not touch nodes that are not affected (that is, all of them)
707                         //assert(b.getNode(v3s16(1,2,3)).getLight() == LIGHT_SUN);
708                         // Should set light of non-sunlighted blocks to 0.
709                         assert(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == 0);
710                 }
711                 /*
712                         Set up a situation where:
713                         - There is only air in this block
714                         - There is a valid non-sunlighted block at the bottom, and
715                         - Invalid blocks elsewhere.
716                         - the block is not underground.
717
718                         This should result in bottom block invalidity
719                 */
720                 {
721                         b.setIsUnderground(false);
722                         // Clear block
723                         for(u16 z=0; z<MAP_BLOCKSIZE; z++){
724                                 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
725                                         for(u16 x=0; x<MAP_BLOCKSIZE; x++){
726                                                 MapNode n;
727                                                 n.setContent(CONTENT_AIR);
728                                                 n.setLight(LIGHTBANK_DAY, 0);
729                                                 b.setNode(v3s16(x,y,z), n);
730                                         }
731                                 }
732                         }
733                         // Make neighbours invalid
734                         parent.position_valid = false;
735                         // Add exceptions to the top of the bottom block
736                         for(u16 x=0; x<MAP_BLOCKSIZE; x++)
737                         for(u16 z=0; z<MAP_BLOCKSIZE; z++)
738                         {
739                                 parent.validity_exceptions.push_back(v3s16(MAP_BLOCKSIZE+x, MAP_BLOCKSIZE-1, MAP_BLOCKSIZE+z));
740                         }
741                         // Lighting value for the valid nodes
742                         parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
743                         core::map<v3s16, bool> light_sources;
744                         // Bottom block is not valid
745                         assert(b.propagateSunlight(light_sources) == false);
746                 }
747         }
748 };
749
750 struct TestMapSector
751 {
752         class TC : public NodeContainer
753         {
754         public:
755
756                 MapNode node;
757                 bool position_valid;
758
759                 TC()
760                 {
761                         position_valid = true;
762                 }
763
764                 virtual bool isValidPosition(v3s16 p)
765                 {
766                         return position_valid;
767                 }
768
769                 virtual MapNode getNode(v3s16 p)
770                 {
771                         if(position_valid == false)
772                                 throw InvalidPositionException();
773                         return node;
774                 }
775
776                 virtual void setNode(v3s16 p, MapNode & n)
777                 {
778                         if(position_valid == false)
779                                 throw InvalidPositionException();
780                 };
781                 
782                 virtual u16 nodeContainerId() const
783                 {
784                         return 666;
785                 }
786         };
787         
788         void Run()
789         {
790                 TC parent;
791                 parent.position_valid = false;
792                 
793                 // Create one with no heightmaps
794                 ServerMapSector sector(&parent, v2s16(1,1));
795                 
796                 assert(sector.getBlockNoCreateNoEx(0) == 0);
797                 assert(sector.getBlockNoCreateNoEx(1) == 0);
798
799                 MapBlock * bref = sector.createBlankBlock(-2);
800                 
801                 assert(sector.getBlockNoCreateNoEx(0) == 0);
802                 assert(sector.getBlockNoCreateNoEx(-2) == bref);
803                 
804                 //TODO: Check for AlreadyExistsException
805
806                 /*bool exception_thrown = false;
807                 try{
808                         sector.getBlock(0);
809                 }
810                 catch(InvalidPositionException &e){
811                         exception_thrown = true;
812                 }
813                 assert(exception_thrown);*/
814
815         }
816 };
817 #endif
818
819 struct TestSocket
820 {
821         void Run()
822         {
823                 const int port = 30003;
824                 UDPSocket socket;
825                 socket.Bind(port);
826
827                 const char sendbuffer[] = "hello world!";
828                 socket.Send(Address(127,0,0,1,port), sendbuffer, sizeof(sendbuffer));
829
830                 sleep_ms(50);
831
832                 char rcvbuffer[256];
833                 memset(rcvbuffer, 0, sizeof(rcvbuffer));
834                 Address sender;
835                 for(;;)
836                 {
837                         int bytes_read = socket.Receive(sender, rcvbuffer, sizeof(rcvbuffer));
838                         if(bytes_read < 0)
839                                 break;
840                 }
841                 //FIXME: This fails on some systems
842                 assert(strncmp(sendbuffer, rcvbuffer, sizeof(sendbuffer))==0);
843                 assert(sender.getAddress() == Address(127,0,0,1, 0).getAddress());
844         }
845 };
846
847 struct TestConnection
848 {
849         void TestHelpers()
850         {
851                 /*
852                         Test helper functions
853                 */
854
855                 // Some constants for testing
856                 u32 proto_id = 0x12345678;
857                 u16 peer_id = 123;
858                 u8 channel = 2;
859                 SharedBuffer<u8> data1(1);
860                 data1[0] = 100;
861                 Address a(127,0,0,1, 10);
862                 u16 seqnum = 34352;
863
864                 con::BufferedPacket p1 = con::makePacket(a, data1,
865                                 proto_id, peer_id, channel);
866                 /*
867                         We should now have a packet with this data:
868                         Header:
869                                 [0] u32 protocol_id
870                                 [4] u16 sender_peer_id
871                                 [6] u8 channel
872                         Data:
873                                 [7] u8 data1[0]
874                 */
875                 assert(readU32(&p1.data[0]) == proto_id);
876                 assert(readU16(&p1.data[4]) == peer_id);
877                 assert(readU8(&p1.data[6]) == channel);
878                 assert(readU8(&p1.data[7]) == data1[0]);
879                 
880                 //infostream<<"initial data1[0]="<<((u32)data1[0]&0xff)<<std::endl;
881
882                 SharedBuffer<u8> p2 = con::makeReliablePacket(data1, seqnum);
883
884                 /*infostream<<"p2.getSize()="<<p2.getSize()<<", data1.getSize()="
885                                 <<data1.getSize()<<std::endl;
886                 infostream<<"readU8(&p2[3])="<<readU8(&p2[3])
887                                 <<" p2[3]="<<((u32)p2[3]&0xff)<<std::endl;
888                 infostream<<"data1[0]="<<((u32)data1[0]&0xff)<<std::endl;*/
889
890                 assert(p2.getSize() == 3 + data1.getSize());
891                 assert(readU8(&p2[0]) == TYPE_RELIABLE);
892                 assert(readU16(&p2[1]) == seqnum);
893                 assert(readU8(&p2[3]) == data1[0]);
894         }
895
896         struct Handler : public con::PeerHandler
897         {
898                 Handler(const char *a_name)
899                 {
900                         count = 0;
901                         last_id = 0;
902                         name = a_name;
903                 }
904                 void peerAdded(con::Peer *peer)
905                 {
906                         infostream<<"Handler("<<name<<")::peerAdded(): "
907                                         "id="<<peer->id<<std::endl;
908                         last_id = peer->id;
909                         count++;
910                 }
911                 void deletingPeer(con::Peer *peer, bool timeout)
912                 {
913                         infostream<<"Handler("<<name<<")::deletingPeer(): "
914                                         "id="<<peer->id
915                                         <<", timeout="<<timeout<<std::endl;
916                         last_id = peer->id;
917                         count--;
918                 }
919
920                 s32 count;
921                 u16 last_id;
922                 const char *name;
923         };
924
925         void Run()
926         {
927                 DSTACK("TestConnection::Run");
928
929                 TestHelpers();
930
931                 /*
932                         Test some real connections
933
934                         NOTE: This mostly tests the legacy interface.
935                 */
936
937                 u32 proto_id = 0xad26846a;
938
939                 Handler hand_server("server");
940                 Handler hand_client("client");
941                 
942                 infostream<<"** Creating server Connection"<<std::endl;
943                 con::Connection server(proto_id, 512, 5.0, &hand_server);
944                 server.Serve(30001);
945                 
946                 infostream<<"** Creating client Connection"<<std::endl;
947                 con::Connection client(proto_id, 512, 5.0, &hand_client);
948
949                 assert(hand_server.count == 0);
950                 assert(hand_client.count == 0);
951                 
952                 sleep_ms(50);
953                 
954                 Address server_address(127,0,0,1, 30001);
955                 infostream<<"** running client.Connect()"<<std::endl;
956                 client.Connect(server_address);
957
958                 sleep_ms(50);
959                 
960                 // Client should not have added client yet
961                 assert(hand_client.count == 0);
962                 
963                 try
964                 {
965                         u16 peer_id;
966                         SharedBuffer<u8> data;
967                         infostream<<"** running client.Receive()"<<std::endl;
968                         u32 size = client.Receive(peer_id, data);
969                         infostream<<"** Client received: peer_id="<<peer_id
970                                         <<", size="<<size
971                                         <<std::endl;
972                 }
973                 catch(con::NoIncomingDataException &e)
974                 {
975                 }
976
977                 // Client should have added server now
978                 assert(hand_client.count == 1);
979                 assert(hand_client.last_id == 1);
980                 // Server should not have added client yet
981                 assert(hand_server.count == 0);
982                 
983                 sleep_ms(50);
984
985                 try
986                 {
987                         u16 peer_id;
988                         SharedBuffer<u8> data;
989                         infostream<<"** running server.Receive()"<<std::endl;
990                         u32 size = server.Receive(peer_id, data);
991                         infostream<<"** Server received: peer_id="<<peer_id
992                                         <<", size="<<size
993                                         <<std::endl;
994                 }
995                 catch(con::NoIncomingDataException &e)
996                 {
997                         // No actual data received, but the client has
998                         // probably been connected
999                 }
1000                 
1001                 // Client should be the same
1002                 assert(hand_client.count == 1);
1003                 assert(hand_client.last_id == 1);
1004                 // Server should have the client
1005                 assert(hand_server.count == 1);
1006                 assert(hand_server.last_id == 2);
1007                 
1008                 //sleep_ms(50);
1009
1010                 while(client.Connected() == false)
1011                 {
1012                         try
1013                         {
1014                                 u16 peer_id;
1015                                 SharedBuffer<u8> data;
1016                                 infostream<<"** running client.Receive()"<<std::endl;
1017                                 u32 size = client.Receive(peer_id, data);
1018                                 infostream<<"** Client received: peer_id="<<peer_id
1019                                                 <<", size="<<size
1020                                                 <<std::endl;
1021                         }
1022                         catch(con::NoIncomingDataException &e)
1023                         {
1024                         }
1025                         sleep_ms(50);
1026                 }
1027
1028                 sleep_ms(50);
1029                 
1030                 try
1031                 {
1032                         u16 peer_id;
1033                         SharedBuffer<u8> data;
1034                         infostream<<"** running server.Receive()"<<std::endl;
1035                         u32 size = server.Receive(peer_id, data);
1036                         infostream<<"** Server received: peer_id="<<peer_id
1037                                         <<", size="<<size
1038                                         <<std::endl;
1039                 }
1040                 catch(con::NoIncomingDataException &e)
1041                 {
1042                 }
1043 #if 1
1044                 /*
1045                         Simple send-receive test
1046                 */
1047                 {
1048                         /*u8 data[] = "Hello World!";
1049                         u32 datasize = sizeof(data);*/
1050                         SharedBuffer<u8> data = SharedBufferFromString("Hello World!");
1051
1052                         infostream<<"** running client.Send()"<<std::endl;
1053                         client.Send(PEER_ID_SERVER, 0, data, true);
1054
1055                         sleep_ms(50);
1056
1057                         u16 peer_id;
1058                         SharedBuffer<u8> recvdata;
1059                         infostream<<"** running server.Receive()"<<std::endl;
1060                         u32 size = server.Receive(peer_id, recvdata);
1061                         infostream<<"** Server received: peer_id="<<peer_id
1062                                         <<", size="<<size
1063                                         <<", data="<<*data
1064                                         <<std::endl;
1065                         assert(memcmp(*data, *recvdata, data.getSize()) == 0);
1066                 }
1067 #endif
1068                 u16 peer_id_client = 2;
1069 #if 0
1070                 /*
1071                         Send consequent packets in different order
1072                         Not compatible with new Connection, thus commented out.
1073                 */
1074                 {
1075                         //u8 data1[] = "hello1";
1076                         //u8 data2[] = "hello2";
1077                         SharedBuffer<u8> data1 = SharedBufferFromString("hello1");
1078                         SharedBuffer<u8> data2 = SharedBufferFromString("Hello2");
1079
1080                         Address client_address =
1081                                         server.GetPeerAddress(peer_id_client);
1082                         
1083                         infostream<<"*** Sending packets in wrong order (2,1,2)"
1084                                         <<std::endl;
1085                         
1086                         u8 chn = 0;
1087                         con::Channel *ch = &server.getPeer(peer_id_client)->channels[chn];
1088                         u16 sn = ch->next_outgoing_seqnum;
1089                         ch->next_outgoing_seqnum = sn+1;
1090                         server.Send(peer_id_client, chn, data2, true);
1091                         ch->next_outgoing_seqnum = sn;
1092                         server.Send(peer_id_client, chn, data1, true);
1093                         ch->next_outgoing_seqnum = sn+1;
1094                         server.Send(peer_id_client, chn, data2, true);
1095
1096                         sleep_ms(50);
1097
1098                         infostream<<"*** Receiving the packets"<<std::endl;
1099
1100                         u16 peer_id;
1101                         SharedBuffer<u8> recvdata;
1102                         u32 size;
1103
1104                         infostream<<"** running client.Receive()"<<std::endl;
1105                         peer_id = 132;
1106                         size = client.Receive(peer_id, recvdata);
1107                         infostream<<"** Client received: peer_id="<<peer_id
1108                                         <<", size="<<size
1109                                         <<", data="<<*recvdata
1110                                         <<std::endl;
1111                         assert(size == data1.getSize());
1112                         assert(memcmp(*data1, *recvdata, data1.getSize()) == 0);
1113                         assert(peer_id == PEER_ID_SERVER);
1114                         
1115                         infostream<<"** running client.Receive()"<<std::endl;
1116                         peer_id = 132;
1117                         size = client.Receive(peer_id, recvdata);
1118                         infostream<<"** Client received: peer_id="<<peer_id
1119                                         <<", size="<<size
1120                                         <<", data="<<*recvdata
1121                                         <<std::endl;
1122                         assert(size == data2.getSize());
1123                         assert(memcmp(*data2, *recvdata, data2.getSize()) == 0);
1124                         assert(peer_id == PEER_ID_SERVER);
1125                         
1126                         bool got_exception = false;
1127                         try
1128                         {
1129                                 infostream<<"** running client.Receive()"<<std::endl;
1130                                 peer_id = 132;
1131                                 size = client.Receive(peer_id, recvdata);
1132                                 infostream<<"** Client received: peer_id="<<peer_id
1133                                                 <<", size="<<size
1134                                                 <<", data="<<*recvdata
1135                                                 <<std::endl;
1136                         }
1137                         catch(con::NoIncomingDataException &e)
1138                         {
1139                                 infostream<<"** No incoming data for client"<<std::endl;
1140                                 got_exception = true;
1141                         }
1142                         assert(got_exception);
1143                 }
1144 #endif
1145 #if 0
1146                 /*
1147                         Send large amounts of packets (infinite test)
1148                         Commented out because of infinity.
1149                 */
1150                 {
1151                         infostream<<"Sending large amounts of packets (infinite test)"<<std::endl;
1152                         int sendcount = 0;
1153                         for(;;){
1154                                 int datasize = myrand_range(0,5)==0?myrand_range(100,10000):myrand_range(0,100);
1155                                 infostream<<"datasize="<<datasize<<std::endl;
1156                                 SharedBuffer<u8> data1(datasize);
1157                                 for(u16 i=0; i<datasize; i++)
1158                                         data1[i] = i/4;
1159                                 
1160                                 int sendtimes = myrand_range(1,10);
1161                                 for(int i=0; i<sendtimes; i++){
1162                                         server.Send(peer_id_client, 0, data1, true);
1163                                         sendcount++;
1164                                 }
1165                                 infostream<<"sendcount="<<sendcount<<std::endl;
1166                                 
1167                                 //int receivetimes = myrand_range(1,20);
1168                                 int receivetimes = 20;
1169                                 for(int i=0; i<receivetimes; i++){
1170                                         SharedBuffer<u8> recvdata;
1171                                         u16 peer_id = 132;
1172                                         u16 size = 0;
1173                                         bool received = false;
1174                                         try{
1175                                                 size = client.Receive(peer_id, recvdata);
1176                                                 received = true;
1177                                         }catch(con::NoIncomingDataException &e){
1178                                         }
1179                                 }
1180                         }
1181                 }
1182 #endif
1183                 /*
1184                         Send a large packet
1185                 */
1186                 {
1187                         const int datasize = 30000;
1188                         SharedBuffer<u8> data1(datasize);
1189                         for(u16 i=0; i<datasize; i++){
1190                                 data1[i] = i/4;
1191                         }
1192
1193                         infostream<<"Sending data (size="<<datasize<<"):";
1194                         for(int i=0; i<datasize && i<20; i++){
1195                                 if(i%2==0) DEBUGPRINT(" ");
1196                                 DEBUGPRINT("%.2X", ((int)((const char*)*data1)[i])&0xff);
1197                         }
1198                         if(datasize>20)
1199                                 infostream<<"...";
1200                         infostream<<std::endl;
1201                         
1202                         server.Send(peer_id_client, 0, data1, true);
1203
1204                         sleep_ms(3000);
1205                         
1206                         SharedBuffer<u8> recvdata;
1207                         infostream<<"** running client.Receive()"<<std::endl;
1208                         u16 peer_id = 132;
1209                         u16 size = 0;
1210                         bool received = false;
1211                         u32 timems0 = porting::getTimeMs();
1212                         for(;;){
1213                                 if(porting::getTimeMs() - timems0 > 5000)
1214                                         break;
1215                                 try{
1216                                         size = client.Receive(peer_id, recvdata);
1217                                         received = true;
1218                                 }catch(con::NoIncomingDataException &e){
1219                                 }
1220                                 sleep_ms(10);
1221                         }
1222                         assert(received);
1223                         infostream<<"** Client received: peer_id="<<peer_id
1224                                         <<", size="<<size
1225                                         <<std::endl;
1226
1227                         infostream<<"Received data (size="<<size<<"):";
1228                         for(int i=0; i<size && i<20; i++){
1229                                 if(i%2==0) DEBUGPRINT(" ");
1230                                 DEBUGPRINT("%.2X", ((int)(recvdata[i]))&0xff);
1231                         }
1232                         if(size>20)
1233                                 infostream<<"...";
1234                         infostream<<std::endl;
1235
1236                         assert(memcmp(*data1, *recvdata, data1.getSize()) == 0);
1237                         assert(peer_id == PEER_ID_SERVER);
1238                 }
1239                 
1240                 // Check peer handlers
1241                 assert(hand_client.count == 1);
1242                 assert(hand_client.last_id == 1);
1243                 assert(hand_server.count == 1);
1244                 assert(hand_server.last_id == 2);
1245                 
1246                 //assert(0);
1247         }
1248 };
1249
1250 #define TEST(X)\
1251 {\
1252         X x;\
1253         infostream<<"Running " #X <<std::endl;\
1254         x.Run();\
1255 }
1256
1257 #define TESTPARAMS(X, ...)\
1258 {\
1259         X x;\
1260         infostream<<"Running " #X <<std::endl;\
1261         x.Run(__VA_ARGS__);\
1262 }
1263
1264 void run_tests()
1265 {
1266         DSTACK(__FUNCTION_NAME);
1267         
1268         // Create item and node definitions
1269         IWritableItemDefManager *idef = createItemDefManager();
1270         IWritableNodeDefManager *ndef = createNodeDefManager();
1271         define_some_nodes(idef, ndef);
1272
1273         infostream<<"run_tests() started"<<std::endl;
1274         TEST(TestUtilities);
1275         TEST(TestSettings);
1276         TEST(TestCompress);
1277         TEST(TestSerialization);
1278         TESTPARAMS(TestMapNode, ndef);
1279         TESTPARAMS(TestVoxelManipulator, ndef);
1280         //TEST(TestMapBlock);
1281         //TEST(TestMapSector);
1282         if(INTERNET_SIMULATOR == false){
1283                 TEST(TestSocket);
1284                 dout_con<<"=== BEGIN RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
1285                 TEST(TestConnection);
1286                 dout_con<<"=== END RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
1287         }
1288         infostream<<"run_tests() passed"<<std::endl;
1289 }
1290