]> git.lizzy.rs Git - minetest.git/blob - src/test.cpp
Allow the LUA API to set animations to meshes as well as the animation speed. Also...
[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 TestCompress: public TestBase
318 {
319         void Run()
320         {
321                 { // ver 0
322
323                 SharedBuffer<u8> fromdata(4);
324                 fromdata[0]=1;
325                 fromdata[1]=5;
326                 fromdata[2]=5;
327                 fromdata[3]=1;
328                 
329                 std::ostringstream os(std::ios_base::binary);
330                 compress(fromdata, os, 0);
331
332                 std::string str_out = os.str();
333                 
334                 infostream<<"str_out.size()="<<str_out.size()<<std::endl;
335                 infostream<<"TestCompress: 1,5,5,1 -> ";
336                 for(u32 i=0; i<str_out.size(); i++)
337                 {
338                         infostream<<(u32)str_out[i]<<",";
339                 }
340                 infostream<<std::endl;
341
342                 UASSERT(str_out.size() == 10);
343
344                 UASSERT(str_out[0] == 0);
345                 UASSERT(str_out[1] == 0);
346                 UASSERT(str_out[2] == 0);
347                 UASSERT(str_out[3] == 4);
348                 UASSERT(str_out[4] == 0);
349                 UASSERT(str_out[5] == 1);
350                 UASSERT(str_out[6] == 1);
351                 UASSERT(str_out[7] == 5);
352                 UASSERT(str_out[8] == 0);
353                 UASSERT(str_out[9] == 1);
354
355                 std::istringstream is(str_out, std::ios_base::binary);
356                 std::ostringstream os2(std::ios_base::binary);
357
358                 decompress(is, os2, 0);
359                 std::string str_out2 = os2.str();
360
361                 infostream<<"decompress: ";
362                 for(u32 i=0; i<str_out2.size(); i++)
363                 {
364                         infostream<<(u32)str_out2[i]<<",";
365                 }
366                 infostream<<std::endl;
367
368                 UASSERT(str_out2.size() == fromdata.getSize());
369
370                 for(u32 i=0; i<str_out2.size(); i++)
371                 {
372                         UASSERT(str_out2[i] == fromdata[i]);
373                 }
374
375                 }
376
377                 { // ver HIGHEST
378
379                 SharedBuffer<u8> fromdata(4);
380                 fromdata[0]=1;
381                 fromdata[1]=5;
382                 fromdata[2]=5;
383                 fromdata[3]=1;
384                 
385                 std::ostringstream os(std::ios_base::binary);
386                 compress(fromdata, os, SER_FMT_VER_HIGHEST);
387
388                 std::string str_out = os.str();
389                 
390                 infostream<<"str_out.size()="<<str_out.size()<<std::endl;
391                 infostream<<"TestCompress: 1,5,5,1 -> ";
392                 for(u32 i=0; i<str_out.size(); i++)
393                 {
394                         infostream<<(u32)str_out[i]<<",";
395                 }
396                 infostream<<std::endl;
397
398                 std::istringstream is(str_out, std::ios_base::binary);
399                 std::ostringstream os2(std::ios_base::binary);
400
401                 decompress(is, os2, SER_FMT_VER_HIGHEST);
402                 std::string str_out2 = os2.str();
403
404                 infostream<<"decompress: ";
405                 for(u32 i=0; i<str_out2.size(); i++)
406                 {
407                         infostream<<(u32)str_out2[i]<<",";
408                 }
409                 infostream<<std::endl;
410
411                 UASSERT(str_out2.size() == fromdata.getSize());
412
413                 for(u32 i=0; i<str_out2.size(); i++)
414                 {
415                         UASSERT(str_out2[i] == fromdata[i]);
416                 }
417
418                 }
419
420                 // Test zlib wrapper with large amounts of data (larger than its
421                 // internal buffers)
422                 {
423                         infostream<<"Test: Testing zlib wrappers with a large amount "
424                                         <<"of pseudorandom data"<<std::endl;
425                         u32 size = 50000;
426                         infostream<<"Test: Input size of large compressZlib is "
427                                         <<size<<std::endl;
428                         std::string data_in;
429                         data_in.resize(size);
430                         PseudoRandom pseudorandom(9420);
431                         for(u32 i=0; i<size; i++)
432                                 data_in[i] = pseudorandom.range(0,255);
433                         std::ostringstream os_compressed(std::ios::binary);
434                         compressZlib(data_in, os_compressed);
435                         infostream<<"Test: Output size of large compressZlib is "
436                                         <<os_compressed.str().size()<<std::endl;
437                         std::istringstream is_compressed(os_compressed.str(), std::ios::binary);
438                         std::ostringstream os_decompressed(std::ios::binary);
439                         decompressZlib(is_compressed, os_decompressed);
440                         infostream<<"Test: Output size of large decompressZlib is "
441                                         <<os_decompressed.str().size()<<std::endl;
442                         std::string str_decompressed = os_decompressed.str();
443                         UTEST(str_decompressed.size() == data_in.size(), "Output size not"
444                                         " equal (output: %i, input: %i)",
445                                         str_decompressed.size(), data_in.size());
446                         for(u32 i=0; i<size && i<str_decompressed.size(); i++){
447                                 UTEST(str_decompressed[i] == data_in[i],
448                                                 "index out[%i]=%i differs from in[%i]=%i",
449                                                 i, str_decompressed[i], i, data_in[i]);
450                         }
451                 }
452         }
453 };
454
455 struct TestMapNode: public TestBase
456 {
457         void Run(INodeDefManager *nodedef)
458         {
459                 MapNode n;
460
461                 // Default values
462                 UASSERT(n.getContent() == CONTENT_AIR);
463                 UASSERT(n.getLight(LIGHTBANK_DAY, nodedef) == 0);
464                 UASSERT(n.getLight(LIGHTBANK_NIGHT, nodedef) == 0);
465                 
466                 // Transparency
467                 n.setContent(CONTENT_AIR);
468                 UASSERT(nodedef->get(n).light_propagates == true);
469                 n.setContent(LEGN(nodedef, "CONTENT_STONE"));
470                 UASSERT(nodedef->get(n).light_propagates == false);
471         }
472 };
473
474 struct TestVoxelManipulator: public TestBase
475 {
476         void Run(INodeDefManager *nodedef)
477         {
478                 /*
479                         VoxelArea
480                 */
481
482                 VoxelArea a(v3s16(-1,-1,-1), v3s16(1,1,1));
483                 UASSERT(a.index(0,0,0) == 1*3*3 + 1*3 + 1);
484                 UASSERT(a.index(-1,-1,-1) == 0);
485                 
486                 VoxelArea c(v3s16(-2,-2,-2), v3s16(2,2,2));
487                 // An area that is 1 bigger in x+ and z-
488                 VoxelArea d(v3s16(-2,-2,-3), v3s16(3,2,2));
489                 
490                 core::list<VoxelArea> aa;
491                 d.diff(c, aa);
492                 
493                 // Correct results
494                 core::array<VoxelArea> results;
495                 results.push_back(VoxelArea(v3s16(-2,-2,-3),v3s16(3,2,-3)));
496                 results.push_back(VoxelArea(v3s16(3,-2,-2),v3s16(3,2,2)));
497
498                 UASSERT(aa.size() == results.size());
499                 
500                 infostream<<"Result of diff:"<<std::endl;
501                 for(core::list<VoxelArea>::Iterator
502                                 i = aa.begin(); i != aa.end(); i++)
503                 {
504                         i->print(infostream);
505                         infostream<<std::endl;
506                         
507                         s32 j = results.linear_search(*i);
508                         UASSERT(j != -1);
509                         results.erase(j, 1);
510                 }
511
512
513                 /*
514                         VoxelManipulator
515                 */
516                 
517                 VoxelManipulator v;
518
519                 v.print(infostream, nodedef);
520
521                 infostream<<"*** Setting (-1,0,-1)=2 ***"<<std::endl;
522                 
523                 v.setNodeNoRef(v3s16(-1,0,-1), MapNode(CONTENT_GRASS));
524
525                 v.print(infostream, nodedef);
526
527                 UASSERT(v.getNode(v3s16(-1,0,-1)).getContent() == CONTENT_GRASS);
528
529                 infostream<<"*** Reading from inexistent (0,0,-1) ***"<<std::endl;
530
531                 EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,0,-1)));
532
533                 v.print(infostream, nodedef);
534
535                 infostream<<"*** Adding area ***"<<std::endl;
536
537                 v.addArea(a);
538                 
539                 v.print(infostream, nodedef);
540
541                 UASSERT(v.getNode(v3s16(-1,0,-1)).getContent() == CONTENT_GRASS);
542                 EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,1,1)));
543         }
544 };
545
546 struct TestVoxelAlgorithms: public TestBase
547 {
548         void Run(INodeDefManager *ndef)
549         {
550                 /*
551                         voxalgo::propagateSunlight
552                 */
553                 {
554                         VoxelManipulator v;
555                         for(u16 z=0; z<3; z++)
556                         for(u16 y=0; y<3; y++)
557                         for(u16 x=0; x<3; x++)
558                         {
559                                 v3s16 p(x,y,z);
560                                 v.setNodeNoRef(p, MapNode(CONTENT_AIR));
561                         }
562                         VoxelArea a(v3s16(0,0,0), v3s16(2,2,2));
563                         {
564                                 core::map<v3s16, bool> light_sources;
565                                 voxalgo::setLight(v, a, 0, ndef);
566                                 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
567                                                 v, a, true, light_sources, ndef);
568                                 //v.print(dstream, ndef, VOXELPRINT_LIGHT_DAY);
569                                 UASSERT(res.bottom_sunlight_valid == true);
570                                 UASSERT(v.getNode(v3s16(1,1,1)).getLight(LIGHTBANK_DAY, ndef)
571                                                 == LIGHT_SUN);
572                         }
573                         v.setNodeNoRef(v3s16(0,0,0), MapNode(CONTENT_STONE));
574                         {
575                                 core::map<v3s16, bool> light_sources;
576                                 voxalgo::setLight(v, a, 0, ndef);
577                                 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
578                                                 v, a, true, light_sources, ndef);
579                                 UASSERT(res.bottom_sunlight_valid == true);
580                                 UASSERT(v.getNode(v3s16(1,1,1)).getLight(LIGHTBANK_DAY, ndef)
581                                                 == LIGHT_SUN);
582                         }
583                         {
584                                 core::map<v3s16, bool> light_sources;
585                                 voxalgo::setLight(v, a, 0, ndef);
586                                 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
587                                                 v, a, false, light_sources, ndef);
588                                 UASSERT(res.bottom_sunlight_valid == true);
589                                 UASSERT(v.getNode(v3s16(2,0,2)).getLight(LIGHTBANK_DAY, ndef)
590                                                 == 0);
591                         }
592                         v.setNodeNoRef(v3s16(1,3,2), MapNode(CONTENT_STONE));
593                         {
594                                 core::map<v3s16, bool> light_sources;
595                                 voxalgo::setLight(v, a, 0, ndef);
596                                 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
597                                                 v, a, true, light_sources, ndef);
598                                 UASSERT(res.bottom_sunlight_valid == true);
599                                 UASSERT(v.getNode(v3s16(1,1,2)).getLight(LIGHTBANK_DAY, ndef)
600                                                 == 0);
601                         }
602                         {
603                                 core::map<v3s16, bool> light_sources;
604                                 voxalgo::setLight(v, a, 0, ndef);
605                                 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
606                                                 v, a, false, light_sources, ndef);
607                                 UASSERT(res.bottom_sunlight_valid == true);
608                                 UASSERT(v.getNode(v3s16(1,0,2)).getLight(LIGHTBANK_DAY, ndef)
609                                                 == 0);
610                         }
611                         {
612                                 MapNode n(CONTENT_AIR);
613                                 n.setLight(LIGHTBANK_DAY, 10, ndef);
614                                 v.setNodeNoRef(v3s16(1,-1,2), n);
615                         }
616                         {
617                                 core::map<v3s16, bool> light_sources;
618                                 voxalgo::setLight(v, a, 0, ndef);
619                                 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
620                                                 v, a, true, light_sources, ndef);
621                                 UASSERT(res.bottom_sunlight_valid == true);
622                         }
623                         {
624                                 core::map<v3s16, bool> light_sources;
625                                 voxalgo::setLight(v, a, 0, ndef);
626                                 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
627                                                 v, a, false, light_sources, ndef);
628                                 UASSERT(res.bottom_sunlight_valid == true);
629                         }
630                         {
631                                 MapNode n(CONTENT_AIR);
632                                 n.setLight(LIGHTBANK_DAY, LIGHT_SUN, ndef);
633                                 v.setNodeNoRef(v3s16(1,-1,2), n);
634                         }
635                         {
636                                 core::map<v3s16, bool> light_sources;
637                                 voxalgo::setLight(v, a, 0, ndef);
638                                 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
639                                                 v, a, true, light_sources, ndef);
640                                 UASSERT(res.bottom_sunlight_valid == false);
641                         }
642                         {
643                                 core::map<v3s16, bool> light_sources;
644                                 voxalgo::setLight(v, a, 0, ndef);
645                                 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
646                                                 v, a, false, light_sources, ndef);
647                                 UASSERT(res.bottom_sunlight_valid == false);
648                         }
649                         v.setNodeNoRef(v3s16(1,3,2), MapNode(CONTENT_IGNORE));
650                         {
651                                 core::map<v3s16, bool> light_sources;
652                                 voxalgo::setLight(v, a, 0, ndef);
653                                 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
654                                                 v, a, true, light_sources, ndef);
655                                 UASSERT(res.bottom_sunlight_valid == true);
656                         }
657                 }
658                 /*
659                         voxalgo::clearLightAndCollectSources
660                 */
661                 {
662                         VoxelManipulator v;
663                         for(u16 z=0; z<3; z++)
664                         for(u16 y=0; y<3; y++)
665                         for(u16 x=0; x<3; x++)
666                         {
667                                 v3s16 p(x,y,z);
668                                 v.setNode(p, MapNode(CONTENT_AIR));
669                         }
670                         VoxelArea a(v3s16(0,0,0), v3s16(2,2,2));
671                         v.setNodeNoRef(v3s16(0,0,0), MapNode(CONTENT_STONE));
672                         v.setNodeNoRef(v3s16(1,1,1), MapNode(CONTENT_TORCH));
673                         {
674                                 MapNode n(CONTENT_AIR);
675                                 n.setLight(LIGHTBANK_DAY, 1, ndef);
676                                 v.setNode(v3s16(1,1,2), n);
677                         }
678                         {
679                                 core::map<v3s16, bool> light_sources;
680                                 core::map<v3s16, u8> unlight_from;
681                                 voxalgo::clearLightAndCollectSources(v, a, LIGHTBANK_DAY,
682                                                 ndef, light_sources, unlight_from);
683                                 //v.print(dstream, ndef, VOXELPRINT_LIGHT_DAY);
684                                 UASSERT(v.getNode(v3s16(0,1,1)).getLight(LIGHTBANK_DAY, ndef)
685                                                 == 0);
686                                 UASSERT(light_sources.find(v3s16(1,1,1)) != NULL);
687                                 UASSERT(light_sources.size() == 1);
688                                 UASSERT(unlight_from.find(v3s16(1,1,2)) != NULL);
689                                 UASSERT(unlight_from.size() == 1);
690                         }
691                 }
692         }
693 };
694
695 struct TestInventory: public TestBase
696 {
697         void Run(IItemDefManager *idef)
698         {
699                 std::string serialized_inventory =
700                 "List 0 32\n"
701                 "Width 3\n"
702                 "Empty\n"
703                 "Empty\n"
704                 "Empty\n"
705                 "Empty\n"
706                 "Empty\n"
707                 "Empty\n"
708                 "Empty\n"
709                 "Empty\n"
710                 "Empty\n"
711                 "Item default:cobble 61\n"
712                 "Empty\n"
713                 "Empty\n"
714                 "Empty\n"
715                 "Empty\n"
716                 "Empty\n"
717                 "Empty\n"
718                 "Item default:dirt 71\n"
719                 "Empty\n"
720                 "Empty\n"
721                 "Empty\n"
722                 "Empty\n"
723                 "Empty\n"
724                 "Empty\n"
725                 "Empty\n"
726                 "Item default:dirt 99\n"
727                 "Item default:cobble 38\n"
728                 "Empty\n"
729                 "Empty\n"
730                 "Empty\n"
731                 "Empty\n"
732                 "Empty\n"
733                 "Empty\n"
734                 "EndInventoryList\n"
735                 "EndInventory\n";
736                 
737                 std::string serialized_inventory_2 =
738                 "List main 32\n"
739                 "Width 5\n"
740                 "Empty\n"
741                 "Empty\n"
742                 "Empty\n"
743                 "Empty\n"
744                 "Empty\n"
745                 "Empty\n"
746                 "Empty\n"
747                 "Empty\n"
748                 "Empty\n"
749                 "Item default:cobble 61\n"
750                 "Empty\n"
751                 "Empty\n"
752                 "Empty\n"
753                 "Empty\n"
754                 "Empty\n"
755                 "Empty\n"
756                 "Item default:dirt 71\n"
757                 "Empty\n"
758                 "Empty\n"
759                 "Empty\n"
760                 "Empty\n"
761                 "Empty\n"
762                 "Empty\n"
763                 "Empty\n"
764                 "Item default:dirt 99\n"
765                 "Item default:cobble 38\n"
766                 "Empty\n"
767                 "Empty\n"
768                 "Empty\n"
769                 "Empty\n"
770                 "Empty\n"
771                 "Empty\n"
772                 "EndInventoryList\n"
773                 "EndInventory\n";
774                 
775                 Inventory inv(idef);
776                 std::istringstream is(serialized_inventory, std::ios::binary);
777                 inv.deSerialize(is);
778                 UASSERT(inv.getList("0"));
779                 UASSERT(!inv.getList("main"));
780                 inv.getList("0")->setName("main");
781                 UASSERT(!inv.getList("0"));
782                 UASSERT(inv.getList("main"));
783                 UASSERT(inv.getList("main")->getWidth() == 3);
784                 inv.getList("main")->setWidth(5);
785                 std::ostringstream inv_os(std::ios::binary);
786                 inv.serialize(inv_os);
787                 UASSERT(inv_os.str() == serialized_inventory_2);
788         }
789 };
790
791 /*
792         NOTE: These tests became non-working then NodeContainer was removed.
793               These should be redone, utilizing some kind of a virtual
794                   interface for Map (IMap would be fine).
795 */
796 #if 0
797 struct TestMapBlock: public TestBase
798 {
799         class TC : public NodeContainer
800         {
801         public:
802
803                 MapNode node;
804                 bool position_valid;
805                 core::list<v3s16> validity_exceptions;
806
807                 TC()
808                 {
809                         position_valid = true;
810                 }
811
812                 virtual bool isValidPosition(v3s16 p)
813                 {
814                         //return position_valid ^ (p==position_valid_exception);
815                         bool exception = false;
816                         for(core::list<v3s16>::Iterator i=validity_exceptions.begin();
817                                         i != validity_exceptions.end(); i++)
818                         {
819                                 if(p == *i)
820                                 {
821                                         exception = true;
822                                         break;
823                                 }
824                         }
825                         return exception ? !position_valid : position_valid;
826                 }
827
828                 virtual MapNode getNode(v3s16 p)
829                 {
830                         if(isValidPosition(p) == false)
831                                 throw InvalidPositionException();
832                         return node;
833                 }
834
835                 virtual void setNode(v3s16 p, MapNode & n)
836                 {
837                         if(isValidPosition(p) == false)
838                                 throw InvalidPositionException();
839                 };
840
841                 virtual u16 nodeContainerId() const
842                 {
843                         return 666;
844                 }
845         };
846
847         void Run()
848         {
849                 TC parent;
850                 
851                 MapBlock b(&parent, v3s16(1,1,1));
852                 v3s16 relpos(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
853
854                 UASSERT(b.getPosRelative() == relpos);
855
856                 UASSERT(b.getBox().MinEdge.X == MAP_BLOCKSIZE);
857                 UASSERT(b.getBox().MaxEdge.X == MAP_BLOCKSIZE*2-1);
858                 UASSERT(b.getBox().MinEdge.Y == MAP_BLOCKSIZE);
859                 UASSERT(b.getBox().MaxEdge.Y == MAP_BLOCKSIZE*2-1);
860                 UASSERT(b.getBox().MinEdge.Z == MAP_BLOCKSIZE);
861                 UASSERT(b.getBox().MaxEdge.Z == MAP_BLOCKSIZE*2-1);
862                 
863                 UASSERT(b.isValidPosition(v3s16(0,0,0)) == true);
864                 UASSERT(b.isValidPosition(v3s16(-1,0,0)) == false);
865                 UASSERT(b.isValidPosition(v3s16(-1,-142,-2341)) == false);
866                 UASSERT(b.isValidPosition(v3s16(-124,142,2341)) == false);
867                 UASSERT(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
868                 UASSERT(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE,MAP_BLOCKSIZE-1)) == false);
869
870                 /*
871                         TODO: this method should probably be removed
872                         if the block size isn't going to be set variable
873                 */
874                 /*UASSERT(b.getSizeNodes() == v3s16(MAP_BLOCKSIZE,
875                                 MAP_BLOCKSIZE, MAP_BLOCKSIZE));*/
876                 
877                 // Changed flag should be initially set
878                 UASSERT(b.getModified() == MOD_STATE_WRITE_NEEDED);
879                 b.resetModified();
880                 UASSERT(b.getModified() == MOD_STATE_CLEAN);
881
882                 // All nodes should have been set to
883                 // .d=CONTENT_IGNORE and .getLight() = 0
884                 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
885                 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
886                 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
887                 {
888                         //UASSERT(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_AIR);
889                         UASSERT(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_IGNORE);
890                         UASSERT(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_DAY) == 0);
891                         UASSERT(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_NIGHT) == 0);
892                 }
893                 
894                 {
895                         MapNode n(CONTENT_AIR);
896                         for(u16 z=0; z<MAP_BLOCKSIZE; z++)
897                         for(u16 y=0; y<MAP_BLOCKSIZE; y++)
898                         for(u16 x=0; x<MAP_BLOCKSIZE; x++)
899                         {
900                                 b.setNode(v3s16(x,y,z), n);
901                         }
902                 }
903                         
904                 /*
905                         Parent fetch functions
906                 */
907                 parent.position_valid = false;
908                 parent.node.setContent(5);
909
910                 MapNode n;
911                 
912                 // Positions in the block should still be valid
913                 UASSERT(b.isValidPositionParent(v3s16(0,0,0)) == true);
914                 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
915                 n = b.getNodeParent(v3s16(0,MAP_BLOCKSIZE-1,0));
916                 UASSERT(n.getContent() == CONTENT_AIR);
917
918                 // ...but outside the block they should be invalid
919                 UASSERT(b.isValidPositionParent(v3s16(-121,2341,0)) == false);
920                 UASSERT(b.isValidPositionParent(v3s16(-1,0,0)) == false);
921                 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == false);
922                 
923                 {
924                         bool exception_thrown = false;
925                         try{
926                                 // This should throw an exception
927                                 MapNode n = b.getNodeParent(v3s16(0,0,-1));
928                         }
929                         catch(InvalidPositionException &e)
930                         {
931                                 exception_thrown = true;
932                         }
933                         UASSERT(exception_thrown);
934                 }
935
936                 parent.position_valid = true;
937                 // Now the positions outside should be valid
938                 UASSERT(b.isValidPositionParent(v3s16(-121,2341,0)) == true);
939                 UASSERT(b.isValidPositionParent(v3s16(-1,0,0)) == true);
940                 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == true);
941                 n = b.getNodeParent(v3s16(0,0,MAP_BLOCKSIZE));
942                 UASSERT(n.getContent() == 5);
943
944                 /*
945                         Set a node
946                 */
947                 v3s16 p(1,2,0);
948                 n.setContent(4);
949                 b.setNode(p, n);
950                 UASSERT(b.getNode(p).getContent() == 4);
951                 //TODO: Update to new system
952                 /*UASSERT(b.getNodeTile(p) == 4);
953                 UASSERT(b.getNodeTile(v3s16(-1,-1,0)) == 5);*/
954                 
955                 /*
956                         propagateSunlight()
957                 */
958                 // Set lighting of all nodes to 0
959                 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
960                         for(u16 y=0; y<MAP_BLOCKSIZE; y++){
961                                 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
962                                         MapNode n = b.getNode(v3s16(x,y,z));
963                                         n.setLight(LIGHTBANK_DAY, 0);
964                                         n.setLight(LIGHTBANK_NIGHT, 0);
965                                         b.setNode(v3s16(x,y,z), n);
966                                 }
967                         }
968                 }
969                 {
970                         /*
971                                 Check how the block handles being a lonely sky block
972                         */
973                         parent.position_valid = true;
974                         b.setIsUnderground(false);
975                         parent.node.setContent(CONTENT_AIR);
976                         parent.node.setLight(LIGHTBANK_DAY, LIGHT_SUN);
977                         parent.node.setLight(LIGHTBANK_NIGHT, 0);
978                         core::map<v3s16, bool> light_sources;
979                         // The bottom block is invalid, because we have a shadowing node
980                         UASSERT(b.propagateSunlight(light_sources) == false);
981                         UASSERT(b.getNode(v3s16(1,4,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
982                         UASSERT(b.getNode(v3s16(1,3,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
983                         UASSERT(b.getNode(v3s16(1,2,0)).getLight(LIGHTBANK_DAY) == 0);
984                         UASSERT(b.getNode(v3s16(1,1,0)).getLight(LIGHTBANK_DAY) == 0);
985                         UASSERT(b.getNode(v3s16(1,0,0)).getLight(LIGHTBANK_DAY) == 0);
986                         UASSERT(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
987                         UASSERT(b.getFaceLight2(1000, p, v3s16(0,1,0)) == LIGHT_SUN);
988                         UASSERT(b.getFaceLight2(1000, p, v3s16(0,-1,0)) == 0);
989                         UASSERT(b.getFaceLight2(0, p, v3s16(0,-1,0)) == 0);
990                         // According to MapBlock::getFaceLight,
991                         // The face on the z+ side should have double-diminished light
992                         //UASSERT(b.getFaceLight(p, v3s16(0,0,1)) == diminish_light(diminish_light(LIGHT_MAX)));
993                         // The face on the z+ side should have diminished light
994                         UASSERT(b.getFaceLight2(1000, p, v3s16(0,0,1)) == diminish_light(LIGHT_MAX));
995                 }
996                 /*
997                         Check how the block handles being in between blocks with some non-sunlight
998                         while being underground
999                 */
1000                 {
1001                         // Make neighbours to exist and set some non-sunlight to them
1002                         parent.position_valid = true;
1003                         b.setIsUnderground(true);
1004                         parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
1005                         core::map<v3s16, bool> light_sources;
1006                         // The block below should be valid because there shouldn't be
1007                         // sunlight in there either
1008                         UASSERT(b.propagateSunlight(light_sources, true) == true);
1009                         // Should not touch nodes that are not affected (that is, all of them)
1010                         //UASSERT(b.getNode(v3s16(1,2,3)).getLight() == LIGHT_SUN);
1011                         // Should set light of non-sunlighted blocks to 0.
1012                         UASSERT(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == 0);
1013                 }
1014                 /*
1015                         Set up a situation where:
1016                         - There is only air in this block
1017                         - There is a valid non-sunlighted block at the bottom, and
1018                         - Invalid blocks elsewhere.
1019                         - the block is not underground.
1020
1021                         This should result in bottom block invalidity
1022                 */
1023                 {
1024                         b.setIsUnderground(false);
1025                         // Clear block
1026                         for(u16 z=0; z<MAP_BLOCKSIZE; z++){
1027                                 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
1028                                         for(u16 x=0; x<MAP_BLOCKSIZE; x++){
1029                                                 MapNode n;
1030                                                 n.setContent(CONTENT_AIR);
1031                                                 n.setLight(LIGHTBANK_DAY, 0);
1032                                                 b.setNode(v3s16(x,y,z), n);
1033                                         }
1034                                 }
1035                         }
1036                         // Make neighbours invalid
1037                         parent.position_valid = false;
1038                         // Add exceptions to the top of the bottom block
1039                         for(u16 x=0; x<MAP_BLOCKSIZE; x++)
1040                         for(u16 z=0; z<MAP_BLOCKSIZE; z++)
1041                         {
1042                                 parent.validity_exceptions.push_back(v3s16(MAP_BLOCKSIZE+x, MAP_BLOCKSIZE-1, MAP_BLOCKSIZE+z));
1043                         }
1044                         // Lighting value for the valid nodes
1045                         parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
1046                         core::map<v3s16, bool> light_sources;
1047                         // Bottom block is not valid
1048                         UASSERT(b.propagateSunlight(light_sources) == false);
1049                 }
1050         }
1051 };
1052
1053 struct TestMapSector: public TestBase
1054 {
1055         class TC : public NodeContainer
1056         {
1057         public:
1058
1059                 MapNode node;
1060                 bool position_valid;
1061
1062                 TC()
1063                 {
1064                         position_valid = true;
1065                 }
1066
1067                 virtual bool isValidPosition(v3s16 p)
1068                 {
1069                         return position_valid;
1070                 }
1071
1072                 virtual MapNode getNode(v3s16 p)
1073                 {
1074                         if(position_valid == false)
1075                                 throw InvalidPositionException();
1076                         return node;
1077                 }
1078
1079                 virtual void setNode(v3s16 p, MapNode & n)
1080                 {
1081                         if(position_valid == false)
1082                                 throw InvalidPositionException();
1083                 };
1084                 
1085                 virtual u16 nodeContainerId() const
1086                 {
1087                         return 666;
1088                 }
1089         };
1090         
1091         void Run()
1092         {
1093                 TC parent;
1094                 parent.position_valid = false;
1095                 
1096                 // Create one with no heightmaps
1097                 ServerMapSector sector(&parent, v2s16(1,1));
1098                 
1099                 UASSERT(sector.getBlockNoCreateNoEx(0) == 0);
1100                 UASSERT(sector.getBlockNoCreateNoEx(1) == 0);
1101
1102                 MapBlock * bref = sector.createBlankBlock(-2);
1103                 
1104                 UASSERT(sector.getBlockNoCreateNoEx(0) == 0);
1105                 UASSERT(sector.getBlockNoCreateNoEx(-2) == bref);
1106                 
1107                 //TODO: Check for AlreadyExistsException
1108
1109                 /*bool exception_thrown = false;
1110                 try{
1111                         sector.getBlock(0);
1112                 }
1113                 catch(InvalidPositionException &e){
1114                         exception_thrown = true;
1115                 }
1116                 UASSERT(exception_thrown);*/
1117
1118         }
1119 };
1120 #endif
1121
1122 struct TestCollision: public TestBase
1123 {
1124         void Run()
1125         {
1126                 /*
1127                         axisAlignedCollision
1128                 */
1129
1130                 for(s16 bx = -3; bx <= 3; bx++)
1131                 for(s16 by = -3; by <= 3; by++)
1132                 for(s16 bz = -3; bz <= 3; bz++)
1133                 {
1134                         // X-
1135                         {
1136                                 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1137                                 aabb3f m(bx-2, by, bz, bx-1, by+1, bz+1);
1138                                 v3f v(1, 0, 0);
1139                                 f32 dtime = 0;
1140                                 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1141                                 UASSERT(fabs(dtime - 1.000) < 0.001);
1142                         }
1143                         {
1144                                 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1145                                 aabb3f m(bx-2, by, bz, bx-1, by+1, bz+1);
1146                                 v3f v(-1, 0, 0);
1147                                 f32 dtime = 0;
1148                                 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == -1);
1149                         }
1150                         {
1151                                 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1152                                 aabb3f m(bx-2, by+1.5, bz, bx-1, by+2.5, bz-1);
1153                                 v3f v(1, 0, 0);
1154                                 f32 dtime;
1155                                 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == -1);
1156                         }
1157                         {
1158                                 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1159                                 aabb3f m(bx-2, by-1.5, bz, bx-1.5, by+0.5, bz+1);
1160                                 v3f v(0.5, 0.1, 0);
1161                                 f32 dtime;
1162                                 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1163                                 UASSERT(fabs(dtime - 3.000) < 0.001);
1164                         }
1165                         {
1166                                 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1167                                 aabb3f m(bx-2, by-1.5, bz, bx-1.5, by+0.5, bz+1);
1168                                 v3f v(0.5, 0.1, 0);
1169                                 f32 dtime;
1170                                 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1171                                 UASSERT(fabs(dtime - 3.000) < 0.001);
1172                         }
1173
1174                         // X+
1175                         {
1176                                 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1177                                 aabb3f m(bx+2, by, bz, bx+3, by+1, bz+1);
1178                                 v3f v(-1, 0, 0);
1179                                 f32 dtime;
1180                                 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1181                                 UASSERT(fabs(dtime - 1.000) < 0.001);
1182                         }
1183                         {
1184                                 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1185                                 aabb3f m(bx+2, by, bz, bx+3, by+1, bz+1);
1186                                 v3f v(1, 0, 0);
1187                                 f32 dtime;
1188                                 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == -1);
1189                         }
1190                         {
1191                                 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1192                                 aabb3f m(bx+2, by, bz+1.5, bx+3, by+1, bz+3.5);
1193                                 v3f v(-1, 0, 0);
1194                                 f32 dtime;
1195                                 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == -1);
1196                         }
1197                         {
1198                                 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1199                                 aabb3f m(bx+2, by-1.5, bz, bx+2.5, by-0.5, bz+1);
1200                                 v3f v(-0.5, 0.2, 0);
1201                                 f32 dtime;
1202                                 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 1);  // Y, not X!
1203                                 UASSERT(fabs(dtime - 2.500) < 0.001);
1204                         }
1205                         {
1206                                 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1207                                 aabb3f m(bx+2, by-1.5, bz, bx+2.5, by-0.5, bz+1);
1208                                 v3f v(-0.5, 0.3, 0);
1209                                 f32 dtime;
1210                                 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1211                                 UASSERT(fabs(dtime - 2.000) < 0.001);
1212                         }
1213
1214                         // TODO: Y-, Y+, Z-, Z+
1215
1216                         // misc
1217                         {
1218                                 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1219                                 aabb3f m(bx+2.3, by+2.29, bz+2.29, bx+4.2, by+4.2, bz+4.2);
1220                                 v3f v(-1./3, -1./3, -1./3);
1221                                 f32 dtime;
1222                                 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1223                                 UASSERT(fabs(dtime - 0.9) < 0.001);
1224                         }
1225                         {
1226                                 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1227                                 aabb3f m(bx+2.29, by+2.3, bz+2.29, bx+4.2, by+4.2, bz+4.2);
1228                                 v3f v(-1./3, -1./3, -1./3);
1229                                 f32 dtime;
1230                                 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 1);
1231                                 UASSERT(fabs(dtime - 0.9) < 0.001);
1232                         }
1233                         {
1234                                 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1235                                 aabb3f m(bx+2.29, by+2.29, bz+2.3, bx+4.2, by+4.2, bz+4.2);
1236                                 v3f v(-1./3, -1./3, -1./3);
1237                                 f32 dtime;
1238                                 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 2);
1239                                 UASSERT(fabs(dtime - 0.9) < 0.001);
1240                         }
1241                         {
1242                                 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1243                                 aabb3f m(bx-4.2, by-4.2, bz-4.2, bx-2.3, by-2.29, bz-2.29);
1244                                 v3f v(1./7, 1./7, 1./7);
1245                                 f32 dtime;
1246                                 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1247                                 UASSERT(fabs(dtime - 16.1) < 0.001);
1248                         }
1249                         {
1250                                 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1251                                 aabb3f m(bx-4.2, by-4.2, bz-4.2, bx-2.29, by-2.3, bz-2.29);
1252                                 v3f v(1./7, 1./7, 1./7);
1253                                 f32 dtime;
1254                                 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 1);
1255                                 UASSERT(fabs(dtime - 16.1) < 0.001);
1256                         }
1257                         {
1258                                 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1259                                 aabb3f m(bx-4.2, by-4.2, bz-4.2, bx-2.29, by-2.29, bz-2.3);
1260                                 v3f v(1./7, 1./7, 1./7);
1261                                 f32 dtime;
1262                                 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 2);
1263                                 UASSERT(fabs(dtime - 16.1) < 0.001);
1264                         }
1265                 }
1266         }
1267 };
1268
1269 struct TestSocket: public TestBase
1270 {
1271         void Run()
1272         {
1273                 const int port = 30003;
1274                 UDPSocket socket;
1275                 socket.Bind(port);
1276
1277                 const char sendbuffer[] = "hello world!";
1278                 socket.Send(Address(127,0,0,1,port), sendbuffer, sizeof(sendbuffer));
1279
1280                 sleep_ms(50);
1281
1282                 char rcvbuffer[256];
1283                 memset(rcvbuffer, 0, sizeof(rcvbuffer));
1284                 Address sender;
1285                 for(;;)
1286                 {
1287                         int bytes_read = socket.Receive(sender, rcvbuffer, sizeof(rcvbuffer));
1288                         if(bytes_read < 0)
1289                                 break;
1290                 }
1291                 //FIXME: This fails on some systems
1292                 UASSERT(strncmp(sendbuffer, rcvbuffer, sizeof(sendbuffer))==0);
1293                 UASSERT(sender.getAddress() == Address(127,0,0,1, 0).getAddress());
1294         }
1295 };
1296
1297 struct TestConnection: public TestBase
1298 {
1299         void TestHelpers()
1300         {
1301                 /*
1302                         Test helper functions
1303                 */
1304
1305                 // Some constants for testing
1306                 u32 proto_id = 0x12345678;
1307                 u16 peer_id = 123;
1308                 u8 channel = 2;
1309                 SharedBuffer<u8> data1(1);
1310                 data1[0] = 100;
1311                 Address a(127,0,0,1, 10);
1312                 u16 seqnum = 34352;
1313
1314                 con::BufferedPacket p1 = con::makePacket(a, data1,
1315                                 proto_id, peer_id, channel);
1316                 /*
1317                         We should now have a packet with this data:
1318                         Header:
1319                                 [0] u32 protocol_id
1320                                 [4] u16 sender_peer_id
1321                                 [6] u8 channel
1322                         Data:
1323                                 [7] u8 data1[0]
1324                 */
1325                 UASSERT(readU32(&p1.data[0]) == proto_id);
1326                 UASSERT(readU16(&p1.data[4]) == peer_id);
1327                 UASSERT(readU8(&p1.data[6]) == channel);
1328                 UASSERT(readU8(&p1.data[7]) == data1[0]);
1329                 
1330                 //infostream<<"initial data1[0]="<<((u32)data1[0]&0xff)<<std::endl;
1331
1332                 SharedBuffer<u8> p2 = con::makeReliablePacket(data1, seqnum);
1333
1334                 /*infostream<<"p2.getSize()="<<p2.getSize()<<", data1.getSize()="
1335                                 <<data1.getSize()<<std::endl;
1336                 infostream<<"readU8(&p2[3])="<<readU8(&p2[3])
1337                                 <<" p2[3]="<<((u32)p2[3]&0xff)<<std::endl;
1338                 infostream<<"data1[0]="<<((u32)data1[0]&0xff)<<std::endl;*/
1339
1340                 UASSERT(p2.getSize() == 3 + data1.getSize());
1341                 UASSERT(readU8(&p2[0]) == TYPE_RELIABLE);
1342                 UASSERT(readU16(&p2[1]) == seqnum);
1343                 UASSERT(readU8(&p2[3]) == data1[0]);
1344         }
1345
1346         struct Handler : public con::PeerHandler
1347         {
1348                 Handler(const char *a_name)
1349                 {
1350                         count = 0;
1351                         last_id = 0;
1352                         name = a_name;
1353                 }
1354                 void peerAdded(con::Peer *peer)
1355                 {
1356                         infostream<<"Handler("<<name<<")::peerAdded(): "
1357                                         "id="<<peer->id<<std::endl;
1358                         last_id = peer->id;
1359                         count++;
1360                 }
1361                 void deletingPeer(con::Peer *peer, bool timeout)
1362                 {
1363                         infostream<<"Handler("<<name<<")::deletingPeer(): "
1364                                         "id="<<peer->id
1365                                         <<", timeout="<<timeout<<std::endl;
1366                         last_id = peer->id;
1367                         count--;
1368                 }
1369
1370                 s32 count;
1371                 u16 last_id;
1372                 const char *name;
1373         };
1374
1375         void Run()
1376         {
1377                 DSTACK("TestConnection::Run");
1378
1379                 TestHelpers();
1380
1381                 /*
1382                         Test some real connections
1383
1384                         NOTE: This mostly tests the legacy interface.
1385                 */
1386
1387                 u32 proto_id = 0xad26846a;
1388
1389                 Handler hand_server("server");
1390                 Handler hand_client("client");
1391                 
1392                 infostream<<"** Creating server Connection"<<std::endl;
1393                 con::Connection server(proto_id, 512, 5.0, &hand_server);
1394                 server.Serve(30001);
1395                 
1396                 infostream<<"** Creating client Connection"<<std::endl;
1397                 con::Connection client(proto_id, 512, 5.0, &hand_client);
1398
1399                 UASSERT(hand_server.count == 0);
1400                 UASSERT(hand_client.count == 0);
1401                 
1402                 sleep_ms(50);
1403                 
1404                 Address server_address(127,0,0,1, 30001);
1405                 infostream<<"** running client.Connect()"<<std::endl;
1406                 client.Connect(server_address);
1407
1408                 sleep_ms(50);
1409                 
1410                 // Client should not have added client yet
1411                 UASSERT(hand_client.count == 0);
1412                 
1413                 try
1414                 {
1415                         u16 peer_id;
1416                         SharedBuffer<u8> data;
1417                         infostream<<"** running client.Receive()"<<std::endl;
1418                         u32 size = client.Receive(peer_id, data);
1419                         infostream<<"** Client received: peer_id="<<peer_id
1420                                         <<", size="<<size
1421                                         <<std::endl;
1422                 }
1423                 catch(con::NoIncomingDataException &e)
1424                 {
1425                 }
1426
1427                 // Client should have added server now
1428                 UASSERT(hand_client.count == 1);
1429                 UASSERT(hand_client.last_id == 1);
1430                 // Server should not have added client yet
1431                 UASSERT(hand_server.count == 0);
1432                 
1433                 sleep_ms(50);
1434
1435                 try
1436                 {
1437                         u16 peer_id;
1438                         SharedBuffer<u8> data;
1439                         infostream<<"** running server.Receive()"<<std::endl;
1440                         u32 size = server.Receive(peer_id, data);
1441                         infostream<<"** Server received: peer_id="<<peer_id
1442                                         <<", size="<<size
1443                                         <<std::endl;
1444                 }
1445                 catch(con::NoIncomingDataException &e)
1446                 {
1447                         // No actual data received, but the client has
1448                         // probably been connected
1449                 }
1450                 
1451                 // Client should be the same
1452                 UASSERT(hand_client.count == 1);
1453                 UASSERT(hand_client.last_id == 1);
1454                 // Server should have the client
1455                 UASSERT(hand_server.count == 1);
1456                 UASSERT(hand_server.last_id == 2);
1457                 
1458                 //sleep_ms(50);
1459
1460                 while(client.Connected() == false)
1461                 {
1462                         try
1463                         {
1464                                 u16 peer_id;
1465                                 SharedBuffer<u8> data;
1466                                 infostream<<"** running client.Receive()"<<std::endl;
1467                                 u32 size = client.Receive(peer_id, data);
1468                                 infostream<<"** Client received: peer_id="<<peer_id
1469                                                 <<", size="<<size
1470                                                 <<std::endl;
1471                         }
1472                         catch(con::NoIncomingDataException &e)
1473                         {
1474                         }
1475                         sleep_ms(50);
1476                 }
1477
1478                 sleep_ms(50);
1479                 
1480                 try
1481                 {
1482                         u16 peer_id;
1483                         SharedBuffer<u8> data;
1484                         infostream<<"** running server.Receive()"<<std::endl;
1485                         u32 size = server.Receive(peer_id, data);
1486                         infostream<<"** Server received: peer_id="<<peer_id
1487                                         <<", size="<<size
1488                                         <<std::endl;
1489                 }
1490                 catch(con::NoIncomingDataException &e)
1491                 {
1492                 }
1493 #if 1
1494                 /*
1495                         Simple send-receive test
1496                 */
1497                 {
1498                         /*u8 data[] = "Hello World!";
1499                         u32 datasize = sizeof(data);*/
1500                         SharedBuffer<u8> data = SharedBufferFromString("Hello World!");
1501
1502                         infostream<<"** running client.Send()"<<std::endl;
1503                         client.Send(PEER_ID_SERVER, 0, data, true);
1504
1505                         sleep_ms(50);
1506
1507                         u16 peer_id;
1508                         SharedBuffer<u8> recvdata;
1509                         infostream<<"** running server.Receive()"<<std::endl;
1510                         u32 size = server.Receive(peer_id, recvdata);
1511                         infostream<<"** Server received: peer_id="<<peer_id
1512                                         <<", size="<<size
1513                                         <<", data="<<*data
1514                                         <<std::endl;
1515                         UASSERT(memcmp(*data, *recvdata, data.getSize()) == 0);
1516                 }
1517 #endif
1518                 u16 peer_id_client = 2;
1519 #if 0
1520                 /*
1521                         Send consequent packets in different order
1522                         Not compatible with new Connection, thus commented out.
1523                 */
1524                 {
1525                         //u8 data1[] = "hello1";
1526                         //u8 data2[] = "hello2";
1527                         SharedBuffer<u8> data1 = SharedBufferFromString("hello1");
1528                         SharedBuffer<u8> data2 = SharedBufferFromString("Hello2");
1529
1530                         Address client_address =
1531                                         server.GetPeerAddress(peer_id_client);
1532                         
1533                         infostream<<"*** Sending packets in wrong order (2,1,2)"
1534                                         <<std::endl;
1535                         
1536                         u8 chn = 0;
1537                         con::Channel *ch = &server.getPeer(peer_id_client)->channels[chn];
1538                         u16 sn = ch->next_outgoing_seqnum;
1539                         ch->next_outgoing_seqnum = sn+1;
1540                         server.Send(peer_id_client, chn, data2, true);
1541                         ch->next_outgoing_seqnum = sn;
1542                         server.Send(peer_id_client, chn, data1, true);
1543                         ch->next_outgoing_seqnum = sn+1;
1544                         server.Send(peer_id_client, chn, data2, true);
1545
1546                         sleep_ms(50);
1547
1548                         infostream<<"*** Receiving the packets"<<std::endl;
1549
1550                         u16 peer_id;
1551                         SharedBuffer<u8> recvdata;
1552                         u32 size;
1553
1554                         infostream<<"** running client.Receive()"<<std::endl;
1555                         peer_id = 132;
1556                         size = client.Receive(peer_id, recvdata);
1557                         infostream<<"** Client received: peer_id="<<peer_id
1558                                         <<", size="<<size
1559                                         <<", data="<<*recvdata
1560                                         <<std::endl;
1561                         UASSERT(size == data1.getSize());
1562                         UASSERT(memcmp(*data1, *recvdata, data1.getSize()) == 0);
1563                         UASSERT(peer_id == PEER_ID_SERVER);
1564                         
1565                         infostream<<"** running client.Receive()"<<std::endl;
1566                         peer_id = 132;
1567                         size = client.Receive(peer_id, recvdata);
1568                         infostream<<"** Client received: peer_id="<<peer_id
1569                                         <<", size="<<size
1570                                         <<", data="<<*recvdata
1571                                         <<std::endl;
1572                         UASSERT(size == data2.getSize());
1573                         UASSERT(memcmp(*data2, *recvdata, data2.getSize()) == 0);
1574                         UASSERT(peer_id == PEER_ID_SERVER);
1575                         
1576                         bool got_exception = false;
1577                         try
1578                         {
1579                                 infostream<<"** running client.Receive()"<<std::endl;
1580                                 peer_id = 132;
1581                                 size = client.Receive(peer_id, recvdata);
1582                                 infostream<<"** Client received: peer_id="<<peer_id
1583                                                 <<", size="<<size
1584                                                 <<", data="<<*recvdata
1585                                                 <<std::endl;
1586                         }
1587                         catch(con::NoIncomingDataException &e)
1588                         {
1589                                 infostream<<"** No incoming data for client"<<std::endl;
1590                                 got_exception = true;
1591                         }
1592                         UASSERT(got_exception);
1593                 }
1594 #endif
1595 #if 0
1596                 /*
1597                         Send large amounts of packets (infinite test)
1598                         Commented out because of infinity.
1599                 */
1600                 {
1601                         infostream<<"Sending large amounts of packets (infinite test)"<<std::endl;
1602                         int sendcount = 0;
1603                         for(;;){
1604                                 int datasize = myrand_range(0,5)==0?myrand_range(100,10000):myrand_range(0,100);
1605                                 infostream<<"datasize="<<datasize<<std::endl;
1606                                 SharedBuffer<u8> data1(datasize);
1607                                 for(u16 i=0; i<datasize; i++)
1608                                         data1[i] = i/4;
1609                                 
1610                                 int sendtimes = myrand_range(1,10);
1611                                 for(int i=0; i<sendtimes; i++){
1612                                         server.Send(peer_id_client, 0, data1, true);
1613                                         sendcount++;
1614                                 }
1615                                 infostream<<"sendcount="<<sendcount<<std::endl;
1616                                 
1617                                 //int receivetimes = myrand_range(1,20);
1618                                 int receivetimes = 20;
1619                                 for(int i=0; i<receivetimes; i++){
1620                                         SharedBuffer<u8> recvdata;
1621                                         u16 peer_id = 132;
1622                                         u16 size = 0;
1623                                         bool received = false;
1624                                         try{
1625                                                 size = client.Receive(peer_id, recvdata);
1626                                                 received = true;
1627                                         }catch(con::NoIncomingDataException &e){
1628                                         }
1629                                 }
1630                         }
1631                 }
1632 #endif
1633                 /*
1634                         Send a large packet
1635                 */
1636                 {
1637                         const int datasize = 30000;
1638                         SharedBuffer<u8> data1(datasize);
1639                         for(u16 i=0; i<datasize; i++){
1640                                 data1[i] = i/4;
1641                         }
1642
1643                         infostream<<"Sending data (size="<<datasize<<"):";
1644                         for(int i=0; i<datasize && i<20; i++){
1645                                 if(i%2==0) infostream<<" ";
1646                                 char buf[10];
1647                                 snprintf(buf, 10, "%.2X", ((int)((const char*)*data1)[i])&0xff);
1648                                 infostream<<buf;
1649                         }
1650                         if(datasize>20)
1651                                 infostream<<"...";
1652                         infostream<<std::endl;
1653                         
1654                         server.Send(peer_id_client, 0, data1, true);
1655
1656                         //sleep_ms(3000);
1657                         
1658                         SharedBuffer<u8> recvdata;
1659                         infostream<<"** running client.Receive()"<<std::endl;
1660                         u16 peer_id = 132;
1661                         u16 size = 0;
1662                         bool received = false;
1663                         u32 timems0 = porting::getTimeMs();
1664                         for(;;){
1665                                 if(porting::getTimeMs() - timems0 > 5000 || received)
1666                                         break;
1667                                 try{
1668                                         size = client.Receive(peer_id, recvdata);
1669                                         received = true;
1670                                 }catch(con::NoIncomingDataException &e){
1671                                 }
1672                                 sleep_ms(10);
1673                         }
1674                         UASSERT(received);
1675                         infostream<<"** Client received: peer_id="<<peer_id
1676                                         <<", size="<<size
1677                                         <<std::endl;
1678
1679                         infostream<<"Received data (size="<<size<<"): ";
1680                         for(int i=0; i<size && i<20; i++){
1681                                 if(i%2==0) infostream<<" ";
1682                                 char buf[10];
1683                                 snprintf(buf, 10, "%.2X", ((int)(recvdata[i]))&0xff);
1684                                 infostream<<buf;
1685                         }
1686                         if(size>20)
1687                                 infostream<<"...";
1688                         infostream<<std::endl;
1689
1690                         UASSERT(memcmp(*data1, *recvdata, data1.getSize()) == 0);
1691                         UASSERT(peer_id == PEER_ID_SERVER);
1692                 }
1693                 
1694                 // Check peer handlers
1695                 UASSERT(hand_client.count == 1);
1696                 UASSERT(hand_client.last_id == 1);
1697                 UASSERT(hand_server.count == 1);
1698                 UASSERT(hand_server.last_id == 2);
1699                 
1700                 //assert(0);
1701         }
1702 };
1703
1704 #define TEST(X)\
1705 {\
1706         X x;\
1707         infostream<<"Running " #X <<std::endl;\
1708         x.Run();\
1709         tests_run++;\
1710         tests_failed += x.test_failed ? 1 : 0;\
1711 }
1712
1713 #define TESTPARAMS(X, ...)\
1714 {\
1715         X x;\
1716         infostream<<"Running " #X <<std::endl;\
1717         x.Run(__VA_ARGS__);\
1718         tests_run++;\
1719         tests_failed += x.test_failed ? 1 : 0;\
1720 }
1721
1722 void run_tests()
1723 {
1724         DSTACK(__FUNCTION_NAME);
1725
1726         int tests_run = 0;
1727         int tests_failed = 0;
1728         
1729         // Create item and node definitions
1730         IWritableItemDefManager *idef = createItemDefManager();
1731         IWritableNodeDefManager *ndef = createNodeDefManager();
1732         define_some_nodes(idef, ndef);
1733
1734         infostream<<"run_tests() started"<<std::endl;
1735         TEST(TestUtilities);
1736         TEST(TestSettings);
1737         TEST(TestCompress);
1738         TEST(TestSerialization);
1739         TESTPARAMS(TestMapNode, ndef);
1740         TESTPARAMS(TestVoxelManipulator, ndef);
1741         TESTPARAMS(TestVoxelAlgorithms, ndef);
1742         TESTPARAMS(TestInventory, idef);
1743         //TEST(TestMapBlock);
1744         //TEST(TestMapSector);
1745         TEST(TestCollision);
1746         if(INTERNET_SIMULATOR == false){
1747                 TEST(TestSocket);
1748                 dout_con<<"=== BEGIN RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
1749                 TEST(TestConnection);
1750                 dout_con<<"=== END RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
1751         }
1752         if(tests_failed == 0){
1753                 infostream<<"run_tests(): "<<tests_failed<<" / "<<tests_run<<" tests failed."<<std::endl;
1754                 infostream<<"run_tests() passed."<<std::endl;
1755                 return;
1756         } else {
1757                 errorstream<<"run_tests(): "<<tests_failed<<" / "<<tests_run<<" tests failed."<<std::endl;
1758                 errorstream<<"run_tests() aborting."<<std::endl;
1759                 abort();
1760         }
1761 }
1762