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?\"");
178 UASSERT(padStringRight("hello", 8) == "hello ");
179 UASSERT(str_equal(narrow_to_wide("abc"), narrow_to_wide("abc")));
180 UASSERT(str_equal(narrow_to_wide("ABC"), narrow_to_wide("abc"), true));
181 UASSERT(trim(" a") == "a");
182 UASSERT(trim(" a ") == "a");
183 UASSERT(trim("a ") == "a");
184 UASSERT(trim("") == "");
185 UASSERT(mystoi("123", 0, 1000) == 123);
186 UASSERT(mystoi("123", 0, 10) == 10);
187 std::string test_str;
188 test_str = "Hello there";
189 str_replace(test_str, "there", "world");
190 UASSERT(test_str == "Hello world");
191 test_str = "ThisAisAaAtest";
192 str_replace_char(test_str, 'A', ' ');
193 UASSERT(test_str == "This is a test");
194 UASSERT(string_allowed("hello", "abcdefghijklmno") == true);
195 UASSERT(string_allowed("123", "abcdefghijklmno") == false);
196 UASSERT(string_allowed_blacklist("hello", "123") == true);
197 UASSERT(string_allowed_blacklist("hello123", "123") == false);
198 UASSERT(wrap_rows("12345678",4) == "1234\n5678");
199 UASSERT(is_number("123") == true);
200 UASSERT(is_number("") == false);
201 UASSERT(is_number("123a") == false);
205 struct TestPath: public TestBase
207 // adjusts a POSIX path to system-specific conventions
208 // -> changes '/' to DIR_DELIM
209 // -> absolute paths start with "C:\\" on windows
210 std::string p(std::string path)
212 for(size_t i = 0; i < path.size(); ++i){
214 path.replace(i, 1, DIR_DELIM);
215 i += std::string(DIR_DELIM).size() - 1; // generally a no-op
229 std::string path, result, removed;
232 Test fs::IsDirDelimiter
234 UASSERT(fs::IsDirDelimiter('/') == true);
235 UASSERT(fs::IsDirDelimiter('A') == false);
236 UASSERT(fs::IsDirDelimiter(0) == false);
238 UASSERT(fs::IsDirDelimiter('\\') == true);
240 UASSERT(fs::IsDirDelimiter('\\') == false);
244 Test fs::PathStartsWith
247 const int numpaths = 12;
248 std::string paths[numpaths] = {
251 p("/home/user/minetest"),
252 p("/home/user/minetest/bin"),
253 p("/home/user/.minetest"),
259 p("/home/user2/minetest/worlds"),
260 p("/home/user2/minetest/world"),
263 expected fs::PathStartsWith results
266 2 = returns false on windows, false elsewhere
267 3 = returns true on windows, true elsewhere
268 4 = returns true if and only if
269 FILESYS_CASE_INSENSITIVE is true
271 int expected_results[numpaths][numpaths] = {
272 {1,2,0,0,0,0,0,0,0,0,0,0},
273 {1,1,0,0,0,0,0,0,0,0,0,0},
274 {1,1,1,0,0,0,0,0,0,0,0,0},
275 {1,1,1,1,0,0,0,0,0,0,0,0},
276 {1,1,0,0,1,0,0,0,0,0,0,0},
277 {1,1,0,0,0,1,0,0,1,1,0,0},
278 {1,1,0,0,0,0,1,4,1,0,0,0},
279 {1,1,0,0,0,0,4,1,4,0,0,0},
280 {1,1,0,0,0,0,0,0,1,0,0,0},
281 {1,1,0,0,0,0,0,0,1,1,0,0},
282 {1,1,0,0,0,0,0,0,0,0,1,0},
283 {1,1,0,0,0,0,0,0,0,0,0,1},
286 for (int i = 0; i < numpaths; i++)
287 for (int j = 0; j < numpaths; j++){
288 /*verbosestream<<"testing fs::PathStartsWith(\""
290 <<paths[j]<<"\")"<<std::endl;*/
291 bool starts = fs::PathStartsWith(paths[i], paths[j]);
292 int expected = expected_results[i][j];
294 UASSERT(starts == false);
296 else if(expected == 1){
297 UASSERT(starts == true);
300 else if(expected == 2){
301 UASSERT(starts == false);
303 else if(expected == 3){
304 UASSERT(starts == true);
307 else if(expected == 2){
308 UASSERT(starts == true);
310 else if(expected == 3){
311 UASSERT(starts == false);
314 else if(expected == 4){
315 UASSERT(starts == (bool)FILESYS_CASE_INSENSITIVE);
321 Test fs::RemoveLastPathComponent
323 UASSERT(fs::RemoveLastPathComponent("") == "");
324 path = p("/home/user/minetest/bin/..//worlds/world1");
325 result = fs::RemoveLastPathComponent(path, &removed, 0);
326 UASSERT(result == path);
327 UASSERT(removed == "");
328 result = fs::RemoveLastPathComponent(path, &removed, 1);
329 UASSERT(result == p("/home/user/minetest/bin/..//worlds"));
330 UASSERT(removed == p("world1"));
331 result = fs::RemoveLastPathComponent(path, &removed, 2);
332 UASSERT(result == p("/home/user/minetest/bin/.."));
333 UASSERT(removed == p("worlds/world1"));
334 result = fs::RemoveLastPathComponent(path, &removed, 3);
335 UASSERT(result == p("/home/user/minetest/bin"));
336 UASSERT(removed == p("../worlds/world1"));
337 result = fs::RemoveLastPathComponent(path, &removed, 4);
338 UASSERT(result == p("/home/user/minetest"));
339 UASSERT(removed == p("bin/../worlds/world1"));
340 result = fs::RemoveLastPathComponent(path, &removed, 5);
341 UASSERT(result == p("/home/user"));
342 UASSERT(removed == p("minetest/bin/../worlds/world1"));
343 result = fs::RemoveLastPathComponent(path, &removed, 6);
344 UASSERT(result == p("/home"));
345 UASSERT(removed == p("user/minetest/bin/../worlds/world1"));
346 result = fs::RemoveLastPathComponent(path, &removed, 7);
348 UASSERT(result == "C:");
350 UASSERT(result == "");
352 UASSERT(removed == p("home/user/minetest/bin/../worlds/world1"));
355 Now repeat the test with a trailing delimiter
357 path = p("/home/user/minetest/bin/..//worlds/world1/");
358 result = fs::RemoveLastPathComponent(path, &removed, 0);
359 UASSERT(result == path);
360 UASSERT(removed == "");
361 result = fs::RemoveLastPathComponent(path, &removed, 1);
362 UASSERT(result == p("/home/user/minetest/bin/..//worlds"));
363 UASSERT(removed == p("world1"));
364 result = fs::RemoveLastPathComponent(path, &removed, 2);
365 UASSERT(result == p("/home/user/minetest/bin/.."));
366 UASSERT(removed == p("worlds/world1"));
367 result = fs::RemoveLastPathComponent(path, &removed, 3);
368 UASSERT(result == p("/home/user/minetest/bin"));
369 UASSERT(removed == p("../worlds/world1"));
370 result = fs::RemoveLastPathComponent(path, &removed, 4);
371 UASSERT(result == p("/home/user/minetest"));
372 UASSERT(removed == p("bin/../worlds/world1"));
373 result = fs::RemoveLastPathComponent(path, &removed, 5);
374 UASSERT(result == p("/home/user"));
375 UASSERT(removed == p("minetest/bin/../worlds/world1"));
376 result = fs::RemoveLastPathComponent(path, &removed, 6);
377 UASSERT(result == p("/home"));
378 UASSERT(removed == p("user/minetest/bin/../worlds/world1"));
379 result = fs::RemoveLastPathComponent(path, &removed, 7);
381 UASSERT(result == "C:");
383 UASSERT(result == "");
385 UASSERT(removed == p("home/user/minetest/bin/../worlds/world1"));
388 Test fs::RemoveRelativePathComponent
390 path = p("/home/user/minetest/bin");
391 result = fs::RemoveRelativePathComponents(path);
392 UASSERT(result == path);
393 path = p("/home/user/minetest/bin/../worlds/world1");
394 result = fs::RemoveRelativePathComponents(path);
395 UASSERT(result == p("/home/user/minetest/worlds/world1"));
396 path = p("/home/user/minetest/bin/../worlds/world1/");
397 result = fs::RemoveRelativePathComponents(path);
398 UASSERT(result == p("/home/user/minetest/worlds/world1"));
400 result = fs::RemoveRelativePathComponents(path);
401 UASSERT(result == "");
402 path = p("./subdir/../..");
403 result = fs::RemoveRelativePathComponents(path);
404 UASSERT(result == "");
405 path = p("/a/b/c/.././../d/../e/f/g/../h/i/j/../../../..");
406 result = fs::RemoveRelativePathComponents(path);
407 UASSERT(result == p("/a/e"));
411 struct TestSettings: public TestBase
416 // Test reading of settings
417 std::istringstream is(
419 "leetleet = 13371337\n"
420 "leetleet_neg = -13371337\n"
421 "floaty_thing = 1.1\n"
422 "stringy_thing = asd /( ¤%&(/\" BLÖÄRP\n"
423 "coord = (1, 2, 4.5)");
424 s.parseConfigLines(is);
425 UASSERT(s.getS32("leet") == 1337);
426 UASSERT(s.getS16("leetleet") == 32767);
427 UASSERT(s.getS16("leetleet_neg") == -32768);
428 // Not sure if 1.1 is an exact value as a float, but doesn't matter
429 UASSERT(fabs(s.getFloat("floaty_thing") - 1.1) < 0.001);
430 UASSERT(s.get("stringy_thing") == "asd /( ¤%&(/\" BLÖÄRP");
431 UASSERT(fabs(s.getV3F("coord").X - 1.0) < 0.001);
432 UASSERT(fabs(s.getV3F("coord").Y - 2.0) < 0.001);
433 UASSERT(fabs(s.getV3F("coord").Z - 4.5) < 0.001);
434 // Test the setting of settings too
435 s.setFloat("floaty_thing_2", 1.2);
436 s.setV3F("coord2", v3f(1, 2, 3.3));
437 UASSERT(s.get("floaty_thing_2").substr(0,3) == "1.2");
438 UASSERT(fabs(s.getFloat("floaty_thing_2") - 1.2) < 0.001);
439 UASSERT(fabs(s.getV3F("coord2").X - 1.0) < 0.001);
440 UASSERT(fabs(s.getV3F("coord2").Y - 2.0) < 0.001);
441 UASSERT(fabs(s.getV3F("coord2").Z - 3.3) < 0.001);
445 struct TestSerialization: public TestBase
447 // To be used like this:
448 // mkstr("Some\0string\0with\0embedded\0nuls")
449 // since std::string("...") doesn't work as expected in that case.
450 template<size_t N> std::string mkstr(const char (&s)[N])
452 return std::string(s, N - 1);
457 // Tests some serialization primitives
459 UASSERT(serializeString("") == mkstr("\0\0"));
460 UASSERT(serializeWideString(L"") == mkstr("\0\0"));
461 UASSERT(serializeLongString("") == mkstr("\0\0\0\0"));
462 UASSERT(serializeJsonString("") == "\"\"");
464 std::string teststring = "Hello world!";
465 UASSERT(serializeString(teststring) ==
466 mkstr("\0\14Hello world!"));
467 UASSERT(serializeWideString(narrow_to_wide(teststring)) ==
468 mkstr("\0\14\0H\0e\0l\0l\0o\0 \0w\0o\0r\0l\0d\0!"));
469 UASSERT(serializeLongString(teststring) ==
470 mkstr("\0\0\0\14Hello world!"));
471 UASSERT(serializeJsonString(teststring) ==
474 std::string teststring2;
475 std::wstring teststring2_w;
476 std::string teststring2_w_encoded;
478 std::ostringstream tmp_os;
479 std::wostringstream tmp_os_w;
480 std::ostringstream tmp_os_w_encoded;
481 for(int i = 0; i < 256; i++)
484 tmp_os_w<<(wchar_t)i;
485 tmp_os_w_encoded<<(char)0<<(char)i;
487 teststring2 = tmp_os.str();
488 teststring2_w = tmp_os_w.str();
489 teststring2_w_encoded = tmp_os_w_encoded.str();
491 UASSERT(serializeString(teststring2) ==
492 mkstr("\1\0") + teststring2);
493 UASSERT(serializeWideString(teststring2_w) ==
494 mkstr("\1\0") + teststring2_w_encoded);
495 UASSERT(serializeLongString(teststring2) ==
496 mkstr("\0\0\1\0") + teststring2);
497 // MSVC fails when directly using "\\\\"
498 std::string backslash = "\\";
499 UASSERT(serializeJsonString(teststring2) ==
501 "\\u0000\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007" +
502 "\\b\\t\\n\\u000b\\f\\r\\u000e\\u000f" +
503 "\\u0010\\u0011\\u0012\\u0013\\u0014\\u0015\\u0016\\u0017" +
504 "\\u0018\\u0019\\u001a\\u001b\\u001c\\u001d\\u001e\\u001f" +
505 " !\\\"" + teststring2.substr(0x23, 0x2f-0x23) +
506 "\\/" + teststring2.substr(0x30, 0x5c-0x30) +
507 backslash + backslash + teststring2.substr(0x5d, 0x7f-0x5d) + "\\u007f" +
508 "\\u0080\\u0081\\u0082\\u0083\\u0084\\u0085\\u0086\\u0087" +
509 "\\u0088\\u0089\\u008a\\u008b\\u008c\\u008d\\u008e\\u008f" +
510 "\\u0090\\u0091\\u0092\\u0093\\u0094\\u0095\\u0096\\u0097" +
511 "\\u0098\\u0099\\u009a\\u009b\\u009c\\u009d\\u009e\\u009f" +
512 "\\u00a0\\u00a1\\u00a2\\u00a3\\u00a4\\u00a5\\u00a6\\u00a7" +
513 "\\u00a8\\u00a9\\u00aa\\u00ab\\u00ac\\u00ad\\u00ae\\u00af" +
514 "\\u00b0\\u00b1\\u00b2\\u00b3\\u00b4\\u00b5\\u00b6\\u00b7" +
515 "\\u00b8\\u00b9\\u00ba\\u00bb\\u00bc\\u00bd\\u00be\\u00bf" +
516 "\\u00c0\\u00c1\\u00c2\\u00c3\\u00c4\\u00c5\\u00c6\\u00c7" +
517 "\\u00c8\\u00c9\\u00ca\\u00cb\\u00cc\\u00cd\\u00ce\\u00cf" +
518 "\\u00d0\\u00d1\\u00d2\\u00d3\\u00d4\\u00d5\\u00d6\\u00d7" +
519 "\\u00d8\\u00d9\\u00da\\u00db\\u00dc\\u00dd\\u00de\\u00df" +
520 "\\u00e0\\u00e1\\u00e2\\u00e3\\u00e4\\u00e5\\u00e6\\u00e7" +
521 "\\u00e8\\u00e9\\u00ea\\u00eb\\u00ec\\u00ed\\u00ee\\u00ef" +
522 "\\u00f0\\u00f1\\u00f2\\u00f3\\u00f4\\u00f5\\u00f6\\u00f7" +
523 "\\u00f8\\u00f9\\u00fa\\u00fb\\u00fc\\u00fd\\u00fe\\u00ff" +
527 std::istringstream is(serializeString(teststring2), std::ios::binary);
528 UASSERT(deSerializeString(is) == teststring2);
534 std::istringstream is(serializeWideString(teststring2_w), std::ios::binary);
535 UASSERT(deSerializeWideString(is) == teststring2_w);
541 std::istringstream is(serializeLongString(teststring2), std::ios::binary);
542 UASSERT(deSerializeLongString(is) == teststring2);
548 std::istringstream is(serializeJsonString(teststring2), std::ios::binary);
549 //dstream<<serializeJsonString(deSerializeJsonString(is));
550 UASSERT(deSerializeJsonString(is) == teststring2);
558 struct TestNodedefSerialization: public TestBase
563 f.name = "default:stone";
564 for(int i = 0; i < 6; i++)
565 f.tiledef[i].name = "default_stone.png";
566 f.is_ground_content = true;
567 std::ostringstream os(std::ios::binary);
568 f.serialize(os, LATEST_PROTOCOL_VERSION);
569 verbosestream<<"Test ContentFeatures size: "<<os.str().size()<<std::endl;
570 std::istringstream is(os.str(), std::ios::binary);
573 UASSERT(f.walkable == f2.walkable);
574 UASSERT(f.node_box.type == f2.node_box.type);
578 struct TestCompress: public TestBase
584 SharedBuffer<u8> fromdata(4);
590 std::ostringstream os(std::ios_base::binary);
591 compress(fromdata, os, 0);
593 std::string str_out = os.str();
595 infostream<<"str_out.size()="<<str_out.size()<<std::endl;
596 infostream<<"TestCompress: 1,5,5,1 -> ";
597 for(u32 i=0; i<str_out.size(); i++)
599 infostream<<(u32)str_out[i]<<",";
601 infostream<<std::endl;
603 UASSERT(str_out.size() == 10);
605 UASSERT(str_out[0] == 0);
606 UASSERT(str_out[1] == 0);
607 UASSERT(str_out[2] == 0);
608 UASSERT(str_out[3] == 4);
609 UASSERT(str_out[4] == 0);
610 UASSERT(str_out[5] == 1);
611 UASSERT(str_out[6] == 1);
612 UASSERT(str_out[7] == 5);
613 UASSERT(str_out[8] == 0);
614 UASSERT(str_out[9] == 1);
616 std::istringstream is(str_out, std::ios_base::binary);
617 std::ostringstream os2(std::ios_base::binary);
619 decompress(is, os2, 0);
620 std::string str_out2 = os2.str();
622 infostream<<"decompress: ";
623 for(u32 i=0; i<str_out2.size(); i++)
625 infostream<<(u32)str_out2[i]<<",";
627 infostream<<std::endl;
629 UASSERT(str_out2.size() == fromdata.getSize());
631 for(u32 i=0; i<str_out2.size(); i++)
633 UASSERT(str_out2[i] == fromdata[i]);
640 SharedBuffer<u8> fromdata(4);
646 std::ostringstream os(std::ios_base::binary);
647 compress(fromdata, os, SER_FMT_VER_HIGHEST_READ);
649 std::string str_out = os.str();
651 infostream<<"str_out.size()="<<str_out.size()<<std::endl;
652 infostream<<"TestCompress: 1,5,5,1 -> ";
653 for(u32 i=0; i<str_out.size(); i++)
655 infostream<<(u32)str_out[i]<<",";
657 infostream<<std::endl;
659 std::istringstream is(str_out, std::ios_base::binary);
660 std::ostringstream os2(std::ios_base::binary);
662 decompress(is, os2, SER_FMT_VER_HIGHEST_READ);
663 std::string str_out2 = os2.str();
665 infostream<<"decompress: ";
666 for(u32 i=0; i<str_out2.size(); i++)
668 infostream<<(u32)str_out2[i]<<",";
670 infostream<<std::endl;
672 UASSERT(str_out2.size() == fromdata.getSize());
674 for(u32 i=0; i<str_out2.size(); i++)
676 UASSERT(str_out2[i] == fromdata[i]);
681 // Test zlib wrapper with large amounts of data (larger than its
684 infostream<<"Test: Testing zlib wrappers with a large amount "
685 <<"of pseudorandom data"<<std::endl;
687 infostream<<"Test: Input size of large compressZlib is "
690 data_in.resize(size);
691 PseudoRandom pseudorandom(9420);
692 for(u32 i=0; i<size; i++)
693 data_in[i] = pseudorandom.range(0,255);
694 std::ostringstream os_compressed(std::ios::binary);
695 compressZlib(data_in, os_compressed);
696 infostream<<"Test: Output size of large compressZlib is "
697 <<os_compressed.str().size()<<std::endl;
698 std::istringstream is_compressed(os_compressed.str(), std::ios::binary);
699 std::ostringstream os_decompressed(std::ios::binary);
700 decompressZlib(is_compressed, os_decompressed);
701 infostream<<"Test: Output size of large decompressZlib is "
702 <<os_decompressed.str().size()<<std::endl;
703 std::string str_decompressed = os_decompressed.str();
704 UTEST(str_decompressed.size() == data_in.size(), "Output size not"
705 " equal (output: %u, input: %u)",
706 (unsigned int)str_decompressed.size(), (unsigned int)data_in.size());
707 for(u32 i=0; i<size && i<str_decompressed.size(); i++){
708 UTEST(str_decompressed[i] == data_in[i],
709 "index out[%i]=%i differs from in[%i]=%i",
710 i, str_decompressed[i], i, data_in[i]);
716 struct TestMapNode: public TestBase
718 void Run(INodeDefManager *nodedef)
723 UASSERT(n.getContent() == CONTENT_AIR);
724 UASSERT(n.getLight(LIGHTBANK_DAY, nodedef) == 0);
725 UASSERT(n.getLight(LIGHTBANK_NIGHT, nodedef) == 0);
728 n.setContent(CONTENT_AIR);
729 UASSERT(nodedef->get(n).light_propagates == true);
730 n.setContent(LEGN(nodedef, "CONTENT_STONE"));
731 UASSERT(nodedef->get(n).light_propagates == false);
735 struct TestVoxelManipulator: public TestBase
737 void Run(INodeDefManager *nodedef)
743 VoxelArea a(v3s16(-1,-1,-1), v3s16(1,1,1));
744 UASSERT(a.index(0,0,0) == 1*3*3 + 1*3 + 1);
745 UASSERT(a.index(-1,-1,-1) == 0);
747 VoxelArea c(v3s16(-2,-2,-2), v3s16(2,2,2));
748 // An area that is 1 bigger in x+ and z-
749 VoxelArea d(v3s16(-2,-2,-3), v3s16(3,2,2));
751 std::list<VoxelArea> aa;
755 std::vector<VoxelArea> results;
756 results.push_back(VoxelArea(v3s16(-2,-2,-3),v3s16(3,2,-3)));
757 results.push_back(VoxelArea(v3s16(3,-2,-2),v3s16(3,2,2)));
759 UASSERT(aa.size() == results.size());
761 infostream<<"Result of diff:"<<std::endl;
762 for(std::list<VoxelArea>::const_iterator
763 i = aa.begin(); i != aa.end(); ++i)
765 i->print(infostream);
766 infostream<<std::endl;
768 std::vector<VoxelArea>::iterator j = std::find(results.begin(), results.end(), *i);
769 UASSERT(j != results.end());
780 v.print(infostream, nodedef);
782 infostream<<"*** Setting (-1,0,-1)=2 ***"<<std::endl;
784 v.setNodeNoRef(v3s16(-1,0,-1), MapNode(CONTENT_GRASS));
786 v.print(infostream, nodedef);
788 UASSERT(v.getNode(v3s16(-1,0,-1)).getContent() == CONTENT_GRASS);
790 infostream<<"*** Reading from inexistent (0,0,-1) ***"<<std::endl;
792 EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,0,-1)));
794 v.print(infostream, nodedef);
796 infostream<<"*** Adding area ***"<<std::endl;
800 v.print(infostream, nodedef);
802 UASSERT(v.getNode(v3s16(-1,0,-1)).getContent() == CONTENT_GRASS);
803 EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,1,1)));
807 struct TestVoxelAlgorithms: public TestBase
809 void Run(INodeDefManager *ndef)
812 voxalgo::propagateSunlight
816 for(u16 z=0; z<3; z++)
817 for(u16 y=0; y<3; y++)
818 for(u16 x=0; x<3; x++)
821 v.setNodeNoRef(p, MapNode(CONTENT_AIR));
823 VoxelArea a(v3s16(0,0,0), v3s16(2,2,2));
825 std::set<v3s16> light_sources;
826 voxalgo::setLight(v, a, 0, ndef);
827 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
828 v, a, true, light_sources, ndef);
829 //v.print(dstream, ndef, VOXELPRINT_LIGHT_DAY);
830 UASSERT(res.bottom_sunlight_valid == true);
831 UASSERT(v.getNode(v3s16(1,1,1)).getLight(LIGHTBANK_DAY, ndef)
834 v.setNodeNoRef(v3s16(0,0,0), MapNode(CONTENT_STONE));
836 std::set<v3s16> light_sources;
837 voxalgo::setLight(v, a, 0, ndef);
838 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
839 v, a, true, light_sources, ndef);
840 UASSERT(res.bottom_sunlight_valid == true);
841 UASSERT(v.getNode(v3s16(1,1,1)).getLight(LIGHTBANK_DAY, ndef)
845 std::set<v3s16> light_sources;
846 voxalgo::setLight(v, a, 0, ndef);
847 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
848 v, a, false, light_sources, ndef);
849 UASSERT(res.bottom_sunlight_valid == true);
850 UASSERT(v.getNode(v3s16(2,0,2)).getLight(LIGHTBANK_DAY, ndef)
853 v.setNodeNoRef(v3s16(1,3,2), MapNode(CONTENT_STONE));
855 std::set<v3s16> light_sources;
856 voxalgo::setLight(v, a, 0, ndef);
857 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
858 v, a, true, light_sources, ndef);
859 UASSERT(res.bottom_sunlight_valid == true);
860 UASSERT(v.getNode(v3s16(1,1,2)).getLight(LIGHTBANK_DAY, ndef)
864 std::set<v3s16> light_sources;
865 voxalgo::setLight(v, a, 0, ndef);
866 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
867 v, a, false, light_sources, ndef);
868 UASSERT(res.bottom_sunlight_valid == true);
869 UASSERT(v.getNode(v3s16(1,0,2)).getLight(LIGHTBANK_DAY, ndef)
873 MapNode n(CONTENT_AIR);
874 n.setLight(LIGHTBANK_DAY, 10, ndef);
875 v.setNodeNoRef(v3s16(1,-1,2), n);
878 std::set<v3s16> light_sources;
879 voxalgo::setLight(v, a, 0, ndef);
880 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
881 v, a, true, light_sources, ndef);
882 UASSERT(res.bottom_sunlight_valid == true);
885 std::set<v3s16> light_sources;
886 voxalgo::setLight(v, a, 0, ndef);
887 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
888 v, a, false, light_sources, ndef);
889 UASSERT(res.bottom_sunlight_valid == true);
892 MapNode n(CONTENT_AIR);
893 n.setLight(LIGHTBANK_DAY, LIGHT_SUN, ndef);
894 v.setNodeNoRef(v3s16(1,-1,2), n);
897 std::set<v3s16> light_sources;
898 voxalgo::setLight(v, a, 0, ndef);
899 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
900 v, a, true, light_sources, ndef);
901 UASSERT(res.bottom_sunlight_valid == false);
904 std::set<v3s16> light_sources;
905 voxalgo::setLight(v, a, 0, ndef);
906 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
907 v, a, false, light_sources, ndef);
908 UASSERT(res.bottom_sunlight_valid == false);
910 v.setNodeNoRef(v3s16(1,3,2), MapNode(CONTENT_IGNORE));
912 std::set<v3s16> light_sources;
913 voxalgo::setLight(v, a, 0, ndef);
914 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
915 v, a, true, light_sources, ndef);
916 UASSERT(res.bottom_sunlight_valid == true);
920 voxalgo::clearLightAndCollectSources
924 for(u16 z=0; z<3; z++)
925 for(u16 y=0; y<3; y++)
926 for(u16 x=0; x<3; x++)
929 v.setNode(p, MapNode(CONTENT_AIR));
931 VoxelArea a(v3s16(0,0,0), v3s16(2,2,2));
932 v.setNodeNoRef(v3s16(0,0,0), MapNode(CONTENT_STONE));
933 v.setNodeNoRef(v3s16(1,1,1), MapNode(CONTENT_TORCH));
935 MapNode n(CONTENT_AIR);
936 n.setLight(LIGHTBANK_DAY, 1, ndef);
937 v.setNode(v3s16(1,1,2), n);
940 std::set<v3s16> light_sources;
941 std::map<v3s16, u8> unlight_from;
942 voxalgo::clearLightAndCollectSources(v, a, LIGHTBANK_DAY,
943 ndef, light_sources, unlight_from);
944 //v.print(dstream, ndef, VOXELPRINT_LIGHT_DAY);
945 UASSERT(v.getNode(v3s16(0,1,1)).getLight(LIGHTBANK_DAY, ndef)
947 UASSERT(light_sources.find(v3s16(1,1,1)) != light_sources.end());
948 UASSERT(light_sources.size() == 1);
949 UASSERT(unlight_from.find(v3s16(1,1,2)) != unlight_from.end());
950 UASSERT(unlight_from.size() == 1);
956 struct TestInventory: public TestBase
958 void Run(IItemDefManager *idef)
960 std::string serialized_inventory =
972 "Item default:cobble 61\n"
979 "Item default:dirt 71\n"
987 "Item default:dirt 99\n"
988 "Item default:cobble 38\n"
998 std::string serialized_inventory_2 =
1010 "Item default:cobble 61\n"
1017 "Item default:dirt 71\n"
1025 "Item default:dirt 99\n"
1026 "Item default:cobble 38\n"
1033 "EndInventoryList\n"
1036 Inventory inv(idef);
1037 std::istringstream is(serialized_inventory, std::ios::binary);
1038 inv.deSerialize(is);
1039 UASSERT(inv.getList("0"));
1040 UASSERT(!inv.getList("main"));
1041 inv.getList("0")->setName("main");
1042 UASSERT(!inv.getList("0"));
1043 UASSERT(inv.getList("main"));
1044 UASSERT(inv.getList("main")->getWidth() == 3);
1045 inv.getList("main")->setWidth(5);
1046 std::ostringstream inv_os(std::ios::binary);
1047 inv.serialize(inv_os);
1048 UASSERT(inv_os.str() == serialized_inventory_2);
1053 NOTE: These tests became non-working then NodeContainer was removed.
1054 These should be redone, utilizing some kind of a virtual
1055 interface for Map (IMap would be fine).
1058 struct TestMapBlock: public TestBase
1060 class TC : public NodeContainer
1065 bool position_valid;
1066 core::list<v3s16> validity_exceptions;
1070 position_valid = true;
1073 virtual bool isValidPosition(v3s16 p)
1075 //return position_valid ^ (p==position_valid_exception);
1076 bool exception = false;
1077 for(core::list<v3s16>::Iterator i=validity_exceptions.begin();
1078 i != validity_exceptions.end(); i++)
1086 return exception ? !position_valid : position_valid;
1089 virtual MapNode getNode(v3s16 p)
1091 if(isValidPosition(p) == false)
1092 throw InvalidPositionException();
1096 virtual void setNode(v3s16 p, MapNode & n)
1098 if(isValidPosition(p) == false)
1099 throw InvalidPositionException();
1102 virtual u16 nodeContainerId() const
1112 MapBlock b(&parent, v3s16(1,1,1));
1113 v3s16 relpos(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
1115 UASSERT(b.getPosRelative() == relpos);
1117 UASSERT(b.getBox().MinEdge.X == MAP_BLOCKSIZE);
1118 UASSERT(b.getBox().MaxEdge.X == MAP_BLOCKSIZE*2-1);
1119 UASSERT(b.getBox().MinEdge.Y == MAP_BLOCKSIZE);
1120 UASSERT(b.getBox().MaxEdge.Y == MAP_BLOCKSIZE*2-1);
1121 UASSERT(b.getBox().MinEdge.Z == MAP_BLOCKSIZE);
1122 UASSERT(b.getBox().MaxEdge.Z == MAP_BLOCKSIZE*2-1);
1124 UASSERT(b.isValidPosition(v3s16(0,0,0)) == true);
1125 UASSERT(b.isValidPosition(v3s16(-1,0,0)) == false);
1126 UASSERT(b.isValidPosition(v3s16(-1,-142,-2341)) == false);
1127 UASSERT(b.isValidPosition(v3s16(-124,142,2341)) == false);
1128 UASSERT(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
1129 UASSERT(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE,MAP_BLOCKSIZE-1)) == false);
1132 TODO: this method should probably be removed
1133 if the block size isn't going to be set variable
1135 /*UASSERT(b.getSizeNodes() == v3s16(MAP_BLOCKSIZE,
1136 MAP_BLOCKSIZE, MAP_BLOCKSIZE));*/
1138 // Changed flag should be initially set
1139 UASSERT(b.getModified() == MOD_STATE_WRITE_NEEDED);
1141 UASSERT(b.getModified() == MOD_STATE_CLEAN);
1143 // All nodes should have been set to
1144 // .d=CONTENT_IGNORE and .getLight() = 0
1145 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
1146 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
1147 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
1149 //UASSERT(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_AIR);
1150 UASSERT(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_IGNORE);
1151 UASSERT(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_DAY) == 0);
1152 UASSERT(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_NIGHT) == 0);
1156 MapNode n(CONTENT_AIR);
1157 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
1158 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
1159 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
1161 b.setNode(v3s16(x,y,z), n);
1166 Parent fetch functions
1168 parent.position_valid = false;
1169 parent.node.setContent(5);
1173 // Positions in the block should still be valid
1174 UASSERT(b.isValidPositionParent(v3s16(0,0,0)) == true);
1175 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
1176 n = b.getNodeParent(v3s16(0,MAP_BLOCKSIZE-1,0));
1177 UASSERT(n.getContent() == CONTENT_AIR);
1179 // ...but outside the block they should be invalid
1180 UASSERT(b.isValidPositionParent(v3s16(-121,2341,0)) == false);
1181 UASSERT(b.isValidPositionParent(v3s16(-1,0,0)) == false);
1182 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == false);
1185 bool exception_thrown = false;
1187 // This should throw an exception
1188 MapNode n = b.getNodeParent(v3s16(0,0,-1));
1190 catch(InvalidPositionException &e)
1192 exception_thrown = true;
1194 UASSERT(exception_thrown);
1197 parent.position_valid = true;
1198 // Now the positions outside should be valid
1199 UASSERT(b.isValidPositionParent(v3s16(-121,2341,0)) == true);
1200 UASSERT(b.isValidPositionParent(v3s16(-1,0,0)) == true);
1201 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == true);
1202 n = b.getNodeParent(v3s16(0,0,MAP_BLOCKSIZE));
1203 UASSERT(n.getContent() == 5);
1211 UASSERT(b.getNode(p).getContent() == 4);
1212 //TODO: Update to new system
1213 /*UASSERT(b.getNodeTile(p) == 4);
1214 UASSERT(b.getNodeTile(v3s16(-1,-1,0)) == 5);*/
1219 // Set lighting of all nodes to 0
1220 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
1221 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
1222 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
1223 MapNode n = b.getNode(v3s16(x,y,z));
1224 n.setLight(LIGHTBANK_DAY, 0);
1225 n.setLight(LIGHTBANK_NIGHT, 0);
1226 b.setNode(v3s16(x,y,z), n);
1232 Check how the block handles being a lonely sky block
1234 parent.position_valid = true;
1235 b.setIsUnderground(false);
1236 parent.node.setContent(CONTENT_AIR);
1237 parent.node.setLight(LIGHTBANK_DAY, LIGHT_SUN);
1238 parent.node.setLight(LIGHTBANK_NIGHT, 0);
1239 core::map<v3s16, bool> light_sources;
1240 // The bottom block is invalid, because we have a shadowing node
1241 UASSERT(b.propagateSunlight(light_sources) == false);
1242 UASSERT(b.getNode(v3s16(1,4,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
1243 UASSERT(b.getNode(v3s16(1,3,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
1244 UASSERT(b.getNode(v3s16(1,2,0)).getLight(LIGHTBANK_DAY) == 0);
1245 UASSERT(b.getNode(v3s16(1,1,0)).getLight(LIGHTBANK_DAY) == 0);
1246 UASSERT(b.getNode(v3s16(1,0,0)).getLight(LIGHTBANK_DAY) == 0);
1247 UASSERT(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
1248 UASSERT(b.getFaceLight2(1000, p, v3s16(0,1,0)) == LIGHT_SUN);
1249 UASSERT(b.getFaceLight2(1000, p, v3s16(0,-1,0)) == 0);
1250 UASSERT(b.getFaceLight2(0, p, v3s16(0,-1,0)) == 0);
1251 // According to MapBlock::getFaceLight,
1252 // The face on the z+ side should have double-diminished light
1253 //UASSERT(b.getFaceLight(p, v3s16(0,0,1)) == diminish_light(diminish_light(LIGHT_MAX)));
1254 // The face on the z+ side should have diminished light
1255 UASSERT(b.getFaceLight2(1000, p, v3s16(0,0,1)) == diminish_light(LIGHT_MAX));
1258 Check how the block handles being in between blocks with some non-sunlight
1259 while being underground
1262 // Make neighbours to exist and set some non-sunlight to them
1263 parent.position_valid = true;
1264 b.setIsUnderground(true);
1265 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
1266 core::map<v3s16, bool> light_sources;
1267 // The block below should be valid because there shouldn't be
1268 // sunlight in there either
1269 UASSERT(b.propagateSunlight(light_sources, true) == true);
1270 // Should not touch nodes that are not affected (that is, all of them)
1271 //UASSERT(b.getNode(v3s16(1,2,3)).getLight() == LIGHT_SUN);
1272 // Should set light of non-sunlighted blocks to 0.
1273 UASSERT(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == 0);
1276 Set up a situation where:
1277 - There is only air in this block
1278 - There is a valid non-sunlighted block at the bottom, and
1279 - Invalid blocks elsewhere.
1280 - the block is not underground.
1282 This should result in bottom block invalidity
1285 b.setIsUnderground(false);
1287 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
1288 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
1289 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
1291 n.setContent(CONTENT_AIR);
1292 n.setLight(LIGHTBANK_DAY, 0);
1293 b.setNode(v3s16(x,y,z), n);
1297 // Make neighbours invalid
1298 parent.position_valid = false;
1299 // Add exceptions to the top of the bottom block
1300 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
1301 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
1303 parent.validity_exceptions.push_back(v3s16(MAP_BLOCKSIZE+x, MAP_BLOCKSIZE-1, MAP_BLOCKSIZE+z));
1305 // Lighting value for the valid nodes
1306 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
1307 core::map<v3s16, bool> light_sources;
1308 // Bottom block is not valid
1309 UASSERT(b.propagateSunlight(light_sources) == false);
1314 struct TestMapSector: public TestBase
1316 class TC : public NodeContainer
1321 bool position_valid;
1325 position_valid = true;
1328 virtual bool isValidPosition(v3s16 p)
1330 return position_valid;
1333 virtual MapNode getNode(v3s16 p)
1335 if(position_valid == false)
1336 throw InvalidPositionException();
1340 virtual void setNode(v3s16 p, MapNode & n)
1342 if(position_valid == false)
1343 throw InvalidPositionException();
1346 virtual u16 nodeContainerId() const
1355 parent.position_valid = false;
1357 // Create one with no heightmaps
1358 ServerMapSector sector(&parent, v2s16(1,1));
1360 UASSERT(sector.getBlockNoCreateNoEx(0) == 0);
1361 UASSERT(sector.getBlockNoCreateNoEx(1) == 0);
1363 MapBlock * bref = sector.createBlankBlock(-2);
1365 UASSERT(sector.getBlockNoCreateNoEx(0) == 0);
1366 UASSERT(sector.getBlockNoCreateNoEx(-2) == bref);
1368 //TODO: Check for AlreadyExistsException
1370 /*bool exception_thrown = false;
1374 catch(InvalidPositionException &e){
1375 exception_thrown = true;
1377 UASSERT(exception_thrown);*/
1383 struct TestCollision: public TestBase
1388 axisAlignedCollision
1391 for(s16 bx = -3; bx <= 3; bx++)
1392 for(s16 by = -3; by <= 3; by++)
1393 for(s16 bz = -3; bz <= 3; bz++)
1397 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1398 aabb3f m(bx-2, by, bz, bx-1, by+1, bz+1);
1401 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1402 UASSERT(fabs(dtime - 1.000) < 0.001);
1405 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1406 aabb3f m(bx-2, by, bz, bx-1, by+1, bz+1);
1409 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == -1);
1412 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1413 aabb3f m(bx-2, by+1.5, bz, bx-1, by+2.5, bz-1);
1416 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == -1);
1419 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1420 aabb3f m(bx-2, by-1.5, bz, bx-1.5, by+0.5, bz+1);
1423 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1424 UASSERT(fabs(dtime - 3.000) < 0.001);
1427 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1428 aabb3f m(bx-2, by-1.5, bz, bx-1.5, by+0.5, bz+1);
1431 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1432 UASSERT(fabs(dtime - 3.000) < 0.001);
1437 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1438 aabb3f m(bx+2, by, bz, bx+3, by+1, bz+1);
1441 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1442 UASSERT(fabs(dtime - 1.000) < 0.001);
1445 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1446 aabb3f m(bx+2, by, bz, bx+3, by+1, bz+1);
1449 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == -1);
1452 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1453 aabb3f m(bx+2, by, bz+1.5, bx+3, by+1, bz+3.5);
1456 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == -1);
1459 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1460 aabb3f m(bx+2, by-1.5, bz, bx+2.5, by-0.5, bz+1);
1461 v3f v(-0.5, 0.2, 0);
1463 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 1); // Y, not X!
1464 UASSERT(fabs(dtime - 2.500) < 0.001);
1467 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1468 aabb3f m(bx+2, by-1.5, bz, bx+2.5, by-0.5, bz+1);
1469 v3f v(-0.5, 0.3, 0);
1471 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1472 UASSERT(fabs(dtime - 2.000) < 0.001);
1475 // TODO: Y-, Y+, Z-, Z+
1479 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1480 aabb3f m(bx+2.3, by+2.29, bz+2.29, bx+4.2, by+4.2, bz+4.2);
1481 v3f v(-1./3, -1./3, -1./3);
1483 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1484 UASSERT(fabs(dtime - 0.9) < 0.001);
1487 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1488 aabb3f m(bx+2.29, by+2.3, bz+2.29, bx+4.2, by+4.2, bz+4.2);
1489 v3f v(-1./3, -1./3, -1./3);
1491 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 1);
1492 UASSERT(fabs(dtime - 0.9) < 0.001);
1495 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1496 aabb3f m(bx+2.29, by+2.29, bz+2.3, bx+4.2, by+4.2, bz+4.2);
1497 v3f v(-1./3, -1./3, -1./3);
1499 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 2);
1500 UASSERT(fabs(dtime - 0.9) < 0.001);
1503 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1504 aabb3f m(bx-4.2, by-4.2, bz-4.2, bx-2.3, by-2.29, bz-2.29);
1505 v3f v(1./7, 1./7, 1./7);
1507 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1508 UASSERT(fabs(dtime - 16.1) < 0.001);
1511 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1512 aabb3f m(bx-4.2, by-4.2, bz-4.2, bx-2.29, by-2.3, bz-2.29);
1513 v3f v(1./7, 1./7, 1./7);
1515 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 1);
1516 UASSERT(fabs(dtime - 16.1) < 0.001);
1519 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1520 aabb3f m(bx-4.2, by-4.2, bz-4.2, bx-2.29, by-2.29, bz-2.3);
1521 v3f v(1./7, 1./7, 1./7);
1523 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 2);
1524 UASSERT(fabs(dtime - 16.1) < 0.001);
1530 struct TestSocket: public TestBase
1534 const int port = 30003;
1535 Address address(0,0,0,0, port);
1536 Address address6((IPv6AddressBytes*) NULL, port);
1542 if (!socket6.init(true, true)) {
1543 /* Note: Failing to create an IPv6 socket is not technically an
1544 error because the OS may not support IPv6 or it may
1545 have been disabled. IPv6 is not /required/ by
1546 minetest and therefore this should not cause the unit
1549 dstream << "WARNING: IPv6 socket creation failed (unit test)"
1552 const char sendbuffer[] = "hello world!";
1553 IPv6AddressBytes bytes;
1554 bytes.bytes[15] = 1;
1556 socket6.Bind(address6);
1559 socket6.Send(Address(&bytes, port), sendbuffer, sizeof(sendbuffer));
1563 char rcvbuffer[256] = { 0 };
1567 if (socket6.Receive(sender, rcvbuffer, sizeof(rcvbuffer )) < 0)
1570 //FIXME: This fails on some systems
1571 UASSERT(strncmp(sendbuffer, rcvbuffer, sizeof(sendbuffer)) == 0);
1572 UASSERT(memcmp(sender.getAddress6().sin6_addr.s6_addr,
1573 Address(&bytes, 0).getAddress6().sin6_addr.s6_addr, 16) == 0);
1575 catch (SendFailedException e) {
1576 errorstream << "IPv6 support enabled but not available!"
1584 UDPSocket socket(false);
1585 socket.Bind(address);
1587 const char sendbuffer[] = "hello world!";
1588 socket.Send(Address(127, 0, 0 ,1, port), sendbuffer, sizeof(sendbuffer));
1592 char rcvbuffer[256] = { 0 };
1595 if (socket.Receive(sender, rcvbuffer, sizeof(rcvbuffer)) < 0)
1598 //FIXME: This fails on some systems
1599 UASSERT(strncmp(sendbuffer, rcvbuffer, sizeof(sendbuffer)) == 0);
1600 UASSERT(sender.getAddress().sin_addr.s_addr ==
1601 Address(127, 0, 0, 1, 0).getAddress().sin_addr.s_addr);
1606 struct TestConnection: public TestBase
1611 Test helper functions
1614 // Some constants for testing
1615 u32 proto_id = 0x12345678;
1618 SharedBuffer<u8> data1(1);
1620 Address a(127,0,0,1, 10);
1621 const u16 seqnum = 34352;
1623 con::BufferedPacket p1 = con::makePacket(a, data1,
1624 proto_id, peer_id, channel);
1626 We should now have a packet with this data:
1629 [4] u16 sender_peer_id
1634 UASSERT(readU32(&p1.data[0]) == proto_id);
1635 UASSERT(readU16(&p1.data[4]) == peer_id);
1636 UASSERT(readU8(&p1.data[6]) == channel);
1637 UASSERT(readU8(&p1.data[7]) == data1[0]);
1639 //infostream<<"initial data1[0]="<<((u32)data1[0]&0xff)<<std::endl;
1641 SharedBuffer<u8> p2 = con::makeReliablePacket(data1, seqnum);
1643 /*infostream<<"p2.getSize()="<<p2.getSize()<<", data1.getSize()="
1644 <<data1.getSize()<<std::endl;
1645 infostream<<"readU8(&p2[3])="<<readU8(&p2[3])
1646 <<" p2[3]="<<((u32)p2[3]&0xff)<<std::endl;
1647 infostream<<"data1[0]="<<((u32)data1[0]&0xff)<<std::endl;*/
1649 UASSERT(p2.getSize() == 3 + data1.getSize());
1650 UASSERT(readU8(&p2[0]) == TYPE_RELIABLE);
1651 UASSERT(readU16(&p2[1]) == seqnum);
1652 UASSERT(readU8(&p2[3]) == data1[0]);
1655 struct Handler : public con::PeerHandler
1657 Handler(const char *a_name)
1663 void peerAdded(con::Peer *peer)
1665 infostream<<"Handler("<<name<<")::peerAdded(): "
1666 "id="<<peer->id<<std::endl;
1670 void deletingPeer(con::Peer *peer, bool timeout)
1672 infostream<<"Handler("<<name<<")::deletingPeer(): "
1674 <<", timeout="<<timeout<<std::endl;
1686 DSTACK("TestConnection::Run");
1691 Test some real connections
1693 NOTE: This mostly tests the legacy interface.
1696 u32 proto_id = 0xad26846a;
1698 Handler hand_server("server");
1699 Handler hand_client("client");
1701 infostream<<"** Creating server Connection"<<std::endl;
1702 con::Connection server(proto_id, 512, 5.0, false, &hand_server);
1703 Address address(0,0,0,0, 30001);
1704 server.Serve(address);
1706 infostream<<"** Creating client Connection"<<std::endl;
1707 con::Connection client(proto_id, 512, 5.0, false, &hand_client);
1709 UASSERT(hand_server.count == 0);
1710 UASSERT(hand_client.count == 0);
1714 Address server_address(127,0,0,1, 30001);
1715 infostream<<"** running client.Connect()"<<std::endl;
1716 client.Connect(server_address);
1720 // Client should not have added client yet
1721 UASSERT(hand_client.count == 0);
1726 SharedBuffer<u8> data;
1727 infostream<<"** running client.Receive()"<<std::endl;
1728 u32 size = client.Receive(peer_id, data);
1729 infostream<<"** Client received: peer_id="<<peer_id
1733 catch(con::NoIncomingDataException &e)
1737 // Client should have added server now
1738 UASSERT(hand_client.count == 1);
1739 UASSERT(hand_client.last_id == 1);
1740 // Server should not have added client yet
1741 UASSERT(hand_server.count == 0);
1748 SharedBuffer<u8> data;
1749 infostream<<"** running server.Receive()"<<std::endl;
1750 u32 size = server.Receive(peer_id, data);
1751 infostream<<"** Server received: peer_id="<<peer_id
1755 catch(con::NoIncomingDataException &e)
1757 // No actual data received, but the client has
1758 // probably been connected
1761 // Client should be the same
1762 UASSERT(hand_client.count == 1);
1763 UASSERT(hand_client.last_id == 1);
1764 // Server should have the client
1765 UASSERT(hand_server.count == 1);
1766 UASSERT(hand_server.last_id == 2);
1770 while(client.Connected() == false)
1775 SharedBuffer<u8> data;
1776 infostream<<"** running client.Receive()"<<std::endl;
1777 u32 size = client.Receive(peer_id, data);
1778 infostream<<"** Client received: peer_id="<<peer_id
1782 catch(con::NoIncomingDataException &e)
1793 SharedBuffer<u8> data;
1794 infostream<<"** running server.Receive()"<<std::endl;
1795 u32 size = server.Receive(peer_id, data);
1796 infostream<<"** Server received: peer_id="<<peer_id
1800 catch(con::NoIncomingDataException &e)
1805 Simple send-receive test
1808 /*u8 data[] = "Hello World!";
1809 u32 datasize = sizeof(data);*/
1810 SharedBuffer<u8> data = SharedBufferFromString("Hello World!");
1812 infostream<<"** running client.Send()"<<std::endl;
1813 client.Send(PEER_ID_SERVER, 0, data, true);
1818 SharedBuffer<u8> recvdata;
1819 infostream<<"** running server.Receive()"<<std::endl;
1820 u32 size = server.Receive(peer_id, recvdata);
1821 infostream<<"** Server received: peer_id="<<peer_id
1825 UASSERT(memcmp(*data, *recvdata, data.getSize()) == 0);
1828 u16 peer_id_client = 2;
1831 Send consequent packets in different order
1832 Not compatible with new Connection, thus commented out.
1835 //u8 data1[] = "hello1";
1836 //u8 data2[] = "hello2";
1837 SharedBuffer<u8> data1 = SharedBufferFromString("hello1");
1838 SharedBuffer<u8> data2 = SharedBufferFromString("Hello2");
1840 Address client_address =
1841 server.GetPeerAddress(peer_id_client);
1843 infostream<<"*** Sending packets in wrong order (2,1,2)"
1847 con::Channel *ch = &server.getPeer(peer_id_client)->channels[chn];
1848 u16 sn = ch->next_outgoing_seqnum;
1849 ch->next_outgoing_seqnum = sn+1;
1850 server.Send(peer_id_client, chn, data2, true);
1851 ch->next_outgoing_seqnum = sn;
1852 server.Send(peer_id_client, chn, data1, true);
1853 ch->next_outgoing_seqnum = sn+1;
1854 server.Send(peer_id_client, chn, data2, true);
1858 infostream<<"*** Receiving the packets"<<std::endl;
1861 SharedBuffer<u8> recvdata;
1864 infostream<<"** running client.Receive()"<<std::endl;
1866 size = client.Receive(peer_id, recvdata);
1867 infostream<<"** Client received: peer_id="<<peer_id
1869 <<", data="<<*recvdata
1871 UASSERT(size == data1.getSize());
1872 UASSERT(memcmp(*data1, *recvdata, data1.getSize()) == 0);
1873 UASSERT(peer_id == PEER_ID_SERVER);
1875 infostream<<"** running client.Receive()"<<std::endl;
1877 size = client.Receive(peer_id, recvdata);
1878 infostream<<"** Client received: peer_id="<<peer_id
1880 <<", data="<<*recvdata
1882 UASSERT(size == data2.getSize());
1883 UASSERT(memcmp(*data2, *recvdata, data2.getSize()) == 0);
1884 UASSERT(peer_id == PEER_ID_SERVER);
1886 bool got_exception = false;
1889 infostream<<"** running client.Receive()"<<std::endl;
1891 size = client.Receive(peer_id, recvdata);
1892 infostream<<"** Client received: peer_id="<<peer_id
1894 <<", data="<<*recvdata
1897 catch(con::NoIncomingDataException &e)
1899 infostream<<"** No incoming data for client"<<std::endl;
1900 got_exception = true;
1902 UASSERT(got_exception);
1907 Send large amounts of packets (infinite test)
1908 Commented out because of infinity.
1911 infostream<<"Sending large amounts of packets (infinite test)"<<std::endl;
1914 int datasize = myrand_range(0,5)==0?myrand_range(100,10000):myrand_range(0,100);
1915 infostream<<"datasize="<<datasize<<std::endl;
1916 SharedBuffer<u8> data1(datasize);
1917 for(u16 i=0; i<datasize; i++)
1920 int sendtimes = myrand_range(1,10);
1921 for(int i=0; i<sendtimes; i++){
1922 server.Send(peer_id_client, 0, data1, true);
1925 infostream<<"sendcount="<<sendcount<<std::endl;
1927 //int receivetimes = myrand_range(1,20);
1928 int receivetimes = 20;
1929 for(int i=0; i<receivetimes; i++){
1930 SharedBuffer<u8> recvdata;
1933 bool received = false;
1935 size = client.Receive(peer_id, recvdata);
1937 }catch(con::NoIncomingDataException &e){
1947 const int datasize = 30000;
1948 SharedBuffer<u8> data1(datasize);
1949 for(u16 i=0; i<datasize; i++){
1953 infostream<<"Sending data (size="<<datasize<<"):";
1954 for(int i=0; i<datasize && i<20; i++){
1955 if(i%2==0) infostream<<" ";
1957 snprintf(buf, 10, "%.2X", ((int)((const char*)*data1)[i])&0xff);
1962 infostream<<std::endl;
1964 server.Send(peer_id_client, 0, data1, true);
1968 SharedBuffer<u8> recvdata;
1969 infostream<<"** running client.Receive()"<<std::endl;
1972 bool received = false;
1973 u32 timems0 = porting::getTimeMs();
1975 if(porting::getTimeMs() - timems0 > 5000 || received)
1978 size = client.Receive(peer_id, recvdata);
1980 }catch(con::NoIncomingDataException &e){
1985 infostream<<"** Client received: peer_id="<<peer_id
1989 infostream<<"Received data (size="<<size<<"): ";
1990 for(int i=0; i<size && i<20; i++){
1991 if(i%2==0) infostream<<" ";
1993 snprintf(buf, 10, "%.2X", ((int)(recvdata[i]))&0xff);
1998 infostream<<std::endl;
2000 UASSERT(memcmp(*data1, *recvdata, data1.getSize()) == 0);
2001 UASSERT(peer_id == PEER_ID_SERVER);
2004 // Check peer handlers
2005 UASSERT(hand_client.count == 1);
2006 UASSERT(hand_client.last_id == 1);
2007 UASSERT(hand_server.count == 1);
2008 UASSERT(hand_server.last_id == 2);
2014 #define TEST(X) do {\
2016 infostream<<"Running " #X <<std::endl;\
2019 tests_failed += x.test_failed ? 1 : 0;\
2022 #define TESTPARAMS(X, ...) do {\
2024 infostream<<"Running " #X <<std::endl;\
2025 x.Run(__VA_ARGS__);\
2027 tests_failed += x.test_failed ? 1 : 0;\
2032 DSTACK(__FUNCTION_NAME);
2035 int tests_failed = 0;
2037 // Create item and node definitions
2038 IWritableItemDefManager *idef = createItemDefManager();
2039 IWritableNodeDefManager *ndef = createNodeDefManager();
2040 define_some_nodes(idef, ndef);
2042 infostream<<"run_tests() started"<<std::endl;
2043 TEST(TestUtilities);
2047 TEST(TestSerialization);
2048 TEST(TestNodedefSerialization);
2049 TESTPARAMS(TestMapNode, ndef);
2050 TESTPARAMS(TestVoxelManipulator, ndef);
2051 TESTPARAMS(TestVoxelAlgorithms, ndef);
2052 TESTPARAMS(TestInventory, idef);
2053 //TEST(TestMapBlock);
2054 //TEST(TestMapSector);
2055 TEST(TestCollision);
2056 if(INTERNET_SIMULATOR == false){
2058 dout_con<<"=== BEGIN RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
2059 TEST(TestConnection);
2060 dout_con<<"=== END RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
2066 if(tests_failed == 0){
2067 infostream<<"run_tests(): "<<tests_failed<<" / "<<tests_run<<" tests failed."<<std::endl;
2068 infostream<<"run_tests() passed."<<std::endl;
2071 errorstream<<"run_tests(): "<<tests_failed<<" / "<<tests_run<<" tests failed."<<std::endl;
2072 errorstream<<"run_tests() aborting."<<std::endl;