Update Copyright Years
[oweals/minetest.git] / src / test.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 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 Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser 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 "irrlichttypes_extrabloated.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 "serialization.h"
29 #include "voxel.h"
30 #include "collision.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 #include "util/string.h"
39 #include "voxelalgorithms.h"
40 #include "inventory.h"
41 #include "util/numeric.h"
42 #include "util/serialize.h"
43 #include "noise.h" // PseudoRandom used for random data for compression
44 #include "clientserver.h" // LATEST_PROTOCOL_VERSION
45
46 /*
47         Asserts that the exception occurs
48 */
49 #define EXCEPTION_CHECK(EType, code)\
50 {\
51         bool exception_thrown = false;\
52         try{ code; }\
53         catch(EType &e) { exception_thrown = true; }\
54         UASSERT(exception_thrown);\
55 }
56
57 #define UTEST(x, fmt, ...)\
58 {\
59         if(!(x)){\
60                 LOGLINEF(LMT_ERROR, "Test (%s) failed: " fmt, #x, ##__VA_ARGS__);\
61                 test_failed = true;\
62         }\
63 }
64
65 #define UASSERT(x) UTEST(x, "UASSERT")
66
67 /*
68         A few item and node definitions for those tests that need them
69 */
70
71 #define CONTENT_STONE 0
72 #define CONTENT_GRASS 0x800
73 #define CONTENT_TORCH 100
74
75 void define_some_nodes(IWritableItemDefManager *idef, IWritableNodeDefManager *ndef)
76 {
77         content_t i;
78         ItemDefinition itemdef;
79         ContentFeatures f;
80
81         /*
82                 Stone
83         */
84         i = CONTENT_STONE;
85         itemdef = ItemDefinition();
86         itemdef.type = ITEM_NODE;
87         itemdef.name = "default:stone";
88         itemdef.description = "Stone";
89         itemdef.groups["cracky"] = 3;
90         itemdef.inventory_image = "[inventorycube"
91                 "{default_stone.png"
92                 "{default_stone.png"
93                 "{default_stone.png";
94         f = ContentFeatures();
95         f.name = itemdef.name;
96         for(int i = 0; i < 6; i++)
97                 f.tiledef[i].name = "default_stone.png";
98         f.is_ground_content = true;
99         idef->registerItem(itemdef);
100         ndef->set(i, f);
101
102         /*
103                 Grass
104         */
105         i = CONTENT_GRASS;
106         itemdef = ItemDefinition();
107         itemdef.type = ITEM_NODE;
108         itemdef.name = "default:dirt_with_grass";
109         itemdef.description = "Dirt with grass";
110         itemdef.groups["crumbly"] = 3;
111         itemdef.inventory_image = "[inventorycube"
112                 "{default_grass.png"
113                 "{default_dirt.png&default_grass_side.png"
114                 "{default_dirt.png&default_grass_side.png";
115         f = ContentFeatures();
116         f.name = itemdef.name;
117         f.tiledef[0].name = "default_grass.png";
118         f.tiledef[1].name = "default_dirt.png";
119         for(int i = 2; i < 6; i++)
120                 f.tiledef[i].name = "default_dirt.png^default_grass_side.png";
121         f.is_ground_content = true;
122         idef->registerItem(itemdef);
123         ndef->set(i, f);
124
125         /*
126                 Torch (minimal definition for lighting tests)
127         */
128         i = CONTENT_TORCH;
129         itemdef = ItemDefinition();
130         itemdef.type = ITEM_NODE;
131         itemdef.name = "default:torch";
132         f = ContentFeatures();
133         f.name = itemdef.name;
134         f.param_type = CPT_LIGHT;
135         f.light_propagates = true;
136         f.sunlight_propagates = true;
137         f.light_source = LIGHT_MAX-1;
138         idef->registerItem(itemdef);
139         ndef->set(i, f);
140 }
141
142 struct TestBase
143 {
144         bool test_failed;
145         TestBase():
146                 test_failed(false)
147         {}
148 };
149
150 struct TestUtilities: public TestBase
151 {
152         void Run()
153         {
154                 /*infostream<<"wrapDegrees(100.0) = "<<wrapDegrees(100.0)<<std::endl;
155                 infostream<<"wrapDegrees(720.5) = "<<wrapDegrees(720.5)<<std::endl;
156                 infostream<<"wrapDegrees(-0.5) = "<<wrapDegrees(-0.5)<<std::endl;*/
157                 UASSERT(fabs(wrapDegrees(100.0) - 100.0) < 0.001);
158                 UASSERT(fabs(wrapDegrees(720.5) - 0.5) < 0.001);
159                 UASSERT(fabs(wrapDegrees(-0.5) - (-0.5)) < 0.001);
160                 UASSERT(fabs(wrapDegrees(-365.5) - (-5.5)) < 0.001);
161                 UASSERT(lowercase("Foo bAR") == "foo bar");
162                 UASSERT(is_yes("YeS") == true);
163                 UASSERT(is_yes("") == false);
164                 UASSERT(is_yes("FAlse") == false);
165                 const char *ends[] = {"abc", "c", "bc", NULL};
166                 UASSERT(removeStringEnd("abc", ends) == "");
167                 UASSERT(removeStringEnd("bc", ends) == "b");
168                 UASSERT(removeStringEnd("12c", ends) == "12");
169                 UASSERT(removeStringEnd("foo", ends) == "");
170         }
171 };
172
173 struct TestSettings: public TestBase
174 {
175         void Run()
176         {
177                 Settings s;
178                 // Test reading of settings
179                 s.parseConfigLine("leet = 1337");
180                 s.parseConfigLine("leetleet = 13371337");
181                 s.parseConfigLine("leetleet_neg = -13371337");
182                 s.parseConfigLine("floaty_thing = 1.1");
183                 s.parseConfigLine("stringy_thing = asd /( ¤%&(/\" BLÖÄRP");
184                 s.parseConfigLine("coord = (1, 2, 4.5)");
185                 UASSERT(s.getS32("leet") == 1337);
186                 UASSERT(s.getS16("leetleet") == 32767);
187                 UASSERT(s.getS16("leetleet_neg") == -32768);
188                 // Not sure if 1.1 is an exact value as a float, but doesn't matter
189                 UASSERT(fabs(s.getFloat("floaty_thing") - 1.1) < 0.001);
190                 UASSERT(s.get("stringy_thing") == "asd /( ¤%&(/\" BLÖÄRP");
191                 UASSERT(fabs(s.getV3F("coord").X - 1.0) < 0.001);
192                 UASSERT(fabs(s.getV3F("coord").Y - 2.0) < 0.001);
193                 UASSERT(fabs(s.getV3F("coord").Z - 4.5) < 0.001);
194                 // Test the setting of settings too
195                 s.setFloat("floaty_thing_2", 1.2);
196                 s.setV3F("coord2", v3f(1, 2, 3.3));
197                 UASSERT(s.get("floaty_thing_2").substr(0,3) == "1.2");
198                 UASSERT(fabs(s.getFloat("floaty_thing_2") - 1.2) < 0.001);
199                 UASSERT(fabs(s.getV3F("coord2").X - 1.0) < 0.001);
200                 UASSERT(fabs(s.getV3F("coord2").Y - 2.0) < 0.001);
201                 UASSERT(fabs(s.getV3F("coord2").Z - 3.3) < 0.001);
202         }
203 };
204
205 struct TestSerialization: public TestBase
206 {
207         // To be used like this:
208         //   mkstr("Some\0string\0with\0embedded\0nuls")
209         // since std::string("...") doesn't work as expected in that case.
210         template<size_t N> std::string mkstr(const char (&s)[N])
211         {
212                 return std::string(s, N - 1);
213         }
214
215         void Run()
216         {
217                 // Tests some serialization primitives
218
219                 UASSERT(serializeString("") == mkstr("\0\0"));
220                 UASSERT(serializeWideString(L"") == mkstr("\0\0"));
221                 UASSERT(serializeLongString("") == mkstr("\0\0\0\0"));
222                 UASSERT(serializeJsonString("") == "\"\"");
223                 
224                 std::string teststring = "Hello world!";
225                 UASSERT(serializeString(teststring) ==
226                         mkstr("\0\14Hello world!"));
227                 UASSERT(serializeWideString(narrow_to_wide(teststring)) ==
228                         mkstr("\0\14\0H\0e\0l\0l\0o\0 \0w\0o\0r\0l\0d\0!"));
229                 UASSERT(serializeLongString(teststring) ==
230                         mkstr("\0\0\0\14Hello world!"));
231                 UASSERT(serializeJsonString(teststring) ==
232                         "\"Hello world!\"");
233
234                 std::string teststring2;
235                 std::wstring teststring2_w;
236                 std::string teststring2_w_encoded;
237                 {
238                         std::ostringstream tmp_os;
239                         std::wostringstream tmp_os_w;
240                         std::ostringstream tmp_os_w_encoded;
241                         for(int i = 0; i < 256; i++)
242                         {
243                                 tmp_os<<(char)i;
244                                 tmp_os_w<<(wchar_t)i;
245                                 tmp_os_w_encoded<<(char)0<<(char)i;
246                         }
247                         teststring2 = tmp_os.str();
248                         teststring2_w = tmp_os_w.str();
249                         teststring2_w_encoded = tmp_os_w_encoded.str();
250                 }
251                 UASSERT(serializeString(teststring2) ==
252                         mkstr("\1\0") + teststring2);
253                 UASSERT(serializeWideString(teststring2_w) ==
254                         mkstr("\1\0") + teststring2_w_encoded);
255                 UASSERT(serializeLongString(teststring2) ==
256                         mkstr("\0\0\1\0") + teststring2);
257                 // MSVC fails when directly using "\\\\"
258                 std::string backslash = "\\";
259                 UASSERT(serializeJsonString(teststring2) ==
260                         mkstr("\"") +
261                         "\\u0000\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007" +
262                         "\\b\\t\\n\\u000b\\f\\r\\u000e\\u000f" +
263                         "\\u0010\\u0011\\u0012\\u0013\\u0014\\u0015\\u0016\\u0017" +
264                         "\\u0018\\u0019\\u001a\\u001b\\u001c\\u001d\\u001e\\u001f" +
265                         " !\\\"" + teststring2.substr(0x23, 0x2f-0x23) +
266                         "\\/" + teststring2.substr(0x30, 0x5c-0x30) +
267                         backslash + backslash + teststring2.substr(0x5d, 0x7f-0x5d) + "\\u007f" +
268                         "\\u0080\\u0081\\u0082\\u0083\\u0084\\u0085\\u0086\\u0087" +
269                         "\\u0088\\u0089\\u008a\\u008b\\u008c\\u008d\\u008e\\u008f" +
270                         "\\u0090\\u0091\\u0092\\u0093\\u0094\\u0095\\u0096\\u0097" +
271                         "\\u0098\\u0099\\u009a\\u009b\\u009c\\u009d\\u009e\\u009f" +
272                         "\\u00a0\\u00a1\\u00a2\\u00a3\\u00a4\\u00a5\\u00a6\\u00a7" +
273                         "\\u00a8\\u00a9\\u00aa\\u00ab\\u00ac\\u00ad\\u00ae\\u00af" +
274                         "\\u00b0\\u00b1\\u00b2\\u00b3\\u00b4\\u00b5\\u00b6\\u00b7" +
275                         "\\u00b8\\u00b9\\u00ba\\u00bb\\u00bc\\u00bd\\u00be\\u00bf" +
276                         "\\u00c0\\u00c1\\u00c2\\u00c3\\u00c4\\u00c5\\u00c6\\u00c7" +
277                         "\\u00c8\\u00c9\\u00ca\\u00cb\\u00cc\\u00cd\\u00ce\\u00cf" +
278                         "\\u00d0\\u00d1\\u00d2\\u00d3\\u00d4\\u00d5\\u00d6\\u00d7" +
279                         "\\u00d8\\u00d9\\u00da\\u00db\\u00dc\\u00dd\\u00de\\u00df" +
280                         "\\u00e0\\u00e1\\u00e2\\u00e3\\u00e4\\u00e5\\u00e6\\u00e7" +
281                         "\\u00e8\\u00e9\\u00ea\\u00eb\\u00ec\\u00ed\\u00ee\\u00ef" +
282                         "\\u00f0\\u00f1\\u00f2\\u00f3\\u00f4\\u00f5\\u00f6\\u00f7" +
283                         "\\u00f8\\u00f9\\u00fa\\u00fb\\u00fc\\u00fd\\u00fe\\u00ff" +
284                         "\"");
285
286                 {
287                         std::istringstream is(serializeString(teststring2), std::ios::binary);
288                         UASSERT(deSerializeString(is) == teststring2);
289                         UASSERT(!is.eof());
290                         is.get();
291                         UASSERT(is.eof());
292                 }
293                 {
294                         std::istringstream is(serializeWideString(teststring2_w), std::ios::binary);
295                         UASSERT(deSerializeWideString(is) == teststring2_w);
296                         UASSERT(!is.eof());
297                         is.get();
298                         UASSERT(is.eof());
299                 }
300                 {
301                         std::istringstream is(serializeLongString(teststring2), std::ios::binary);
302                         UASSERT(deSerializeLongString(is) == teststring2);
303                         UASSERT(!is.eof());
304                         is.get();
305                         UASSERT(is.eof());
306                 }
307                 {
308                         std::istringstream is(serializeJsonString(teststring2), std::ios::binary);
309                         //dstream<<serializeJsonString(deSerializeJsonString(is));
310                         UASSERT(deSerializeJsonString(is) == teststring2);
311                         UASSERT(!is.eof());
312                         is.get();
313                         UASSERT(is.eof());
314                 }
315         }
316 };
317
318 struct TestNodedefSerialization: public TestBase
319 {
320         void Run()
321         {
322                 ContentFeatures f;
323                 f.name = "default:stone";
324                 for(int i = 0; i < 6; i++)
325                         f.tiledef[i].name = "default_stone.png";
326                 f.is_ground_content = true;
327                 std::ostringstream os(std::ios::binary);
328                 f.serialize(os, LATEST_PROTOCOL_VERSION);
329                 verbosestream<<"Test ContentFeatures size: "<<os.str().size()<<std::endl;
330                 std::istringstream is(os.str(), std::ios::binary);
331                 ContentFeatures f2;
332                 f2.deSerialize(is);
333                 UASSERT(f.walkable == f2.walkable);
334                 UASSERT(f.node_box.type == f2.node_box.type);
335         }
336 };
337
338 struct TestCompress: public TestBase
339 {
340         void Run()
341         {
342                 { // ver 0
343
344                 SharedBuffer<u8> fromdata(4);
345                 fromdata[0]=1;
346                 fromdata[1]=5;
347                 fromdata[2]=5;
348                 fromdata[3]=1;
349                 
350                 std::ostringstream os(std::ios_base::binary);
351                 compress(fromdata, os, 0);
352
353                 std::string str_out = os.str();
354                 
355                 infostream<<"str_out.size()="<<str_out.size()<<std::endl;
356                 infostream<<"TestCompress: 1,5,5,1 -> ";
357                 for(u32 i=0; i<str_out.size(); i++)
358                 {
359                         infostream<<(u32)str_out[i]<<",";
360                 }
361                 infostream<<std::endl;
362
363                 UASSERT(str_out.size() == 10);
364
365                 UASSERT(str_out[0] == 0);
366                 UASSERT(str_out[1] == 0);
367                 UASSERT(str_out[2] == 0);
368                 UASSERT(str_out[3] == 4);
369                 UASSERT(str_out[4] == 0);
370                 UASSERT(str_out[5] == 1);
371                 UASSERT(str_out[6] == 1);
372                 UASSERT(str_out[7] == 5);
373                 UASSERT(str_out[8] == 0);
374                 UASSERT(str_out[9] == 1);
375
376                 std::istringstream is(str_out, std::ios_base::binary);
377                 std::ostringstream os2(std::ios_base::binary);
378
379                 decompress(is, os2, 0);
380                 std::string str_out2 = os2.str();
381
382                 infostream<<"decompress: ";
383                 for(u32 i=0; i<str_out2.size(); i++)
384                 {
385                         infostream<<(u32)str_out2[i]<<",";
386                 }
387                 infostream<<std::endl;
388
389                 UASSERT(str_out2.size() == fromdata.getSize());
390
391                 for(u32 i=0; i<str_out2.size(); i++)
392                 {
393                         UASSERT(str_out2[i] == fromdata[i]);
394                 }
395
396                 }
397
398                 { // ver HIGHEST
399
400                 SharedBuffer<u8> fromdata(4);
401                 fromdata[0]=1;
402                 fromdata[1]=5;
403                 fromdata[2]=5;
404                 fromdata[3]=1;
405                 
406                 std::ostringstream os(std::ios_base::binary);
407                 compress(fromdata, os, SER_FMT_VER_HIGHEST);
408
409                 std::string str_out = os.str();
410                 
411                 infostream<<"str_out.size()="<<str_out.size()<<std::endl;
412                 infostream<<"TestCompress: 1,5,5,1 -> ";
413                 for(u32 i=0; i<str_out.size(); i++)
414                 {
415                         infostream<<(u32)str_out[i]<<",";
416                 }
417                 infostream<<std::endl;
418
419                 std::istringstream is(str_out, std::ios_base::binary);
420                 std::ostringstream os2(std::ios_base::binary);
421
422                 decompress(is, os2, SER_FMT_VER_HIGHEST);
423                 std::string str_out2 = os2.str();
424
425                 infostream<<"decompress: ";
426                 for(u32 i=0; i<str_out2.size(); i++)
427                 {
428                         infostream<<(u32)str_out2[i]<<",";
429                 }
430                 infostream<<std::endl;
431
432                 UASSERT(str_out2.size() == fromdata.getSize());
433
434                 for(u32 i=0; i<str_out2.size(); i++)
435                 {
436                         UASSERT(str_out2[i] == fromdata[i]);
437                 }
438
439                 }
440
441                 // Test zlib wrapper with large amounts of data (larger than its
442                 // internal buffers)
443                 {
444                         infostream<<"Test: Testing zlib wrappers with a large amount "
445                                         <<"of pseudorandom data"<<std::endl;
446                         u32 size = 50000;
447                         infostream<<"Test: Input size of large compressZlib is "
448                                         <<size<<std::endl;
449                         std::string data_in;
450                         data_in.resize(size);
451                         PseudoRandom pseudorandom(9420);
452                         for(u32 i=0; i<size; i++)
453                                 data_in[i] = pseudorandom.range(0,255);
454                         std::ostringstream os_compressed(std::ios::binary);
455                         compressZlib(data_in, os_compressed);
456                         infostream<<"Test: Output size of large compressZlib is "
457                                         <<os_compressed.str().size()<<std::endl;
458                         std::istringstream is_compressed(os_compressed.str(), std::ios::binary);
459                         std::ostringstream os_decompressed(std::ios::binary);
460                         decompressZlib(is_compressed, os_decompressed);
461                         infostream<<"Test: Output size of large decompressZlib is "
462                                         <<os_decompressed.str().size()<<std::endl;
463                         std::string str_decompressed = os_decompressed.str();
464                         UTEST(str_decompressed.size() == data_in.size(), "Output size not"
465                                         " equal (output: %i, input: %i)",
466                                         str_decompressed.size(), data_in.size());
467                         for(u32 i=0; i<size && i<str_decompressed.size(); i++){
468                                 UTEST(str_decompressed[i] == data_in[i],
469                                                 "index out[%i]=%i differs from in[%i]=%i",
470                                                 i, str_decompressed[i], i, data_in[i]);
471                         }
472                 }
473         }
474 };
475
476 struct TestMapNode: public TestBase
477 {
478         void Run(INodeDefManager *nodedef)
479         {
480                 MapNode n;
481
482                 // Default values
483                 UASSERT(n.getContent() == CONTENT_AIR);
484                 UASSERT(n.getLight(LIGHTBANK_DAY, nodedef) == 0);
485                 UASSERT(n.getLight(LIGHTBANK_NIGHT, nodedef) == 0);
486                 
487                 // Transparency
488                 n.setContent(CONTENT_AIR);
489                 UASSERT(nodedef->get(n).light_propagates == true);
490                 n.setContent(LEGN(nodedef, "CONTENT_STONE"));
491                 UASSERT(nodedef->get(n).light_propagates == false);
492         }
493 };
494
495 struct TestVoxelManipulator: public TestBase
496 {
497         void Run(INodeDefManager *nodedef)
498         {
499                 /*
500                         VoxelArea
501                 */
502
503                 VoxelArea a(v3s16(-1,-1,-1), v3s16(1,1,1));
504                 UASSERT(a.index(0,0,0) == 1*3*3 + 1*3 + 1);
505                 UASSERT(a.index(-1,-1,-1) == 0);
506                 
507                 VoxelArea c(v3s16(-2,-2,-2), v3s16(2,2,2));
508                 // An area that is 1 bigger in x+ and z-
509                 VoxelArea d(v3s16(-2,-2,-3), v3s16(3,2,2));
510                 
511                 core::list<VoxelArea> aa;
512                 d.diff(c, aa);
513                 
514                 // Correct results
515                 core::array<VoxelArea> results;
516                 results.push_back(VoxelArea(v3s16(-2,-2,-3),v3s16(3,2,-3)));
517                 results.push_back(VoxelArea(v3s16(3,-2,-2),v3s16(3,2,2)));
518
519                 UASSERT(aa.size() == results.size());
520                 
521                 infostream<<"Result of diff:"<<std::endl;
522                 for(core::list<VoxelArea>::Iterator
523                                 i = aa.begin(); i != aa.end(); i++)
524                 {
525                         i->print(infostream);
526                         infostream<<std::endl;
527                         
528                         s32 j = results.linear_search(*i);
529                         UASSERT(j != -1);
530                         results.erase(j, 1);
531                 }
532
533
534                 /*
535                         VoxelManipulator
536                 */
537                 
538                 VoxelManipulator v;
539
540                 v.print(infostream, nodedef);
541
542                 infostream<<"*** Setting (-1,0,-1)=2 ***"<<std::endl;
543                 
544                 v.setNodeNoRef(v3s16(-1,0,-1), MapNode(CONTENT_GRASS));
545
546                 v.print(infostream, nodedef);
547
548                 UASSERT(v.getNode(v3s16(-1,0,-1)).getContent() == CONTENT_GRASS);
549
550                 infostream<<"*** Reading from inexistent (0,0,-1) ***"<<std::endl;
551
552                 EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,0,-1)));
553
554                 v.print(infostream, nodedef);
555
556                 infostream<<"*** Adding area ***"<<std::endl;
557
558                 v.addArea(a);
559                 
560                 v.print(infostream, nodedef);
561
562                 UASSERT(v.getNode(v3s16(-1,0,-1)).getContent() == CONTENT_GRASS);
563                 EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,1,1)));
564         }
565 };
566
567 struct TestVoxelAlgorithms: public TestBase
568 {
569         void Run(INodeDefManager *ndef)
570         {
571                 /*
572                         voxalgo::propagateSunlight
573                 */
574                 {
575                         VoxelManipulator v;
576                         for(u16 z=0; z<3; z++)
577                         for(u16 y=0; y<3; y++)
578                         for(u16 x=0; x<3; x++)
579                         {
580                                 v3s16 p(x,y,z);
581                                 v.setNodeNoRef(p, MapNode(CONTENT_AIR));
582                         }
583                         VoxelArea a(v3s16(0,0,0), v3s16(2,2,2));
584                         {
585                                 core::map<v3s16, bool> light_sources;
586                                 voxalgo::setLight(v, a, 0, ndef);
587                                 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
588                                                 v, a, true, light_sources, ndef);
589                                 //v.print(dstream, ndef, VOXELPRINT_LIGHT_DAY);
590                                 UASSERT(res.bottom_sunlight_valid == true);
591                                 UASSERT(v.getNode(v3s16(1,1,1)).getLight(LIGHTBANK_DAY, ndef)
592                                                 == LIGHT_SUN);
593                         }
594                         v.setNodeNoRef(v3s16(0,0,0), MapNode(CONTENT_STONE));
595                         {
596                                 core::map<v3s16, bool> light_sources;
597                                 voxalgo::setLight(v, a, 0, ndef);
598                                 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
599                                                 v, a, true, light_sources, ndef);
600                                 UASSERT(res.bottom_sunlight_valid == true);
601                                 UASSERT(v.getNode(v3s16(1,1,1)).getLight(LIGHTBANK_DAY, ndef)
602                                                 == LIGHT_SUN);
603                         }
604                         {
605                                 core::map<v3s16, bool> light_sources;
606                                 voxalgo::setLight(v, a, 0, ndef);
607                                 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
608                                                 v, a, false, light_sources, ndef);
609                                 UASSERT(res.bottom_sunlight_valid == true);
610                                 UASSERT(v.getNode(v3s16(2,0,2)).getLight(LIGHTBANK_DAY, ndef)
611                                                 == 0);
612                         }
613                         v.setNodeNoRef(v3s16(1,3,2), MapNode(CONTENT_STONE));
614                         {
615                                 core::map<v3s16, bool> light_sources;
616                                 voxalgo::setLight(v, a, 0, ndef);
617                                 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
618                                                 v, a, true, light_sources, ndef);
619                                 UASSERT(res.bottom_sunlight_valid == true);
620                                 UASSERT(v.getNode(v3s16(1,1,2)).getLight(LIGHTBANK_DAY, ndef)
621                                                 == 0);
622                         }
623                         {
624                                 core::map<v3s16, bool> light_sources;
625                                 voxalgo::setLight(v, a, 0, ndef);
626                                 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
627                                                 v, a, false, light_sources, ndef);
628                                 UASSERT(res.bottom_sunlight_valid == true);
629                                 UASSERT(v.getNode(v3s16(1,0,2)).getLight(LIGHTBANK_DAY, ndef)
630                                                 == 0);
631                         }
632                         {
633                                 MapNode n(CONTENT_AIR);
634                                 n.setLight(LIGHTBANK_DAY, 10, ndef);
635                                 v.setNodeNoRef(v3s16(1,-1,2), n);
636                         }
637                         {
638                                 core::map<v3s16, bool> light_sources;
639                                 voxalgo::setLight(v, a, 0, ndef);
640                                 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
641                                                 v, a, true, light_sources, ndef);
642                                 UASSERT(res.bottom_sunlight_valid == true);
643                         }
644                         {
645                                 core::map<v3s16, bool> light_sources;
646                                 voxalgo::setLight(v, a, 0, ndef);
647                                 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
648                                                 v, a, false, light_sources, ndef);
649                                 UASSERT(res.bottom_sunlight_valid == true);
650                         }
651                         {
652                                 MapNode n(CONTENT_AIR);
653                                 n.setLight(LIGHTBANK_DAY, LIGHT_SUN, ndef);
654                                 v.setNodeNoRef(v3s16(1,-1,2), n);
655                         }
656                         {
657                                 core::map<v3s16, bool> light_sources;
658                                 voxalgo::setLight(v, a, 0, ndef);
659                                 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
660                                                 v, a, true, light_sources, ndef);
661                                 UASSERT(res.bottom_sunlight_valid == false);
662                         }
663                         {
664                                 core::map<v3s16, bool> light_sources;
665                                 voxalgo::setLight(v, a, 0, ndef);
666                                 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
667                                                 v, a, false, light_sources, ndef);
668                                 UASSERT(res.bottom_sunlight_valid == false);
669                         }
670                         v.setNodeNoRef(v3s16(1,3,2), MapNode(CONTENT_IGNORE));
671                         {
672                                 core::map<v3s16, bool> light_sources;
673                                 voxalgo::setLight(v, a, 0, ndef);
674                                 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
675                                                 v, a, true, light_sources, ndef);
676                                 UASSERT(res.bottom_sunlight_valid == true);
677                         }
678                 }
679                 /*
680                         voxalgo::clearLightAndCollectSources
681                 */
682                 {
683                         VoxelManipulator v;
684                         for(u16 z=0; z<3; z++)
685                         for(u16 y=0; y<3; y++)
686                         for(u16 x=0; x<3; x++)
687                         {
688                                 v3s16 p(x,y,z);
689                                 v.setNode(p, MapNode(CONTENT_AIR));
690                         }
691                         VoxelArea a(v3s16(0,0,0), v3s16(2,2,2));
692                         v.setNodeNoRef(v3s16(0,0,0), MapNode(CONTENT_STONE));
693                         v.setNodeNoRef(v3s16(1,1,1), MapNode(CONTENT_TORCH));
694                         {
695                                 MapNode n(CONTENT_AIR);
696                                 n.setLight(LIGHTBANK_DAY, 1, ndef);
697                                 v.setNode(v3s16(1,1,2), n);
698                         }
699                         {
700                                 core::map<v3s16, bool> light_sources;
701                                 core::map<v3s16, u8> unlight_from;
702                                 voxalgo::clearLightAndCollectSources(v, a, LIGHTBANK_DAY,
703                                                 ndef, light_sources, unlight_from);
704                                 //v.print(dstream, ndef, VOXELPRINT_LIGHT_DAY);
705                                 UASSERT(v.getNode(v3s16(0,1,1)).getLight(LIGHTBANK_DAY, ndef)
706                                                 == 0);
707                                 UASSERT(light_sources.find(v3s16(1,1,1)) != NULL);
708                                 UASSERT(light_sources.size() == 1);
709                                 UASSERT(unlight_from.find(v3s16(1,1,2)) != NULL);
710                                 UASSERT(unlight_from.size() == 1);
711                         }
712                 }
713         }
714 };
715
716 struct TestInventory: public TestBase
717 {
718         void Run(IItemDefManager *idef)
719         {
720                 std::string serialized_inventory =
721                 "List 0 32\n"
722                 "Width 3\n"
723                 "Empty\n"
724                 "Empty\n"
725                 "Empty\n"
726                 "Empty\n"
727                 "Empty\n"
728                 "Empty\n"
729                 "Empty\n"
730                 "Empty\n"
731                 "Empty\n"
732                 "Item default:cobble 61\n"
733                 "Empty\n"
734                 "Empty\n"
735                 "Empty\n"
736                 "Empty\n"
737                 "Empty\n"
738                 "Empty\n"
739                 "Item default:dirt 71\n"
740                 "Empty\n"
741                 "Empty\n"
742                 "Empty\n"
743                 "Empty\n"
744                 "Empty\n"
745                 "Empty\n"
746                 "Empty\n"
747                 "Item default:dirt 99\n"
748                 "Item default:cobble 38\n"
749                 "Empty\n"
750                 "Empty\n"
751                 "Empty\n"
752                 "Empty\n"
753                 "Empty\n"
754                 "Empty\n"
755                 "EndInventoryList\n"
756                 "EndInventory\n";
757                 
758                 std::string serialized_inventory_2 =
759                 "List main 32\n"
760                 "Width 5\n"
761                 "Empty\n"
762                 "Empty\n"
763                 "Empty\n"
764                 "Empty\n"
765                 "Empty\n"
766                 "Empty\n"
767                 "Empty\n"
768                 "Empty\n"
769                 "Empty\n"
770                 "Item default:cobble 61\n"
771                 "Empty\n"
772                 "Empty\n"
773                 "Empty\n"
774                 "Empty\n"
775                 "Empty\n"
776                 "Empty\n"
777                 "Item default:dirt 71\n"
778                 "Empty\n"
779                 "Empty\n"
780                 "Empty\n"
781                 "Empty\n"
782                 "Empty\n"
783                 "Empty\n"
784                 "Empty\n"
785                 "Item default:dirt 99\n"
786                 "Item default:cobble 38\n"
787                 "Empty\n"
788                 "Empty\n"
789                 "Empty\n"
790                 "Empty\n"
791                 "Empty\n"
792                 "Empty\n"
793                 "EndInventoryList\n"
794                 "EndInventory\n";
795                 
796                 Inventory inv(idef);
797                 std::istringstream is(serialized_inventory, std::ios::binary);
798                 inv.deSerialize(is);
799                 UASSERT(inv.getList("0"));
800                 UASSERT(!inv.getList("main"));
801                 inv.getList("0")->setName("main");
802                 UASSERT(!inv.getList("0"));
803                 UASSERT(inv.getList("main"));
804                 UASSERT(inv.getList("main")->getWidth() == 3);
805                 inv.getList("main")->setWidth(5);
806                 std::ostringstream inv_os(std::ios::binary);
807                 inv.serialize(inv_os);
808                 UASSERT(inv_os.str() == serialized_inventory_2);
809         }
810 };
811
812 /*
813         NOTE: These tests became non-working then NodeContainer was removed.
814               These should be redone, utilizing some kind of a virtual
815                   interface for Map (IMap would be fine).
816 */
817 #if 0
818 struct TestMapBlock: public TestBase
819 {
820         class TC : public NodeContainer
821         {
822         public:
823
824                 MapNode node;
825                 bool position_valid;
826                 core::list<v3s16> validity_exceptions;
827
828                 TC()
829                 {
830                         position_valid = true;
831                 }
832
833                 virtual bool isValidPosition(v3s16 p)
834                 {
835                         //return position_valid ^ (p==position_valid_exception);
836                         bool exception = false;
837                         for(core::list<v3s16>::Iterator i=validity_exceptions.begin();
838                                         i != validity_exceptions.end(); i++)
839                         {
840                                 if(p == *i)
841                                 {
842                                         exception = true;
843                                         break;
844                                 }
845                         }
846                         return exception ? !position_valid : position_valid;
847                 }
848
849                 virtual MapNode getNode(v3s16 p)
850                 {
851                         if(isValidPosition(p) == false)
852                                 throw InvalidPositionException();
853                         return node;
854                 }
855
856                 virtual void setNode(v3s16 p, MapNode & n)
857                 {
858                         if(isValidPosition(p) == false)
859                                 throw InvalidPositionException();
860                 };
861
862                 virtual u16 nodeContainerId() const
863                 {
864                         return 666;
865                 }
866         };
867
868         void Run()
869         {
870                 TC parent;
871                 
872                 MapBlock b(&parent, v3s16(1,1,1));
873                 v3s16 relpos(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
874
875                 UASSERT(b.getPosRelative() == relpos);
876
877                 UASSERT(b.getBox().MinEdge.X == MAP_BLOCKSIZE);
878                 UASSERT(b.getBox().MaxEdge.X == MAP_BLOCKSIZE*2-1);
879                 UASSERT(b.getBox().MinEdge.Y == MAP_BLOCKSIZE);
880                 UASSERT(b.getBox().MaxEdge.Y == MAP_BLOCKSIZE*2-1);
881                 UASSERT(b.getBox().MinEdge.Z == MAP_BLOCKSIZE);
882                 UASSERT(b.getBox().MaxEdge.Z == MAP_BLOCKSIZE*2-1);
883                 
884                 UASSERT(b.isValidPosition(v3s16(0,0,0)) == true);
885                 UASSERT(b.isValidPosition(v3s16(-1,0,0)) == false);
886                 UASSERT(b.isValidPosition(v3s16(-1,-142,-2341)) == false);
887                 UASSERT(b.isValidPosition(v3s16(-124,142,2341)) == false);
888                 UASSERT(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
889                 UASSERT(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE,MAP_BLOCKSIZE-1)) == false);
890
891                 /*
892                         TODO: this method should probably be removed
893                         if the block size isn't going to be set variable
894                 */
895                 /*UASSERT(b.getSizeNodes() == v3s16(MAP_BLOCKSIZE,
896                                 MAP_BLOCKSIZE, MAP_BLOCKSIZE));*/
897                 
898                 // Changed flag should be initially set
899                 UASSERT(b.getModified() == MOD_STATE_WRITE_NEEDED);
900                 b.resetModified();
901                 UASSERT(b.getModified() == MOD_STATE_CLEAN);
902
903                 // All nodes should have been set to
904                 // .d=CONTENT_IGNORE and .getLight() = 0
905                 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
906                 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
907                 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
908                 {
909                         //UASSERT(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_AIR);
910                         UASSERT(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_IGNORE);
911                         UASSERT(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_DAY) == 0);
912                         UASSERT(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_NIGHT) == 0);
913                 }
914                 
915                 {
916                         MapNode n(CONTENT_AIR);
917                         for(u16 z=0; z<MAP_BLOCKSIZE; z++)
918                         for(u16 y=0; y<MAP_BLOCKSIZE; y++)
919                         for(u16 x=0; x<MAP_BLOCKSIZE; x++)
920                         {
921                                 b.setNode(v3s16(x,y,z), n);
922                         }
923                 }
924                         
925                 /*
926                         Parent fetch functions
927                 */
928                 parent.position_valid = false;
929                 parent.node.setContent(5);
930
931                 MapNode n;
932                 
933                 // Positions in the block should still be valid
934                 UASSERT(b.isValidPositionParent(v3s16(0,0,0)) == true);
935                 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
936                 n = b.getNodeParent(v3s16(0,MAP_BLOCKSIZE-1,0));
937                 UASSERT(n.getContent() == CONTENT_AIR);
938
939                 // ...but outside the block they should be invalid
940                 UASSERT(b.isValidPositionParent(v3s16(-121,2341,0)) == false);
941                 UASSERT(b.isValidPositionParent(v3s16(-1,0,0)) == false);
942                 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == false);
943                 
944                 {
945                         bool exception_thrown = false;
946                         try{
947                                 // This should throw an exception
948                                 MapNode n = b.getNodeParent(v3s16(0,0,-1));
949                         }
950                         catch(InvalidPositionException &e)
951                         {
952                                 exception_thrown = true;
953                         }
954                         UASSERT(exception_thrown);
955                 }
956
957                 parent.position_valid = true;
958                 // Now the positions outside should be valid
959                 UASSERT(b.isValidPositionParent(v3s16(-121,2341,0)) == true);
960                 UASSERT(b.isValidPositionParent(v3s16(-1,0,0)) == true);
961                 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == true);
962                 n = b.getNodeParent(v3s16(0,0,MAP_BLOCKSIZE));
963                 UASSERT(n.getContent() == 5);
964
965                 /*
966                         Set a node
967                 */
968                 v3s16 p(1,2,0);
969                 n.setContent(4);
970                 b.setNode(p, n);
971                 UASSERT(b.getNode(p).getContent() == 4);
972                 //TODO: Update to new system
973                 /*UASSERT(b.getNodeTile(p) == 4);
974                 UASSERT(b.getNodeTile(v3s16(-1,-1,0)) == 5);*/
975                 
976                 /*
977                         propagateSunlight()
978                 */
979                 // Set lighting of all nodes to 0
980                 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
981                         for(u16 y=0; y<MAP_BLOCKSIZE; y++){
982                                 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
983                                         MapNode n = b.getNode(v3s16(x,y,z));
984                                         n.setLight(LIGHTBANK_DAY, 0);
985                                         n.setLight(LIGHTBANK_NIGHT, 0);
986                                         b.setNode(v3s16(x,y,z), n);
987                                 }
988                         }
989                 }
990                 {
991                         /*
992                                 Check how the block handles being a lonely sky block
993                         */
994                         parent.position_valid = true;
995                         b.setIsUnderground(false);
996                         parent.node.setContent(CONTENT_AIR);
997                         parent.node.setLight(LIGHTBANK_DAY, LIGHT_SUN);
998                         parent.node.setLight(LIGHTBANK_NIGHT, 0);
999                         core::map<v3s16, bool> light_sources;
1000                         // The bottom block is invalid, because we have a shadowing node
1001                         UASSERT(b.propagateSunlight(light_sources) == false);
1002                         UASSERT(b.getNode(v3s16(1,4,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
1003                         UASSERT(b.getNode(v3s16(1,3,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
1004                         UASSERT(b.getNode(v3s16(1,2,0)).getLight(LIGHTBANK_DAY) == 0);
1005                         UASSERT(b.getNode(v3s16(1,1,0)).getLight(LIGHTBANK_DAY) == 0);
1006                         UASSERT(b.getNode(v3s16(1,0,0)).getLight(LIGHTBANK_DAY) == 0);
1007                         UASSERT(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
1008                         UASSERT(b.getFaceLight2(1000, p, v3s16(0,1,0)) == LIGHT_SUN);
1009                         UASSERT(b.getFaceLight2(1000, p, v3s16(0,-1,0)) == 0);
1010                         UASSERT(b.getFaceLight2(0, p, v3s16(0,-1,0)) == 0);
1011                         // According to MapBlock::getFaceLight,
1012                         // The face on the z+ side should have double-diminished light
1013                         //UASSERT(b.getFaceLight(p, v3s16(0,0,1)) == diminish_light(diminish_light(LIGHT_MAX)));
1014                         // The face on the z+ side should have diminished light
1015                         UASSERT(b.getFaceLight2(1000, p, v3s16(0,0,1)) == diminish_light(LIGHT_MAX));
1016                 }
1017                 /*
1018                         Check how the block handles being in between blocks with some non-sunlight
1019                         while being underground
1020                 */
1021                 {
1022                         // Make neighbours to exist and set some non-sunlight to them
1023                         parent.position_valid = true;
1024                         b.setIsUnderground(true);
1025                         parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
1026                         core::map<v3s16, bool> light_sources;
1027                         // The block below should be valid because there shouldn't be
1028                         // sunlight in there either
1029                         UASSERT(b.propagateSunlight(light_sources, true) == true);
1030                         // Should not touch nodes that are not affected (that is, all of them)
1031                         //UASSERT(b.getNode(v3s16(1,2,3)).getLight() == LIGHT_SUN);
1032                         // Should set light of non-sunlighted blocks to 0.
1033                         UASSERT(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == 0);
1034                 }
1035                 /*
1036                         Set up a situation where:
1037                         - There is only air in this block
1038                         - There is a valid non-sunlighted block at the bottom, and
1039                         - Invalid blocks elsewhere.
1040                         - the block is not underground.
1041
1042                         This should result in bottom block invalidity
1043                 */
1044                 {
1045                         b.setIsUnderground(false);
1046                         // Clear block
1047                         for(u16 z=0; z<MAP_BLOCKSIZE; z++){
1048                                 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
1049                                         for(u16 x=0; x<MAP_BLOCKSIZE; x++){
1050                                                 MapNode n;
1051                                                 n.setContent(CONTENT_AIR);
1052                                                 n.setLight(LIGHTBANK_DAY, 0);
1053                                                 b.setNode(v3s16(x,y,z), n);
1054                                         }
1055                                 }
1056                         }
1057                         // Make neighbours invalid
1058                         parent.position_valid = false;
1059                         // Add exceptions to the top of the bottom block
1060                         for(u16 x=0; x<MAP_BLOCKSIZE; x++)
1061                         for(u16 z=0; z<MAP_BLOCKSIZE; z++)
1062                         {
1063                                 parent.validity_exceptions.push_back(v3s16(MAP_BLOCKSIZE+x, MAP_BLOCKSIZE-1, MAP_BLOCKSIZE+z));
1064                         }
1065                         // Lighting value for the valid nodes
1066                         parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
1067                         core::map<v3s16, bool> light_sources;
1068                         // Bottom block is not valid
1069                         UASSERT(b.propagateSunlight(light_sources) == false);
1070                 }
1071         }
1072 };
1073
1074 struct TestMapSector: public TestBase
1075 {
1076         class TC : public NodeContainer
1077         {
1078         public:
1079
1080                 MapNode node;
1081                 bool position_valid;
1082
1083                 TC()
1084                 {
1085                         position_valid = true;
1086                 }
1087
1088                 virtual bool isValidPosition(v3s16 p)
1089                 {
1090                         return position_valid;
1091                 }
1092
1093                 virtual MapNode getNode(v3s16 p)
1094                 {
1095                         if(position_valid == false)
1096                                 throw InvalidPositionException();
1097                         return node;
1098                 }
1099
1100                 virtual void setNode(v3s16 p, MapNode & n)
1101                 {
1102                         if(position_valid == false)
1103                                 throw InvalidPositionException();
1104                 };
1105                 
1106                 virtual u16 nodeContainerId() const
1107                 {
1108                         return 666;
1109                 }
1110         };
1111         
1112         void Run()
1113         {
1114                 TC parent;
1115                 parent.position_valid = false;
1116                 
1117                 // Create one with no heightmaps
1118                 ServerMapSector sector(&parent, v2s16(1,1));
1119                 
1120                 UASSERT(sector.getBlockNoCreateNoEx(0) == 0);
1121                 UASSERT(sector.getBlockNoCreateNoEx(1) == 0);
1122
1123                 MapBlock * bref = sector.createBlankBlock(-2);
1124                 
1125                 UASSERT(sector.getBlockNoCreateNoEx(0) == 0);
1126                 UASSERT(sector.getBlockNoCreateNoEx(-2) == bref);
1127                 
1128                 //TODO: Check for AlreadyExistsException
1129
1130                 /*bool exception_thrown = false;
1131                 try{
1132                         sector.getBlock(0);
1133                 }
1134                 catch(InvalidPositionException &e){
1135                         exception_thrown = true;
1136                 }
1137                 UASSERT(exception_thrown);*/
1138
1139         }
1140 };
1141 #endif
1142
1143 struct TestCollision: public TestBase
1144 {
1145         void Run()
1146         {
1147                 /*
1148                         axisAlignedCollision
1149                 */
1150
1151                 for(s16 bx = -3; bx <= 3; bx++)
1152                 for(s16 by = -3; by <= 3; by++)
1153                 for(s16 bz = -3; bz <= 3; bz++)
1154                 {
1155                         // X-
1156                         {
1157                                 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1158                                 aabb3f m(bx-2, by, bz, bx-1, by+1, bz+1);
1159                                 v3f v(1, 0, 0);
1160                                 f32 dtime = 0;
1161                                 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1162                                 UASSERT(fabs(dtime - 1.000) < 0.001);
1163                         }
1164                         {
1165                                 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1166                                 aabb3f m(bx-2, by, bz, bx-1, by+1, bz+1);
1167                                 v3f v(-1, 0, 0);
1168                                 f32 dtime = 0;
1169                                 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == -1);
1170                         }
1171                         {
1172                                 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1173                                 aabb3f m(bx-2, by+1.5, bz, bx-1, by+2.5, bz-1);
1174                                 v3f v(1, 0, 0);
1175                                 f32 dtime;
1176                                 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == -1);
1177                         }
1178                         {
1179                                 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1180                                 aabb3f m(bx-2, by-1.5, bz, bx-1.5, by+0.5, bz+1);
1181                                 v3f v(0.5, 0.1, 0);
1182                                 f32 dtime;
1183                                 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1184                                 UASSERT(fabs(dtime - 3.000) < 0.001);
1185                         }
1186                         {
1187                                 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1188                                 aabb3f m(bx-2, by-1.5, bz, bx-1.5, by+0.5, bz+1);
1189                                 v3f v(0.5, 0.1, 0);
1190                                 f32 dtime;
1191                                 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1192                                 UASSERT(fabs(dtime - 3.000) < 0.001);
1193                         }
1194
1195                         // X+
1196                         {
1197                                 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1198                                 aabb3f m(bx+2, by, bz, bx+3, by+1, bz+1);
1199                                 v3f v(-1, 0, 0);
1200                                 f32 dtime;
1201                                 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1202                                 UASSERT(fabs(dtime - 1.000) < 0.001);
1203                         }
1204                         {
1205                                 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1206                                 aabb3f m(bx+2, by, bz, bx+3, by+1, bz+1);
1207                                 v3f v(1, 0, 0);
1208                                 f32 dtime;
1209                                 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == -1);
1210                         }
1211                         {
1212                                 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1213                                 aabb3f m(bx+2, by, bz+1.5, bx+3, by+1, bz+3.5);
1214                                 v3f v(-1, 0, 0);
1215                                 f32 dtime;
1216                                 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == -1);
1217                         }
1218                         {
1219                                 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1220                                 aabb3f m(bx+2, by-1.5, bz, bx+2.5, by-0.5, bz+1);
1221                                 v3f v(-0.5, 0.2, 0);
1222                                 f32 dtime;
1223                                 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 1);  // Y, not X!
1224                                 UASSERT(fabs(dtime - 2.500) < 0.001);
1225                         }
1226                         {
1227                                 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1228                                 aabb3f m(bx+2, by-1.5, bz, bx+2.5, by-0.5, bz+1);
1229                                 v3f v(-0.5, 0.3, 0);
1230                                 f32 dtime;
1231                                 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1232                                 UASSERT(fabs(dtime - 2.000) < 0.001);
1233                         }
1234
1235                         // TODO: Y-, Y+, Z-, Z+
1236
1237                         // misc
1238                         {
1239                                 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1240                                 aabb3f m(bx+2.3, by+2.29, bz+2.29, bx+4.2, by+4.2, bz+4.2);
1241                                 v3f v(-1./3, -1./3, -1./3);
1242                                 f32 dtime;
1243                                 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1244                                 UASSERT(fabs(dtime - 0.9) < 0.001);
1245                         }
1246                         {
1247                                 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1248                                 aabb3f m(bx+2.29, by+2.3, bz+2.29, bx+4.2, by+4.2, bz+4.2);
1249                                 v3f v(-1./3, -1./3, -1./3);
1250                                 f32 dtime;
1251                                 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 1);
1252                                 UASSERT(fabs(dtime - 0.9) < 0.001);
1253                         }
1254                         {
1255                                 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1256                                 aabb3f m(bx+2.29, by+2.29, bz+2.3, bx+4.2, by+4.2, bz+4.2);
1257                                 v3f v(-1./3, -1./3, -1./3);
1258                                 f32 dtime;
1259                                 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 2);
1260                                 UASSERT(fabs(dtime - 0.9) < 0.001);
1261                         }
1262                         {
1263                                 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1264                                 aabb3f m(bx-4.2, by-4.2, bz-4.2, bx-2.3, by-2.29, bz-2.29);
1265                                 v3f v(1./7, 1./7, 1./7);
1266                                 f32 dtime;
1267                                 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1268                                 UASSERT(fabs(dtime - 16.1) < 0.001);
1269                         }
1270                         {
1271                                 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1272                                 aabb3f m(bx-4.2, by-4.2, bz-4.2, bx-2.29, by-2.3, bz-2.29);
1273                                 v3f v(1./7, 1./7, 1./7);
1274                                 f32 dtime;
1275                                 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 1);
1276                                 UASSERT(fabs(dtime - 16.1) < 0.001);
1277                         }
1278                         {
1279                                 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1280                                 aabb3f m(bx-4.2, by-4.2, bz-4.2, bx-2.29, by-2.29, bz-2.3);
1281                                 v3f v(1./7, 1./7, 1./7);
1282                                 f32 dtime;
1283                                 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 2);
1284                                 UASSERT(fabs(dtime - 16.1) < 0.001);
1285                         }
1286                 }
1287         }
1288 };
1289
1290 struct TestSocket: public TestBase
1291 {
1292         void Run()
1293         {
1294                 const int port = 30003;
1295                 UDPSocket socket;
1296                 socket.Bind(port);
1297
1298                 const char sendbuffer[] = "hello world!";
1299                 socket.Send(Address(127,0,0,1,port), sendbuffer, sizeof(sendbuffer));
1300
1301                 sleep_ms(50);
1302
1303                 char rcvbuffer[256];
1304                 memset(rcvbuffer, 0, sizeof(rcvbuffer));
1305                 Address sender;
1306                 for(;;)
1307                 {
1308                         int bytes_read = socket.Receive(sender, rcvbuffer, sizeof(rcvbuffer));
1309                         if(bytes_read < 0)
1310                                 break;
1311                 }
1312                 //FIXME: This fails on some systems
1313                 UASSERT(strncmp(sendbuffer, rcvbuffer, sizeof(sendbuffer))==0);
1314                 UASSERT(sender.getAddress() == Address(127,0,0,1, 0).getAddress());
1315         }
1316 };
1317
1318 struct TestConnection: public TestBase
1319 {
1320         void TestHelpers()
1321         {
1322                 /*
1323                         Test helper functions
1324                 */
1325
1326                 // Some constants for testing
1327                 u32 proto_id = 0x12345678;
1328                 u16 peer_id = 123;
1329                 u8 channel = 2;
1330                 SharedBuffer<u8> data1(1);
1331                 data1[0] = 100;
1332                 Address a(127,0,0,1, 10);
1333                 u16 seqnum = 34352;
1334
1335                 con::BufferedPacket p1 = con::makePacket(a, data1,
1336                                 proto_id, peer_id, channel);
1337                 /*
1338                         We should now have a packet with this data:
1339                         Header:
1340                                 [0] u32 protocol_id
1341                                 [4] u16 sender_peer_id
1342                                 [6] u8 channel
1343                         Data:
1344                                 [7] u8 data1[0]
1345                 */
1346                 UASSERT(readU32(&p1.data[0]) == proto_id);
1347                 UASSERT(readU16(&p1.data[4]) == peer_id);
1348                 UASSERT(readU8(&p1.data[6]) == channel);
1349                 UASSERT(readU8(&p1.data[7]) == data1[0]);
1350                 
1351                 //infostream<<"initial data1[0]="<<((u32)data1[0]&0xff)<<std::endl;
1352
1353                 SharedBuffer<u8> p2 = con::makeReliablePacket(data1, seqnum);
1354
1355                 /*infostream<<"p2.getSize()="<<p2.getSize()<<", data1.getSize()="
1356                                 <<data1.getSize()<<std::endl;
1357                 infostream<<"readU8(&p2[3])="<<readU8(&p2[3])
1358                                 <<" p2[3]="<<((u32)p2[3]&0xff)<<std::endl;
1359                 infostream<<"data1[0]="<<((u32)data1[0]&0xff)<<std::endl;*/
1360
1361                 UASSERT(p2.getSize() == 3 + data1.getSize());
1362                 UASSERT(readU8(&p2[0]) == TYPE_RELIABLE);
1363                 UASSERT(readU16(&p2[1]) == seqnum);
1364                 UASSERT(readU8(&p2[3]) == data1[0]);
1365         }
1366
1367         struct Handler : public con::PeerHandler
1368         {
1369                 Handler(const char *a_name)
1370                 {
1371                         count = 0;
1372                         last_id = 0;
1373                         name = a_name;
1374                 }
1375                 void peerAdded(con::Peer *peer)
1376                 {
1377                         infostream<<"Handler("<<name<<")::peerAdded(): "
1378                                         "id="<<peer->id<<std::endl;
1379                         last_id = peer->id;
1380                         count++;
1381                 }
1382                 void deletingPeer(con::Peer *peer, bool timeout)
1383                 {
1384                         infostream<<"Handler("<<name<<")::deletingPeer(): "
1385                                         "id="<<peer->id
1386                                         <<", timeout="<<timeout<<std::endl;
1387                         last_id = peer->id;
1388                         count--;
1389                 }
1390
1391                 s32 count;
1392                 u16 last_id;
1393                 const char *name;
1394         };
1395
1396         void Run()
1397         {
1398                 DSTACK("TestConnection::Run");
1399
1400                 TestHelpers();
1401
1402                 /*
1403                         Test some real connections
1404
1405                         NOTE: This mostly tests the legacy interface.
1406                 */
1407
1408                 u32 proto_id = 0xad26846a;
1409
1410                 Handler hand_server("server");
1411                 Handler hand_client("client");
1412                 
1413                 infostream<<"** Creating server Connection"<<std::endl;
1414                 con::Connection server(proto_id, 512, 5.0, &hand_server);
1415                 server.Serve(30001);
1416                 
1417                 infostream<<"** Creating client Connection"<<std::endl;
1418                 con::Connection client(proto_id, 512, 5.0, &hand_client);
1419
1420                 UASSERT(hand_server.count == 0);
1421                 UASSERT(hand_client.count == 0);
1422                 
1423                 sleep_ms(50);
1424                 
1425                 Address server_address(127,0,0,1, 30001);
1426                 infostream<<"** running client.Connect()"<<std::endl;
1427                 client.Connect(server_address);
1428
1429                 sleep_ms(50);
1430                 
1431                 // Client should not have added client yet
1432                 UASSERT(hand_client.count == 0);
1433                 
1434                 try
1435                 {
1436                         u16 peer_id;
1437                         SharedBuffer<u8> data;
1438                         infostream<<"** running client.Receive()"<<std::endl;
1439                         u32 size = client.Receive(peer_id, data);
1440                         infostream<<"** Client received: peer_id="<<peer_id
1441                                         <<", size="<<size
1442                                         <<std::endl;
1443                 }
1444                 catch(con::NoIncomingDataException &e)
1445                 {
1446                 }
1447
1448                 // Client should have added server now
1449                 UASSERT(hand_client.count == 1);
1450                 UASSERT(hand_client.last_id == 1);
1451                 // Server should not have added client yet
1452                 UASSERT(hand_server.count == 0);
1453                 
1454                 sleep_ms(50);
1455
1456                 try
1457                 {
1458                         u16 peer_id;
1459                         SharedBuffer<u8> data;
1460                         infostream<<"** running server.Receive()"<<std::endl;
1461                         u32 size = server.Receive(peer_id, data);
1462                         infostream<<"** Server received: peer_id="<<peer_id
1463                                         <<", size="<<size
1464                                         <<std::endl;
1465                 }
1466                 catch(con::NoIncomingDataException &e)
1467                 {
1468                         // No actual data received, but the client has
1469                         // probably been connected
1470                 }
1471                 
1472                 // Client should be the same
1473                 UASSERT(hand_client.count == 1);
1474                 UASSERT(hand_client.last_id == 1);
1475                 // Server should have the client
1476                 UASSERT(hand_server.count == 1);
1477                 UASSERT(hand_server.last_id == 2);
1478                 
1479                 //sleep_ms(50);
1480
1481                 while(client.Connected() == false)
1482                 {
1483                         try
1484                         {
1485                                 u16 peer_id;
1486                                 SharedBuffer<u8> data;
1487                                 infostream<<"** running client.Receive()"<<std::endl;
1488                                 u32 size = client.Receive(peer_id, data);
1489                                 infostream<<"** Client received: peer_id="<<peer_id
1490                                                 <<", size="<<size
1491                                                 <<std::endl;
1492                         }
1493                         catch(con::NoIncomingDataException &e)
1494                         {
1495                         }
1496                         sleep_ms(50);
1497                 }
1498
1499                 sleep_ms(50);
1500                 
1501                 try
1502                 {
1503                         u16 peer_id;
1504                         SharedBuffer<u8> data;
1505                         infostream<<"** running server.Receive()"<<std::endl;
1506                         u32 size = server.Receive(peer_id, data);
1507                         infostream<<"** Server received: peer_id="<<peer_id
1508                                         <<", size="<<size
1509                                         <<std::endl;
1510                 }
1511                 catch(con::NoIncomingDataException &e)
1512                 {
1513                 }
1514 #if 1
1515                 /*
1516                         Simple send-receive test
1517                 */
1518                 {
1519                         /*u8 data[] = "Hello World!";
1520                         u32 datasize = sizeof(data);*/
1521                         SharedBuffer<u8> data = SharedBufferFromString("Hello World!");
1522
1523                         infostream<<"** running client.Send()"<<std::endl;
1524                         client.Send(PEER_ID_SERVER, 0, data, true);
1525
1526                         sleep_ms(50);
1527
1528                         u16 peer_id;
1529                         SharedBuffer<u8> recvdata;
1530                         infostream<<"** running server.Receive()"<<std::endl;
1531                         u32 size = server.Receive(peer_id, recvdata);
1532                         infostream<<"** Server received: peer_id="<<peer_id
1533                                         <<", size="<<size
1534                                         <<", data="<<*data
1535                                         <<std::endl;
1536                         UASSERT(memcmp(*data, *recvdata, data.getSize()) == 0);
1537                 }
1538 #endif
1539                 u16 peer_id_client = 2;
1540 #if 0
1541                 /*
1542                         Send consequent packets in different order
1543                         Not compatible with new Connection, thus commented out.
1544                 */
1545                 {
1546                         //u8 data1[] = "hello1";
1547                         //u8 data2[] = "hello2";
1548                         SharedBuffer<u8> data1 = SharedBufferFromString("hello1");
1549                         SharedBuffer<u8> data2 = SharedBufferFromString("Hello2");
1550
1551                         Address client_address =
1552                                         server.GetPeerAddress(peer_id_client);
1553                         
1554                         infostream<<"*** Sending packets in wrong order (2,1,2)"
1555                                         <<std::endl;
1556                         
1557                         u8 chn = 0;
1558                         con::Channel *ch = &server.getPeer(peer_id_client)->channels[chn];
1559                         u16 sn = ch->next_outgoing_seqnum;
1560                         ch->next_outgoing_seqnum = sn+1;
1561                         server.Send(peer_id_client, chn, data2, true);
1562                         ch->next_outgoing_seqnum = sn;
1563                         server.Send(peer_id_client, chn, data1, true);
1564                         ch->next_outgoing_seqnum = sn+1;
1565                         server.Send(peer_id_client, chn, data2, true);
1566
1567                         sleep_ms(50);
1568
1569                         infostream<<"*** Receiving the packets"<<std::endl;
1570
1571                         u16 peer_id;
1572                         SharedBuffer<u8> recvdata;
1573                         u32 size;
1574
1575                         infostream<<"** running client.Receive()"<<std::endl;
1576                         peer_id = 132;
1577                         size = client.Receive(peer_id, recvdata);
1578                         infostream<<"** Client received: peer_id="<<peer_id
1579                                         <<", size="<<size
1580                                         <<", data="<<*recvdata
1581                                         <<std::endl;
1582                         UASSERT(size == data1.getSize());
1583                         UASSERT(memcmp(*data1, *recvdata, data1.getSize()) == 0);
1584                         UASSERT(peer_id == PEER_ID_SERVER);
1585                         
1586                         infostream<<"** running client.Receive()"<<std::endl;
1587                         peer_id = 132;
1588                         size = client.Receive(peer_id, recvdata);
1589                         infostream<<"** Client received: peer_id="<<peer_id
1590                                         <<", size="<<size
1591                                         <<", data="<<*recvdata
1592                                         <<std::endl;
1593                         UASSERT(size == data2.getSize());
1594                         UASSERT(memcmp(*data2, *recvdata, data2.getSize()) == 0);
1595                         UASSERT(peer_id == PEER_ID_SERVER);
1596                         
1597                         bool got_exception = false;
1598                         try
1599                         {
1600                                 infostream<<"** running client.Receive()"<<std::endl;
1601                                 peer_id = 132;
1602                                 size = client.Receive(peer_id, recvdata);
1603                                 infostream<<"** Client received: peer_id="<<peer_id
1604                                                 <<", size="<<size
1605                                                 <<", data="<<*recvdata
1606                                                 <<std::endl;
1607                         }
1608                         catch(con::NoIncomingDataException &e)
1609                         {
1610                                 infostream<<"** No incoming data for client"<<std::endl;
1611                                 got_exception = true;
1612                         }
1613                         UASSERT(got_exception);
1614                 }
1615 #endif
1616 #if 0
1617                 /*
1618                         Send large amounts of packets (infinite test)
1619                         Commented out because of infinity.
1620                 */
1621                 {
1622                         infostream<<"Sending large amounts of packets (infinite test)"<<std::endl;
1623                         int sendcount = 0;
1624                         for(;;){
1625                                 int datasize = myrand_range(0,5)==0?myrand_range(100,10000):myrand_range(0,100);
1626                                 infostream<<"datasize="<<datasize<<std::endl;
1627                                 SharedBuffer<u8> data1(datasize);
1628                                 for(u16 i=0; i<datasize; i++)
1629                                         data1[i] = i/4;
1630                                 
1631                                 int sendtimes = myrand_range(1,10);
1632                                 for(int i=0; i<sendtimes; i++){
1633                                         server.Send(peer_id_client, 0, data1, true);
1634                                         sendcount++;
1635                                 }
1636                                 infostream<<"sendcount="<<sendcount<<std::endl;
1637                                 
1638                                 //int receivetimes = myrand_range(1,20);
1639                                 int receivetimes = 20;
1640                                 for(int i=0; i<receivetimes; i++){
1641                                         SharedBuffer<u8> recvdata;
1642                                         u16 peer_id = 132;
1643                                         u16 size = 0;
1644                                         bool received = false;
1645                                         try{
1646                                                 size = client.Receive(peer_id, recvdata);
1647                                                 received = true;
1648                                         }catch(con::NoIncomingDataException &e){
1649                                         }
1650                                 }
1651                         }
1652                 }
1653 #endif
1654                 /*
1655                         Send a large packet
1656                 */
1657                 {
1658                         const int datasize = 30000;
1659                         SharedBuffer<u8> data1(datasize);
1660                         for(u16 i=0; i<datasize; i++){
1661                                 data1[i] = i/4;
1662                         }
1663
1664                         infostream<<"Sending data (size="<<datasize<<"):";
1665                         for(int i=0; i<datasize && i<20; i++){
1666                                 if(i%2==0) infostream<<" ";
1667                                 char buf[10];
1668                                 snprintf(buf, 10, "%.2X", ((int)((const char*)*data1)[i])&0xff);
1669                                 infostream<<buf;
1670                         }
1671                         if(datasize>20)
1672                                 infostream<<"...";
1673                         infostream<<std::endl;
1674                         
1675                         server.Send(peer_id_client, 0, data1, true);
1676
1677                         //sleep_ms(3000);
1678                         
1679                         SharedBuffer<u8> recvdata;
1680                         infostream<<"** running client.Receive()"<<std::endl;
1681                         u16 peer_id = 132;
1682                         u16 size = 0;
1683                         bool received = false;
1684                         u32 timems0 = porting::getTimeMs();
1685                         for(;;){
1686                                 if(porting::getTimeMs() - timems0 > 5000 || received)
1687                                         break;
1688                                 try{
1689                                         size = client.Receive(peer_id, recvdata);
1690                                         received = true;
1691                                 }catch(con::NoIncomingDataException &e){
1692                                 }
1693                                 sleep_ms(10);
1694                         }
1695                         UASSERT(received);
1696                         infostream<<"** Client received: peer_id="<<peer_id
1697                                         <<", size="<<size
1698                                         <<std::endl;
1699
1700                         infostream<<"Received data (size="<<size<<"): ";
1701                         for(int i=0; i<size && i<20; i++){
1702                                 if(i%2==0) infostream<<" ";
1703                                 char buf[10];
1704                                 snprintf(buf, 10, "%.2X", ((int)(recvdata[i]))&0xff);
1705                                 infostream<<buf;
1706                         }
1707                         if(size>20)
1708                                 infostream<<"...";
1709                         infostream<<std::endl;
1710
1711                         UASSERT(memcmp(*data1, *recvdata, data1.getSize()) == 0);
1712                         UASSERT(peer_id == PEER_ID_SERVER);
1713                 }
1714                 
1715                 // Check peer handlers
1716                 UASSERT(hand_client.count == 1);
1717                 UASSERT(hand_client.last_id == 1);
1718                 UASSERT(hand_server.count == 1);
1719                 UASSERT(hand_server.last_id == 2);
1720                 
1721                 //assert(0);
1722         }
1723 };
1724
1725 #define TEST(X)\
1726 {\
1727         X x;\
1728         infostream<<"Running " #X <<std::endl;\
1729         x.Run();\
1730         tests_run++;\
1731         tests_failed += x.test_failed ? 1 : 0;\
1732 }
1733
1734 #define TESTPARAMS(X, ...)\
1735 {\
1736         X x;\
1737         infostream<<"Running " #X <<std::endl;\
1738         x.Run(__VA_ARGS__);\
1739         tests_run++;\
1740         tests_failed += x.test_failed ? 1 : 0;\
1741 }
1742
1743 void run_tests()
1744 {
1745         DSTACK(__FUNCTION_NAME);
1746
1747         int tests_run = 0;
1748         int tests_failed = 0;
1749         
1750         // Create item and node definitions
1751         IWritableItemDefManager *idef = createItemDefManager();
1752         IWritableNodeDefManager *ndef = createNodeDefManager();
1753         define_some_nodes(idef, ndef);
1754
1755         infostream<<"run_tests() started"<<std::endl;
1756         TEST(TestUtilities);
1757         TEST(TestSettings);
1758         TEST(TestCompress);
1759         TEST(TestSerialization);
1760         TEST(TestNodedefSerialization);
1761         TESTPARAMS(TestMapNode, ndef);
1762         TESTPARAMS(TestVoxelManipulator, ndef);
1763         TESTPARAMS(TestVoxelAlgorithms, ndef);
1764         TESTPARAMS(TestInventory, idef);
1765         //TEST(TestMapBlock);
1766         //TEST(TestMapSector);
1767         TEST(TestCollision);
1768         if(INTERNET_SIMULATOR == false){
1769                 TEST(TestSocket);
1770                 dout_con<<"=== BEGIN RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
1771                 TEST(TestConnection);
1772                 dout_con<<"=== END RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
1773         }
1774         if(tests_failed == 0){
1775                 infostream<<"run_tests(): "<<tests_failed<<" / "<<tests_run<<" tests failed."<<std::endl;
1776                 infostream<<"run_tests() passed."<<std::endl;
1777                 return;
1778         } else {
1779                 errorstream<<"run_tests(): "<<tests_failed<<" / "<<tests_run<<" tests failed."<<std::endl;
1780                 errorstream<<"run_tests() aborting."<<std::endl;
1781                 abort();
1782         }
1783 }
1784