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"
39 #include "voxelalgorithms.h"
40 #include "inventory.h"
41 #include "util/numeric.h"
42 #include "util/serialize.h"
43 #include "noise.h" // PseudoRandom used for random data for compression
44 #include "clientserver.h" // LATEST_PROTOCOL_VERSION
48 Asserts that the exception occurs
50 #define EXCEPTION_CHECK(EType, code)\
52 bool exception_thrown = false;\
54 catch(EType &e) { exception_thrown = true; }\
55 UASSERT(exception_thrown);\
58 #define UTEST(x, fmt, ...)\
61 LOGLINEF(LMT_ERROR, "Test (%s) failed: " fmt, #x, ##__VA_ARGS__);\
66 #define UASSERT(x) UTEST(x, "UASSERT")
69 A few item and node definitions for those tests that need them
72 #define CONTENT_STONE 0
73 #define CONTENT_GRASS 0x800
74 #define CONTENT_TORCH 100
76 void define_some_nodes(IWritableItemDefManager *idef, IWritableNodeDefManager *ndef)
79 ItemDefinition itemdef;
86 itemdef = ItemDefinition();
87 itemdef.type = ITEM_NODE;
88 itemdef.name = "default:stone";
89 itemdef.description = "Stone";
90 itemdef.groups["cracky"] = 3;
91 itemdef.inventory_image = "[inventorycube"
95 f = ContentFeatures();
96 f.name = itemdef.name;
97 for(int i = 0; i < 6; i++)
98 f.tiledef[i].name = "default_stone.png";
99 f.is_ground_content = true;
100 idef->registerItem(itemdef);
107 itemdef = ItemDefinition();
108 itemdef.type = ITEM_NODE;
109 itemdef.name = "default:dirt_with_grass";
110 itemdef.description = "Dirt with grass";
111 itemdef.groups["crumbly"] = 3;
112 itemdef.inventory_image = "[inventorycube"
114 "{default_dirt.png&default_grass_side.png"
115 "{default_dirt.png&default_grass_side.png";
116 f = ContentFeatures();
117 f.name = itemdef.name;
118 f.tiledef[0].name = "default_grass.png";
119 f.tiledef[1].name = "default_dirt.png";
120 for(int i = 2; i < 6; i++)
121 f.tiledef[i].name = "default_dirt.png^default_grass_side.png";
122 f.is_ground_content = true;
123 idef->registerItem(itemdef);
127 Torch (minimal definition for lighting tests)
130 itemdef = ItemDefinition();
131 itemdef.type = ITEM_NODE;
132 itemdef.name = "default:torch";
133 f = ContentFeatures();
134 f.name = itemdef.name;
135 f.param_type = CPT_LIGHT;
136 f.light_propagates = true;
137 f.sunlight_propagates = true;
138 f.light_source = LIGHT_MAX-1;
139 idef->registerItem(itemdef);
151 struct TestUtilities: public TestBase
155 /*infostream<<"wrapDegrees(100.0) = "<<wrapDegrees(100.0)<<std::endl;
156 infostream<<"wrapDegrees(720.5) = "<<wrapDegrees(720.5)<<std::endl;
157 infostream<<"wrapDegrees(-0.5) = "<<wrapDegrees(-0.5)<<std::endl;*/
158 UASSERT(fabs(wrapDegrees(100.0) - 100.0) < 0.001);
159 UASSERT(fabs(wrapDegrees(720.5) - 0.5) < 0.001);
160 UASSERT(fabs(wrapDegrees(-0.5) - (-0.5)) < 0.001);
161 UASSERT(fabs(wrapDegrees(-365.5) - (-5.5)) < 0.001);
162 UASSERT(lowercase("Foo bAR") == "foo bar");
163 UASSERT(is_yes("YeS") == true);
164 UASSERT(is_yes("") == false);
165 UASSERT(is_yes("FAlse") == false);
166 const char *ends[] = {"abc", "c", "bc", NULL};
167 UASSERT(removeStringEnd("abc", ends) == "");
168 UASSERT(removeStringEnd("bc", ends) == "b");
169 UASSERT(removeStringEnd("12c", ends) == "12");
170 UASSERT(removeStringEnd("foo", ends) == "");
174 struct TestSettings: public TestBase
179 // Test reading of settings
180 s.parseConfigLine("leet = 1337");
181 s.parseConfigLine("leetleet = 13371337");
182 s.parseConfigLine("leetleet_neg = -13371337");
183 s.parseConfigLine("floaty_thing = 1.1");
184 s.parseConfigLine("stringy_thing = asd /( ¤%&(/\" BLÖÄRP");
185 s.parseConfigLine("coord = (1, 2, 4.5)");
186 UASSERT(s.getS32("leet") == 1337);
187 UASSERT(s.getS16("leetleet") == 32767);
188 UASSERT(s.getS16("leetleet_neg") == -32768);
189 // Not sure if 1.1 is an exact value as a float, but doesn't matter
190 UASSERT(fabs(s.getFloat("floaty_thing") - 1.1) < 0.001);
191 UASSERT(s.get("stringy_thing") == "asd /( ¤%&(/\" BLÖÄRP");
192 UASSERT(fabs(s.getV3F("coord").X - 1.0) < 0.001);
193 UASSERT(fabs(s.getV3F("coord").Y - 2.0) < 0.001);
194 UASSERT(fabs(s.getV3F("coord").Z - 4.5) < 0.001);
195 // Test the setting of settings too
196 s.setFloat("floaty_thing_2", 1.2);
197 s.setV3F("coord2", v3f(1, 2, 3.3));
198 UASSERT(s.get("floaty_thing_2").substr(0,3) == "1.2");
199 UASSERT(fabs(s.getFloat("floaty_thing_2") - 1.2) < 0.001);
200 UASSERT(fabs(s.getV3F("coord2").X - 1.0) < 0.001);
201 UASSERT(fabs(s.getV3F("coord2").Y - 2.0) < 0.001);
202 UASSERT(fabs(s.getV3F("coord2").Z - 3.3) < 0.001);
206 struct TestSerialization: public TestBase
208 // To be used like this:
209 // mkstr("Some\0string\0with\0embedded\0nuls")
210 // since std::string("...") doesn't work as expected in that case.
211 template<size_t N> std::string mkstr(const char (&s)[N])
213 return std::string(s, N - 1);
218 // Tests some serialization primitives
220 UASSERT(serializeString("") == mkstr("\0\0"));
221 UASSERT(serializeWideString(L"") == mkstr("\0\0"));
222 UASSERT(serializeLongString("") == mkstr("\0\0\0\0"));
223 UASSERT(serializeJsonString("") == "\"\"");
225 std::string teststring = "Hello world!";
226 UASSERT(serializeString(teststring) ==
227 mkstr("\0\14Hello world!"));
228 UASSERT(serializeWideString(narrow_to_wide(teststring)) ==
229 mkstr("\0\14\0H\0e\0l\0l\0o\0 \0w\0o\0r\0l\0d\0!"));
230 UASSERT(serializeLongString(teststring) ==
231 mkstr("\0\0\0\14Hello world!"));
232 UASSERT(serializeJsonString(teststring) ==
235 std::string teststring2;
236 std::wstring teststring2_w;
237 std::string teststring2_w_encoded;
239 std::ostringstream tmp_os;
240 std::wostringstream tmp_os_w;
241 std::ostringstream tmp_os_w_encoded;
242 for(int i = 0; i < 256; i++)
245 tmp_os_w<<(wchar_t)i;
246 tmp_os_w_encoded<<(char)0<<(char)i;
248 teststring2 = tmp_os.str();
249 teststring2_w = tmp_os_w.str();
250 teststring2_w_encoded = tmp_os_w_encoded.str();
252 UASSERT(serializeString(teststring2) ==
253 mkstr("\1\0") + teststring2);
254 UASSERT(serializeWideString(teststring2_w) ==
255 mkstr("\1\0") + teststring2_w_encoded);
256 UASSERT(serializeLongString(teststring2) ==
257 mkstr("\0\0\1\0") + teststring2);
258 // MSVC fails when directly using "\\\\"
259 std::string backslash = "\\";
260 UASSERT(serializeJsonString(teststring2) ==
262 "\\u0000\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007" +
263 "\\b\\t\\n\\u000b\\f\\r\\u000e\\u000f" +
264 "\\u0010\\u0011\\u0012\\u0013\\u0014\\u0015\\u0016\\u0017" +
265 "\\u0018\\u0019\\u001a\\u001b\\u001c\\u001d\\u001e\\u001f" +
266 " !\\\"" + teststring2.substr(0x23, 0x2f-0x23) +
267 "\\/" + teststring2.substr(0x30, 0x5c-0x30) +
268 backslash + backslash + teststring2.substr(0x5d, 0x7f-0x5d) + "\\u007f" +
269 "\\u0080\\u0081\\u0082\\u0083\\u0084\\u0085\\u0086\\u0087" +
270 "\\u0088\\u0089\\u008a\\u008b\\u008c\\u008d\\u008e\\u008f" +
271 "\\u0090\\u0091\\u0092\\u0093\\u0094\\u0095\\u0096\\u0097" +
272 "\\u0098\\u0099\\u009a\\u009b\\u009c\\u009d\\u009e\\u009f" +
273 "\\u00a0\\u00a1\\u00a2\\u00a3\\u00a4\\u00a5\\u00a6\\u00a7" +
274 "\\u00a8\\u00a9\\u00aa\\u00ab\\u00ac\\u00ad\\u00ae\\u00af" +
275 "\\u00b0\\u00b1\\u00b2\\u00b3\\u00b4\\u00b5\\u00b6\\u00b7" +
276 "\\u00b8\\u00b9\\u00ba\\u00bb\\u00bc\\u00bd\\u00be\\u00bf" +
277 "\\u00c0\\u00c1\\u00c2\\u00c3\\u00c4\\u00c5\\u00c6\\u00c7" +
278 "\\u00c8\\u00c9\\u00ca\\u00cb\\u00cc\\u00cd\\u00ce\\u00cf" +
279 "\\u00d0\\u00d1\\u00d2\\u00d3\\u00d4\\u00d5\\u00d6\\u00d7" +
280 "\\u00d8\\u00d9\\u00da\\u00db\\u00dc\\u00dd\\u00de\\u00df" +
281 "\\u00e0\\u00e1\\u00e2\\u00e3\\u00e4\\u00e5\\u00e6\\u00e7" +
282 "\\u00e8\\u00e9\\u00ea\\u00eb\\u00ec\\u00ed\\u00ee\\u00ef" +
283 "\\u00f0\\u00f1\\u00f2\\u00f3\\u00f4\\u00f5\\u00f6\\u00f7" +
284 "\\u00f8\\u00f9\\u00fa\\u00fb\\u00fc\\u00fd\\u00fe\\u00ff" +
288 std::istringstream is(serializeString(teststring2), std::ios::binary);
289 UASSERT(deSerializeString(is) == teststring2);
295 std::istringstream is(serializeWideString(teststring2_w), std::ios::binary);
296 UASSERT(deSerializeWideString(is) == teststring2_w);
302 std::istringstream is(serializeLongString(teststring2), std::ios::binary);
303 UASSERT(deSerializeLongString(is) == teststring2);
309 std::istringstream is(serializeJsonString(teststring2), std::ios::binary);
310 //dstream<<serializeJsonString(deSerializeJsonString(is));
311 UASSERT(deSerializeJsonString(is) == teststring2);
319 struct TestNodedefSerialization: public TestBase
324 f.name = "default:stone";
325 for(int i = 0; i < 6; i++)
326 f.tiledef[i].name = "default_stone.png";
327 f.is_ground_content = true;
328 std::ostringstream os(std::ios::binary);
329 f.serialize(os, LATEST_PROTOCOL_VERSION);
330 verbosestream<<"Test ContentFeatures size: "<<os.str().size()<<std::endl;
331 std::istringstream is(os.str(), std::ios::binary);
334 UASSERT(f.walkable == f2.walkable);
335 UASSERT(f.node_box.type == f2.node_box.type);
339 struct TestCompress: public TestBase
345 SharedBuffer<u8> fromdata(4);
351 std::ostringstream os(std::ios_base::binary);
352 compress(fromdata, os, 0);
354 std::string str_out = os.str();
356 infostream<<"str_out.size()="<<str_out.size()<<std::endl;
357 infostream<<"TestCompress: 1,5,5,1 -> ";
358 for(u32 i=0; i<str_out.size(); i++)
360 infostream<<(u32)str_out[i]<<",";
362 infostream<<std::endl;
364 UASSERT(str_out.size() == 10);
366 UASSERT(str_out[0] == 0);
367 UASSERT(str_out[1] == 0);
368 UASSERT(str_out[2] == 0);
369 UASSERT(str_out[3] == 4);
370 UASSERT(str_out[4] == 0);
371 UASSERT(str_out[5] == 1);
372 UASSERT(str_out[6] == 1);
373 UASSERT(str_out[7] == 5);
374 UASSERT(str_out[8] == 0);
375 UASSERT(str_out[9] == 1);
377 std::istringstream is(str_out, std::ios_base::binary);
378 std::ostringstream os2(std::ios_base::binary);
380 decompress(is, os2, 0);
381 std::string str_out2 = os2.str();
383 infostream<<"decompress: ";
384 for(u32 i=0; i<str_out2.size(); i++)
386 infostream<<(u32)str_out2[i]<<",";
388 infostream<<std::endl;
390 UASSERT(str_out2.size() == fromdata.getSize());
392 for(u32 i=0; i<str_out2.size(); i++)
394 UASSERT(str_out2[i] == fromdata[i]);
401 SharedBuffer<u8> fromdata(4);
407 std::ostringstream os(std::ios_base::binary);
408 compress(fromdata, os, SER_FMT_VER_HIGHEST);
410 std::string str_out = os.str();
412 infostream<<"str_out.size()="<<str_out.size()<<std::endl;
413 infostream<<"TestCompress: 1,5,5,1 -> ";
414 for(u32 i=0; i<str_out.size(); i++)
416 infostream<<(u32)str_out[i]<<",";
418 infostream<<std::endl;
420 std::istringstream is(str_out, std::ios_base::binary);
421 std::ostringstream os2(std::ios_base::binary);
423 decompress(is, os2, SER_FMT_VER_HIGHEST);
424 std::string str_out2 = os2.str();
426 infostream<<"decompress: ";
427 for(u32 i=0; i<str_out2.size(); i++)
429 infostream<<(u32)str_out2[i]<<",";
431 infostream<<std::endl;
433 UASSERT(str_out2.size() == fromdata.getSize());
435 for(u32 i=0; i<str_out2.size(); i++)
437 UASSERT(str_out2[i] == fromdata[i]);
442 // Test zlib wrapper with large amounts of data (larger than its
445 infostream<<"Test: Testing zlib wrappers with a large amount "
446 <<"of pseudorandom data"<<std::endl;
448 infostream<<"Test: Input size of large compressZlib is "
451 data_in.resize(size);
452 PseudoRandom pseudorandom(9420);
453 for(u32 i=0; i<size; i++)
454 data_in[i] = pseudorandom.range(0,255);
455 std::ostringstream os_compressed(std::ios::binary);
456 compressZlib(data_in, os_compressed);
457 infostream<<"Test: Output size of large compressZlib is "
458 <<os_compressed.str().size()<<std::endl;
459 std::istringstream is_compressed(os_compressed.str(), std::ios::binary);
460 std::ostringstream os_decompressed(std::ios::binary);
461 decompressZlib(is_compressed, os_decompressed);
462 infostream<<"Test: Output size of large decompressZlib is "
463 <<os_decompressed.str().size()<<std::endl;
464 std::string str_decompressed = os_decompressed.str();
465 UTEST(str_decompressed.size() == data_in.size(), "Output size not"
466 " equal (output: %u, input: %u)",
467 str_decompressed.size(), data_in.size());
468 for(u32 i=0; i<size && i<str_decompressed.size(); i++){
469 UTEST(str_decompressed[i] == data_in[i],
470 "index out[%i]=%i differs from in[%i]=%i",
471 i, str_decompressed[i], i, data_in[i]);
477 struct TestMapNode: public TestBase
479 void Run(INodeDefManager *nodedef)
484 UASSERT(n.getContent() == CONTENT_AIR);
485 UASSERT(n.getLight(LIGHTBANK_DAY, nodedef) == 0);
486 UASSERT(n.getLight(LIGHTBANK_NIGHT, nodedef) == 0);
489 n.setContent(CONTENT_AIR);
490 UASSERT(nodedef->get(n).light_propagates == true);
491 n.setContent(LEGN(nodedef, "CONTENT_STONE"));
492 UASSERT(nodedef->get(n).light_propagates == false);
496 struct TestVoxelManipulator: public TestBase
498 void Run(INodeDefManager *nodedef)
504 VoxelArea a(v3s16(-1,-1,-1), v3s16(1,1,1));
505 UASSERT(a.index(0,0,0) == 1*3*3 + 1*3 + 1);
506 UASSERT(a.index(-1,-1,-1) == 0);
508 VoxelArea c(v3s16(-2,-2,-2), v3s16(2,2,2));
509 // An area that is 1 bigger in x+ and z-
510 VoxelArea d(v3s16(-2,-2,-3), v3s16(3,2,2));
512 std::list<VoxelArea> aa;
516 std::vector<VoxelArea> results;
517 results.push_back(VoxelArea(v3s16(-2,-2,-3),v3s16(3,2,-3)));
518 results.push_back(VoxelArea(v3s16(3,-2,-2),v3s16(3,2,2)));
520 UASSERT(aa.size() == results.size());
522 infostream<<"Result of diff:"<<std::endl;
523 for(std::list<VoxelArea>::const_iterator
524 i = aa.begin(); i != aa.end(); ++i)
526 i->print(infostream);
527 infostream<<std::endl;
529 std::vector<VoxelArea>::iterator j = std::find(results.begin(), results.end(), *i);
530 UASSERT(j != results.end());
541 v.print(infostream, nodedef);
543 infostream<<"*** Setting (-1,0,-1)=2 ***"<<std::endl;
545 v.setNodeNoRef(v3s16(-1,0,-1), MapNode(CONTENT_GRASS));
547 v.print(infostream, nodedef);
549 UASSERT(v.getNode(v3s16(-1,0,-1)).getContent() == CONTENT_GRASS);
551 infostream<<"*** Reading from inexistent (0,0,-1) ***"<<std::endl;
553 EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,0,-1)));
555 v.print(infostream, nodedef);
557 infostream<<"*** Adding area ***"<<std::endl;
561 v.print(infostream, nodedef);
563 UASSERT(v.getNode(v3s16(-1,0,-1)).getContent() == CONTENT_GRASS);
564 EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,1,1)));
568 struct TestVoxelAlgorithms: public TestBase
570 void Run(INodeDefManager *ndef)
573 voxalgo::propagateSunlight
577 for(u16 z=0; z<3; z++)
578 for(u16 y=0; y<3; y++)
579 for(u16 x=0; x<3; x++)
582 v.setNodeNoRef(p, MapNode(CONTENT_AIR));
584 VoxelArea a(v3s16(0,0,0), v3s16(2,2,2));
586 std::set<v3s16> light_sources;
587 voxalgo::setLight(v, a, 0, ndef);
588 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
589 v, a, true, light_sources, ndef);
590 //v.print(dstream, ndef, VOXELPRINT_LIGHT_DAY);
591 UASSERT(res.bottom_sunlight_valid == true);
592 UASSERT(v.getNode(v3s16(1,1,1)).getLight(LIGHTBANK_DAY, ndef)
595 v.setNodeNoRef(v3s16(0,0,0), MapNode(CONTENT_STONE));
597 std::set<v3s16> light_sources;
598 voxalgo::setLight(v, a, 0, ndef);
599 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
600 v, a, true, light_sources, ndef);
601 UASSERT(res.bottom_sunlight_valid == true);
602 UASSERT(v.getNode(v3s16(1,1,1)).getLight(LIGHTBANK_DAY, ndef)
606 std::set<v3s16> light_sources;
607 voxalgo::setLight(v, a, 0, ndef);
608 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
609 v, a, false, light_sources, ndef);
610 UASSERT(res.bottom_sunlight_valid == true);
611 UASSERT(v.getNode(v3s16(2,0,2)).getLight(LIGHTBANK_DAY, ndef)
614 v.setNodeNoRef(v3s16(1,3,2), MapNode(CONTENT_STONE));
616 std::set<v3s16> light_sources;
617 voxalgo::setLight(v, a, 0, ndef);
618 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
619 v, a, true, light_sources, ndef);
620 UASSERT(res.bottom_sunlight_valid == true);
621 UASSERT(v.getNode(v3s16(1,1,2)).getLight(LIGHTBANK_DAY, ndef)
625 std::set<v3s16> light_sources;
626 voxalgo::setLight(v, a, 0, ndef);
627 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
628 v, a, false, light_sources, ndef);
629 UASSERT(res.bottom_sunlight_valid == true);
630 UASSERT(v.getNode(v3s16(1,0,2)).getLight(LIGHTBANK_DAY, ndef)
634 MapNode n(CONTENT_AIR);
635 n.setLight(LIGHTBANK_DAY, 10, ndef);
636 v.setNodeNoRef(v3s16(1,-1,2), n);
639 std::set<v3s16> light_sources;
640 voxalgo::setLight(v, a, 0, ndef);
641 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
642 v, a, true, light_sources, ndef);
643 UASSERT(res.bottom_sunlight_valid == true);
646 std::set<v3s16> light_sources;
647 voxalgo::setLight(v, a, 0, ndef);
648 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
649 v, a, false, light_sources, ndef);
650 UASSERT(res.bottom_sunlight_valid == true);
653 MapNode n(CONTENT_AIR);
654 n.setLight(LIGHTBANK_DAY, LIGHT_SUN, ndef);
655 v.setNodeNoRef(v3s16(1,-1,2), n);
658 std::set<v3s16> light_sources;
659 voxalgo::setLight(v, a, 0, ndef);
660 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
661 v, a, true, light_sources, ndef);
662 UASSERT(res.bottom_sunlight_valid == false);
665 std::set<v3s16> light_sources;
666 voxalgo::setLight(v, a, 0, ndef);
667 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
668 v, a, false, light_sources, ndef);
669 UASSERT(res.bottom_sunlight_valid == false);
671 v.setNodeNoRef(v3s16(1,3,2), MapNode(CONTENT_IGNORE));
673 std::set<v3s16> light_sources;
674 voxalgo::setLight(v, a, 0, ndef);
675 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
676 v, a, true, light_sources, ndef);
677 UASSERT(res.bottom_sunlight_valid == true);
681 voxalgo::clearLightAndCollectSources
685 for(u16 z=0; z<3; z++)
686 for(u16 y=0; y<3; y++)
687 for(u16 x=0; x<3; x++)
690 v.setNode(p, MapNode(CONTENT_AIR));
692 VoxelArea a(v3s16(0,0,0), v3s16(2,2,2));
693 v.setNodeNoRef(v3s16(0,0,0), MapNode(CONTENT_STONE));
694 v.setNodeNoRef(v3s16(1,1,1), MapNode(CONTENT_TORCH));
696 MapNode n(CONTENT_AIR);
697 n.setLight(LIGHTBANK_DAY, 1, ndef);
698 v.setNode(v3s16(1,1,2), n);
701 std::set<v3s16> light_sources;
702 std::map<v3s16, u8> unlight_from;
703 voxalgo::clearLightAndCollectSources(v, a, LIGHTBANK_DAY,
704 ndef, light_sources, unlight_from);
705 //v.print(dstream, ndef, VOXELPRINT_LIGHT_DAY);
706 UASSERT(v.getNode(v3s16(0,1,1)).getLight(LIGHTBANK_DAY, ndef)
708 UASSERT(light_sources.find(v3s16(1,1,1)) != light_sources.end());
709 UASSERT(light_sources.size() == 1);
710 UASSERT(unlight_from.find(v3s16(1,1,2)) != unlight_from.end());
711 UASSERT(unlight_from.size() == 1);
717 struct TestInventory: public TestBase
719 void Run(IItemDefManager *idef)
721 std::string serialized_inventory =
733 "Item default:cobble 61\n"
740 "Item default:dirt 71\n"
748 "Item default:dirt 99\n"
749 "Item default:cobble 38\n"
759 std::string serialized_inventory_2 =
771 "Item default:cobble 61\n"
778 "Item default:dirt 71\n"
786 "Item default:dirt 99\n"
787 "Item default:cobble 38\n"
798 std::istringstream is(serialized_inventory, std::ios::binary);
800 UASSERT(inv.getList("0"));
801 UASSERT(!inv.getList("main"));
802 inv.getList("0")->setName("main");
803 UASSERT(!inv.getList("0"));
804 UASSERT(inv.getList("main"));
805 UASSERT(inv.getList("main")->getWidth() == 3);
806 inv.getList("main")->setWidth(5);
807 std::ostringstream inv_os(std::ios::binary);
808 inv.serialize(inv_os);
809 UASSERT(inv_os.str() == serialized_inventory_2);
814 NOTE: These tests became non-working then NodeContainer was removed.
815 These should be redone, utilizing some kind of a virtual
816 interface for Map (IMap would be fine).
819 struct TestMapBlock: public TestBase
821 class TC : public NodeContainer
827 core::list<v3s16> validity_exceptions;
831 position_valid = true;
834 virtual bool isValidPosition(v3s16 p)
836 //return position_valid ^ (p==position_valid_exception);
837 bool exception = false;
838 for(core::list<v3s16>::Iterator i=validity_exceptions.begin();
839 i != validity_exceptions.end(); i++)
847 return exception ? !position_valid : position_valid;
850 virtual MapNode getNode(v3s16 p)
852 if(isValidPosition(p) == false)
853 throw InvalidPositionException();
857 virtual void setNode(v3s16 p, MapNode & n)
859 if(isValidPosition(p) == false)
860 throw InvalidPositionException();
863 virtual u16 nodeContainerId() const
873 MapBlock b(&parent, v3s16(1,1,1));
874 v3s16 relpos(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
876 UASSERT(b.getPosRelative() == relpos);
878 UASSERT(b.getBox().MinEdge.X == MAP_BLOCKSIZE);
879 UASSERT(b.getBox().MaxEdge.X == MAP_BLOCKSIZE*2-1);
880 UASSERT(b.getBox().MinEdge.Y == MAP_BLOCKSIZE);
881 UASSERT(b.getBox().MaxEdge.Y == MAP_BLOCKSIZE*2-1);
882 UASSERT(b.getBox().MinEdge.Z == MAP_BLOCKSIZE);
883 UASSERT(b.getBox().MaxEdge.Z == MAP_BLOCKSIZE*2-1);
885 UASSERT(b.isValidPosition(v3s16(0,0,0)) == true);
886 UASSERT(b.isValidPosition(v3s16(-1,0,0)) == false);
887 UASSERT(b.isValidPosition(v3s16(-1,-142,-2341)) == false);
888 UASSERT(b.isValidPosition(v3s16(-124,142,2341)) == false);
889 UASSERT(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
890 UASSERT(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE,MAP_BLOCKSIZE-1)) == false);
893 TODO: this method should probably be removed
894 if the block size isn't going to be set variable
896 /*UASSERT(b.getSizeNodes() == v3s16(MAP_BLOCKSIZE,
897 MAP_BLOCKSIZE, MAP_BLOCKSIZE));*/
899 // Changed flag should be initially set
900 UASSERT(b.getModified() == MOD_STATE_WRITE_NEEDED);
902 UASSERT(b.getModified() == MOD_STATE_CLEAN);
904 // All nodes should have been set to
905 // .d=CONTENT_IGNORE and .getLight() = 0
906 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
907 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
908 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
910 //UASSERT(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_AIR);
911 UASSERT(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_IGNORE);
912 UASSERT(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_DAY) == 0);
913 UASSERT(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_NIGHT) == 0);
917 MapNode n(CONTENT_AIR);
918 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
919 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
920 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
922 b.setNode(v3s16(x,y,z), n);
927 Parent fetch functions
929 parent.position_valid = false;
930 parent.node.setContent(5);
934 // Positions in the block should still be valid
935 UASSERT(b.isValidPositionParent(v3s16(0,0,0)) == true);
936 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
937 n = b.getNodeParent(v3s16(0,MAP_BLOCKSIZE-1,0));
938 UASSERT(n.getContent() == CONTENT_AIR);
940 // ...but outside the block they should be invalid
941 UASSERT(b.isValidPositionParent(v3s16(-121,2341,0)) == false);
942 UASSERT(b.isValidPositionParent(v3s16(-1,0,0)) == false);
943 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == false);
946 bool exception_thrown = false;
948 // This should throw an exception
949 MapNode n = b.getNodeParent(v3s16(0,0,-1));
951 catch(InvalidPositionException &e)
953 exception_thrown = true;
955 UASSERT(exception_thrown);
958 parent.position_valid = true;
959 // Now the positions outside should be valid
960 UASSERT(b.isValidPositionParent(v3s16(-121,2341,0)) == true);
961 UASSERT(b.isValidPositionParent(v3s16(-1,0,0)) == true);
962 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == true);
963 n = b.getNodeParent(v3s16(0,0,MAP_BLOCKSIZE));
964 UASSERT(n.getContent() == 5);
972 UASSERT(b.getNode(p).getContent() == 4);
973 //TODO: Update to new system
974 /*UASSERT(b.getNodeTile(p) == 4);
975 UASSERT(b.getNodeTile(v3s16(-1,-1,0)) == 5);*/
980 // Set lighting of all nodes to 0
981 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
982 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
983 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
984 MapNode n = b.getNode(v3s16(x,y,z));
985 n.setLight(LIGHTBANK_DAY, 0);
986 n.setLight(LIGHTBANK_NIGHT, 0);
987 b.setNode(v3s16(x,y,z), n);
993 Check how the block handles being a lonely sky block
995 parent.position_valid = true;
996 b.setIsUnderground(false);
997 parent.node.setContent(CONTENT_AIR);
998 parent.node.setLight(LIGHTBANK_DAY, LIGHT_SUN);
999 parent.node.setLight(LIGHTBANK_NIGHT, 0);
1000 core::map<v3s16, bool> light_sources;
1001 // The bottom block is invalid, because we have a shadowing node
1002 UASSERT(b.propagateSunlight(light_sources) == false);
1003 UASSERT(b.getNode(v3s16(1,4,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
1004 UASSERT(b.getNode(v3s16(1,3,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
1005 UASSERT(b.getNode(v3s16(1,2,0)).getLight(LIGHTBANK_DAY) == 0);
1006 UASSERT(b.getNode(v3s16(1,1,0)).getLight(LIGHTBANK_DAY) == 0);
1007 UASSERT(b.getNode(v3s16(1,0,0)).getLight(LIGHTBANK_DAY) == 0);
1008 UASSERT(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
1009 UASSERT(b.getFaceLight2(1000, p, v3s16(0,1,0)) == LIGHT_SUN);
1010 UASSERT(b.getFaceLight2(1000, p, v3s16(0,-1,0)) == 0);
1011 UASSERT(b.getFaceLight2(0, p, v3s16(0,-1,0)) == 0);
1012 // According to MapBlock::getFaceLight,
1013 // The face on the z+ side should have double-diminished light
1014 //UASSERT(b.getFaceLight(p, v3s16(0,0,1)) == diminish_light(diminish_light(LIGHT_MAX)));
1015 // The face on the z+ side should have diminished light
1016 UASSERT(b.getFaceLight2(1000, p, v3s16(0,0,1)) == diminish_light(LIGHT_MAX));
1019 Check how the block handles being in between blocks with some non-sunlight
1020 while being underground
1023 // Make neighbours to exist and set some non-sunlight to them
1024 parent.position_valid = true;
1025 b.setIsUnderground(true);
1026 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
1027 core::map<v3s16, bool> light_sources;
1028 // The block below should be valid because there shouldn't be
1029 // sunlight in there either
1030 UASSERT(b.propagateSunlight(light_sources, true) == true);
1031 // Should not touch nodes that are not affected (that is, all of them)
1032 //UASSERT(b.getNode(v3s16(1,2,3)).getLight() == LIGHT_SUN);
1033 // Should set light of non-sunlighted blocks to 0.
1034 UASSERT(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == 0);
1037 Set up a situation where:
1038 - There is only air in this block
1039 - There is a valid non-sunlighted block at the bottom, and
1040 - Invalid blocks elsewhere.
1041 - the block is not underground.
1043 This should result in bottom block invalidity
1046 b.setIsUnderground(false);
1048 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
1049 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
1050 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
1052 n.setContent(CONTENT_AIR);
1053 n.setLight(LIGHTBANK_DAY, 0);
1054 b.setNode(v3s16(x,y,z), n);
1058 // Make neighbours invalid
1059 parent.position_valid = false;
1060 // Add exceptions to the top of the bottom block
1061 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
1062 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
1064 parent.validity_exceptions.push_back(v3s16(MAP_BLOCKSIZE+x, MAP_BLOCKSIZE-1, MAP_BLOCKSIZE+z));
1066 // Lighting value for the valid nodes
1067 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
1068 core::map<v3s16, bool> light_sources;
1069 // Bottom block is not valid
1070 UASSERT(b.propagateSunlight(light_sources) == false);
1075 struct TestMapSector: public TestBase
1077 class TC : public NodeContainer
1082 bool position_valid;
1086 position_valid = true;
1089 virtual bool isValidPosition(v3s16 p)
1091 return position_valid;
1094 virtual MapNode getNode(v3s16 p)
1096 if(position_valid == false)
1097 throw InvalidPositionException();
1101 virtual void setNode(v3s16 p, MapNode & n)
1103 if(position_valid == false)
1104 throw InvalidPositionException();
1107 virtual u16 nodeContainerId() const
1116 parent.position_valid = false;
1118 // Create one with no heightmaps
1119 ServerMapSector sector(&parent, v2s16(1,1));
1121 UASSERT(sector.getBlockNoCreateNoEx(0) == 0);
1122 UASSERT(sector.getBlockNoCreateNoEx(1) == 0);
1124 MapBlock * bref = sector.createBlankBlock(-2);
1126 UASSERT(sector.getBlockNoCreateNoEx(0) == 0);
1127 UASSERT(sector.getBlockNoCreateNoEx(-2) == bref);
1129 //TODO: Check for AlreadyExistsException
1131 /*bool exception_thrown = false;
1135 catch(InvalidPositionException &e){
1136 exception_thrown = true;
1138 UASSERT(exception_thrown);*/
1144 struct TestCollision: public TestBase
1149 axisAlignedCollision
1152 for(s16 bx = -3; bx <= 3; bx++)
1153 for(s16 by = -3; by <= 3; by++)
1154 for(s16 bz = -3; bz <= 3; bz++)
1158 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1159 aabb3f m(bx-2, by, bz, bx-1, by+1, bz+1);
1162 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1163 UASSERT(fabs(dtime - 1.000) < 0.001);
1166 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1167 aabb3f m(bx-2, by, bz, bx-1, by+1, bz+1);
1170 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == -1);
1173 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1174 aabb3f m(bx-2, by+1.5, bz, bx-1, by+2.5, bz-1);
1177 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == -1);
1180 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1181 aabb3f m(bx-2, by-1.5, bz, bx-1.5, by+0.5, bz+1);
1184 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1185 UASSERT(fabs(dtime - 3.000) < 0.001);
1188 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1189 aabb3f m(bx-2, by-1.5, bz, bx-1.5, by+0.5, bz+1);
1192 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1193 UASSERT(fabs(dtime - 3.000) < 0.001);
1198 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1199 aabb3f m(bx+2, by, bz, bx+3, by+1, bz+1);
1202 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1203 UASSERT(fabs(dtime - 1.000) < 0.001);
1206 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1207 aabb3f m(bx+2, by, bz, bx+3, by+1, bz+1);
1210 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == -1);
1213 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1214 aabb3f m(bx+2, by, bz+1.5, bx+3, by+1, bz+3.5);
1217 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == -1);
1220 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1221 aabb3f m(bx+2, by-1.5, bz, bx+2.5, by-0.5, bz+1);
1222 v3f v(-0.5, 0.2, 0);
1224 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 1); // Y, not X!
1225 UASSERT(fabs(dtime - 2.500) < 0.001);
1228 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1229 aabb3f m(bx+2, by-1.5, bz, bx+2.5, by-0.5, bz+1);
1230 v3f v(-0.5, 0.3, 0);
1232 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1233 UASSERT(fabs(dtime - 2.000) < 0.001);
1236 // TODO: Y-, Y+, Z-, Z+
1240 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1241 aabb3f m(bx+2.3, by+2.29, bz+2.29, bx+4.2, by+4.2, bz+4.2);
1242 v3f v(-1./3, -1./3, -1./3);
1244 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1245 UASSERT(fabs(dtime - 0.9) < 0.001);
1248 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1249 aabb3f m(bx+2.29, by+2.3, bz+2.29, bx+4.2, by+4.2, bz+4.2);
1250 v3f v(-1./3, -1./3, -1./3);
1252 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 1);
1253 UASSERT(fabs(dtime - 0.9) < 0.001);
1256 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1257 aabb3f m(bx+2.29, by+2.29, bz+2.3, bx+4.2, by+4.2, bz+4.2);
1258 v3f v(-1./3, -1./3, -1./3);
1260 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 2);
1261 UASSERT(fabs(dtime - 0.9) < 0.001);
1264 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1265 aabb3f m(bx-4.2, by-4.2, bz-4.2, bx-2.3, by-2.29, bz-2.29);
1266 v3f v(1./7, 1./7, 1./7);
1268 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1269 UASSERT(fabs(dtime - 16.1) < 0.001);
1272 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1273 aabb3f m(bx-4.2, by-4.2, bz-4.2, bx-2.29, by-2.3, bz-2.29);
1274 v3f v(1./7, 1./7, 1./7);
1276 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 1);
1277 UASSERT(fabs(dtime - 16.1) < 0.001);
1280 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1281 aabb3f m(bx-4.2, by-4.2, bz-4.2, bx-2.29, by-2.29, bz-2.3);
1282 v3f v(1./7, 1./7, 1./7);
1284 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 2);
1285 UASSERT(fabs(dtime - 16.1) < 0.001);
1291 struct TestSocket: public TestBase
1295 const int port = 30003;
1299 const char sendbuffer[] = "hello world!";
1300 socket.Send(Address(127,0,0,1,port), sendbuffer, sizeof(sendbuffer));
1304 char rcvbuffer[256];
1305 memset(rcvbuffer, 0, sizeof(rcvbuffer));
1309 int bytes_read = socket.Receive(sender, rcvbuffer, sizeof(rcvbuffer));
1313 //FIXME: This fails on some systems
1314 UASSERT(strncmp(sendbuffer, rcvbuffer, sizeof(sendbuffer))==0);
1315 UASSERT(sender.getAddress() == Address(127,0,0,1, 0).getAddress());
1319 struct TestConnection: public TestBase
1324 Test helper functions
1327 // Some constants for testing
1328 u32 proto_id = 0x12345678;
1331 SharedBuffer<u8> data1(1);
1333 Address a(127,0,0,1, 10);
1336 con::BufferedPacket p1 = con::makePacket(a, data1,
1337 proto_id, peer_id, channel);
1339 We should now have a packet with this data:
1342 [4] u16 sender_peer_id
1347 UASSERT(readU32(&p1.data[0]) == proto_id);
1348 UASSERT(readU16(&p1.data[4]) == peer_id);
1349 UASSERT(readU8(&p1.data[6]) == channel);
1350 UASSERT(readU8(&p1.data[7]) == data1[0]);
1352 //infostream<<"initial data1[0]="<<((u32)data1[0]&0xff)<<std::endl;
1354 SharedBuffer<u8> p2 = con::makeReliablePacket(data1, seqnum);
1356 /*infostream<<"p2.getSize()="<<p2.getSize()<<", data1.getSize()="
1357 <<data1.getSize()<<std::endl;
1358 infostream<<"readU8(&p2[3])="<<readU8(&p2[3])
1359 <<" p2[3]="<<((u32)p2[3]&0xff)<<std::endl;
1360 infostream<<"data1[0]="<<((u32)data1[0]&0xff)<<std::endl;*/
1362 UASSERT(p2.getSize() == 3 + data1.getSize());
1363 UASSERT(readU8(&p2[0]) == TYPE_RELIABLE);
1364 UASSERT(readU16(&p2[1]) == seqnum);
1365 UASSERT(readU8(&p2[3]) == data1[0]);
1368 struct Handler : public con::PeerHandler
1370 Handler(const char *a_name)
1376 void peerAdded(con::Peer *peer)
1378 infostream<<"Handler("<<name<<")::peerAdded(): "
1379 "id="<<peer->id<<std::endl;
1383 void deletingPeer(con::Peer *peer, bool timeout)
1385 infostream<<"Handler("<<name<<")::deletingPeer(): "
1387 <<", timeout="<<timeout<<std::endl;
1399 DSTACK("TestConnection::Run");
1404 Test some real connections
1406 NOTE: This mostly tests the legacy interface.
1409 u32 proto_id = 0xad26846a;
1411 Handler hand_server("server");
1412 Handler hand_client("client");
1414 infostream<<"** Creating server Connection"<<std::endl;
1415 con::Connection server(proto_id, 512, 5.0, &hand_server);
1416 server.Serve(30001);
1418 infostream<<"** Creating client Connection"<<std::endl;
1419 con::Connection client(proto_id, 512, 5.0, &hand_client);
1421 UASSERT(hand_server.count == 0);
1422 UASSERT(hand_client.count == 0);
1426 Address server_address(127,0,0,1, 30001);
1427 infostream<<"** running client.Connect()"<<std::endl;
1428 client.Connect(server_address);
1432 // Client should not have added client yet
1433 UASSERT(hand_client.count == 0);
1438 SharedBuffer<u8> data;
1439 infostream<<"** running client.Receive()"<<std::endl;
1440 u32 size = client.Receive(peer_id, data);
1441 infostream<<"** Client received: peer_id="<<peer_id
1445 catch(con::NoIncomingDataException &e)
1449 // Client should have added server now
1450 UASSERT(hand_client.count == 1);
1451 UASSERT(hand_client.last_id == 1);
1452 // Server should not have added client yet
1453 UASSERT(hand_server.count == 0);
1460 SharedBuffer<u8> data;
1461 infostream<<"** running server.Receive()"<<std::endl;
1462 u32 size = server.Receive(peer_id, data);
1463 infostream<<"** Server received: peer_id="<<peer_id
1467 catch(con::NoIncomingDataException &e)
1469 // No actual data received, but the client has
1470 // probably been connected
1473 // Client should be the same
1474 UASSERT(hand_client.count == 1);
1475 UASSERT(hand_client.last_id == 1);
1476 // Server should have the client
1477 UASSERT(hand_server.count == 1);
1478 UASSERT(hand_server.last_id == 2);
1482 while(client.Connected() == false)
1487 SharedBuffer<u8> data;
1488 infostream<<"** running client.Receive()"<<std::endl;
1489 u32 size = client.Receive(peer_id, data);
1490 infostream<<"** Client received: peer_id="<<peer_id
1494 catch(con::NoIncomingDataException &e)
1505 SharedBuffer<u8> data;
1506 infostream<<"** running server.Receive()"<<std::endl;
1507 u32 size = server.Receive(peer_id, data);
1508 infostream<<"** Server received: peer_id="<<peer_id
1512 catch(con::NoIncomingDataException &e)
1517 Simple send-receive test
1520 /*u8 data[] = "Hello World!";
1521 u32 datasize = sizeof(data);*/
1522 SharedBuffer<u8> data = SharedBufferFromString("Hello World!");
1524 infostream<<"** running client.Send()"<<std::endl;
1525 client.Send(PEER_ID_SERVER, 0, data, true);
1530 SharedBuffer<u8> recvdata;
1531 infostream<<"** running server.Receive()"<<std::endl;
1532 u32 size = server.Receive(peer_id, recvdata);
1533 infostream<<"** Server received: peer_id="<<peer_id
1537 UASSERT(memcmp(*data, *recvdata, data.getSize()) == 0);
1540 u16 peer_id_client = 2;
1543 Send consequent packets in different order
1544 Not compatible with new Connection, thus commented out.
1547 //u8 data1[] = "hello1";
1548 //u8 data2[] = "hello2";
1549 SharedBuffer<u8> data1 = SharedBufferFromString("hello1");
1550 SharedBuffer<u8> data2 = SharedBufferFromString("Hello2");
1552 Address client_address =
1553 server.GetPeerAddress(peer_id_client);
1555 infostream<<"*** Sending packets in wrong order (2,1,2)"
1559 con::Channel *ch = &server.getPeer(peer_id_client)->channels[chn];
1560 u16 sn = ch->next_outgoing_seqnum;
1561 ch->next_outgoing_seqnum = sn+1;
1562 server.Send(peer_id_client, chn, data2, true);
1563 ch->next_outgoing_seqnum = sn;
1564 server.Send(peer_id_client, chn, data1, true);
1565 ch->next_outgoing_seqnum = sn+1;
1566 server.Send(peer_id_client, chn, data2, true);
1570 infostream<<"*** Receiving the packets"<<std::endl;
1573 SharedBuffer<u8> recvdata;
1576 infostream<<"** running client.Receive()"<<std::endl;
1578 size = client.Receive(peer_id, recvdata);
1579 infostream<<"** Client received: peer_id="<<peer_id
1581 <<", data="<<*recvdata
1583 UASSERT(size == data1.getSize());
1584 UASSERT(memcmp(*data1, *recvdata, data1.getSize()) == 0);
1585 UASSERT(peer_id == PEER_ID_SERVER);
1587 infostream<<"** running client.Receive()"<<std::endl;
1589 size = client.Receive(peer_id, recvdata);
1590 infostream<<"** Client received: peer_id="<<peer_id
1592 <<", data="<<*recvdata
1594 UASSERT(size == data2.getSize());
1595 UASSERT(memcmp(*data2, *recvdata, data2.getSize()) == 0);
1596 UASSERT(peer_id == PEER_ID_SERVER);
1598 bool got_exception = false;
1601 infostream<<"** running client.Receive()"<<std::endl;
1603 size = client.Receive(peer_id, recvdata);
1604 infostream<<"** Client received: peer_id="<<peer_id
1606 <<", data="<<*recvdata
1609 catch(con::NoIncomingDataException &e)
1611 infostream<<"** No incoming data for client"<<std::endl;
1612 got_exception = true;
1614 UASSERT(got_exception);
1619 Send large amounts of packets (infinite test)
1620 Commented out because of infinity.
1623 infostream<<"Sending large amounts of packets (infinite test)"<<std::endl;
1626 int datasize = myrand_range(0,5)==0?myrand_range(100,10000):myrand_range(0,100);
1627 infostream<<"datasize="<<datasize<<std::endl;
1628 SharedBuffer<u8> data1(datasize);
1629 for(u16 i=0; i<datasize; i++)
1632 int sendtimes = myrand_range(1,10);
1633 for(int i=0; i<sendtimes; i++){
1634 server.Send(peer_id_client, 0, data1, true);
1637 infostream<<"sendcount="<<sendcount<<std::endl;
1639 //int receivetimes = myrand_range(1,20);
1640 int receivetimes = 20;
1641 for(int i=0; i<receivetimes; i++){
1642 SharedBuffer<u8> recvdata;
1645 bool received = false;
1647 size = client.Receive(peer_id, recvdata);
1649 }catch(con::NoIncomingDataException &e){
1659 const int datasize = 30000;
1660 SharedBuffer<u8> data1(datasize);
1661 for(u16 i=0; i<datasize; i++){
1665 infostream<<"Sending data (size="<<datasize<<"):";
1666 for(int i=0; i<datasize && i<20; i++){
1667 if(i%2==0) infostream<<" ";
1669 snprintf(buf, 10, "%.2X", ((int)((const char*)*data1)[i])&0xff);
1674 infostream<<std::endl;
1676 server.Send(peer_id_client, 0, data1, true);
1680 SharedBuffer<u8> recvdata;
1681 infostream<<"** running client.Receive()"<<std::endl;
1684 bool received = false;
1685 u32 timems0 = porting::getTimeMs();
1687 if(porting::getTimeMs() - timems0 > 5000 || received)
1690 size = client.Receive(peer_id, recvdata);
1692 }catch(con::NoIncomingDataException &e){
1697 infostream<<"** Client received: peer_id="<<peer_id
1701 infostream<<"Received data (size="<<size<<"): ";
1702 for(int i=0; i<size && i<20; i++){
1703 if(i%2==0) infostream<<" ";
1705 snprintf(buf, 10, "%.2X", ((int)(recvdata[i]))&0xff);
1710 infostream<<std::endl;
1712 UASSERT(memcmp(*data1, *recvdata, data1.getSize()) == 0);
1713 UASSERT(peer_id == PEER_ID_SERVER);
1716 // Check peer handlers
1717 UASSERT(hand_client.count == 1);
1718 UASSERT(hand_client.last_id == 1);
1719 UASSERT(hand_server.count == 1);
1720 UASSERT(hand_server.last_id == 2);
1729 infostream<<"Running " #X <<std::endl;\
1732 tests_failed += x.test_failed ? 1 : 0;\
1735 #define TESTPARAMS(X, ...)\
1738 infostream<<"Running " #X <<std::endl;\
1739 x.Run(__VA_ARGS__);\
1741 tests_failed += x.test_failed ? 1 : 0;\
1746 DSTACK(__FUNCTION_NAME);
1749 int tests_failed = 0;
1751 // Create item and node definitions
1752 IWritableItemDefManager *idef = createItemDefManager();
1753 IWritableNodeDefManager *ndef = createNodeDefManager();
1754 define_some_nodes(idef, ndef);
1756 infostream<<"run_tests() started"<<std::endl;
1757 TEST(TestUtilities);
1760 TEST(TestSerialization);
1761 TEST(TestNodedefSerialization);
1762 TESTPARAMS(TestMapNode, ndef);
1763 TESTPARAMS(TestVoxelManipulator, ndef);
1764 TESTPARAMS(TestVoxelAlgorithms, ndef);
1765 TESTPARAMS(TestInventory, idef);
1766 //TEST(TestMapBlock);
1767 //TEST(TestMapSector);
1768 TEST(TestCollision);
1769 if(INTERNET_SIMULATOR == false){
1771 dout_con<<"=== BEGIN RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
1772 TEST(TestConnection);
1773 dout_con<<"=== END RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
1775 if(tests_failed == 0){
1776 infostream<<"run_tests(): "<<tests_failed<<" / "<<tests_run<<" tests failed."<<std::endl;
1777 infostream<<"run_tests() passed."<<std::endl;
1780 errorstream<<"run_tests(): "<<tests_failed<<" / "<<tests_run<<" tests failed."<<std::endl;
1781 errorstream<<"run_tests() aborting."<<std::endl;