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