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