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