3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
21 #include "irrlichttypes_extrabloated.h"
27 #include "connection.h"
28 #include "serialization.h"
30 #include "collision.h"
33 #include "content_mapnode.h"
35 #include "mapsector.h"
38 #include "util/string.h"
40 #include "voxelalgorithms.h"
41 #include "inventory.h"
42 #include "util/numeric.h"
43 #include "util/serialize.h"
44 #include "noise.h" // PseudoRandom used for random data for compression
45 #include "clientserver.h" // LATEST_PROTOCOL_VERSION
49 Asserts that the exception occurs
51 #define EXCEPTION_CHECK(EType, code)\
53 bool exception_thrown = false;\
55 catch(EType &e) { exception_thrown = true; }\
56 UASSERT(exception_thrown);\
59 #define UTEST(x, fmt, ...)\
62 LOGLINEF(LMT_ERROR, "Test (%s) failed: " fmt, #x, ##__VA_ARGS__);\
67 #define UASSERT(x) UTEST(x, "UASSERT")
70 A few item and node definitions for those tests that need them
73 static content_t CONTENT_STONE;
74 static content_t CONTENT_GRASS;
75 static content_t CONTENT_TORCH;
77 void define_some_nodes(IWritableItemDefManager *idef, IWritableNodeDefManager *ndef)
79 ItemDefinition itemdef;
85 itemdef = ItemDefinition();
86 itemdef.type = ITEM_NODE;
87 itemdef.name = "default:stone";
88 itemdef.description = "Stone";
89 itemdef.groups["cracky"] = 3;
90 itemdef.inventory_image = "[inventorycube"
94 f = ContentFeatures();
95 f.name = itemdef.name;
96 for(int i = 0; i < 6; i++)
97 f.tiledef[i].name = "default_stone.png";
98 f.is_ground_content = true;
99 idef->registerItem(itemdef);
100 CONTENT_STONE = ndef->set(f.name, f);
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"
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 CONTENT_GRASS = ndef->set(f.name, f);
125 Torch (minimal definition for lighting tests)
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 CONTENT_TORCH = ndef->set(f.name, f);
148 struct TestUtilities: public TestBase
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(trim("\n \t\r Foo bAR \r\n\t\t ") == "Foo bAR");
161 UASSERT(trim("\n \t\r \r\n\t\t ") == "");
162 UASSERT(is_yes("YeS") == true);
163 UASSERT(is_yes("") == false);
164 UASSERT(is_yes("FAlse") == false);
165 UASSERT(is_yes("-1") == true);
166 UASSERT(is_yes("0") == false);
167 UASSERT(is_yes("1") == true);
168 UASSERT(is_yes("2") == true);
169 const char *ends[] = {"abc", "c", "bc", NULL};
170 UASSERT(removeStringEnd("abc", ends) == "");
171 UASSERT(removeStringEnd("bc", ends) == "b");
172 UASSERT(removeStringEnd("12c", ends) == "12");
173 UASSERT(removeStringEnd("foo", ends) == "");
174 UASSERT(urlencode("\"Aardvarks lurk, OK?\"")
175 == "%22Aardvarks%20lurk%2C%20OK%3F%22");
176 UASSERT(urldecode("%22Aardvarks%20lurk%2C%20OK%3F%22")
177 == "\"Aardvarks lurk, OK?\"");
181 struct TestPath: public TestBase
183 // adjusts a POSIX path to system-specific conventions
184 // -> changes '/' to DIR_DELIM
185 // -> absolute paths start with "C:\\" on windows
186 std::string p(std::string path)
188 for(size_t i = 0; i < path.size(); ++i){
190 path.replace(i, 1, DIR_DELIM);
191 i += std::string(DIR_DELIM).size() - 1; // generally a no-op
205 std::string path, result, removed;
208 Test fs::IsDirDelimiter
210 UASSERT(fs::IsDirDelimiter('/') == true);
211 UASSERT(fs::IsDirDelimiter('A') == false);
212 UASSERT(fs::IsDirDelimiter(0) == false);
214 UASSERT(fs::IsDirDelimiter('\\') == true);
216 UASSERT(fs::IsDirDelimiter('\\') == false);
220 Test fs::PathStartsWith
223 const int numpaths = 12;
224 std::string paths[numpaths] = {
227 p("/home/user/minetest"),
228 p("/home/user/minetest/bin"),
229 p("/home/user/.minetest"),
235 p("/home/user2/minetest/worlds"),
236 p("/home/user2/minetest/world"),
239 expected fs::PathStartsWith results
242 2 = returns false on windows, false elsewhere
243 3 = returns true on windows, true elsewhere
244 4 = returns true if and only if
245 FILESYS_CASE_INSENSITIVE is true
247 int expected_results[numpaths][numpaths] = {
248 {1,2,0,0,0,0,0,0,0,0,0,0},
249 {1,1,0,0,0,0,0,0,0,0,0,0},
250 {1,1,1,0,0,0,0,0,0,0,0,0},
251 {1,1,1,1,0,0,0,0,0,0,0,0},
252 {1,1,0,0,1,0,0,0,0,0,0,0},
253 {1,1,0,0,0,1,0,0,1,1,0,0},
254 {1,1,0,0,0,0,1,4,1,0,0,0},
255 {1,1,0,0,0,0,4,1,4,0,0,0},
256 {1,1,0,0,0,0,0,0,1,0,0,0},
257 {1,1,0,0,0,0,0,0,1,1,0,0},
258 {1,1,0,0,0,0,0,0,0,0,1,0},
259 {1,1,0,0,0,0,0,0,0,0,0,1},
262 for (int i = 0; i < numpaths; i++)
263 for (int j = 0; j < numpaths; j++){
264 /*verbosestream<<"testing fs::PathStartsWith(\""
266 <<paths[j]<<"\")"<<std::endl;*/
267 bool starts = fs::PathStartsWith(paths[i], paths[j]);
268 int expected = expected_results[i][j];
270 UASSERT(starts == false);
272 else if(expected == 1){
273 UASSERT(starts == true);
276 else if(expected == 2){
277 UASSERT(starts == false);
279 else if(expected == 3){
280 UASSERT(starts == true);
283 else if(expected == 2){
284 UASSERT(starts == true);
286 else if(expected == 3){
287 UASSERT(starts == false);
290 else if(expected == 4){
291 UASSERT(starts == (bool)FILESYS_CASE_INSENSITIVE);
297 Test fs::RemoveLastPathComponent
299 UASSERT(fs::RemoveLastPathComponent("") == "");
300 path = p("/home/user/minetest/bin/..//worlds/world1");
301 result = fs::RemoveLastPathComponent(path, &removed, 0);
302 UASSERT(result == path);
303 UASSERT(removed == "");
304 result = fs::RemoveLastPathComponent(path, &removed, 1);
305 UASSERT(result == p("/home/user/minetest/bin/..//worlds"));
306 UASSERT(removed == p("world1"));
307 result = fs::RemoveLastPathComponent(path, &removed, 2);
308 UASSERT(result == p("/home/user/minetest/bin/.."));
309 UASSERT(removed == p("worlds/world1"));
310 result = fs::RemoveLastPathComponent(path, &removed, 3);
311 UASSERT(result == p("/home/user/minetest/bin"));
312 UASSERT(removed == p("../worlds/world1"));
313 result = fs::RemoveLastPathComponent(path, &removed, 4);
314 UASSERT(result == p("/home/user/minetest"));
315 UASSERT(removed == p("bin/../worlds/world1"));
316 result = fs::RemoveLastPathComponent(path, &removed, 5);
317 UASSERT(result == p("/home/user"));
318 UASSERT(removed == p("minetest/bin/../worlds/world1"));
319 result = fs::RemoveLastPathComponent(path, &removed, 6);
320 UASSERT(result == p("/home"));
321 UASSERT(removed == p("user/minetest/bin/../worlds/world1"));
322 result = fs::RemoveLastPathComponent(path, &removed, 7);
324 UASSERT(result == "C:");
326 UASSERT(result == "");
328 UASSERT(removed == p("home/user/minetest/bin/../worlds/world1"));
331 Now repeat the test with a trailing delimiter
333 path = p("/home/user/minetest/bin/..//worlds/world1/");
334 result = fs::RemoveLastPathComponent(path, &removed, 0);
335 UASSERT(result == path);
336 UASSERT(removed == "");
337 result = fs::RemoveLastPathComponent(path, &removed, 1);
338 UASSERT(result == p("/home/user/minetest/bin/..//worlds"));
339 UASSERT(removed == p("world1"));
340 result = fs::RemoveLastPathComponent(path, &removed, 2);
341 UASSERT(result == p("/home/user/minetest/bin/.."));
342 UASSERT(removed == p("worlds/world1"));
343 result = fs::RemoveLastPathComponent(path, &removed, 3);
344 UASSERT(result == p("/home/user/minetest/bin"));
345 UASSERT(removed == p("../worlds/world1"));
346 result = fs::RemoveLastPathComponent(path, &removed, 4);
347 UASSERT(result == p("/home/user/minetest"));
348 UASSERT(removed == p("bin/../worlds/world1"));
349 result = fs::RemoveLastPathComponent(path, &removed, 5);
350 UASSERT(result == p("/home/user"));
351 UASSERT(removed == p("minetest/bin/../worlds/world1"));
352 result = fs::RemoveLastPathComponent(path, &removed, 6);
353 UASSERT(result == p("/home"));
354 UASSERT(removed == p("user/minetest/bin/../worlds/world1"));
355 result = fs::RemoveLastPathComponent(path, &removed, 7);
357 UASSERT(result == "C:");
359 UASSERT(result == "");
361 UASSERT(removed == p("home/user/minetest/bin/../worlds/world1"));
364 Test fs::RemoveRelativePathComponent
366 path = p("/home/user/minetest/bin");
367 result = fs::RemoveRelativePathComponents(path);
368 UASSERT(result == path);
369 path = p("/home/user/minetest/bin/../worlds/world1");
370 result = fs::RemoveRelativePathComponents(path);
371 UASSERT(result == p("/home/user/minetest/worlds/world1"));
372 path = p("/home/user/minetest/bin/../worlds/world1/");
373 result = fs::RemoveRelativePathComponents(path);
374 UASSERT(result == p("/home/user/minetest/worlds/world1"));
376 result = fs::RemoveRelativePathComponents(path);
377 UASSERT(result == "");
378 path = p("./subdir/../..");
379 result = fs::RemoveRelativePathComponents(path);
380 UASSERT(result == "");
381 path = p("/a/b/c/.././../d/../e/f/g/../h/i/j/../../../..");
382 result = fs::RemoveRelativePathComponents(path);
383 UASSERT(result == p("/a/e"));
387 struct TestSettings: public TestBase
392 // Test reading of settings
393 std::istringstream is(
395 "leetleet = 13371337\n"
396 "leetleet_neg = -13371337\n"
397 "floaty_thing = 1.1\n"
398 "stringy_thing = asd /( ¤%&(/\" BLÖÄRP\n"
399 "coord = (1, 2, 4.5)");
400 s.parseConfigLines(is);
401 UASSERT(s.getS32("leet") == 1337);
402 UASSERT(s.getS16("leetleet") == 32767);
403 UASSERT(s.getS16("leetleet_neg") == -32768);
404 // Not sure if 1.1 is an exact value as a float, but doesn't matter
405 UASSERT(fabs(s.getFloat("floaty_thing") - 1.1) < 0.001);
406 UASSERT(s.get("stringy_thing") == "asd /( ¤%&(/\" BLÖÄRP");
407 UASSERT(fabs(s.getV3F("coord").X - 1.0) < 0.001);
408 UASSERT(fabs(s.getV3F("coord").Y - 2.0) < 0.001);
409 UASSERT(fabs(s.getV3F("coord").Z - 4.5) < 0.001);
410 // Test the setting of settings too
411 s.setFloat("floaty_thing_2", 1.2);
412 s.setV3F("coord2", v3f(1, 2, 3.3));
413 UASSERT(s.get("floaty_thing_2").substr(0,3) == "1.2");
414 UASSERT(fabs(s.getFloat("floaty_thing_2") - 1.2) < 0.001);
415 UASSERT(fabs(s.getV3F("coord2").X - 1.0) < 0.001);
416 UASSERT(fabs(s.getV3F("coord2").Y - 2.0) < 0.001);
417 UASSERT(fabs(s.getV3F("coord2").Z - 3.3) < 0.001);
421 struct TestSerialization: public TestBase
423 // To be used like this:
424 // mkstr("Some\0string\0with\0embedded\0nuls")
425 // since std::string("...") doesn't work as expected in that case.
426 template<size_t N> std::string mkstr(const char (&s)[N])
428 return std::string(s, N - 1);
433 // Tests some serialization primitives
435 UASSERT(serializeString("") == mkstr("\0\0"));
436 UASSERT(serializeWideString(L"") == mkstr("\0\0"));
437 UASSERT(serializeLongString("") == mkstr("\0\0\0\0"));
438 UASSERT(serializeJsonString("") == "\"\"");
440 std::string teststring = "Hello world!";
441 UASSERT(serializeString(teststring) ==
442 mkstr("\0\14Hello world!"));
443 UASSERT(serializeWideString(narrow_to_wide(teststring)) ==
444 mkstr("\0\14\0H\0e\0l\0l\0o\0 \0w\0o\0r\0l\0d\0!"));
445 UASSERT(serializeLongString(teststring) ==
446 mkstr("\0\0\0\14Hello world!"));
447 UASSERT(serializeJsonString(teststring) ==
450 std::string teststring2;
451 std::wstring teststring2_w;
452 std::string teststring2_w_encoded;
454 std::ostringstream tmp_os;
455 std::wostringstream tmp_os_w;
456 std::ostringstream tmp_os_w_encoded;
457 for(int i = 0; i < 256; i++)
460 tmp_os_w<<(wchar_t)i;
461 tmp_os_w_encoded<<(char)0<<(char)i;
463 teststring2 = tmp_os.str();
464 teststring2_w = tmp_os_w.str();
465 teststring2_w_encoded = tmp_os_w_encoded.str();
467 UASSERT(serializeString(teststring2) ==
468 mkstr("\1\0") + teststring2);
469 UASSERT(serializeWideString(teststring2_w) ==
470 mkstr("\1\0") + teststring2_w_encoded);
471 UASSERT(serializeLongString(teststring2) ==
472 mkstr("\0\0\1\0") + teststring2);
473 // MSVC fails when directly using "\\\\"
474 std::string backslash = "\\";
475 UASSERT(serializeJsonString(teststring2) ==
477 "\\u0000\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007" +
478 "\\b\\t\\n\\u000b\\f\\r\\u000e\\u000f" +
479 "\\u0010\\u0011\\u0012\\u0013\\u0014\\u0015\\u0016\\u0017" +
480 "\\u0018\\u0019\\u001a\\u001b\\u001c\\u001d\\u001e\\u001f" +
481 " !\\\"" + teststring2.substr(0x23, 0x2f-0x23) +
482 "\\/" + teststring2.substr(0x30, 0x5c-0x30) +
483 backslash + backslash + teststring2.substr(0x5d, 0x7f-0x5d) + "\\u007f" +
484 "\\u0080\\u0081\\u0082\\u0083\\u0084\\u0085\\u0086\\u0087" +
485 "\\u0088\\u0089\\u008a\\u008b\\u008c\\u008d\\u008e\\u008f" +
486 "\\u0090\\u0091\\u0092\\u0093\\u0094\\u0095\\u0096\\u0097" +
487 "\\u0098\\u0099\\u009a\\u009b\\u009c\\u009d\\u009e\\u009f" +
488 "\\u00a0\\u00a1\\u00a2\\u00a3\\u00a4\\u00a5\\u00a6\\u00a7" +
489 "\\u00a8\\u00a9\\u00aa\\u00ab\\u00ac\\u00ad\\u00ae\\u00af" +
490 "\\u00b0\\u00b1\\u00b2\\u00b3\\u00b4\\u00b5\\u00b6\\u00b7" +
491 "\\u00b8\\u00b9\\u00ba\\u00bb\\u00bc\\u00bd\\u00be\\u00bf" +
492 "\\u00c0\\u00c1\\u00c2\\u00c3\\u00c4\\u00c5\\u00c6\\u00c7" +
493 "\\u00c8\\u00c9\\u00ca\\u00cb\\u00cc\\u00cd\\u00ce\\u00cf" +
494 "\\u00d0\\u00d1\\u00d2\\u00d3\\u00d4\\u00d5\\u00d6\\u00d7" +
495 "\\u00d8\\u00d9\\u00da\\u00db\\u00dc\\u00dd\\u00de\\u00df" +
496 "\\u00e0\\u00e1\\u00e2\\u00e3\\u00e4\\u00e5\\u00e6\\u00e7" +
497 "\\u00e8\\u00e9\\u00ea\\u00eb\\u00ec\\u00ed\\u00ee\\u00ef" +
498 "\\u00f0\\u00f1\\u00f2\\u00f3\\u00f4\\u00f5\\u00f6\\u00f7" +
499 "\\u00f8\\u00f9\\u00fa\\u00fb\\u00fc\\u00fd\\u00fe\\u00ff" +
503 std::istringstream is(serializeString(teststring2), std::ios::binary);
504 UASSERT(deSerializeString(is) == teststring2);
510 std::istringstream is(serializeWideString(teststring2_w), std::ios::binary);
511 UASSERT(deSerializeWideString(is) == teststring2_w);
517 std::istringstream is(serializeLongString(teststring2), std::ios::binary);
518 UASSERT(deSerializeLongString(is) == teststring2);
524 std::istringstream is(serializeJsonString(teststring2), std::ios::binary);
525 //dstream<<serializeJsonString(deSerializeJsonString(is));
526 UASSERT(deSerializeJsonString(is) == teststring2);
534 struct TestNodedefSerialization: public TestBase
539 f.name = "default:stone";
540 for(int i = 0; i < 6; i++)
541 f.tiledef[i].name = "default_stone.png";
542 f.is_ground_content = true;
543 std::ostringstream os(std::ios::binary);
544 f.serialize(os, LATEST_PROTOCOL_VERSION);
545 verbosestream<<"Test ContentFeatures size: "<<os.str().size()<<std::endl;
546 std::istringstream is(os.str(), std::ios::binary);
549 UASSERT(f.walkable == f2.walkable);
550 UASSERT(f.node_box.type == f2.node_box.type);
554 struct TestCompress: public TestBase
560 SharedBuffer<u8> fromdata(4);
566 std::ostringstream os(std::ios_base::binary);
567 compress(fromdata, os, 0);
569 std::string str_out = os.str();
571 infostream<<"str_out.size()="<<str_out.size()<<std::endl;
572 infostream<<"TestCompress: 1,5,5,1 -> ";
573 for(u32 i=0; i<str_out.size(); i++)
575 infostream<<(u32)str_out[i]<<",";
577 infostream<<std::endl;
579 UASSERT(str_out.size() == 10);
581 UASSERT(str_out[0] == 0);
582 UASSERT(str_out[1] == 0);
583 UASSERT(str_out[2] == 0);
584 UASSERT(str_out[3] == 4);
585 UASSERT(str_out[4] == 0);
586 UASSERT(str_out[5] == 1);
587 UASSERT(str_out[6] == 1);
588 UASSERT(str_out[7] == 5);
589 UASSERT(str_out[8] == 0);
590 UASSERT(str_out[9] == 1);
592 std::istringstream is(str_out, std::ios_base::binary);
593 std::ostringstream os2(std::ios_base::binary);
595 decompress(is, os2, 0);
596 std::string str_out2 = os2.str();
598 infostream<<"decompress: ";
599 for(u32 i=0; i<str_out2.size(); i++)
601 infostream<<(u32)str_out2[i]<<",";
603 infostream<<std::endl;
605 UASSERT(str_out2.size() == fromdata.getSize());
607 for(u32 i=0; i<str_out2.size(); i++)
609 UASSERT(str_out2[i] == fromdata[i]);
616 SharedBuffer<u8> fromdata(4);
622 std::ostringstream os(std::ios_base::binary);
623 compress(fromdata, os, SER_FMT_VER_HIGHEST_READ);
625 std::string str_out = os.str();
627 infostream<<"str_out.size()="<<str_out.size()<<std::endl;
628 infostream<<"TestCompress: 1,5,5,1 -> ";
629 for(u32 i=0; i<str_out.size(); i++)
631 infostream<<(u32)str_out[i]<<",";
633 infostream<<std::endl;
635 std::istringstream is(str_out, std::ios_base::binary);
636 std::ostringstream os2(std::ios_base::binary);
638 decompress(is, os2, SER_FMT_VER_HIGHEST_READ);
639 std::string str_out2 = os2.str();
641 infostream<<"decompress: ";
642 for(u32 i=0; i<str_out2.size(); i++)
644 infostream<<(u32)str_out2[i]<<",";
646 infostream<<std::endl;
648 UASSERT(str_out2.size() == fromdata.getSize());
650 for(u32 i=0; i<str_out2.size(); i++)
652 UASSERT(str_out2[i] == fromdata[i]);
657 // Test zlib wrapper with large amounts of data (larger than its
660 infostream<<"Test: Testing zlib wrappers with a large amount "
661 <<"of pseudorandom data"<<std::endl;
663 infostream<<"Test: Input size of large compressZlib is "
666 data_in.resize(size);
667 PseudoRandom pseudorandom(9420);
668 for(u32 i=0; i<size; i++)
669 data_in[i] = pseudorandom.range(0,255);
670 std::ostringstream os_compressed(std::ios::binary);
671 compressZlib(data_in, os_compressed);
672 infostream<<"Test: Output size of large compressZlib is "
673 <<os_compressed.str().size()<<std::endl;
674 std::istringstream is_compressed(os_compressed.str(), std::ios::binary);
675 std::ostringstream os_decompressed(std::ios::binary);
676 decompressZlib(is_compressed, os_decompressed);
677 infostream<<"Test: Output size of large decompressZlib is "
678 <<os_decompressed.str().size()<<std::endl;
679 std::string str_decompressed = os_decompressed.str();
680 UTEST(str_decompressed.size() == data_in.size(), "Output size not"
681 " equal (output: %u, input: %u)",
682 (unsigned int)str_decompressed.size(), (unsigned int)data_in.size());
683 for(u32 i=0; i<size && i<str_decompressed.size(); i++){
684 UTEST(str_decompressed[i] == data_in[i],
685 "index out[%i]=%i differs from in[%i]=%i",
686 i, str_decompressed[i], i, data_in[i]);
692 struct TestMapNode: public TestBase
694 void Run(INodeDefManager *nodedef)
699 UASSERT(n.getContent() == CONTENT_AIR);
700 UASSERT(n.getLight(LIGHTBANK_DAY, nodedef) == 0);
701 UASSERT(n.getLight(LIGHTBANK_NIGHT, nodedef) == 0);
704 n.setContent(CONTENT_AIR);
705 UASSERT(nodedef->get(n).light_propagates == true);
706 n.setContent(LEGN(nodedef, "CONTENT_STONE"));
707 UASSERT(nodedef->get(n).light_propagates == false);
711 struct TestVoxelManipulator: public TestBase
713 void Run(INodeDefManager *nodedef)
719 VoxelArea a(v3s16(-1,-1,-1), v3s16(1,1,1));
720 UASSERT(a.index(0,0,0) == 1*3*3 + 1*3 + 1);
721 UASSERT(a.index(-1,-1,-1) == 0);
723 VoxelArea c(v3s16(-2,-2,-2), v3s16(2,2,2));
724 // An area that is 1 bigger in x+ and z-
725 VoxelArea d(v3s16(-2,-2,-3), v3s16(3,2,2));
727 std::list<VoxelArea> aa;
731 std::vector<VoxelArea> results;
732 results.push_back(VoxelArea(v3s16(-2,-2,-3),v3s16(3,2,-3)));
733 results.push_back(VoxelArea(v3s16(3,-2,-2),v3s16(3,2,2)));
735 UASSERT(aa.size() == results.size());
737 infostream<<"Result of diff:"<<std::endl;
738 for(std::list<VoxelArea>::const_iterator
739 i = aa.begin(); i != aa.end(); ++i)
741 i->print(infostream);
742 infostream<<std::endl;
744 std::vector<VoxelArea>::iterator j = std::find(results.begin(), results.end(), *i);
745 UASSERT(j != results.end());
756 v.print(infostream, nodedef);
758 infostream<<"*** Setting (-1,0,-1)=2 ***"<<std::endl;
760 v.setNodeNoRef(v3s16(-1,0,-1), MapNode(CONTENT_GRASS));
762 v.print(infostream, nodedef);
764 UASSERT(v.getNode(v3s16(-1,0,-1)).getContent() == CONTENT_GRASS);
766 infostream<<"*** Reading from inexistent (0,0,-1) ***"<<std::endl;
768 EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,0,-1)));
770 v.print(infostream, nodedef);
772 infostream<<"*** Adding area ***"<<std::endl;
776 v.print(infostream, nodedef);
778 UASSERT(v.getNode(v3s16(-1,0,-1)).getContent() == CONTENT_GRASS);
779 EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,1,1)));
783 struct TestVoxelAlgorithms: public TestBase
785 void Run(INodeDefManager *ndef)
788 voxalgo::propagateSunlight
792 for(u16 z=0; z<3; z++)
793 for(u16 y=0; y<3; y++)
794 for(u16 x=0; x<3; x++)
797 v.setNodeNoRef(p, MapNode(CONTENT_AIR));
799 VoxelArea a(v3s16(0,0,0), v3s16(2,2,2));
801 std::set<v3s16> light_sources;
802 voxalgo::setLight(v, a, 0, ndef);
803 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
804 v, a, true, light_sources, ndef);
805 //v.print(dstream, ndef, VOXELPRINT_LIGHT_DAY);
806 UASSERT(res.bottom_sunlight_valid == true);
807 UASSERT(v.getNode(v3s16(1,1,1)).getLight(LIGHTBANK_DAY, ndef)
810 v.setNodeNoRef(v3s16(0,0,0), MapNode(CONTENT_STONE));
812 std::set<v3s16> light_sources;
813 voxalgo::setLight(v, a, 0, ndef);
814 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
815 v, a, true, light_sources, ndef);
816 UASSERT(res.bottom_sunlight_valid == true);
817 UASSERT(v.getNode(v3s16(1,1,1)).getLight(LIGHTBANK_DAY, ndef)
821 std::set<v3s16> light_sources;
822 voxalgo::setLight(v, a, 0, ndef);
823 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
824 v, a, false, light_sources, ndef);
825 UASSERT(res.bottom_sunlight_valid == true);
826 UASSERT(v.getNode(v3s16(2,0,2)).getLight(LIGHTBANK_DAY, ndef)
829 v.setNodeNoRef(v3s16(1,3,2), MapNode(CONTENT_STONE));
831 std::set<v3s16> light_sources;
832 voxalgo::setLight(v, a, 0, ndef);
833 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
834 v, a, true, light_sources, ndef);
835 UASSERT(res.bottom_sunlight_valid == true);
836 UASSERT(v.getNode(v3s16(1,1,2)).getLight(LIGHTBANK_DAY, ndef)
840 std::set<v3s16> light_sources;
841 voxalgo::setLight(v, a, 0, ndef);
842 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
843 v, a, false, light_sources, ndef);
844 UASSERT(res.bottom_sunlight_valid == true);
845 UASSERT(v.getNode(v3s16(1,0,2)).getLight(LIGHTBANK_DAY, ndef)
849 MapNode n(CONTENT_AIR);
850 n.setLight(LIGHTBANK_DAY, 10, ndef);
851 v.setNodeNoRef(v3s16(1,-1,2), n);
854 std::set<v3s16> light_sources;
855 voxalgo::setLight(v, a, 0, ndef);
856 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
857 v, a, true, light_sources, ndef);
858 UASSERT(res.bottom_sunlight_valid == true);
861 std::set<v3s16> light_sources;
862 voxalgo::setLight(v, a, 0, ndef);
863 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
864 v, a, false, light_sources, ndef);
865 UASSERT(res.bottom_sunlight_valid == true);
868 MapNode n(CONTENT_AIR);
869 n.setLight(LIGHTBANK_DAY, LIGHT_SUN, ndef);
870 v.setNodeNoRef(v3s16(1,-1,2), n);
873 std::set<v3s16> light_sources;
874 voxalgo::setLight(v, a, 0, ndef);
875 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
876 v, a, true, light_sources, ndef);
877 UASSERT(res.bottom_sunlight_valid == false);
880 std::set<v3s16> light_sources;
881 voxalgo::setLight(v, a, 0, ndef);
882 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
883 v, a, false, light_sources, ndef);
884 UASSERT(res.bottom_sunlight_valid == false);
886 v.setNodeNoRef(v3s16(1,3,2), MapNode(CONTENT_IGNORE));
888 std::set<v3s16> light_sources;
889 voxalgo::setLight(v, a, 0, ndef);
890 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
891 v, a, true, light_sources, ndef);
892 UASSERT(res.bottom_sunlight_valid == true);
896 voxalgo::clearLightAndCollectSources
900 for(u16 z=0; z<3; z++)
901 for(u16 y=0; y<3; y++)
902 for(u16 x=0; x<3; x++)
905 v.setNode(p, MapNode(CONTENT_AIR));
907 VoxelArea a(v3s16(0,0,0), v3s16(2,2,2));
908 v.setNodeNoRef(v3s16(0,0,0), MapNode(CONTENT_STONE));
909 v.setNodeNoRef(v3s16(1,1,1), MapNode(CONTENT_TORCH));
911 MapNode n(CONTENT_AIR);
912 n.setLight(LIGHTBANK_DAY, 1, ndef);
913 v.setNode(v3s16(1,1,2), n);
916 std::set<v3s16> light_sources;
917 std::map<v3s16, u8> unlight_from;
918 voxalgo::clearLightAndCollectSources(v, a, LIGHTBANK_DAY,
919 ndef, light_sources, unlight_from);
920 //v.print(dstream, ndef, VOXELPRINT_LIGHT_DAY);
921 UASSERT(v.getNode(v3s16(0,1,1)).getLight(LIGHTBANK_DAY, ndef)
923 UASSERT(light_sources.find(v3s16(1,1,1)) != light_sources.end());
924 UASSERT(light_sources.size() == 1);
925 UASSERT(unlight_from.find(v3s16(1,1,2)) != unlight_from.end());
926 UASSERT(unlight_from.size() == 1);
932 struct TestInventory: public TestBase
934 void Run(IItemDefManager *idef)
936 std::string serialized_inventory =
948 "Item default:cobble 61\n"
955 "Item default:dirt 71\n"
963 "Item default:dirt 99\n"
964 "Item default:cobble 38\n"
974 std::string serialized_inventory_2 =
986 "Item default:cobble 61\n"
993 "Item default:dirt 71\n"
1001 "Item default:dirt 99\n"
1002 "Item default:cobble 38\n"
1009 "EndInventoryList\n"
1012 Inventory inv(idef);
1013 std::istringstream is(serialized_inventory, std::ios::binary);
1014 inv.deSerialize(is);
1015 UASSERT(inv.getList("0"));
1016 UASSERT(!inv.getList("main"));
1017 inv.getList("0")->setName("main");
1018 UASSERT(!inv.getList("0"));
1019 UASSERT(inv.getList("main"));
1020 UASSERT(inv.getList("main")->getWidth() == 3);
1021 inv.getList("main")->setWidth(5);
1022 std::ostringstream inv_os(std::ios::binary);
1023 inv.serialize(inv_os);
1024 UASSERT(inv_os.str() == serialized_inventory_2);
1029 NOTE: These tests became non-working then NodeContainer was removed.
1030 These should be redone, utilizing some kind of a virtual
1031 interface for Map (IMap would be fine).
1034 struct TestMapBlock: public TestBase
1036 class TC : public NodeContainer
1041 bool position_valid;
1042 core::list<v3s16> validity_exceptions;
1046 position_valid = true;
1049 virtual bool isValidPosition(v3s16 p)
1051 //return position_valid ^ (p==position_valid_exception);
1052 bool exception = false;
1053 for(core::list<v3s16>::Iterator i=validity_exceptions.begin();
1054 i != validity_exceptions.end(); i++)
1062 return exception ? !position_valid : position_valid;
1065 virtual MapNode getNode(v3s16 p)
1067 if(isValidPosition(p) == false)
1068 throw InvalidPositionException();
1072 virtual void setNode(v3s16 p, MapNode & n)
1074 if(isValidPosition(p) == false)
1075 throw InvalidPositionException();
1078 virtual u16 nodeContainerId() const
1088 MapBlock b(&parent, v3s16(1,1,1));
1089 v3s16 relpos(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
1091 UASSERT(b.getPosRelative() == relpos);
1093 UASSERT(b.getBox().MinEdge.X == MAP_BLOCKSIZE);
1094 UASSERT(b.getBox().MaxEdge.X == MAP_BLOCKSIZE*2-1);
1095 UASSERT(b.getBox().MinEdge.Y == MAP_BLOCKSIZE);
1096 UASSERT(b.getBox().MaxEdge.Y == MAP_BLOCKSIZE*2-1);
1097 UASSERT(b.getBox().MinEdge.Z == MAP_BLOCKSIZE);
1098 UASSERT(b.getBox().MaxEdge.Z == MAP_BLOCKSIZE*2-1);
1100 UASSERT(b.isValidPosition(v3s16(0,0,0)) == true);
1101 UASSERT(b.isValidPosition(v3s16(-1,0,0)) == false);
1102 UASSERT(b.isValidPosition(v3s16(-1,-142,-2341)) == false);
1103 UASSERT(b.isValidPosition(v3s16(-124,142,2341)) == false);
1104 UASSERT(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
1105 UASSERT(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE,MAP_BLOCKSIZE-1)) == false);
1108 TODO: this method should probably be removed
1109 if the block size isn't going to be set variable
1111 /*UASSERT(b.getSizeNodes() == v3s16(MAP_BLOCKSIZE,
1112 MAP_BLOCKSIZE, MAP_BLOCKSIZE));*/
1114 // Changed flag should be initially set
1115 UASSERT(b.getModified() == MOD_STATE_WRITE_NEEDED);
1117 UASSERT(b.getModified() == MOD_STATE_CLEAN);
1119 // All nodes should have been set to
1120 // .d=CONTENT_IGNORE and .getLight() = 0
1121 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
1122 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
1123 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
1125 //UASSERT(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_AIR);
1126 UASSERT(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_IGNORE);
1127 UASSERT(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_DAY) == 0);
1128 UASSERT(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_NIGHT) == 0);
1132 MapNode n(CONTENT_AIR);
1133 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
1134 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
1135 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
1137 b.setNode(v3s16(x,y,z), n);
1142 Parent fetch functions
1144 parent.position_valid = false;
1145 parent.node.setContent(5);
1149 // Positions in the block should still be valid
1150 UASSERT(b.isValidPositionParent(v3s16(0,0,0)) == true);
1151 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
1152 n = b.getNodeParent(v3s16(0,MAP_BLOCKSIZE-1,0));
1153 UASSERT(n.getContent() == CONTENT_AIR);
1155 // ...but outside the block they should be invalid
1156 UASSERT(b.isValidPositionParent(v3s16(-121,2341,0)) == false);
1157 UASSERT(b.isValidPositionParent(v3s16(-1,0,0)) == false);
1158 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == false);
1161 bool exception_thrown = false;
1163 // This should throw an exception
1164 MapNode n = b.getNodeParent(v3s16(0,0,-1));
1166 catch(InvalidPositionException &e)
1168 exception_thrown = true;
1170 UASSERT(exception_thrown);
1173 parent.position_valid = true;
1174 // Now the positions outside should be valid
1175 UASSERT(b.isValidPositionParent(v3s16(-121,2341,0)) == true);
1176 UASSERT(b.isValidPositionParent(v3s16(-1,0,0)) == true);
1177 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == true);
1178 n = b.getNodeParent(v3s16(0,0,MAP_BLOCKSIZE));
1179 UASSERT(n.getContent() == 5);
1187 UASSERT(b.getNode(p).getContent() == 4);
1188 //TODO: Update to new system
1189 /*UASSERT(b.getNodeTile(p) == 4);
1190 UASSERT(b.getNodeTile(v3s16(-1,-1,0)) == 5);*/
1195 // Set lighting of all nodes to 0
1196 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
1197 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
1198 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
1199 MapNode n = b.getNode(v3s16(x,y,z));
1200 n.setLight(LIGHTBANK_DAY, 0);
1201 n.setLight(LIGHTBANK_NIGHT, 0);
1202 b.setNode(v3s16(x,y,z), n);
1208 Check how the block handles being a lonely sky block
1210 parent.position_valid = true;
1211 b.setIsUnderground(false);
1212 parent.node.setContent(CONTENT_AIR);
1213 parent.node.setLight(LIGHTBANK_DAY, LIGHT_SUN);
1214 parent.node.setLight(LIGHTBANK_NIGHT, 0);
1215 core::map<v3s16, bool> light_sources;
1216 // The bottom block is invalid, because we have a shadowing node
1217 UASSERT(b.propagateSunlight(light_sources) == false);
1218 UASSERT(b.getNode(v3s16(1,4,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
1219 UASSERT(b.getNode(v3s16(1,3,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
1220 UASSERT(b.getNode(v3s16(1,2,0)).getLight(LIGHTBANK_DAY) == 0);
1221 UASSERT(b.getNode(v3s16(1,1,0)).getLight(LIGHTBANK_DAY) == 0);
1222 UASSERT(b.getNode(v3s16(1,0,0)).getLight(LIGHTBANK_DAY) == 0);
1223 UASSERT(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
1224 UASSERT(b.getFaceLight2(1000, p, v3s16(0,1,0)) == LIGHT_SUN);
1225 UASSERT(b.getFaceLight2(1000, p, v3s16(0,-1,0)) == 0);
1226 UASSERT(b.getFaceLight2(0, p, v3s16(0,-1,0)) == 0);
1227 // According to MapBlock::getFaceLight,
1228 // The face on the z+ side should have double-diminished light
1229 //UASSERT(b.getFaceLight(p, v3s16(0,0,1)) == diminish_light(diminish_light(LIGHT_MAX)));
1230 // The face on the z+ side should have diminished light
1231 UASSERT(b.getFaceLight2(1000, p, v3s16(0,0,1)) == diminish_light(LIGHT_MAX));
1234 Check how the block handles being in between blocks with some non-sunlight
1235 while being underground
1238 // Make neighbours to exist and set some non-sunlight to them
1239 parent.position_valid = true;
1240 b.setIsUnderground(true);
1241 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
1242 core::map<v3s16, bool> light_sources;
1243 // The block below should be valid because there shouldn't be
1244 // sunlight in there either
1245 UASSERT(b.propagateSunlight(light_sources, true) == true);
1246 // Should not touch nodes that are not affected (that is, all of them)
1247 //UASSERT(b.getNode(v3s16(1,2,3)).getLight() == LIGHT_SUN);
1248 // Should set light of non-sunlighted blocks to 0.
1249 UASSERT(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == 0);
1252 Set up a situation where:
1253 - There is only air in this block
1254 - There is a valid non-sunlighted block at the bottom, and
1255 - Invalid blocks elsewhere.
1256 - the block is not underground.
1258 This should result in bottom block invalidity
1261 b.setIsUnderground(false);
1263 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
1264 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
1265 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
1267 n.setContent(CONTENT_AIR);
1268 n.setLight(LIGHTBANK_DAY, 0);
1269 b.setNode(v3s16(x,y,z), n);
1273 // Make neighbours invalid
1274 parent.position_valid = false;
1275 // Add exceptions to the top of the bottom block
1276 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
1277 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
1279 parent.validity_exceptions.push_back(v3s16(MAP_BLOCKSIZE+x, MAP_BLOCKSIZE-1, MAP_BLOCKSIZE+z));
1281 // Lighting value for the valid nodes
1282 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
1283 core::map<v3s16, bool> light_sources;
1284 // Bottom block is not valid
1285 UASSERT(b.propagateSunlight(light_sources) == false);
1290 struct TestMapSector: public TestBase
1292 class TC : public NodeContainer
1297 bool position_valid;
1301 position_valid = true;
1304 virtual bool isValidPosition(v3s16 p)
1306 return position_valid;
1309 virtual MapNode getNode(v3s16 p)
1311 if(position_valid == false)
1312 throw InvalidPositionException();
1316 virtual void setNode(v3s16 p, MapNode & n)
1318 if(position_valid == false)
1319 throw InvalidPositionException();
1322 virtual u16 nodeContainerId() const
1331 parent.position_valid = false;
1333 // Create one with no heightmaps
1334 ServerMapSector sector(&parent, v2s16(1,1));
1336 UASSERT(sector.getBlockNoCreateNoEx(0) == 0);
1337 UASSERT(sector.getBlockNoCreateNoEx(1) == 0);
1339 MapBlock * bref = sector.createBlankBlock(-2);
1341 UASSERT(sector.getBlockNoCreateNoEx(0) == 0);
1342 UASSERT(sector.getBlockNoCreateNoEx(-2) == bref);
1344 //TODO: Check for AlreadyExistsException
1346 /*bool exception_thrown = false;
1350 catch(InvalidPositionException &e){
1351 exception_thrown = true;
1353 UASSERT(exception_thrown);*/
1359 struct TestCollision: public TestBase
1364 axisAlignedCollision
1367 for(s16 bx = -3; bx <= 3; bx++)
1368 for(s16 by = -3; by <= 3; by++)
1369 for(s16 bz = -3; bz <= 3; bz++)
1373 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1374 aabb3f m(bx-2, by, bz, bx-1, by+1, bz+1);
1377 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1378 UASSERT(fabs(dtime - 1.000) < 0.001);
1381 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1382 aabb3f m(bx-2, by, bz, bx-1, by+1, bz+1);
1385 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == -1);
1388 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1389 aabb3f m(bx-2, by+1.5, bz, bx-1, by+2.5, bz-1);
1392 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == -1);
1395 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1396 aabb3f m(bx-2, by-1.5, bz, bx-1.5, by+0.5, bz+1);
1399 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1400 UASSERT(fabs(dtime - 3.000) < 0.001);
1403 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1404 aabb3f m(bx-2, by-1.5, bz, bx-1.5, by+0.5, bz+1);
1407 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1408 UASSERT(fabs(dtime - 3.000) < 0.001);
1413 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1414 aabb3f m(bx+2, by, bz, bx+3, by+1, bz+1);
1417 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1418 UASSERT(fabs(dtime - 1.000) < 0.001);
1421 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1422 aabb3f m(bx+2, by, bz, bx+3, by+1, bz+1);
1425 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == -1);
1428 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1429 aabb3f m(bx+2, by, bz+1.5, bx+3, by+1, bz+3.5);
1432 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == -1);
1435 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1436 aabb3f m(bx+2, by-1.5, bz, bx+2.5, by-0.5, bz+1);
1437 v3f v(-0.5, 0.2, 0);
1439 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 1); // Y, not X!
1440 UASSERT(fabs(dtime - 2.500) < 0.001);
1443 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1444 aabb3f m(bx+2, by-1.5, bz, bx+2.5, by-0.5, bz+1);
1445 v3f v(-0.5, 0.3, 0);
1447 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1448 UASSERT(fabs(dtime - 2.000) < 0.001);
1451 // TODO: Y-, Y+, Z-, Z+
1455 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1456 aabb3f m(bx+2.3, by+2.29, bz+2.29, bx+4.2, by+4.2, bz+4.2);
1457 v3f v(-1./3, -1./3, -1./3);
1459 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1460 UASSERT(fabs(dtime - 0.9) < 0.001);
1463 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1464 aabb3f m(bx+2.29, by+2.3, bz+2.29, bx+4.2, by+4.2, bz+4.2);
1465 v3f v(-1./3, -1./3, -1./3);
1467 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 1);
1468 UASSERT(fabs(dtime - 0.9) < 0.001);
1471 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1472 aabb3f m(bx+2.29, by+2.29, bz+2.3, bx+4.2, by+4.2, bz+4.2);
1473 v3f v(-1./3, -1./3, -1./3);
1475 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 2);
1476 UASSERT(fabs(dtime - 0.9) < 0.001);
1479 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1480 aabb3f m(bx-4.2, by-4.2, bz-4.2, bx-2.3, by-2.29, bz-2.29);
1481 v3f v(1./7, 1./7, 1./7);
1483 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1484 UASSERT(fabs(dtime - 16.1) < 0.001);
1487 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1488 aabb3f m(bx-4.2, by-4.2, bz-4.2, bx-2.29, by-2.3, bz-2.29);
1489 v3f v(1./7, 1./7, 1./7);
1491 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 1);
1492 UASSERT(fabs(dtime - 16.1) < 0.001);
1495 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1496 aabb3f m(bx-4.2, by-4.2, bz-4.2, bx-2.29, by-2.29, bz-2.3);
1497 v3f v(1./7, 1./7, 1./7);
1499 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 2);
1500 UASSERT(fabs(dtime - 16.1) < 0.001);
1506 struct TestSocket: public TestBase
1510 const int port = 30003;
1511 Address address(0,0,0,0, port);
1512 Address address6((IPv6AddressBytes*) NULL, port);
1516 UDPSocket socket6(true);
1517 socket6.Bind(address6);
1519 const char sendbuffer[] = "hello world!";
1520 IPv6AddressBytes bytes;
1521 bytes.bytes[15] = 1;
1524 socket6.Send(Address(&bytes, port), sendbuffer, sizeof(sendbuffer));
1528 char rcvbuffer[256];
1529 memset(rcvbuffer, 0, sizeof(rcvbuffer));
1533 int bytes_read = socket6.Receive(sender, rcvbuffer, sizeof(rcvbuffer));
1537 //FIXME: This fails on some systems
1538 UASSERT(strncmp(sendbuffer, rcvbuffer, sizeof(sendbuffer))==0);
1539 UASSERT(memcmp(sender.getAddress6().sin6_addr.s6_addr, Address(&bytes, 0).getAddress6().sin6_addr.s6_addr, 16) == 0);
1541 catch (SendFailedException e) {
1542 errorstream << "IPv6 support enabled but not available!" << std::endl;
1549 UDPSocket socket(false);
1550 socket.Bind(address);
1552 const char sendbuffer[] = "hello world!";
1553 socket.Send(Address(127,0,0,1,port), sendbuffer, sizeof(sendbuffer));
1557 char rcvbuffer[256];
1558 memset(rcvbuffer, 0, sizeof(rcvbuffer));
1562 int bytes_read = socket.Receive(sender, rcvbuffer, sizeof(rcvbuffer));
1566 //FIXME: This fails on some systems
1567 UASSERT(strncmp(sendbuffer, rcvbuffer, sizeof(sendbuffer))==0);
1568 UASSERT(sender.getAddress().sin_addr.s_addr == Address(127,0,0,1, 0).getAddress().sin_addr.s_addr);
1573 struct TestConnection: public TestBase
1578 Test helper functions
1581 // Some constants for testing
1582 u32 proto_id = 0x12345678;
1585 SharedBuffer<u8> data1(1);
1587 Address a(127,0,0,1, 10);
1590 con::BufferedPacket p1 = con::makePacket(a, data1,
1591 proto_id, peer_id, channel);
1593 We should now have a packet with this data:
1596 [4] u16 sender_peer_id
1601 UASSERT(readU32(&p1.data[0]) == proto_id);
1602 UASSERT(readU16(&p1.data[4]) == peer_id);
1603 UASSERT(readU8(&p1.data[6]) == channel);
1604 UASSERT(readU8(&p1.data[7]) == data1[0]);
1606 //infostream<<"initial data1[0]="<<((u32)data1[0]&0xff)<<std::endl;
1608 SharedBuffer<u8> p2 = con::makeReliablePacket(data1, seqnum);
1610 /*infostream<<"p2.getSize()="<<p2.getSize()<<", data1.getSize()="
1611 <<data1.getSize()<<std::endl;
1612 infostream<<"readU8(&p2[3])="<<readU8(&p2[3])
1613 <<" p2[3]="<<((u32)p2[3]&0xff)<<std::endl;
1614 infostream<<"data1[0]="<<((u32)data1[0]&0xff)<<std::endl;*/
1616 UASSERT(p2.getSize() == 3 + data1.getSize());
1617 UASSERT(readU8(&p2[0]) == TYPE_RELIABLE);
1618 UASSERT(readU16(&p2[1]) == seqnum);
1619 UASSERT(readU8(&p2[3]) == data1[0]);
1622 struct Handler : public con::PeerHandler
1624 Handler(const char *a_name)
1630 void peerAdded(con::Peer *peer)
1632 infostream<<"Handler("<<name<<")::peerAdded(): "
1633 "id="<<peer->id<<std::endl;
1637 void deletingPeer(con::Peer *peer, bool timeout)
1639 infostream<<"Handler("<<name<<")::deletingPeer(): "
1641 <<", timeout="<<timeout<<std::endl;
1653 DSTACK("TestConnection::Run");
1658 Test some real connections
1660 NOTE: This mostly tests the legacy interface.
1663 u32 proto_id = 0xad26846a;
1665 Handler hand_server("server");
1666 Handler hand_client("client");
1668 infostream<<"** Creating server Connection"<<std::endl;
1669 con::Connection server(proto_id, 512, 5.0, false, &hand_server);
1670 Address address(0,0,0,0, 30001);
1671 server.Serve(address);
1673 infostream<<"** Creating client Connection"<<std::endl;
1674 con::Connection client(proto_id, 512, 5.0, false, &hand_client);
1676 UASSERT(hand_server.count == 0);
1677 UASSERT(hand_client.count == 0);
1681 Address server_address(127,0,0,1, 30001);
1682 infostream<<"** running client.Connect()"<<std::endl;
1683 client.Connect(server_address);
1687 // Client should not have added client yet
1688 UASSERT(hand_client.count == 0);
1693 SharedBuffer<u8> data;
1694 infostream<<"** running client.Receive()"<<std::endl;
1695 u32 size = client.Receive(peer_id, data);
1696 infostream<<"** Client received: peer_id="<<peer_id
1700 catch(con::NoIncomingDataException &e)
1704 // Client should have added server now
1705 UASSERT(hand_client.count == 1);
1706 UASSERT(hand_client.last_id == 1);
1707 // Server should not have added client yet
1708 UASSERT(hand_server.count == 0);
1715 SharedBuffer<u8> data;
1716 infostream<<"** running server.Receive()"<<std::endl;
1717 u32 size = server.Receive(peer_id, data);
1718 infostream<<"** Server received: peer_id="<<peer_id
1722 catch(con::NoIncomingDataException &e)
1724 // No actual data received, but the client has
1725 // probably been connected
1728 // Client should be the same
1729 UASSERT(hand_client.count == 1);
1730 UASSERT(hand_client.last_id == 1);
1731 // Server should have the client
1732 UASSERT(hand_server.count == 1);
1733 UASSERT(hand_server.last_id == 2);
1737 while(client.Connected() == false)
1742 SharedBuffer<u8> data;
1743 infostream<<"** running client.Receive()"<<std::endl;
1744 u32 size = client.Receive(peer_id, data);
1745 infostream<<"** Client received: peer_id="<<peer_id
1749 catch(con::NoIncomingDataException &e)
1760 SharedBuffer<u8> data;
1761 infostream<<"** running server.Receive()"<<std::endl;
1762 u32 size = server.Receive(peer_id, data);
1763 infostream<<"** Server received: peer_id="<<peer_id
1767 catch(con::NoIncomingDataException &e)
1772 Simple send-receive test
1775 /*u8 data[] = "Hello World!";
1776 u32 datasize = sizeof(data);*/
1777 SharedBuffer<u8> data = SharedBufferFromString("Hello World!");
1779 infostream<<"** running client.Send()"<<std::endl;
1780 client.Send(PEER_ID_SERVER, 0, data, true);
1785 SharedBuffer<u8> recvdata;
1786 infostream<<"** running server.Receive()"<<std::endl;
1787 u32 size = server.Receive(peer_id, recvdata);
1788 infostream<<"** Server received: peer_id="<<peer_id
1792 UASSERT(memcmp(*data, *recvdata, data.getSize()) == 0);
1795 u16 peer_id_client = 2;
1798 Send consequent packets in different order
1799 Not compatible with new Connection, thus commented out.
1802 //u8 data1[] = "hello1";
1803 //u8 data2[] = "hello2";
1804 SharedBuffer<u8> data1 = SharedBufferFromString("hello1");
1805 SharedBuffer<u8> data2 = SharedBufferFromString("Hello2");
1807 Address client_address =
1808 server.GetPeerAddress(peer_id_client);
1810 infostream<<"*** Sending packets in wrong order (2,1,2)"
1814 con::Channel *ch = &server.getPeer(peer_id_client)->channels[chn];
1815 u16 sn = ch->next_outgoing_seqnum;
1816 ch->next_outgoing_seqnum = sn+1;
1817 server.Send(peer_id_client, chn, data2, true);
1818 ch->next_outgoing_seqnum = sn;
1819 server.Send(peer_id_client, chn, data1, true);
1820 ch->next_outgoing_seqnum = sn+1;
1821 server.Send(peer_id_client, chn, data2, true);
1825 infostream<<"*** Receiving the packets"<<std::endl;
1828 SharedBuffer<u8> recvdata;
1831 infostream<<"** running client.Receive()"<<std::endl;
1833 size = client.Receive(peer_id, recvdata);
1834 infostream<<"** Client received: peer_id="<<peer_id
1836 <<", data="<<*recvdata
1838 UASSERT(size == data1.getSize());
1839 UASSERT(memcmp(*data1, *recvdata, data1.getSize()) == 0);
1840 UASSERT(peer_id == PEER_ID_SERVER);
1842 infostream<<"** running client.Receive()"<<std::endl;
1844 size = client.Receive(peer_id, recvdata);
1845 infostream<<"** Client received: peer_id="<<peer_id
1847 <<", data="<<*recvdata
1849 UASSERT(size == data2.getSize());
1850 UASSERT(memcmp(*data2, *recvdata, data2.getSize()) == 0);
1851 UASSERT(peer_id == PEER_ID_SERVER);
1853 bool got_exception = false;
1856 infostream<<"** running client.Receive()"<<std::endl;
1858 size = client.Receive(peer_id, recvdata);
1859 infostream<<"** Client received: peer_id="<<peer_id
1861 <<", data="<<*recvdata
1864 catch(con::NoIncomingDataException &e)
1866 infostream<<"** No incoming data for client"<<std::endl;
1867 got_exception = true;
1869 UASSERT(got_exception);
1874 Send large amounts of packets (infinite test)
1875 Commented out because of infinity.
1878 infostream<<"Sending large amounts of packets (infinite test)"<<std::endl;
1881 int datasize = myrand_range(0,5)==0?myrand_range(100,10000):myrand_range(0,100);
1882 infostream<<"datasize="<<datasize<<std::endl;
1883 SharedBuffer<u8> data1(datasize);
1884 for(u16 i=0; i<datasize; i++)
1887 int sendtimes = myrand_range(1,10);
1888 for(int i=0; i<sendtimes; i++){
1889 server.Send(peer_id_client, 0, data1, true);
1892 infostream<<"sendcount="<<sendcount<<std::endl;
1894 //int receivetimes = myrand_range(1,20);
1895 int receivetimes = 20;
1896 for(int i=0; i<receivetimes; i++){
1897 SharedBuffer<u8> recvdata;
1900 bool received = false;
1902 size = client.Receive(peer_id, recvdata);
1904 }catch(con::NoIncomingDataException &e){
1914 const int datasize = 30000;
1915 SharedBuffer<u8> data1(datasize);
1916 for(u16 i=0; i<datasize; i++){
1920 infostream<<"Sending data (size="<<datasize<<"):";
1921 for(int i=0; i<datasize && i<20; i++){
1922 if(i%2==0) infostream<<" ";
1924 snprintf(buf, 10, "%.2X", ((int)((const char*)*data1)[i])&0xff);
1929 infostream<<std::endl;
1931 server.Send(peer_id_client, 0, data1, true);
1935 SharedBuffer<u8> recvdata;
1936 infostream<<"** running client.Receive()"<<std::endl;
1939 bool received = false;
1940 u32 timems0 = porting::getTimeMs();
1942 if(porting::getTimeMs() - timems0 > 5000 || received)
1945 size = client.Receive(peer_id, recvdata);
1947 }catch(con::NoIncomingDataException &e){
1952 infostream<<"** Client received: peer_id="<<peer_id
1956 infostream<<"Received data (size="<<size<<"): ";
1957 for(int i=0; i<size && i<20; i++){
1958 if(i%2==0) infostream<<" ";
1960 snprintf(buf, 10, "%.2X", ((int)(recvdata[i]))&0xff);
1965 infostream<<std::endl;
1967 UASSERT(memcmp(*data1, *recvdata, data1.getSize()) == 0);
1968 UASSERT(peer_id == PEER_ID_SERVER);
1971 // Check peer handlers
1972 UASSERT(hand_client.count == 1);
1973 UASSERT(hand_client.last_id == 1);
1974 UASSERT(hand_server.count == 1);
1975 UASSERT(hand_server.last_id == 2);
1984 infostream<<"Running " #X <<std::endl;\
1987 tests_failed += x.test_failed ? 1 : 0;\
1990 #define TESTPARAMS(X, ...)\
1993 infostream<<"Running " #X <<std::endl;\
1994 x.Run(__VA_ARGS__);\
1996 tests_failed += x.test_failed ? 1 : 0;\
2001 DSTACK(__FUNCTION_NAME);
2004 int tests_failed = 0;
2006 // Create item and node definitions
2007 IWritableItemDefManager *idef = createItemDefManager();
2008 IWritableNodeDefManager *ndef = createNodeDefManager();
2009 define_some_nodes(idef, ndef);
2011 infostream<<"run_tests() started"<<std::endl;
2012 TEST(TestUtilities);
2016 TEST(TestSerialization);
2017 TEST(TestNodedefSerialization);
2018 TESTPARAMS(TestMapNode, ndef);
2019 TESTPARAMS(TestVoxelManipulator, ndef);
2020 TESTPARAMS(TestVoxelAlgorithms, ndef);
2021 TESTPARAMS(TestInventory, idef);
2022 //TEST(TestMapBlock);
2023 //TEST(TestMapSector);
2024 TEST(TestCollision);
2025 if(INTERNET_SIMULATOR == false){
2027 dout_con<<"=== BEGIN RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
2028 TEST(TestConnection);
2029 dout_con<<"=== END RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
2035 if(tests_failed == 0){
2036 infostream<<"run_tests(): "<<tests_failed<<" / "<<tests_run<<" tests failed."<<std::endl;
2037 infostream<<"run_tests() passed."<<std::endl;
2040 errorstream<<"run_tests(): "<<tests_failed<<" / "<<tests_run<<" tests failed."<<std::endl;
2041 errorstream<<"run_tests() aborting."<<std::endl;