3 Copyright (C) 2010 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"
39 #include "voxelalgorithms.h"
40 #include "inventory.h"
41 #include "util/numeric.h"
42 #include "util/serialize.h"
45 Asserts that the exception occurs
47 #define EXCEPTION_CHECK(EType, code)\
49 bool exception_thrown = false;\
51 catch(EType &e) { exception_thrown = true; }\
52 UASSERT(exception_thrown);\
55 #define UTEST(x, fmt, ...)\
58 LOGLINEF(LMT_ERROR, "Test (%s) failed: " fmt, #x, ##__VA_ARGS__);\
63 #define UASSERT(x) UTEST(x, "UASSERT")
66 A few item and node definitions for those tests that need them
69 #define CONTENT_STONE 0
70 #define CONTENT_GRASS 0x800
71 #define CONTENT_TORCH 100
73 void define_some_nodes(IWritableItemDefManager *idef, IWritableNodeDefManager *ndef)
76 ItemDefinition itemdef;
83 itemdef = ItemDefinition();
84 itemdef.type = ITEM_NODE;
85 itemdef.name = "default:stone";
86 itemdef.description = "Stone";
87 itemdef.groups["cracky"] = 3;
88 itemdef.inventory_image = "[inventorycube"
92 f = ContentFeatures();
93 f.name = itemdef.name;
94 for(int i = 0; i < 6; i++)
95 f.tiledef[i].name = "default_stone.png";
96 f.is_ground_content = true;
97 idef->registerItem(itemdef);
104 itemdef = ItemDefinition();
105 itemdef.type = ITEM_NODE;
106 itemdef.name = "default:dirt_with_grass";
107 itemdef.description = "Dirt with grass";
108 itemdef.groups["crumbly"] = 3;
109 itemdef.inventory_image = "[inventorycube"
111 "{default_dirt.png&default_grass_side.png"
112 "{default_dirt.png&default_grass_side.png";
113 f = ContentFeatures();
114 f.name = itemdef.name;
115 f.tiledef[0].name = "default_grass.png";
116 f.tiledef[1].name = "default_dirt.png";
117 for(int i = 2; i < 6; i++)
118 f.tiledef[i].name = "default_dirt.png^default_grass_side.png";
119 f.is_ground_content = true;
120 idef->registerItem(itemdef);
124 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);
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(is_yes("YeS") == true);
161 UASSERT(is_yes("") == false);
162 UASSERT(is_yes("FAlse") == false);
163 const char *ends[] = {"abc", "c", "bc", NULL};
164 UASSERT(removeStringEnd("abc", ends) == "");
165 UASSERT(removeStringEnd("bc", ends) == "b");
166 UASSERT(removeStringEnd("12c", ends) == "12");
167 UASSERT(removeStringEnd("foo", ends) == "");
171 struct TestSettings: public TestBase
176 // Test reading of settings
177 s.parseConfigLine("leet = 1337");
178 s.parseConfigLine("leetleet = 13371337");
179 s.parseConfigLine("leetleet_neg = -13371337");
180 s.parseConfigLine("floaty_thing = 1.1");
181 s.parseConfigLine("stringy_thing = asd /( ¤%&(/\" BLÖÄRP");
182 s.parseConfigLine("coord = (1, 2, 4.5)");
183 UASSERT(s.getS32("leet") == 1337);
184 UASSERT(s.getS16("leetleet") == 32767);
185 UASSERT(s.getS16("leetleet_neg") == -32768);
186 // Not sure if 1.1 is an exact value as a float, but doesn't matter
187 UASSERT(fabs(s.getFloat("floaty_thing") - 1.1) < 0.001);
188 UASSERT(s.get("stringy_thing") == "asd /( ¤%&(/\" BLÖÄRP");
189 UASSERT(fabs(s.getV3F("coord").X - 1.0) < 0.001);
190 UASSERT(fabs(s.getV3F("coord").Y - 2.0) < 0.001);
191 UASSERT(fabs(s.getV3F("coord").Z - 4.5) < 0.001);
192 // Test the setting of settings too
193 s.setFloat("floaty_thing_2", 1.2);
194 s.setV3F("coord2", v3f(1, 2, 3.3));
195 UASSERT(s.get("floaty_thing_2").substr(0,3) == "1.2");
196 UASSERT(fabs(s.getFloat("floaty_thing_2") - 1.2) < 0.001);
197 UASSERT(fabs(s.getV3F("coord2").X - 1.0) < 0.001);
198 UASSERT(fabs(s.getV3F("coord2").Y - 2.0) < 0.001);
199 UASSERT(fabs(s.getV3F("coord2").Z - 3.3) < 0.001);
203 struct TestSerialization: public TestBase
205 // To be used like this:
206 // mkstr("Some\0string\0with\0embedded\0nuls")
207 // since std::string("...") doesn't work as expected in that case.
208 template<size_t N> std::string mkstr(const char (&s)[N])
210 return std::string(s, N - 1);
215 // Tests some serialization primitives
217 UASSERT(serializeString("") == mkstr("\0\0"));
218 UASSERT(serializeWideString(L"") == mkstr("\0\0"));
219 UASSERT(serializeLongString("") == mkstr("\0\0\0\0"));
220 UASSERT(serializeJsonString("") == "\"\"");
222 std::string teststring = "Hello world!";
223 UASSERT(serializeString(teststring) ==
224 mkstr("\0\14Hello world!"));
225 UASSERT(serializeWideString(narrow_to_wide(teststring)) ==
226 mkstr("\0\14\0H\0e\0l\0l\0o\0 \0w\0o\0r\0l\0d\0!"));
227 UASSERT(serializeLongString(teststring) ==
228 mkstr("\0\0\0\14Hello world!"));
229 UASSERT(serializeJsonString(teststring) ==
232 std::string teststring2;
233 std::wstring teststring2_w;
234 std::string teststring2_w_encoded;
236 std::ostringstream tmp_os;
237 std::wostringstream tmp_os_w;
238 std::ostringstream tmp_os_w_encoded;
239 for(int i = 0; i < 256; i++)
242 tmp_os_w<<(wchar_t)i;
243 tmp_os_w_encoded<<(char)0<<(char)i;
245 teststring2 = tmp_os.str();
246 teststring2_w = tmp_os_w.str();
247 teststring2_w_encoded = tmp_os_w_encoded.str();
249 UASSERT(serializeString(teststring2) ==
250 mkstr("\1\0") + teststring2);
251 UASSERT(serializeWideString(teststring2_w) ==
252 mkstr("\1\0") + teststring2_w_encoded);
253 UASSERT(serializeLongString(teststring2) ==
254 mkstr("\0\0\1\0") + teststring2);
255 // MSVC fails when directly using "\\\\"
256 std::string backslash = "\\";
257 UASSERT(serializeJsonString(teststring2) ==
259 "\\u0000\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007" +
260 "\\b\\t\\n\\u000b\\f\\r\\u000e\\u000f" +
261 "\\u0010\\u0011\\u0012\\u0013\\u0014\\u0015\\u0016\\u0017" +
262 "\\u0018\\u0019\\u001a\\u001b\\u001c\\u001d\\u001e\\u001f" +
263 " !\\\"" + teststring2.substr(0x23, 0x2f-0x23) +
264 "\\/" + teststring2.substr(0x30, 0x5c-0x30) +
265 backslash + backslash + teststring2.substr(0x5d, 0x7f-0x5d) + "\\u007f" +
266 "\\u0080\\u0081\\u0082\\u0083\\u0084\\u0085\\u0086\\u0087" +
267 "\\u0088\\u0089\\u008a\\u008b\\u008c\\u008d\\u008e\\u008f" +
268 "\\u0090\\u0091\\u0092\\u0093\\u0094\\u0095\\u0096\\u0097" +
269 "\\u0098\\u0099\\u009a\\u009b\\u009c\\u009d\\u009e\\u009f" +
270 "\\u00a0\\u00a1\\u00a2\\u00a3\\u00a4\\u00a5\\u00a6\\u00a7" +
271 "\\u00a8\\u00a9\\u00aa\\u00ab\\u00ac\\u00ad\\u00ae\\u00af" +
272 "\\u00b0\\u00b1\\u00b2\\u00b3\\u00b4\\u00b5\\u00b6\\u00b7" +
273 "\\u00b8\\u00b9\\u00ba\\u00bb\\u00bc\\u00bd\\u00be\\u00bf" +
274 "\\u00c0\\u00c1\\u00c2\\u00c3\\u00c4\\u00c5\\u00c6\\u00c7" +
275 "\\u00c8\\u00c9\\u00ca\\u00cb\\u00cc\\u00cd\\u00ce\\u00cf" +
276 "\\u00d0\\u00d1\\u00d2\\u00d3\\u00d4\\u00d5\\u00d6\\u00d7" +
277 "\\u00d8\\u00d9\\u00da\\u00db\\u00dc\\u00dd\\u00de\\u00df" +
278 "\\u00e0\\u00e1\\u00e2\\u00e3\\u00e4\\u00e5\\u00e6\\u00e7" +
279 "\\u00e8\\u00e9\\u00ea\\u00eb\\u00ec\\u00ed\\u00ee\\u00ef" +
280 "\\u00f0\\u00f1\\u00f2\\u00f3\\u00f4\\u00f5\\u00f6\\u00f7" +
281 "\\u00f8\\u00f9\\u00fa\\u00fb\\u00fc\\u00fd\\u00fe\\u00ff" +
285 std::istringstream is(serializeString(teststring2), std::ios::binary);
286 UASSERT(deSerializeString(is) == teststring2);
292 std::istringstream is(serializeWideString(teststring2_w), std::ios::binary);
293 UASSERT(deSerializeWideString(is) == teststring2_w);
299 std::istringstream is(serializeLongString(teststring2), std::ios::binary);
300 UASSERT(deSerializeLongString(is) == teststring2);
306 std::istringstream is(serializeJsonString(teststring2), std::ios::binary);
307 //dstream<<serializeJsonString(deSerializeJsonString(is));
308 UASSERT(deSerializeJsonString(is) == teststring2);
316 struct TestCompress: public TestBase
322 SharedBuffer<u8> fromdata(4);
328 std::ostringstream os(std::ios_base::binary);
329 compress(fromdata, os, 0);
331 std::string str_out = os.str();
333 infostream<<"str_out.size()="<<str_out.size()<<std::endl;
334 infostream<<"TestCompress: 1,5,5,1 -> ";
335 for(u32 i=0; i<str_out.size(); i++)
337 infostream<<(u32)str_out[i]<<",";
339 infostream<<std::endl;
341 UASSERT(str_out.size() == 10);
343 UASSERT(str_out[0] == 0);
344 UASSERT(str_out[1] == 0);
345 UASSERT(str_out[2] == 0);
346 UASSERT(str_out[3] == 4);
347 UASSERT(str_out[4] == 0);
348 UASSERT(str_out[5] == 1);
349 UASSERT(str_out[6] == 1);
350 UASSERT(str_out[7] == 5);
351 UASSERT(str_out[8] == 0);
352 UASSERT(str_out[9] == 1);
354 std::istringstream is(str_out, std::ios_base::binary);
355 std::ostringstream os2(std::ios_base::binary);
357 decompress(is, os2, 0);
358 std::string str_out2 = os2.str();
360 infostream<<"decompress: ";
361 for(u32 i=0; i<str_out2.size(); i++)
363 infostream<<(u32)str_out2[i]<<",";
365 infostream<<std::endl;
367 UASSERT(str_out2.size() == fromdata.getSize());
369 for(u32 i=0; i<str_out2.size(); i++)
371 UASSERT(str_out2[i] == fromdata[i]);
378 SharedBuffer<u8> fromdata(4);
384 std::ostringstream os(std::ios_base::binary);
385 compress(fromdata, os, SER_FMT_VER_HIGHEST);
387 std::string str_out = os.str();
389 infostream<<"str_out.size()="<<str_out.size()<<std::endl;
390 infostream<<"TestCompress: 1,5,5,1 -> ";
391 for(u32 i=0; i<str_out.size(); i++)
393 infostream<<(u32)str_out[i]<<",";
395 infostream<<std::endl;
397 std::istringstream is(str_out, std::ios_base::binary);
398 std::ostringstream os2(std::ios_base::binary);
400 decompress(is, os2, SER_FMT_VER_HIGHEST);
401 std::string str_out2 = os2.str();
403 infostream<<"decompress: ";
404 for(u32 i=0; i<str_out2.size(); i++)
406 infostream<<(u32)str_out2[i]<<",";
408 infostream<<std::endl;
410 UASSERT(str_out2.size() == fromdata.getSize());
412 for(u32 i=0; i<str_out2.size(); i++)
414 UASSERT(str_out2[i] == fromdata[i]);
421 struct TestMapNode: public TestBase
423 void Run(INodeDefManager *nodedef)
428 UASSERT(n.getContent() == CONTENT_AIR);
429 UASSERT(n.getLight(LIGHTBANK_DAY, nodedef) == 0);
430 UASSERT(n.getLight(LIGHTBANK_NIGHT, nodedef) == 0);
433 n.setContent(CONTENT_AIR);
434 UASSERT(nodedef->get(n).light_propagates == true);
435 n.setContent(LEGN(nodedef, "CONTENT_STONE"));
436 UASSERT(nodedef->get(n).light_propagates == false);
440 struct TestVoxelManipulator: public TestBase
442 void Run(INodeDefManager *nodedef)
448 VoxelArea a(v3s16(-1,-1,-1), v3s16(1,1,1));
449 UASSERT(a.index(0,0,0) == 1*3*3 + 1*3 + 1);
450 UASSERT(a.index(-1,-1,-1) == 0);
452 VoxelArea c(v3s16(-2,-2,-2), v3s16(2,2,2));
453 // An area that is 1 bigger in x+ and z-
454 VoxelArea d(v3s16(-2,-2,-3), v3s16(3,2,2));
456 core::list<VoxelArea> aa;
460 core::array<VoxelArea> results;
461 results.push_back(VoxelArea(v3s16(-2,-2,-3),v3s16(3,2,-3)));
462 results.push_back(VoxelArea(v3s16(3,-2,-2),v3s16(3,2,2)));
464 UASSERT(aa.size() == results.size());
466 infostream<<"Result of diff:"<<std::endl;
467 for(core::list<VoxelArea>::Iterator
468 i = aa.begin(); i != aa.end(); i++)
470 i->print(infostream);
471 infostream<<std::endl;
473 s32 j = results.linear_search(*i);
485 v.print(infostream, nodedef);
487 infostream<<"*** Setting (-1,0,-1)=2 ***"<<std::endl;
489 v.setNodeNoRef(v3s16(-1,0,-1), MapNode(CONTENT_GRASS));
491 v.print(infostream, nodedef);
493 UASSERT(v.getNode(v3s16(-1,0,-1)).getContent() == CONTENT_GRASS);
495 infostream<<"*** Reading from inexistent (0,0,-1) ***"<<std::endl;
497 EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,0,-1)));
499 v.print(infostream, nodedef);
501 infostream<<"*** Adding area ***"<<std::endl;
505 v.print(infostream, nodedef);
507 UASSERT(v.getNode(v3s16(-1,0,-1)).getContent() == CONTENT_GRASS);
508 EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,1,1)));
512 struct TestVoxelAlgorithms: public TestBase
514 void Run(INodeDefManager *ndef)
517 voxalgo::propagateSunlight
521 for(u16 z=0; z<3; z++)
522 for(u16 y=0; y<3; y++)
523 for(u16 x=0; x<3; x++)
526 v.setNodeNoRef(p, MapNode(CONTENT_AIR));
528 VoxelArea a(v3s16(0,0,0), v3s16(2,2,2));
530 core::map<v3s16, bool> light_sources;
531 voxalgo::setLight(v, a, 0, ndef);
532 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
533 v, a, true, light_sources, ndef);
534 //v.print(dstream, ndef, VOXELPRINT_LIGHT_DAY);
535 UASSERT(res.bottom_sunlight_valid == true);
536 UASSERT(v.getNode(v3s16(1,1,1)).getLight(LIGHTBANK_DAY, ndef)
539 v.setNodeNoRef(v3s16(0,0,0), MapNode(CONTENT_STONE));
541 core::map<v3s16, bool> light_sources;
542 voxalgo::setLight(v, a, 0, ndef);
543 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
544 v, a, true, light_sources, ndef);
545 UASSERT(res.bottom_sunlight_valid == true);
546 UASSERT(v.getNode(v3s16(1,1,1)).getLight(LIGHTBANK_DAY, ndef)
550 core::map<v3s16, bool> light_sources;
551 voxalgo::setLight(v, a, 0, ndef);
552 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
553 v, a, false, light_sources, ndef);
554 UASSERT(res.bottom_sunlight_valid == true);
555 UASSERT(v.getNode(v3s16(2,0,2)).getLight(LIGHTBANK_DAY, ndef)
558 v.setNodeNoRef(v3s16(1,3,2), MapNode(CONTENT_STONE));
560 core::map<v3s16, bool> light_sources;
561 voxalgo::setLight(v, a, 0, ndef);
562 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
563 v, a, true, light_sources, ndef);
564 UASSERT(res.bottom_sunlight_valid == true);
565 UASSERT(v.getNode(v3s16(1,1,2)).getLight(LIGHTBANK_DAY, ndef)
569 core::map<v3s16, bool> light_sources;
570 voxalgo::setLight(v, a, 0, ndef);
571 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
572 v, a, false, light_sources, ndef);
573 UASSERT(res.bottom_sunlight_valid == true);
574 UASSERT(v.getNode(v3s16(1,0,2)).getLight(LIGHTBANK_DAY, ndef)
578 MapNode n(CONTENT_AIR);
579 n.setLight(LIGHTBANK_DAY, 10, ndef);
580 v.setNodeNoRef(v3s16(1,-1,2), n);
583 core::map<v3s16, bool> light_sources;
584 voxalgo::setLight(v, a, 0, ndef);
585 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
586 v, a, true, light_sources, ndef);
587 UASSERT(res.bottom_sunlight_valid == true);
590 core::map<v3s16, bool> light_sources;
591 voxalgo::setLight(v, a, 0, ndef);
592 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
593 v, a, false, light_sources, ndef);
594 UASSERT(res.bottom_sunlight_valid == true);
597 MapNode n(CONTENT_AIR);
598 n.setLight(LIGHTBANK_DAY, LIGHT_SUN, ndef);
599 v.setNodeNoRef(v3s16(1,-1,2), n);
602 core::map<v3s16, bool> light_sources;
603 voxalgo::setLight(v, a, 0, ndef);
604 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
605 v, a, true, light_sources, ndef);
606 UASSERT(res.bottom_sunlight_valid == false);
609 core::map<v3s16, bool> light_sources;
610 voxalgo::setLight(v, a, 0, ndef);
611 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
612 v, a, false, light_sources, ndef);
613 UASSERT(res.bottom_sunlight_valid == false);
615 v.setNodeNoRef(v3s16(1,3,2), MapNode(CONTENT_IGNORE));
617 core::map<v3s16, bool> light_sources;
618 voxalgo::setLight(v, a, 0, ndef);
619 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
620 v, a, true, light_sources, ndef);
621 UASSERT(res.bottom_sunlight_valid == true);
625 voxalgo::clearLightAndCollectSources
629 for(u16 z=0; z<3; z++)
630 for(u16 y=0; y<3; y++)
631 for(u16 x=0; x<3; x++)
634 v.setNode(p, MapNode(CONTENT_AIR));
636 VoxelArea a(v3s16(0,0,0), v3s16(2,2,2));
637 v.setNodeNoRef(v3s16(0,0,0), MapNode(CONTENT_STONE));
638 v.setNodeNoRef(v3s16(1,1,1), MapNode(CONTENT_TORCH));
640 MapNode n(CONTENT_AIR);
641 n.setLight(LIGHTBANK_DAY, 1, ndef);
642 v.setNode(v3s16(1,1,2), n);
645 core::map<v3s16, bool> light_sources;
646 core::map<v3s16, u8> unlight_from;
647 voxalgo::clearLightAndCollectSources(v, a, LIGHTBANK_DAY,
648 ndef, light_sources, unlight_from);
649 //v.print(dstream, ndef, VOXELPRINT_LIGHT_DAY);
650 UASSERT(v.getNode(v3s16(0,1,1)).getLight(LIGHTBANK_DAY, ndef)
652 UASSERT(light_sources.find(v3s16(1,1,1)) != NULL);
653 UASSERT(light_sources.size() == 1);
654 UASSERT(unlight_from.find(v3s16(1,1,2)) != NULL);
655 UASSERT(unlight_from.size() == 1);
661 struct TestInventory: public TestBase
663 void Run(IItemDefManager *idef)
665 std::string serialized_inventory =
676 "Item default:cobble 61\n"
683 "Item default:dirt 71\n"
691 "Item default:dirt 99\n"
692 "Item default:cobble 38\n"
702 std::string serialized_inventory_2 =
713 "Item default:cobble 61\n"
720 "Item default:dirt 71\n"
728 "Item default:dirt 99\n"
729 "Item default:cobble 38\n"
740 std::istringstream is(serialized_inventory, std::ios::binary);
742 UASSERT(inv.getList("0"));
743 UASSERT(!inv.getList("main"));
744 inv.getList("0")->setName("main");
745 UASSERT(!inv.getList("0"));
746 UASSERT(inv.getList("main"));
747 std::ostringstream inv_os(std::ios::binary);
748 inv.serialize(inv_os);
749 UASSERT(inv_os.str() == serialized_inventory_2);
754 NOTE: These tests became non-working then NodeContainer was removed.
755 These should be redone, utilizing some kind of a virtual
756 interface for Map (IMap would be fine).
759 struct TestMapBlock: public TestBase
761 class TC : public NodeContainer
767 core::list<v3s16> validity_exceptions;
771 position_valid = true;
774 virtual bool isValidPosition(v3s16 p)
776 //return position_valid ^ (p==position_valid_exception);
777 bool exception = false;
778 for(core::list<v3s16>::Iterator i=validity_exceptions.begin();
779 i != validity_exceptions.end(); i++)
787 return exception ? !position_valid : position_valid;
790 virtual MapNode getNode(v3s16 p)
792 if(isValidPosition(p) == false)
793 throw InvalidPositionException();
797 virtual void setNode(v3s16 p, MapNode & n)
799 if(isValidPosition(p) == false)
800 throw InvalidPositionException();
803 virtual u16 nodeContainerId() const
813 MapBlock b(&parent, v3s16(1,1,1));
814 v3s16 relpos(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
816 UASSERT(b.getPosRelative() == relpos);
818 UASSERT(b.getBox().MinEdge.X == MAP_BLOCKSIZE);
819 UASSERT(b.getBox().MaxEdge.X == MAP_BLOCKSIZE*2-1);
820 UASSERT(b.getBox().MinEdge.Y == MAP_BLOCKSIZE);
821 UASSERT(b.getBox().MaxEdge.Y == MAP_BLOCKSIZE*2-1);
822 UASSERT(b.getBox().MinEdge.Z == MAP_BLOCKSIZE);
823 UASSERT(b.getBox().MaxEdge.Z == MAP_BLOCKSIZE*2-1);
825 UASSERT(b.isValidPosition(v3s16(0,0,0)) == true);
826 UASSERT(b.isValidPosition(v3s16(-1,0,0)) == false);
827 UASSERT(b.isValidPosition(v3s16(-1,-142,-2341)) == false);
828 UASSERT(b.isValidPosition(v3s16(-124,142,2341)) == false);
829 UASSERT(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
830 UASSERT(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE,MAP_BLOCKSIZE-1)) == false);
833 TODO: this method should probably be removed
834 if the block size isn't going to be set variable
836 /*UASSERT(b.getSizeNodes() == v3s16(MAP_BLOCKSIZE,
837 MAP_BLOCKSIZE, MAP_BLOCKSIZE));*/
839 // Changed flag should be initially set
840 UASSERT(b.getModified() == MOD_STATE_WRITE_NEEDED);
842 UASSERT(b.getModified() == MOD_STATE_CLEAN);
844 // All nodes should have been set to
845 // .d=CONTENT_IGNORE and .getLight() = 0
846 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
847 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
848 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
850 //UASSERT(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_AIR);
851 UASSERT(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_IGNORE);
852 UASSERT(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_DAY) == 0);
853 UASSERT(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_NIGHT) == 0);
857 MapNode n(CONTENT_AIR);
858 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
859 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
860 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
862 b.setNode(v3s16(x,y,z), n);
867 Parent fetch functions
869 parent.position_valid = false;
870 parent.node.setContent(5);
874 // Positions in the block should still be valid
875 UASSERT(b.isValidPositionParent(v3s16(0,0,0)) == true);
876 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
877 n = b.getNodeParent(v3s16(0,MAP_BLOCKSIZE-1,0));
878 UASSERT(n.getContent() == CONTENT_AIR);
880 // ...but outside the block they should be invalid
881 UASSERT(b.isValidPositionParent(v3s16(-121,2341,0)) == false);
882 UASSERT(b.isValidPositionParent(v3s16(-1,0,0)) == false);
883 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == false);
886 bool exception_thrown = false;
888 // This should throw an exception
889 MapNode n = b.getNodeParent(v3s16(0,0,-1));
891 catch(InvalidPositionException &e)
893 exception_thrown = true;
895 UASSERT(exception_thrown);
898 parent.position_valid = true;
899 // Now the positions outside should be valid
900 UASSERT(b.isValidPositionParent(v3s16(-121,2341,0)) == true);
901 UASSERT(b.isValidPositionParent(v3s16(-1,0,0)) == true);
902 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == true);
903 n = b.getNodeParent(v3s16(0,0,MAP_BLOCKSIZE));
904 UASSERT(n.getContent() == 5);
912 UASSERT(b.getNode(p).getContent() == 4);
913 //TODO: Update to new system
914 /*UASSERT(b.getNodeTile(p) == 4);
915 UASSERT(b.getNodeTile(v3s16(-1,-1,0)) == 5);*/
920 // Set lighting of all nodes to 0
921 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
922 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
923 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
924 MapNode n = b.getNode(v3s16(x,y,z));
925 n.setLight(LIGHTBANK_DAY, 0);
926 n.setLight(LIGHTBANK_NIGHT, 0);
927 b.setNode(v3s16(x,y,z), n);
933 Check how the block handles being a lonely sky block
935 parent.position_valid = true;
936 b.setIsUnderground(false);
937 parent.node.setContent(CONTENT_AIR);
938 parent.node.setLight(LIGHTBANK_DAY, LIGHT_SUN);
939 parent.node.setLight(LIGHTBANK_NIGHT, 0);
940 core::map<v3s16, bool> light_sources;
941 // The bottom block is invalid, because we have a shadowing node
942 UASSERT(b.propagateSunlight(light_sources) == false);
943 UASSERT(b.getNode(v3s16(1,4,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
944 UASSERT(b.getNode(v3s16(1,3,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
945 UASSERT(b.getNode(v3s16(1,2,0)).getLight(LIGHTBANK_DAY) == 0);
946 UASSERT(b.getNode(v3s16(1,1,0)).getLight(LIGHTBANK_DAY) == 0);
947 UASSERT(b.getNode(v3s16(1,0,0)).getLight(LIGHTBANK_DAY) == 0);
948 UASSERT(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
949 UASSERT(b.getFaceLight2(1000, p, v3s16(0,1,0)) == LIGHT_SUN);
950 UASSERT(b.getFaceLight2(1000, p, v3s16(0,-1,0)) == 0);
951 UASSERT(b.getFaceLight2(0, p, v3s16(0,-1,0)) == 0);
952 // According to MapBlock::getFaceLight,
953 // The face on the z+ side should have double-diminished light
954 //UASSERT(b.getFaceLight(p, v3s16(0,0,1)) == diminish_light(diminish_light(LIGHT_MAX)));
955 // The face on the z+ side should have diminished light
956 UASSERT(b.getFaceLight2(1000, p, v3s16(0,0,1)) == diminish_light(LIGHT_MAX));
959 Check how the block handles being in between blocks with some non-sunlight
960 while being underground
963 // Make neighbours to exist and set some non-sunlight to them
964 parent.position_valid = true;
965 b.setIsUnderground(true);
966 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
967 core::map<v3s16, bool> light_sources;
968 // The block below should be valid because there shouldn't be
969 // sunlight in there either
970 UASSERT(b.propagateSunlight(light_sources, true) == true);
971 // Should not touch nodes that are not affected (that is, all of them)
972 //UASSERT(b.getNode(v3s16(1,2,3)).getLight() == LIGHT_SUN);
973 // Should set light of non-sunlighted blocks to 0.
974 UASSERT(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == 0);
977 Set up a situation where:
978 - There is only air in this block
979 - There is a valid non-sunlighted block at the bottom, and
980 - Invalid blocks elsewhere.
981 - the block is not underground.
983 This should result in bottom block invalidity
986 b.setIsUnderground(false);
988 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
989 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
990 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
992 n.setContent(CONTENT_AIR);
993 n.setLight(LIGHTBANK_DAY, 0);
994 b.setNode(v3s16(x,y,z), n);
998 // Make neighbours invalid
999 parent.position_valid = false;
1000 // Add exceptions to the top of the bottom block
1001 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
1002 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
1004 parent.validity_exceptions.push_back(v3s16(MAP_BLOCKSIZE+x, MAP_BLOCKSIZE-1, MAP_BLOCKSIZE+z));
1006 // Lighting value for the valid nodes
1007 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
1008 core::map<v3s16, bool> light_sources;
1009 // Bottom block is not valid
1010 UASSERT(b.propagateSunlight(light_sources) == false);
1015 struct TestMapSector: public TestBase
1017 class TC : public NodeContainer
1022 bool position_valid;
1026 position_valid = true;
1029 virtual bool isValidPosition(v3s16 p)
1031 return position_valid;
1034 virtual MapNode getNode(v3s16 p)
1036 if(position_valid == false)
1037 throw InvalidPositionException();
1041 virtual void setNode(v3s16 p, MapNode & n)
1043 if(position_valid == false)
1044 throw InvalidPositionException();
1047 virtual u16 nodeContainerId() const
1056 parent.position_valid = false;
1058 // Create one with no heightmaps
1059 ServerMapSector sector(&parent, v2s16(1,1));
1061 UASSERT(sector.getBlockNoCreateNoEx(0) == 0);
1062 UASSERT(sector.getBlockNoCreateNoEx(1) == 0);
1064 MapBlock * bref = sector.createBlankBlock(-2);
1066 UASSERT(sector.getBlockNoCreateNoEx(0) == 0);
1067 UASSERT(sector.getBlockNoCreateNoEx(-2) == bref);
1069 //TODO: Check for AlreadyExistsException
1071 /*bool exception_thrown = false;
1075 catch(InvalidPositionException &e){
1076 exception_thrown = true;
1078 UASSERT(exception_thrown);*/
1084 struct TestCollision: public TestBase
1089 axisAlignedCollision
1092 for(s16 bx = -3; bx <= 3; bx++)
1093 for(s16 by = -3; by <= 3; by++)
1094 for(s16 bz = -3; bz <= 3; bz++)
1098 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1099 aabb3f m(bx-2, by, bz, bx-1, by+1, bz+1);
1102 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1103 UASSERT(fabs(dtime - 1.000) < 0.001);
1106 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1107 aabb3f m(bx-2, by, bz, bx-1, by+1, bz+1);
1110 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == -1);
1113 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1114 aabb3f m(bx-2, by+1.5, bz, bx-1, by+2.5, bz-1);
1117 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == -1);
1120 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1121 aabb3f m(bx-2, by-1.5, bz, bx-1.5, by+0.5, bz+1);
1124 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1125 UASSERT(fabs(dtime - 3.000) < 0.001);
1128 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1129 aabb3f m(bx-2, by-1.5, bz, bx-1.5, by+0.5, bz+1);
1132 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1133 UASSERT(fabs(dtime - 3.000) < 0.001);
1138 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1139 aabb3f m(bx+2, by, bz, bx+3, by+1, bz+1);
1142 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1143 UASSERT(fabs(dtime - 1.000) < 0.001);
1146 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1147 aabb3f m(bx+2, by, bz, bx+3, by+1, bz+1);
1150 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == -1);
1153 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1154 aabb3f m(bx+2, by, bz+1.5, bx+3, by+1, bz+3.5);
1157 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == -1);
1160 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1161 aabb3f m(bx+2, by-1.5, bz, bx+2.5, by-0.5, bz+1);
1162 v3f v(-0.5, 0.2, 0);
1164 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 1); // Y, not X!
1165 UASSERT(fabs(dtime - 2.500) < 0.001);
1168 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1169 aabb3f m(bx+2, by-1.5, bz, bx+2.5, by-0.5, bz+1);
1170 v3f v(-0.5, 0.3, 0);
1172 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1173 UASSERT(fabs(dtime - 2.000) < 0.001);
1176 // TODO: Y-, Y+, Z-, Z+
1180 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1181 aabb3f m(bx+2.3, by+2.29, bz+2.29, bx+4.2, by+4.2, bz+4.2);
1182 v3f v(-1./3, -1./3, -1./3);
1184 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1185 UASSERT(fabs(dtime - 0.9) < 0.001);
1188 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1189 aabb3f m(bx+2.29, by+2.3, bz+2.29, bx+4.2, by+4.2, bz+4.2);
1190 v3f v(-1./3, -1./3, -1./3);
1192 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 1);
1193 UASSERT(fabs(dtime - 0.9) < 0.001);
1196 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1197 aabb3f m(bx+2.29, by+2.29, bz+2.3, bx+4.2, by+4.2, bz+4.2);
1198 v3f v(-1./3, -1./3, -1./3);
1200 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 2);
1201 UASSERT(fabs(dtime - 0.9) < 0.001);
1204 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1205 aabb3f m(bx-4.2, by-4.2, bz-4.2, bx-2.3, by-2.29, bz-2.29);
1206 v3f v(1./7, 1./7, 1./7);
1208 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1209 UASSERT(fabs(dtime - 16.1) < 0.001);
1212 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1213 aabb3f m(bx-4.2, by-4.2, bz-4.2, bx-2.29, by-2.3, bz-2.29);
1214 v3f v(1./7, 1./7, 1./7);
1216 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 1);
1217 UASSERT(fabs(dtime - 16.1) < 0.001);
1220 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1221 aabb3f m(bx-4.2, by-4.2, bz-4.2, bx-2.29, by-2.29, bz-2.3);
1222 v3f v(1./7, 1./7, 1./7);
1224 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 2);
1225 UASSERT(fabs(dtime - 16.1) < 0.001);
1231 struct TestSocket: public TestBase
1235 const int port = 30003;
1239 const char sendbuffer[] = "hello world!";
1240 socket.Send(Address(127,0,0,1,port), sendbuffer, sizeof(sendbuffer));
1244 char rcvbuffer[256];
1245 memset(rcvbuffer, 0, sizeof(rcvbuffer));
1249 int bytes_read = socket.Receive(sender, rcvbuffer, sizeof(rcvbuffer));
1253 //FIXME: This fails on some systems
1254 UASSERT(strncmp(sendbuffer, rcvbuffer, sizeof(sendbuffer))==0);
1255 UASSERT(sender.getAddress() == Address(127,0,0,1, 0).getAddress());
1259 struct TestConnection: public TestBase
1264 Test helper functions
1267 // Some constants for testing
1268 u32 proto_id = 0x12345678;
1271 SharedBuffer<u8> data1(1);
1273 Address a(127,0,0,1, 10);
1276 con::BufferedPacket p1 = con::makePacket(a, data1,
1277 proto_id, peer_id, channel);
1279 We should now have a packet with this data:
1282 [4] u16 sender_peer_id
1287 UASSERT(readU32(&p1.data[0]) == proto_id);
1288 UASSERT(readU16(&p1.data[4]) == peer_id);
1289 UASSERT(readU8(&p1.data[6]) == channel);
1290 UASSERT(readU8(&p1.data[7]) == data1[0]);
1292 //infostream<<"initial data1[0]="<<((u32)data1[0]&0xff)<<std::endl;
1294 SharedBuffer<u8> p2 = con::makeReliablePacket(data1, seqnum);
1296 /*infostream<<"p2.getSize()="<<p2.getSize()<<", data1.getSize()="
1297 <<data1.getSize()<<std::endl;
1298 infostream<<"readU8(&p2[3])="<<readU8(&p2[3])
1299 <<" p2[3]="<<((u32)p2[3]&0xff)<<std::endl;
1300 infostream<<"data1[0]="<<((u32)data1[0]&0xff)<<std::endl;*/
1302 UASSERT(p2.getSize() == 3 + data1.getSize());
1303 UASSERT(readU8(&p2[0]) == TYPE_RELIABLE);
1304 UASSERT(readU16(&p2[1]) == seqnum);
1305 UASSERT(readU8(&p2[3]) == data1[0]);
1308 struct Handler : public con::PeerHandler
1310 Handler(const char *a_name)
1316 void peerAdded(con::Peer *peer)
1318 infostream<<"Handler("<<name<<")::peerAdded(): "
1319 "id="<<peer->id<<std::endl;
1323 void deletingPeer(con::Peer *peer, bool timeout)
1325 infostream<<"Handler("<<name<<")::deletingPeer(): "
1327 <<", timeout="<<timeout<<std::endl;
1339 DSTACK("TestConnection::Run");
1344 Test some real connections
1346 NOTE: This mostly tests the legacy interface.
1349 u32 proto_id = 0xad26846a;
1351 Handler hand_server("server");
1352 Handler hand_client("client");
1354 infostream<<"** Creating server Connection"<<std::endl;
1355 con::Connection server(proto_id, 512, 5.0, &hand_server);
1356 server.Serve(30001);
1358 infostream<<"** Creating client Connection"<<std::endl;
1359 con::Connection client(proto_id, 512, 5.0, &hand_client);
1361 UASSERT(hand_server.count == 0);
1362 UASSERT(hand_client.count == 0);
1366 Address server_address(127,0,0,1, 30001);
1367 infostream<<"** running client.Connect()"<<std::endl;
1368 client.Connect(server_address);
1372 // Client should not have added client yet
1373 UASSERT(hand_client.count == 0);
1378 SharedBuffer<u8> data;
1379 infostream<<"** running client.Receive()"<<std::endl;
1380 u32 size = client.Receive(peer_id, data);
1381 infostream<<"** Client received: peer_id="<<peer_id
1385 catch(con::NoIncomingDataException &e)
1389 // Client should have added server now
1390 UASSERT(hand_client.count == 1);
1391 UASSERT(hand_client.last_id == 1);
1392 // Server should not have added client yet
1393 UASSERT(hand_server.count == 0);
1400 SharedBuffer<u8> data;
1401 infostream<<"** running server.Receive()"<<std::endl;
1402 u32 size = server.Receive(peer_id, data);
1403 infostream<<"** Server received: peer_id="<<peer_id
1407 catch(con::NoIncomingDataException &e)
1409 // No actual data received, but the client has
1410 // probably been connected
1413 // Client should be the same
1414 UASSERT(hand_client.count == 1);
1415 UASSERT(hand_client.last_id == 1);
1416 // Server should have the client
1417 UASSERT(hand_server.count == 1);
1418 UASSERT(hand_server.last_id == 2);
1422 while(client.Connected() == false)
1427 SharedBuffer<u8> data;
1428 infostream<<"** running client.Receive()"<<std::endl;
1429 u32 size = client.Receive(peer_id, data);
1430 infostream<<"** Client received: peer_id="<<peer_id
1434 catch(con::NoIncomingDataException &e)
1445 SharedBuffer<u8> data;
1446 infostream<<"** running server.Receive()"<<std::endl;
1447 u32 size = server.Receive(peer_id, data);
1448 infostream<<"** Server received: peer_id="<<peer_id
1452 catch(con::NoIncomingDataException &e)
1457 Simple send-receive test
1460 /*u8 data[] = "Hello World!";
1461 u32 datasize = sizeof(data);*/
1462 SharedBuffer<u8> data = SharedBufferFromString("Hello World!");
1464 infostream<<"** running client.Send()"<<std::endl;
1465 client.Send(PEER_ID_SERVER, 0, data, true);
1470 SharedBuffer<u8> recvdata;
1471 infostream<<"** running server.Receive()"<<std::endl;
1472 u32 size = server.Receive(peer_id, recvdata);
1473 infostream<<"** Server received: peer_id="<<peer_id
1477 UASSERT(memcmp(*data, *recvdata, data.getSize()) == 0);
1480 u16 peer_id_client = 2;
1483 Send consequent packets in different order
1484 Not compatible with new Connection, thus commented out.
1487 //u8 data1[] = "hello1";
1488 //u8 data2[] = "hello2";
1489 SharedBuffer<u8> data1 = SharedBufferFromString("hello1");
1490 SharedBuffer<u8> data2 = SharedBufferFromString("Hello2");
1492 Address client_address =
1493 server.GetPeerAddress(peer_id_client);
1495 infostream<<"*** Sending packets in wrong order (2,1,2)"
1499 con::Channel *ch = &server.getPeer(peer_id_client)->channels[chn];
1500 u16 sn = ch->next_outgoing_seqnum;
1501 ch->next_outgoing_seqnum = sn+1;
1502 server.Send(peer_id_client, chn, data2, true);
1503 ch->next_outgoing_seqnum = sn;
1504 server.Send(peer_id_client, chn, data1, true);
1505 ch->next_outgoing_seqnum = sn+1;
1506 server.Send(peer_id_client, chn, data2, true);
1510 infostream<<"*** Receiving the packets"<<std::endl;
1513 SharedBuffer<u8> recvdata;
1516 infostream<<"** running client.Receive()"<<std::endl;
1518 size = client.Receive(peer_id, recvdata);
1519 infostream<<"** Client received: peer_id="<<peer_id
1521 <<", data="<<*recvdata
1523 UASSERT(size == data1.getSize());
1524 UASSERT(memcmp(*data1, *recvdata, data1.getSize()) == 0);
1525 UASSERT(peer_id == PEER_ID_SERVER);
1527 infostream<<"** running client.Receive()"<<std::endl;
1529 size = client.Receive(peer_id, recvdata);
1530 infostream<<"** Client received: peer_id="<<peer_id
1532 <<", data="<<*recvdata
1534 UASSERT(size == data2.getSize());
1535 UASSERT(memcmp(*data2, *recvdata, data2.getSize()) == 0);
1536 UASSERT(peer_id == PEER_ID_SERVER);
1538 bool got_exception = false;
1541 infostream<<"** running client.Receive()"<<std::endl;
1543 size = client.Receive(peer_id, recvdata);
1544 infostream<<"** Client received: peer_id="<<peer_id
1546 <<", data="<<*recvdata
1549 catch(con::NoIncomingDataException &e)
1551 infostream<<"** No incoming data for client"<<std::endl;
1552 got_exception = true;
1554 UASSERT(got_exception);
1559 Send large amounts of packets (infinite test)
1560 Commented out because of infinity.
1563 infostream<<"Sending large amounts of packets (infinite test)"<<std::endl;
1566 int datasize = myrand_range(0,5)==0?myrand_range(100,10000):myrand_range(0,100);
1567 infostream<<"datasize="<<datasize<<std::endl;
1568 SharedBuffer<u8> data1(datasize);
1569 for(u16 i=0; i<datasize; i++)
1572 int sendtimes = myrand_range(1,10);
1573 for(int i=0; i<sendtimes; i++){
1574 server.Send(peer_id_client, 0, data1, true);
1577 infostream<<"sendcount="<<sendcount<<std::endl;
1579 //int receivetimes = myrand_range(1,20);
1580 int receivetimes = 20;
1581 for(int i=0; i<receivetimes; i++){
1582 SharedBuffer<u8> recvdata;
1585 bool received = false;
1587 size = client.Receive(peer_id, recvdata);
1589 }catch(con::NoIncomingDataException &e){
1599 const int datasize = 30000;
1600 SharedBuffer<u8> data1(datasize);
1601 for(u16 i=0; i<datasize; i++){
1605 infostream<<"Sending data (size="<<datasize<<"):";
1606 for(int i=0; i<datasize && i<20; i++){
1607 if(i%2==0) infostream<<" ";
1609 snprintf(buf, 10, "%.2X", ((int)((const char*)*data1)[i])&0xff);
1614 infostream<<std::endl;
1616 server.Send(peer_id_client, 0, data1, true);
1620 SharedBuffer<u8> recvdata;
1621 infostream<<"** running client.Receive()"<<std::endl;
1624 bool received = false;
1625 u32 timems0 = porting::getTimeMs();
1627 if(porting::getTimeMs() - timems0 > 5000 || received)
1630 size = client.Receive(peer_id, recvdata);
1632 }catch(con::NoIncomingDataException &e){
1637 infostream<<"** Client received: peer_id="<<peer_id
1641 infostream<<"Received data (size="<<size<<"): ";
1642 for(int i=0; i<size && i<20; i++){
1643 if(i%2==0) infostream<<" ";
1645 snprintf(buf, 10, "%.2X", ((int)(recvdata[i]))&0xff);
1650 infostream<<std::endl;
1652 UASSERT(memcmp(*data1, *recvdata, data1.getSize()) == 0);
1653 UASSERT(peer_id == PEER_ID_SERVER);
1656 // Check peer handlers
1657 UASSERT(hand_client.count == 1);
1658 UASSERT(hand_client.last_id == 1);
1659 UASSERT(hand_server.count == 1);
1660 UASSERT(hand_server.last_id == 2);
1669 infostream<<"Running " #X <<std::endl;\
1672 tests_failed += x.test_failed ? 1 : 0;\
1675 #define TESTPARAMS(X, ...)\
1678 infostream<<"Running " #X <<std::endl;\
1679 x.Run(__VA_ARGS__);\
1681 tests_failed += x.test_failed ? 1 : 0;\
1686 DSTACK(__FUNCTION_NAME);
1689 int tests_failed = 0;
1691 // Create item and node definitions
1692 IWritableItemDefManager *idef = createItemDefManager();
1693 IWritableNodeDefManager *ndef = createNodeDefManager();
1694 define_some_nodes(idef, ndef);
1696 infostream<<"run_tests() started"<<std::endl;
1697 TEST(TestUtilities);
1700 TEST(TestSerialization);
1701 TESTPARAMS(TestMapNode, ndef);
1702 TESTPARAMS(TestVoxelManipulator, ndef);
1703 TESTPARAMS(TestVoxelAlgorithms, ndef);
1704 TESTPARAMS(TestInventory, idef);
1705 //TEST(TestMapBlock);
1706 //TEST(TestMapSector);
1707 TEST(TestCollision);
1708 if(INTERNET_SIMULATOR == false){
1710 dout_con<<"=== BEGIN RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
1711 TEST(TestConnection);
1712 dout_con<<"=== END RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
1714 if(tests_failed == 0){
1715 infostream<<"run_tests(): "<<tests_failed<<" / "<<tests_run<<" tests failed."<<std::endl;
1716 infostream<<"run_tests() passed."<<std::endl;
1719 errorstream<<"run_tests(): "<<tests_failed<<" / "<<tests_run<<" tests failed."<<std::endl;
1720 errorstream<<"run_tests() aborting."<<std::endl;