]> git.lizzy.rs Git - dragonfireclient.git/blob - src/test.cpp
Make shift the default descent control on ladders and when flying
[dragonfireclient.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                 "Empty\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                 "Item default:cobble 61\n"
711                 "Empty\n"
712                 "Empty\n"
713                 "Empty\n"
714                 "Empty\n"
715                 "Empty\n"
716                 "Empty\n"
717                 "Item default:dirt 71\n"
718                 "Empty\n"
719                 "Empty\n"
720                 "Empty\n"
721                 "Empty\n"
722                 "Empty\n"
723                 "Empty\n"
724                 "Empty\n"
725                 "Item default:dirt 99\n"
726                 "Item default:cobble 38\n"
727                 "Empty\n"
728                 "Empty\n"
729                 "Empty\n"
730                 "Empty\n"
731                 "Empty\n"
732                 "Empty\n"
733                 "EndInventoryList\n"
734                 "EndInventory\n";
735                 
736                 std::string serialized_inventory_2 =
737                 "List main 32\n"
738                 "Empty\n"
739                 "Empty\n"
740                 "Empty\n"
741                 "Empty\n"
742                 "Empty\n"
743                 "Empty\n"
744                 "Empty\n"
745                 "Empty\n"
746                 "Empty\n"
747                 "Item default:cobble 61\n"
748                 "Empty\n"
749                 "Empty\n"
750                 "Empty\n"
751                 "Empty\n"
752                 "Empty\n"
753                 "Empty\n"
754                 "Item default:dirt 71\n"
755                 "Empty\n"
756                 "Empty\n"
757                 "Empty\n"
758                 "Empty\n"
759                 "Empty\n"
760                 "Empty\n"
761                 "Empty\n"
762                 "Item default:dirt 99\n"
763                 "Item default:cobble 38\n"
764                 "Empty\n"
765                 "Empty\n"
766                 "Empty\n"
767                 "Empty\n"
768                 "Empty\n"
769                 "Empty\n"
770                 "EndInventoryList\n"
771                 "EndInventory\n";
772                 
773                 Inventory inv(idef);
774                 std::istringstream is(serialized_inventory, std::ios::binary);
775                 inv.deSerialize(is);
776                 UASSERT(inv.getList("0"));
777                 UASSERT(!inv.getList("main"));
778                 inv.getList("0")->setName("main");
779                 UASSERT(!inv.getList("0"));
780                 UASSERT(inv.getList("main"));
781                 std::ostringstream inv_os(std::ios::binary);
782                 inv.serialize(inv_os);
783                 UASSERT(inv_os.str() == serialized_inventory_2);
784         }
785 };
786
787 /*
788         NOTE: These tests became non-working then NodeContainer was removed.
789               These should be redone, utilizing some kind of a virtual
790                   interface for Map (IMap would be fine).
791 */
792 #if 0
793 struct TestMapBlock: public TestBase
794 {
795         class TC : public NodeContainer
796         {
797         public:
798
799                 MapNode node;
800                 bool position_valid;
801                 core::list<v3s16> validity_exceptions;
802
803                 TC()
804                 {
805                         position_valid = true;
806                 }
807
808                 virtual bool isValidPosition(v3s16 p)
809                 {
810                         //return position_valid ^ (p==position_valid_exception);
811                         bool exception = false;
812                         for(core::list<v3s16>::Iterator i=validity_exceptions.begin();
813                                         i != validity_exceptions.end(); i++)
814                         {
815                                 if(p == *i)
816                                 {
817                                         exception = true;
818                                         break;
819                                 }
820                         }
821                         return exception ? !position_valid : position_valid;
822                 }
823
824                 virtual MapNode getNode(v3s16 p)
825                 {
826                         if(isValidPosition(p) == false)
827                                 throw InvalidPositionException();
828                         return node;
829                 }
830
831                 virtual void setNode(v3s16 p, MapNode & n)
832                 {
833                         if(isValidPosition(p) == false)
834                                 throw InvalidPositionException();
835                 };
836
837                 virtual u16 nodeContainerId() const
838                 {
839                         return 666;
840                 }
841         };
842
843         void Run()
844         {
845                 TC parent;
846                 
847                 MapBlock b(&parent, v3s16(1,1,1));
848                 v3s16 relpos(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
849
850                 UASSERT(b.getPosRelative() == relpos);
851
852                 UASSERT(b.getBox().MinEdge.X == MAP_BLOCKSIZE);
853                 UASSERT(b.getBox().MaxEdge.X == MAP_BLOCKSIZE*2-1);
854                 UASSERT(b.getBox().MinEdge.Y == MAP_BLOCKSIZE);
855                 UASSERT(b.getBox().MaxEdge.Y == MAP_BLOCKSIZE*2-1);
856                 UASSERT(b.getBox().MinEdge.Z == MAP_BLOCKSIZE);
857                 UASSERT(b.getBox().MaxEdge.Z == MAP_BLOCKSIZE*2-1);
858                 
859                 UASSERT(b.isValidPosition(v3s16(0,0,0)) == true);
860                 UASSERT(b.isValidPosition(v3s16(-1,0,0)) == false);
861                 UASSERT(b.isValidPosition(v3s16(-1,-142,-2341)) == false);
862                 UASSERT(b.isValidPosition(v3s16(-124,142,2341)) == false);
863                 UASSERT(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
864                 UASSERT(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE,MAP_BLOCKSIZE-1)) == false);
865
866                 /*
867                         TODO: this method should probably be removed
868                         if the block size isn't going to be set variable
869                 */
870                 /*UASSERT(b.getSizeNodes() == v3s16(MAP_BLOCKSIZE,
871                                 MAP_BLOCKSIZE, MAP_BLOCKSIZE));*/
872                 
873                 // Changed flag should be initially set
874                 UASSERT(b.getModified() == MOD_STATE_WRITE_NEEDED);
875                 b.resetModified();
876                 UASSERT(b.getModified() == MOD_STATE_CLEAN);
877
878                 // All nodes should have been set to
879                 // .d=CONTENT_IGNORE and .getLight() = 0
880                 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
881                 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
882                 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
883                 {
884                         //UASSERT(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_AIR);
885                         UASSERT(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_IGNORE);
886                         UASSERT(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_DAY) == 0);
887                         UASSERT(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_NIGHT) == 0);
888                 }
889                 
890                 {
891                         MapNode n(CONTENT_AIR);
892                         for(u16 z=0; z<MAP_BLOCKSIZE; z++)
893                         for(u16 y=0; y<MAP_BLOCKSIZE; y++)
894                         for(u16 x=0; x<MAP_BLOCKSIZE; x++)
895                         {
896                                 b.setNode(v3s16(x,y,z), n);
897                         }
898                 }
899                         
900                 /*
901                         Parent fetch functions
902                 */
903                 parent.position_valid = false;
904                 parent.node.setContent(5);
905
906                 MapNode n;
907                 
908                 // Positions in the block should still be valid
909                 UASSERT(b.isValidPositionParent(v3s16(0,0,0)) == true);
910                 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
911                 n = b.getNodeParent(v3s16(0,MAP_BLOCKSIZE-1,0));
912                 UASSERT(n.getContent() == CONTENT_AIR);
913
914                 // ...but outside the block they should be invalid
915                 UASSERT(b.isValidPositionParent(v3s16(-121,2341,0)) == false);
916                 UASSERT(b.isValidPositionParent(v3s16(-1,0,0)) == false);
917                 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == false);
918                 
919                 {
920                         bool exception_thrown = false;
921                         try{
922                                 // This should throw an exception
923                                 MapNode n = b.getNodeParent(v3s16(0,0,-1));
924                         }
925                         catch(InvalidPositionException &e)
926                         {
927                                 exception_thrown = true;
928                         }
929                         UASSERT(exception_thrown);
930                 }
931
932                 parent.position_valid = true;
933                 // Now the positions outside should be valid
934                 UASSERT(b.isValidPositionParent(v3s16(-121,2341,0)) == true);
935                 UASSERT(b.isValidPositionParent(v3s16(-1,0,0)) == true);
936                 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == true);
937                 n = b.getNodeParent(v3s16(0,0,MAP_BLOCKSIZE));
938                 UASSERT(n.getContent() == 5);
939
940                 /*
941                         Set a node
942                 */
943                 v3s16 p(1,2,0);
944                 n.setContent(4);
945                 b.setNode(p, n);
946                 UASSERT(b.getNode(p).getContent() == 4);
947                 //TODO: Update to new system
948                 /*UASSERT(b.getNodeTile(p) == 4);
949                 UASSERT(b.getNodeTile(v3s16(-1,-1,0)) == 5);*/
950                 
951                 /*
952                         propagateSunlight()
953                 */
954                 // Set lighting of all nodes to 0
955                 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
956                         for(u16 y=0; y<MAP_BLOCKSIZE; y++){
957                                 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
958                                         MapNode n = b.getNode(v3s16(x,y,z));
959                                         n.setLight(LIGHTBANK_DAY, 0);
960                                         n.setLight(LIGHTBANK_NIGHT, 0);
961                                         b.setNode(v3s16(x,y,z), n);
962                                 }
963                         }
964                 }
965                 {
966                         /*
967                                 Check how the block handles being a lonely sky block
968                         */
969                         parent.position_valid = true;
970                         b.setIsUnderground(false);
971                         parent.node.setContent(CONTENT_AIR);
972                         parent.node.setLight(LIGHTBANK_DAY, LIGHT_SUN);
973                         parent.node.setLight(LIGHTBANK_NIGHT, 0);
974                         core::map<v3s16, bool> light_sources;
975                         // The bottom block is invalid, because we have a shadowing node
976                         UASSERT(b.propagateSunlight(light_sources) == false);
977                         UASSERT(b.getNode(v3s16(1,4,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
978                         UASSERT(b.getNode(v3s16(1,3,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
979                         UASSERT(b.getNode(v3s16(1,2,0)).getLight(LIGHTBANK_DAY) == 0);
980                         UASSERT(b.getNode(v3s16(1,1,0)).getLight(LIGHTBANK_DAY) == 0);
981                         UASSERT(b.getNode(v3s16(1,0,0)).getLight(LIGHTBANK_DAY) == 0);
982                         UASSERT(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
983                         UASSERT(b.getFaceLight2(1000, p, v3s16(0,1,0)) == LIGHT_SUN);
984                         UASSERT(b.getFaceLight2(1000, p, v3s16(0,-1,0)) == 0);
985                         UASSERT(b.getFaceLight2(0, p, v3s16(0,-1,0)) == 0);
986                         // According to MapBlock::getFaceLight,
987                         // The face on the z+ side should have double-diminished light
988                         //UASSERT(b.getFaceLight(p, v3s16(0,0,1)) == diminish_light(diminish_light(LIGHT_MAX)));
989                         // The face on the z+ side should have diminished light
990                         UASSERT(b.getFaceLight2(1000, p, v3s16(0,0,1)) == diminish_light(LIGHT_MAX));
991                 }
992                 /*
993                         Check how the block handles being in between blocks with some non-sunlight
994                         while being underground
995                 */
996                 {
997                         // Make neighbours to exist and set some non-sunlight to them
998                         parent.position_valid = true;
999                         b.setIsUnderground(true);
1000                         parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
1001                         core::map<v3s16, bool> light_sources;
1002                         // The block below should be valid because there shouldn't be
1003                         // sunlight in there either
1004                         UASSERT(b.propagateSunlight(light_sources, true) == true);
1005                         // Should not touch nodes that are not affected (that is, all of them)
1006                         //UASSERT(b.getNode(v3s16(1,2,3)).getLight() == LIGHT_SUN);
1007                         // Should set light of non-sunlighted blocks to 0.
1008                         UASSERT(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == 0);
1009                 }
1010                 /*
1011                         Set up a situation where:
1012                         - There is only air in this block
1013                         - There is a valid non-sunlighted block at the bottom, and
1014                         - Invalid blocks elsewhere.
1015                         - the block is not underground.
1016
1017                         This should result in bottom block invalidity
1018                 */
1019                 {
1020                         b.setIsUnderground(false);
1021                         // Clear block
1022                         for(u16 z=0; z<MAP_BLOCKSIZE; z++){
1023                                 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
1024                                         for(u16 x=0; x<MAP_BLOCKSIZE; x++){
1025                                                 MapNode n;
1026                                                 n.setContent(CONTENT_AIR);
1027                                                 n.setLight(LIGHTBANK_DAY, 0);
1028                                                 b.setNode(v3s16(x,y,z), n);
1029                                         }
1030                                 }
1031                         }
1032                         // Make neighbours invalid
1033                         parent.position_valid = false;
1034                         // Add exceptions to the top of the bottom block
1035                         for(u16 x=0; x<MAP_BLOCKSIZE; x++)
1036                         for(u16 z=0; z<MAP_BLOCKSIZE; z++)
1037                         {
1038                                 parent.validity_exceptions.push_back(v3s16(MAP_BLOCKSIZE+x, MAP_BLOCKSIZE-1, MAP_BLOCKSIZE+z));
1039                         }
1040                         // Lighting value for the valid nodes
1041                         parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
1042                         core::map<v3s16, bool> light_sources;
1043                         // Bottom block is not valid
1044                         UASSERT(b.propagateSunlight(light_sources) == false);
1045                 }
1046         }
1047 };
1048
1049 struct TestMapSector: public TestBase
1050 {
1051         class TC : public NodeContainer
1052         {
1053         public:
1054
1055                 MapNode node;
1056                 bool position_valid;
1057
1058                 TC()
1059                 {
1060                         position_valid = true;
1061                 }
1062
1063                 virtual bool isValidPosition(v3s16 p)
1064                 {
1065                         return position_valid;
1066                 }
1067
1068                 virtual MapNode getNode(v3s16 p)
1069                 {
1070                         if(position_valid == false)
1071                                 throw InvalidPositionException();
1072                         return node;
1073                 }
1074
1075                 virtual void setNode(v3s16 p, MapNode & n)
1076                 {
1077                         if(position_valid == false)
1078                                 throw InvalidPositionException();
1079                 };
1080                 
1081                 virtual u16 nodeContainerId() const
1082                 {
1083                         return 666;
1084                 }
1085         };
1086         
1087         void Run()
1088         {
1089                 TC parent;
1090                 parent.position_valid = false;
1091                 
1092                 // Create one with no heightmaps
1093                 ServerMapSector sector(&parent, v2s16(1,1));
1094                 
1095                 UASSERT(sector.getBlockNoCreateNoEx(0) == 0);
1096                 UASSERT(sector.getBlockNoCreateNoEx(1) == 0);
1097
1098                 MapBlock * bref = sector.createBlankBlock(-2);
1099                 
1100                 UASSERT(sector.getBlockNoCreateNoEx(0) == 0);
1101                 UASSERT(sector.getBlockNoCreateNoEx(-2) == bref);
1102                 
1103                 //TODO: Check for AlreadyExistsException
1104
1105                 /*bool exception_thrown = false;
1106                 try{
1107                         sector.getBlock(0);
1108                 }
1109                 catch(InvalidPositionException &e){
1110                         exception_thrown = true;
1111                 }
1112                 UASSERT(exception_thrown);*/
1113
1114         }
1115 };
1116 #endif
1117
1118 struct TestCollision: public TestBase
1119 {
1120         void Run()
1121         {
1122                 /*
1123                         axisAlignedCollision
1124                 */
1125
1126                 for(s16 bx = -3; bx <= 3; bx++)
1127                 for(s16 by = -3; by <= 3; by++)
1128                 for(s16 bz = -3; bz <= 3; bz++)
1129                 {
1130                         // X-
1131                         {
1132                                 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1133                                 aabb3f m(bx-2, by, bz, bx-1, by+1, bz+1);
1134                                 v3f v(1, 0, 0);
1135                                 f32 dtime = 0;
1136                                 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1137                                 UASSERT(fabs(dtime - 1.000) < 0.001);
1138                         }
1139                         {
1140                                 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1141                                 aabb3f m(bx-2, by, bz, bx-1, by+1, bz+1);
1142                                 v3f v(-1, 0, 0);
1143                                 f32 dtime = 0;
1144                                 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == -1);
1145                         }
1146                         {
1147                                 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1148                                 aabb3f m(bx-2, by+1.5, bz, bx-1, by+2.5, bz-1);
1149                                 v3f v(1, 0, 0);
1150                                 f32 dtime;
1151                                 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == -1);
1152                         }
1153                         {
1154                                 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1155                                 aabb3f m(bx-2, by-1.5, bz, bx-1.5, by+0.5, bz+1);
1156                                 v3f v(0.5, 0.1, 0);
1157                                 f32 dtime;
1158                                 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1159                                 UASSERT(fabs(dtime - 3.000) < 0.001);
1160                         }
1161                         {
1162                                 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1163                                 aabb3f m(bx-2, by-1.5, bz, bx-1.5, by+0.5, bz+1);
1164                                 v3f v(0.5, 0.1, 0);
1165                                 f32 dtime;
1166                                 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1167                                 UASSERT(fabs(dtime - 3.000) < 0.001);
1168                         }
1169
1170                         // X+
1171                         {
1172                                 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1173                                 aabb3f m(bx+2, by, bz, bx+3, by+1, bz+1);
1174                                 v3f v(-1, 0, 0);
1175                                 f32 dtime;
1176                                 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1177                                 UASSERT(fabs(dtime - 1.000) < 0.001);
1178                         }
1179                         {
1180                                 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1181                                 aabb3f m(bx+2, by, bz, bx+3, by+1, bz+1);
1182                                 v3f v(1, 0, 0);
1183                                 f32 dtime;
1184                                 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == -1);
1185                         }
1186                         {
1187                                 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1188                                 aabb3f m(bx+2, by, bz+1.5, bx+3, by+1, bz+3.5);
1189                                 v3f v(-1, 0, 0);
1190                                 f32 dtime;
1191                                 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == -1);
1192                         }
1193                         {
1194                                 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1195                                 aabb3f m(bx+2, by-1.5, bz, bx+2.5, by-0.5, bz+1);
1196                                 v3f v(-0.5, 0.2, 0);
1197                                 f32 dtime;
1198                                 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 1);  // Y, not X!
1199                                 UASSERT(fabs(dtime - 2.500) < 0.001);
1200                         }
1201                         {
1202                                 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1203                                 aabb3f m(bx+2, by-1.5, bz, bx+2.5, by-0.5, bz+1);
1204                                 v3f v(-0.5, 0.3, 0);
1205                                 f32 dtime;
1206                                 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1207                                 UASSERT(fabs(dtime - 2.000) < 0.001);
1208                         }
1209
1210                         // TODO: Y-, Y+, Z-, Z+
1211
1212                         // misc
1213                         {
1214                                 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1215                                 aabb3f m(bx+2.3, by+2.29, bz+2.29, bx+4.2, by+4.2, bz+4.2);
1216                                 v3f v(-1./3, -1./3, -1./3);
1217                                 f32 dtime;
1218                                 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1219                                 UASSERT(fabs(dtime - 0.9) < 0.001);
1220                         }
1221                         {
1222                                 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1223                                 aabb3f m(bx+2.29, by+2.3, bz+2.29, bx+4.2, by+4.2, bz+4.2);
1224                                 v3f v(-1./3, -1./3, -1./3);
1225                                 f32 dtime;
1226                                 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 1);
1227                                 UASSERT(fabs(dtime - 0.9) < 0.001);
1228                         }
1229                         {
1230                                 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1231                                 aabb3f m(bx+2.29, by+2.29, bz+2.3, bx+4.2, by+4.2, bz+4.2);
1232                                 v3f v(-1./3, -1./3, -1./3);
1233                                 f32 dtime;
1234                                 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 2);
1235                                 UASSERT(fabs(dtime - 0.9) < 0.001);
1236                         }
1237                         {
1238                                 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1239                                 aabb3f m(bx-4.2, by-4.2, bz-4.2, bx-2.3, by-2.29, bz-2.29);
1240                                 v3f v(1./7, 1./7, 1./7);
1241                                 f32 dtime;
1242                                 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1243                                 UASSERT(fabs(dtime - 16.1) < 0.001);
1244                         }
1245                         {
1246                                 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1247                                 aabb3f m(bx-4.2, by-4.2, bz-4.2, bx-2.29, by-2.3, bz-2.29);
1248                                 v3f v(1./7, 1./7, 1./7);
1249                                 f32 dtime;
1250                                 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 1);
1251                                 UASSERT(fabs(dtime - 16.1) < 0.001);
1252                         }
1253                         {
1254                                 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1255                                 aabb3f m(bx-4.2, by-4.2, bz-4.2, bx-2.29, by-2.29, bz-2.3);
1256                                 v3f v(1./7, 1./7, 1./7);
1257                                 f32 dtime;
1258                                 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 2);
1259                                 UASSERT(fabs(dtime - 16.1) < 0.001);
1260                         }
1261                 }
1262         }
1263 };
1264
1265 struct TestSocket: public TestBase
1266 {
1267         void Run()
1268         {
1269                 const int port = 30003;
1270                 UDPSocket socket;
1271                 socket.Bind(port);
1272
1273                 const char sendbuffer[] = "hello world!";
1274                 socket.Send(Address(127,0,0,1,port), sendbuffer, sizeof(sendbuffer));
1275
1276                 sleep_ms(50);
1277
1278                 char rcvbuffer[256];
1279                 memset(rcvbuffer, 0, sizeof(rcvbuffer));
1280                 Address sender;
1281                 for(;;)
1282                 {
1283                         int bytes_read = socket.Receive(sender, rcvbuffer, sizeof(rcvbuffer));
1284                         if(bytes_read < 0)
1285                                 break;
1286                 }
1287                 //FIXME: This fails on some systems
1288                 UASSERT(strncmp(sendbuffer, rcvbuffer, sizeof(sendbuffer))==0);
1289                 UASSERT(sender.getAddress() == Address(127,0,0,1, 0).getAddress());
1290         }
1291 };
1292
1293 struct TestConnection: public TestBase
1294 {
1295         void TestHelpers()
1296         {
1297                 /*
1298                         Test helper functions
1299                 */
1300
1301                 // Some constants for testing
1302                 u32 proto_id = 0x12345678;
1303                 u16 peer_id = 123;
1304                 u8 channel = 2;
1305                 SharedBuffer<u8> data1(1);
1306                 data1[0] = 100;
1307                 Address a(127,0,0,1, 10);
1308                 u16 seqnum = 34352;
1309
1310                 con::BufferedPacket p1 = con::makePacket(a, data1,
1311                                 proto_id, peer_id, channel);
1312                 /*
1313                         We should now have a packet with this data:
1314                         Header:
1315                                 [0] u32 protocol_id
1316                                 [4] u16 sender_peer_id
1317                                 [6] u8 channel
1318                         Data:
1319                                 [7] u8 data1[0]
1320                 */
1321                 UASSERT(readU32(&p1.data[0]) == proto_id);
1322                 UASSERT(readU16(&p1.data[4]) == peer_id);
1323                 UASSERT(readU8(&p1.data[6]) == channel);
1324                 UASSERT(readU8(&p1.data[7]) == data1[0]);
1325                 
1326                 //infostream<<"initial data1[0]="<<((u32)data1[0]&0xff)<<std::endl;
1327
1328                 SharedBuffer<u8> p2 = con::makeReliablePacket(data1, seqnum);
1329
1330                 /*infostream<<"p2.getSize()="<<p2.getSize()<<", data1.getSize()="
1331                                 <<data1.getSize()<<std::endl;
1332                 infostream<<"readU8(&p2[3])="<<readU8(&p2[3])
1333                                 <<" p2[3]="<<((u32)p2[3]&0xff)<<std::endl;
1334                 infostream<<"data1[0]="<<((u32)data1[0]&0xff)<<std::endl;*/
1335
1336                 UASSERT(p2.getSize() == 3 + data1.getSize());
1337                 UASSERT(readU8(&p2[0]) == TYPE_RELIABLE);
1338                 UASSERT(readU16(&p2[1]) == seqnum);
1339                 UASSERT(readU8(&p2[3]) == data1[0]);
1340         }
1341
1342         struct Handler : public con::PeerHandler
1343         {
1344                 Handler(const char *a_name)
1345                 {
1346                         count = 0;
1347                         last_id = 0;
1348                         name = a_name;
1349                 }
1350                 void peerAdded(con::Peer *peer)
1351                 {
1352                         infostream<<"Handler("<<name<<")::peerAdded(): "
1353                                         "id="<<peer->id<<std::endl;
1354                         last_id = peer->id;
1355                         count++;
1356                 }
1357                 void deletingPeer(con::Peer *peer, bool timeout)
1358                 {
1359                         infostream<<"Handler("<<name<<")::deletingPeer(): "
1360                                         "id="<<peer->id
1361                                         <<", timeout="<<timeout<<std::endl;
1362                         last_id = peer->id;
1363                         count--;
1364                 }
1365
1366                 s32 count;
1367                 u16 last_id;
1368                 const char *name;
1369         };
1370
1371         void Run()
1372         {
1373                 DSTACK("TestConnection::Run");
1374
1375                 TestHelpers();
1376
1377                 /*
1378                         Test some real connections
1379
1380                         NOTE: This mostly tests the legacy interface.
1381                 */
1382
1383                 u32 proto_id = 0xad26846a;
1384
1385                 Handler hand_server("server");
1386                 Handler hand_client("client");
1387                 
1388                 infostream<<"** Creating server Connection"<<std::endl;
1389                 con::Connection server(proto_id, 512, 5.0, &hand_server);
1390                 server.Serve(30001);
1391                 
1392                 infostream<<"** Creating client Connection"<<std::endl;
1393                 con::Connection client(proto_id, 512, 5.0, &hand_client);
1394
1395                 UASSERT(hand_server.count == 0);
1396                 UASSERT(hand_client.count == 0);
1397                 
1398                 sleep_ms(50);
1399                 
1400                 Address server_address(127,0,0,1, 30001);
1401                 infostream<<"** running client.Connect()"<<std::endl;
1402                 client.Connect(server_address);
1403
1404                 sleep_ms(50);
1405                 
1406                 // Client should not have added client yet
1407                 UASSERT(hand_client.count == 0);
1408                 
1409                 try
1410                 {
1411                         u16 peer_id;
1412                         SharedBuffer<u8> data;
1413                         infostream<<"** running client.Receive()"<<std::endl;
1414                         u32 size = client.Receive(peer_id, data);
1415                         infostream<<"** Client received: peer_id="<<peer_id
1416                                         <<", size="<<size
1417                                         <<std::endl;
1418                 }
1419                 catch(con::NoIncomingDataException &e)
1420                 {
1421                 }
1422
1423                 // Client should have added server now
1424                 UASSERT(hand_client.count == 1);
1425                 UASSERT(hand_client.last_id == 1);
1426                 // Server should not have added client yet
1427                 UASSERT(hand_server.count == 0);
1428                 
1429                 sleep_ms(50);
1430
1431                 try
1432                 {
1433                         u16 peer_id;
1434                         SharedBuffer<u8> data;
1435                         infostream<<"** running server.Receive()"<<std::endl;
1436                         u32 size = server.Receive(peer_id, data);
1437                         infostream<<"** Server received: peer_id="<<peer_id
1438                                         <<", size="<<size
1439                                         <<std::endl;
1440                 }
1441                 catch(con::NoIncomingDataException &e)
1442                 {
1443                         // No actual data received, but the client has
1444                         // probably been connected
1445                 }
1446                 
1447                 // Client should be the same
1448                 UASSERT(hand_client.count == 1);
1449                 UASSERT(hand_client.last_id == 1);
1450                 // Server should have the client
1451                 UASSERT(hand_server.count == 1);
1452                 UASSERT(hand_server.last_id == 2);
1453                 
1454                 //sleep_ms(50);
1455
1456                 while(client.Connected() == false)
1457                 {
1458                         try
1459                         {
1460                                 u16 peer_id;
1461                                 SharedBuffer<u8> data;
1462                                 infostream<<"** running client.Receive()"<<std::endl;
1463                                 u32 size = client.Receive(peer_id, data);
1464                                 infostream<<"** Client received: peer_id="<<peer_id
1465                                                 <<", size="<<size
1466                                                 <<std::endl;
1467                         }
1468                         catch(con::NoIncomingDataException &e)
1469                         {
1470                         }
1471                         sleep_ms(50);
1472                 }
1473
1474                 sleep_ms(50);
1475                 
1476                 try
1477                 {
1478                         u16 peer_id;
1479                         SharedBuffer<u8> data;
1480                         infostream<<"** running server.Receive()"<<std::endl;
1481                         u32 size = server.Receive(peer_id, data);
1482                         infostream<<"** Server received: peer_id="<<peer_id
1483                                         <<", size="<<size
1484                                         <<std::endl;
1485                 }
1486                 catch(con::NoIncomingDataException &e)
1487                 {
1488                 }
1489 #if 1
1490                 /*
1491                         Simple send-receive test
1492                 */
1493                 {
1494                         /*u8 data[] = "Hello World!";
1495                         u32 datasize = sizeof(data);*/
1496                         SharedBuffer<u8> data = SharedBufferFromString("Hello World!");
1497
1498                         infostream<<"** running client.Send()"<<std::endl;
1499                         client.Send(PEER_ID_SERVER, 0, data, true);
1500
1501                         sleep_ms(50);
1502
1503                         u16 peer_id;
1504                         SharedBuffer<u8> recvdata;
1505                         infostream<<"** running server.Receive()"<<std::endl;
1506                         u32 size = server.Receive(peer_id, recvdata);
1507                         infostream<<"** Server received: peer_id="<<peer_id
1508                                         <<", size="<<size
1509                                         <<", data="<<*data
1510                                         <<std::endl;
1511                         UASSERT(memcmp(*data, *recvdata, data.getSize()) == 0);
1512                 }
1513 #endif
1514                 u16 peer_id_client = 2;
1515 #if 0
1516                 /*
1517                         Send consequent packets in different order
1518                         Not compatible with new Connection, thus commented out.
1519                 */
1520                 {
1521                         //u8 data1[] = "hello1";
1522                         //u8 data2[] = "hello2";
1523                         SharedBuffer<u8> data1 = SharedBufferFromString("hello1");
1524                         SharedBuffer<u8> data2 = SharedBufferFromString("Hello2");
1525
1526                         Address client_address =
1527                                         server.GetPeerAddress(peer_id_client);
1528                         
1529                         infostream<<"*** Sending packets in wrong order (2,1,2)"
1530                                         <<std::endl;
1531                         
1532                         u8 chn = 0;
1533                         con::Channel *ch = &server.getPeer(peer_id_client)->channels[chn];
1534                         u16 sn = ch->next_outgoing_seqnum;
1535                         ch->next_outgoing_seqnum = sn+1;
1536                         server.Send(peer_id_client, chn, data2, true);
1537                         ch->next_outgoing_seqnum = sn;
1538                         server.Send(peer_id_client, chn, data1, true);
1539                         ch->next_outgoing_seqnum = sn+1;
1540                         server.Send(peer_id_client, chn, data2, true);
1541
1542                         sleep_ms(50);
1543
1544                         infostream<<"*** Receiving the packets"<<std::endl;
1545
1546                         u16 peer_id;
1547                         SharedBuffer<u8> recvdata;
1548                         u32 size;
1549
1550                         infostream<<"** running client.Receive()"<<std::endl;
1551                         peer_id = 132;
1552                         size = client.Receive(peer_id, recvdata);
1553                         infostream<<"** Client received: peer_id="<<peer_id
1554                                         <<", size="<<size
1555                                         <<", data="<<*recvdata
1556                                         <<std::endl;
1557                         UASSERT(size == data1.getSize());
1558                         UASSERT(memcmp(*data1, *recvdata, data1.getSize()) == 0);
1559                         UASSERT(peer_id == PEER_ID_SERVER);
1560                         
1561                         infostream<<"** running client.Receive()"<<std::endl;
1562                         peer_id = 132;
1563                         size = client.Receive(peer_id, recvdata);
1564                         infostream<<"** Client received: peer_id="<<peer_id
1565                                         <<", size="<<size
1566                                         <<", data="<<*recvdata
1567                                         <<std::endl;
1568                         UASSERT(size == data2.getSize());
1569                         UASSERT(memcmp(*data2, *recvdata, data2.getSize()) == 0);
1570                         UASSERT(peer_id == PEER_ID_SERVER);
1571                         
1572                         bool got_exception = false;
1573                         try
1574                         {
1575                                 infostream<<"** running client.Receive()"<<std::endl;
1576                                 peer_id = 132;
1577                                 size = client.Receive(peer_id, recvdata);
1578                                 infostream<<"** Client received: peer_id="<<peer_id
1579                                                 <<", size="<<size
1580                                                 <<", data="<<*recvdata
1581                                                 <<std::endl;
1582                         }
1583                         catch(con::NoIncomingDataException &e)
1584                         {
1585                                 infostream<<"** No incoming data for client"<<std::endl;
1586                                 got_exception = true;
1587                         }
1588                         UASSERT(got_exception);
1589                 }
1590 #endif
1591 #if 0
1592                 /*
1593                         Send large amounts of packets (infinite test)
1594                         Commented out because of infinity.
1595                 */
1596                 {
1597                         infostream<<"Sending large amounts of packets (infinite test)"<<std::endl;
1598                         int sendcount = 0;
1599                         for(;;){
1600                                 int datasize = myrand_range(0,5)==0?myrand_range(100,10000):myrand_range(0,100);
1601                                 infostream<<"datasize="<<datasize<<std::endl;
1602                                 SharedBuffer<u8> data1(datasize);
1603                                 for(u16 i=0; i<datasize; i++)
1604                                         data1[i] = i/4;
1605                                 
1606                                 int sendtimes = myrand_range(1,10);
1607                                 for(int i=0; i<sendtimes; i++){
1608                                         server.Send(peer_id_client, 0, data1, true);
1609                                         sendcount++;
1610                                 }
1611                                 infostream<<"sendcount="<<sendcount<<std::endl;
1612                                 
1613                                 //int receivetimes = myrand_range(1,20);
1614                                 int receivetimes = 20;
1615                                 for(int i=0; i<receivetimes; i++){
1616                                         SharedBuffer<u8> recvdata;
1617                                         u16 peer_id = 132;
1618                                         u16 size = 0;
1619                                         bool received = false;
1620                                         try{
1621                                                 size = client.Receive(peer_id, recvdata);
1622                                                 received = true;
1623                                         }catch(con::NoIncomingDataException &e){
1624                                         }
1625                                 }
1626                         }
1627                 }
1628 #endif
1629                 /*
1630                         Send a large packet
1631                 */
1632                 {
1633                         const int datasize = 30000;
1634                         SharedBuffer<u8> data1(datasize);
1635                         for(u16 i=0; i<datasize; i++){
1636                                 data1[i] = i/4;
1637                         }
1638
1639                         infostream<<"Sending data (size="<<datasize<<"):";
1640                         for(int i=0; i<datasize && i<20; i++){
1641                                 if(i%2==0) infostream<<" ";
1642                                 char buf[10];
1643                                 snprintf(buf, 10, "%.2X", ((int)((const char*)*data1)[i])&0xff);
1644                                 infostream<<buf;
1645                         }
1646                         if(datasize>20)
1647                                 infostream<<"...";
1648                         infostream<<std::endl;
1649                         
1650                         server.Send(peer_id_client, 0, data1, true);
1651
1652                         //sleep_ms(3000);
1653                         
1654                         SharedBuffer<u8> recvdata;
1655                         infostream<<"** running client.Receive()"<<std::endl;
1656                         u16 peer_id = 132;
1657                         u16 size = 0;
1658                         bool received = false;
1659                         u32 timems0 = porting::getTimeMs();
1660                         for(;;){
1661                                 if(porting::getTimeMs() - timems0 > 5000 || received)
1662                                         break;
1663                                 try{
1664                                         size = client.Receive(peer_id, recvdata);
1665                                         received = true;
1666                                 }catch(con::NoIncomingDataException &e){
1667                                 }
1668                                 sleep_ms(10);
1669                         }
1670                         UASSERT(received);
1671                         infostream<<"** Client received: peer_id="<<peer_id
1672                                         <<", size="<<size
1673                                         <<std::endl;
1674
1675                         infostream<<"Received data (size="<<size<<"): ";
1676                         for(int i=0; i<size && i<20; i++){
1677                                 if(i%2==0) infostream<<" ";
1678                                 char buf[10];
1679                                 snprintf(buf, 10, "%.2X", ((int)(recvdata[i]))&0xff);
1680                                 infostream<<buf;
1681                         }
1682                         if(size>20)
1683                                 infostream<<"...";
1684                         infostream<<std::endl;
1685
1686                         UASSERT(memcmp(*data1, *recvdata, data1.getSize()) == 0);
1687                         UASSERT(peer_id == PEER_ID_SERVER);
1688                 }
1689                 
1690                 // Check peer handlers
1691                 UASSERT(hand_client.count == 1);
1692                 UASSERT(hand_client.last_id == 1);
1693                 UASSERT(hand_server.count == 1);
1694                 UASSERT(hand_server.last_id == 2);
1695                 
1696                 //assert(0);
1697         }
1698 };
1699
1700 #define TEST(X)\
1701 {\
1702         X x;\
1703         infostream<<"Running " #X <<std::endl;\
1704         x.Run();\
1705         tests_run++;\
1706         tests_failed += x.test_failed ? 1 : 0;\
1707 }
1708
1709 #define TESTPARAMS(X, ...)\
1710 {\
1711         X x;\
1712         infostream<<"Running " #X <<std::endl;\
1713         x.Run(__VA_ARGS__);\
1714         tests_run++;\
1715         tests_failed += x.test_failed ? 1 : 0;\
1716 }
1717
1718 void run_tests()
1719 {
1720         DSTACK(__FUNCTION_NAME);
1721
1722         int tests_run = 0;
1723         int tests_failed = 0;
1724         
1725         // Create item and node definitions
1726         IWritableItemDefManager *idef = createItemDefManager();
1727         IWritableNodeDefManager *ndef = createNodeDefManager();
1728         define_some_nodes(idef, ndef);
1729
1730         infostream<<"run_tests() started"<<std::endl;
1731         TEST(TestUtilities);
1732         TEST(TestSettings);
1733         TEST(TestCompress);
1734         TEST(TestSerialization);
1735         TESTPARAMS(TestMapNode, ndef);
1736         TESTPARAMS(TestVoxelManipulator, ndef);
1737         TESTPARAMS(TestVoxelAlgorithms, ndef);
1738         TESTPARAMS(TestInventory, idef);
1739         //TEST(TestMapBlock);
1740         //TEST(TestMapSector);
1741         TEST(TestCollision);
1742         if(INTERNET_SIMULATOR == false){
1743                 TEST(TestSocket);
1744                 dout_con<<"=== BEGIN RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
1745                 TEST(TestConnection);
1746                 dout_con<<"=== END RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
1747         }
1748         if(tests_failed == 0){
1749                 infostream<<"run_tests(): "<<tests_failed<<" / "<<tests_run<<" tests failed."<<std::endl;
1750                 infostream<<"run_tests() passed."<<std::endl;
1751                 return;
1752         } else {
1753                 errorstream<<"run_tests(): "<<tests_failed<<" / "<<tests_run<<" tests failed."<<std::endl;
1754                 errorstream<<"run_tests() aborting."<<std::endl;
1755                 abort();
1756         }
1757 }
1758