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
47 Asserts that the exception occurs
49 #define EXCEPTION_CHECK(EType, code)\
51 bool exception_thrown = false;\
53 catch(EType &e) { exception_thrown = true; }\
54 UASSERT(exception_thrown);\
57 #define UTEST(x, fmt, ...)\
60 LOGLINEF(LMT_ERROR, "Test (%s) failed: " fmt, #x, ##__VA_ARGS__);\
65 #define UASSERT(x) UTEST(x, "UASSERT")
68 A few item and node definitions for those tests that need them
71 #define CONTENT_STONE 0
72 #define CONTENT_GRASS 0x800
73 #define CONTENT_TORCH 100
75 void define_some_nodes(IWritableItemDefManager *idef, IWritableNodeDefManager *ndef)
78 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);
106 itemdef = ItemDefinition();
107 itemdef.type = ITEM_NODE;
108 itemdef.name = "default:dirt_with_grass";
109 itemdef.description = "Dirt with grass";
110 itemdef.groups["crumbly"] = 3;
111 itemdef.inventory_image = "[inventorycube"
113 "{default_dirt.png&default_grass_side.png"
114 "{default_dirt.png&default_grass_side.png";
115 f = ContentFeatures();
116 f.name = itemdef.name;
117 f.tiledef[0].name = "default_grass.png";
118 f.tiledef[1].name = "default_dirt.png";
119 for(int i = 2; i < 6; i++)
120 f.tiledef[i].name = "default_dirt.png^default_grass_side.png";
121 f.is_ground_content = true;
122 idef->registerItem(itemdef);
126 Torch (minimal definition for lighting tests)
129 itemdef = ItemDefinition();
130 itemdef.type = ITEM_NODE;
131 itemdef.name = "default:torch";
132 f = ContentFeatures();
133 f.name = itemdef.name;
134 f.param_type = CPT_LIGHT;
135 f.light_propagates = true;
136 f.sunlight_propagates = true;
137 f.light_source = LIGHT_MAX-1;
138 idef->registerItem(itemdef);
150 struct TestUtilities: public TestBase
154 /*infostream<<"wrapDegrees(100.0) = "<<wrapDegrees(100.0)<<std::endl;
155 infostream<<"wrapDegrees(720.5) = "<<wrapDegrees(720.5)<<std::endl;
156 infostream<<"wrapDegrees(-0.5) = "<<wrapDegrees(-0.5)<<std::endl;*/
157 UASSERT(fabs(wrapDegrees(100.0) - 100.0) < 0.001);
158 UASSERT(fabs(wrapDegrees(720.5) - 0.5) < 0.001);
159 UASSERT(fabs(wrapDegrees(-0.5) - (-0.5)) < 0.001);
160 UASSERT(fabs(wrapDegrees(-365.5) - (-5.5)) < 0.001);
161 UASSERT(lowercase("Foo bAR") == "foo bar");
162 UASSERT(is_yes("YeS") == true);
163 UASSERT(is_yes("") == false);
164 UASSERT(is_yes("FAlse") == false);
165 const char *ends[] = {"abc", "c", "bc", NULL};
166 UASSERT(removeStringEnd("abc", ends) == "");
167 UASSERT(removeStringEnd("bc", ends) == "b");
168 UASSERT(removeStringEnd("12c", ends) == "12");
169 UASSERT(removeStringEnd("foo", ends) == "");
173 struct TestSettings: public TestBase
178 // Test reading of settings
179 s.parseConfigLine("leet = 1337");
180 s.parseConfigLine("leetleet = 13371337");
181 s.parseConfigLine("leetleet_neg = -13371337");
182 s.parseConfigLine("floaty_thing = 1.1");
183 s.parseConfigLine("stringy_thing = asd /( ¤%&(/\" BLÖÄRP");
184 s.parseConfigLine("coord = (1, 2, 4.5)");
185 UASSERT(s.getS32("leet") == 1337);
186 UASSERT(s.getS16("leetleet") == 32767);
187 UASSERT(s.getS16("leetleet_neg") == -32768);
188 // Not sure if 1.1 is an exact value as a float, but doesn't matter
189 UASSERT(fabs(s.getFloat("floaty_thing") - 1.1) < 0.001);
190 UASSERT(s.get("stringy_thing") == "asd /( ¤%&(/\" BLÖÄRP");
191 UASSERT(fabs(s.getV3F("coord").X - 1.0) < 0.001);
192 UASSERT(fabs(s.getV3F("coord").Y - 2.0) < 0.001);
193 UASSERT(fabs(s.getV3F("coord").Z - 4.5) < 0.001);
194 // Test the setting of settings too
195 s.setFloat("floaty_thing_2", 1.2);
196 s.setV3F("coord2", v3f(1, 2, 3.3));
197 UASSERT(s.get("floaty_thing_2").substr(0,3) == "1.2");
198 UASSERT(fabs(s.getFloat("floaty_thing_2") - 1.2) < 0.001);
199 UASSERT(fabs(s.getV3F("coord2").X - 1.0) < 0.001);
200 UASSERT(fabs(s.getV3F("coord2").Y - 2.0) < 0.001);
201 UASSERT(fabs(s.getV3F("coord2").Z - 3.3) < 0.001);
205 struct TestSerialization: public TestBase
207 // To be used like this:
208 // mkstr("Some\0string\0with\0embedded\0nuls")
209 // since std::string("...") doesn't work as expected in that case.
210 template<size_t N> std::string mkstr(const char (&s)[N])
212 return std::string(s, N - 1);
217 // Tests some serialization primitives
219 UASSERT(serializeString("") == mkstr("\0\0"));
220 UASSERT(serializeWideString(L"") == mkstr("\0\0"));
221 UASSERT(serializeLongString("") == mkstr("\0\0\0\0"));
222 UASSERT(serializeJsonString("") == "\"\"");
224 std::string teststring = "Hello world!";
225 UASSERT(serializeString(teststring) ==
226 mkstr("\0\14Hello world!"));
227 UASSERT(serializeWideString(narrow_to_wide(teststring)) ==
228 mkstr("\0\14\0H\0e\0l\0l\0o\0 \0w\0o\0r\0l\0d\0!"));
229 UASSERT(serializeLongString(teststring) ==
230 mkstr("\0\0\0\14Hello world!"));
231 UASSERT(serializeJsonString(teststring) ==
234 std::string teststring2;
235 std::wstring teststring2_w;
236 std::string teststring2_w_encoded;
238 std::ostringstream tmp_os;
239 std::wostringstream tmp_os_w;
240 std::ostringstream tmp_os_w_encoded;
241 for(int i = 0; i < 256; i++)
244 tmp_os_w<<(wchar_t)i;
245 tmp_os_w_encoded<<(char)0<<(char)i;
247 teststring2 = tmp_os.str();
248 teststring2_w = tmp_os_w.str();
249 teststring2_w_encoded = tmp_os_w_encoded.str();
251 UASSERT(serializeString(teststring2) ==
252 mkstr("\1\0") + teststring2);
253 UASSERT(serializeWideString(teststring2_w) ==
254 mkstr("\1\0") + teststring2_w_encoded);
255 UASSERT(serializeLongString(teststring2) ==
256 mkstr("\0\0\1\0") + teststring2);
257 // MSVC fails when directly using "\\\\"
258 std::string backslash = "\\";
259 UASSERT(serializeJsonString(teststring2) ==
261 "\\u0000\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007" +
262 "\\b\\t\\n\\u000b\\f\\r\\u000e\\u000f" +
263 "\\u0010\\u0011\\u0012\\u0013\\u0014\\u0015\\u0016\\u0017" +
264 "\\u0018\\u0019\\u001a\\u001b\\u001c\\u001d\\u001e\\u001f" +
265 " !\\\"" + teststring2.substr(0x23, 0x2f-0x23) +
266 "\\/" + teststring2.substr(0x30, 0x5c-0x30) +
267 backslash + backslash + teststring2.substr(0x5d, 0x7f-0x5d) + "\\u007f" +
268 "\\u0080\\u0081\\u0082\\u0083\\u0084\\u0085\\u0086\\u0087" +
269 "\\u0088\\u0089\\u008a\\u008b\\u008c\\u008d\\u008e\\u008f" +
270 "\\u0090\\u0091\\u0092\\u0093\\u0094\\u0095\\u0096\\u0097" +
271 "\\u0098\\u0099\\u009a\\u009b\\u009c\\u009d\\u009e\\u009f" +
272 "\\u00a0\\u00a1\\u00a2\\u00a3\\u00a4\\u00a5\\u00a6\\u00a7" +
273 "\\u00a8\\u00a9\\u00aa\\u00ab\\u00ac\\u00ad\\u00ae\\u00af" +
274 "\\u00b0\\u00b1\\u00b2\\u00b3\\u00b4\\u00b5\\u00b6\\u00b7" +
275 "\\u00b8\\u00b9\\u00ba\\u00bb\\u00bc\\u00bd\\u00be\\u00bf" +
276 "\\u00c0\\u00c1\\u00c2\\u00c3\\u00c4\\u00c5\\u00c6\\u00c7" +
277 "\\u00c8\\u00c9\\u00ca\\u00cb\\u00cc\\u00cd\\u00ce\\u00cf" +
278 "\\u00d0\\u00d1\\u00d2\\u00d3\\u00d4\\u00d5\\u00d6\\u00d7" +
279 "\\u00d8\\u00d9\\u00da\\u00db\\u00dc\\u00dd\\u00de\\u00df" +
280 "\\u00e0\\u00e1\\u00e2\\u00e3\\u00e4\\u00e5\\u00e6\\u00e7" +
281 "\\u00e8\\u00e9\\u00ea\\u00eb\\u00ec\\u00ed\\u00ee\\u00ef" +
282 "\\u00f0\\u00f1\\u00f2\\u00f3\\u00f4\\u00f5\\u00f6\\u00f7" +
283 "\\u00f8\\u00f9\\u00fa\\u00fb\\u00fc\\u00fd\\u00fe\\u00ff" +
287 std::istringstream is(serializeString(teststring2), std::ios::binary);
288 UASSERT(deSerializeString(is) == teststring2);
294 std::istringstream is(serializeWideString(teststring2_w), std::ios::binary);
295 UASSERT(deSerializeWideString(is) == teststring2_w);
301 std::istringstream is(serializeLongString(teststring2), std::ios::binary);
302 UASSERT(deSerializeLongString(is) == teststring2);
308 std::istringstream is(serializeJsonString(teststring2), std::ios::binary);
309 //dstream<<serializeJsonString(deSerializeJsonString(is));
310 UASSERT(deSerializeJsonString(is) == teststring2);
318 struct TestNodedefSerialization: public TestBase
323 f.name = "default:stone";
324 for(int i = 0; i < 6; i++)
325 f.tiledef[i].name = "default_stone.png";
326 f.is_ground_content = true;
327 std::ostringstream os(std::ios::binary);
328 f.serialize(os, LATEST_PROTOCOL_VERSION);
329 verbosestream<<"Test ContentFeatures size: "<<os.str().size()<<std::endl;
330 std::istringstream is(os.str(), std::ios::binary);
333 UASSERT(f.walkable == f2.walkable);
334 UASSERT(f.node_box.type == f2.node_box.type);
338 struct TestCompress: public TestBase
344 SharedBuffer<u8> fromdata(4);
350 std::ostringstream os(std::ios_base::binary);
351 compress(fromdata, os, 0);
353 std::string str_out = os.str();
355 infostream<<"str_out.size()="<<str_out.size()<<std::endl;
356 infostream<<"TestCompress: 1,5,5,1 -> ";
357 for(u32 i=0; i<str_out.size(); i++)
359 infostream<<(u32)str_out[i]<<",";
361 infostream<<std::endl;
363 UASSERT(str_out.size() == 10);
365 UASSERT(str_out[0] == 0);
366 UASSERT(str_out[1] == 0);
367 UASSERT(str_out[2] == 0);
368 UASSERT(str_out[3] == 4);
369 UASSERT(str_out[4] == 0);
370 UASSERT(str_out[5] == 1);
371 UASSERT(str_out[6] == 1);
372 UASSERT(str_out[7] == 5);
373 UASSERT(str_out[8] == 0);
374 UASSERT(str_out[9] == 1);
376 std::istringstream is(str_out, std::ios_base::binary);
377 std::ostringstream os2(std::ios_base::binary);
379 decompress(is, os2, 0);
380 std::string str_out2 = os2.str();
382 infostream<<"decompress: ";
383 for(u32 i=0; i<str_out2.size(); i++)
385 infostream<<(u32)str_out2[i]<<",";
387 infostream<<std::endl;
389 UASSERT(str_out2.size() == fromdata.getSize());
391 for(u32 i=0; i<str_out2.size(); i++)
393 UASSERT(str_out2[i] == fromdata[i]);
400 SharedBuffer<u8> fromdata(4);
406 std::ostringstream os(std::ios_base::binary);
407 compress(fromdata, os, SER_FMT_VER_HIGHEST);
409 std::string str_out = os.str();
411 infostream<<"str_out.size()="<<str_out.size()<<std::endl;
412 infostream<<"TestCompress: 1,5,5,1 -> ";
413 for(u32 i=0; i<str_out.size(); i++)
415 infostream<<(u32)str_out[i]<<",";
417 infostream<<std::endl;
419 std::istringstream is(str_out, std::ios_base::binary);
420 std::ostringstream os2(std::ios_base::binary);
422 decompress(is, os2, SER_FMT_VER_HIGHEST);
423 std::string str_out2 = os2.str();
425 infostream<<"decompress: ";
426 for(u32 i=0; i<str_out2.size(); i++)
428 infostream<<(u32)str_out2[i]<<",";
430 infostream<<std::endl;
432 UASSERT(str_out2.size() == fromdata.getSize());
434 for(u32 i=0; i<str_out2.size(); i++)
436 UASSERT(str_out2[i] == fromdata[i]);
441 // Test zlib wrapper with large amounts of data (larger than its
444 infostream<<"Test: Testing zlib wrappers with a large amount "
445 <<"of pseudorandom data"<<std::endl;
447 infostream<<"Test: Input size of large compressZlib is "
450 data_in.resize(size);
451 PseudoRandom pseudorandom(9420);
452 for(u32 i=0; i<size; i++)
453 data_in[i] = pseudorandom.range(0,255);
454 std::ostringstream os_compressed(std::ios::binary);
455 compressZlib(data_in, os_compressed);
456 infostream<<"Test: Output size of large compressZlib is "
457 <<os_compressed.str().size()<<std::endl;
458 std::istringstream is_compressed(os_compressed.str(), std::ios::binary);
459 std::ostringstream os_decompressed(std::ios::binary);
460 decompressZlib(is_compressed, os_decompressed);
461 infostream<<"Test: Output size of large decompressZlib is "
462 <<os_decompressed.str().size()<<std::endl;
463 std::string str_decompressed = os_decompressed.str();
464 UTEST(str_decompressed.size() == data_in.size(), "Output size not"
465 " equal (output: %i, input: %i)",
466 str_decompressed.size(), data_in.size());
467 for(u32 i=0; i<size && i<str_decompressed.size(); i++){
468 UTEST(str_decompressed[i] == data_in[i],
469 "index out[%i]=%i differs from in[%i]=%i",
470 i, str_decompressed[i], i, data_in[i]);
476 struct TestMapNode: public TestBase
478 void Run(INodeDefManager *nodedef)
483 UASSERT(n.getContent() == CONTENT_AIR);
484 UASSERT(n.getLight(LIGHTBANK_DAY, nodedef) == 0);
485 UASSERT(n.getLight(LIGHTBANK_NIGHT, nodedef) == 0);
488 n.setContent(CONTENT_AIR);
489 UASSERT(nodedef->get(n).light_propagates == true);
490 n.setContent(LEGN(nodedef, "CONTENT_STONE"));
491 UASSERT(nodedef->get(n).light_propagates == false);
495 struct TestVoxelManipulator: public TestBase
497 void Run(INodeDefManager *nodedef)
503 VoxelArea a(v3s16(-1,-1,-1), v3s16(1,1,1));
504 UASSERT(a.index(0,0,0) == 1*3*3 + 1*3 + 1);
505 UASSERT(a.index(-1,-1,-1) == 0);
507 VoxelArea c(v3s16(-2,-2,-2), v3s16(2,2,2));
508 // An area that is 1 bigger in x+ and z-
509 VoxelArea d(v3s16(-2,-2,-3), v3s16(3,2,2));
511 core::list<VoxelArea> aa;
515 core::array<VoxelArea> results;
516 results.push_back(VoxelArea(v3s16(-2,-2,-3),v3s16(3,2,-3)));
517 results.push_back(VoxelArea(v3s16(3,-2,-2),v3s16(3,2,2)));
519 UASSERT(aa.size() == results.size());
521 infostream<<"Result of diff:"<<std::endl;
522 for(core::list<VoxelArea>::Iterator
523 i = aa.begin(); i != aa.end(); i++)
525 i->print(infostream);
526 infostream<<std::endl;
528 s32 j = results.linear_search(*i);
540 v.print(infostream, nodedef);
542 infostream<<"*** Setting (-1,0,-1)=2 ***"<<std::endl;
544 v.setNodeNoRef(v3s16(-1,0,-1), MapNode(CONTENT_GRASS));
546 v.print(infostream, nodedef);
548 UASSERT(v.getNode(v3s16(-1,0,-1)).getContent() == CONTENT_GRASS);
550 infostream<<"*** Reading from inexistent (0,0,-1) ***"<<std::endl;
552 EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,0,-1)));
554 v.print(infostream, nodedef);
556 infostream<<"*** Adding area ***"<<std::endl;
560 v.print(infostream, nodedef);
562 UASSERT(v.getNode(v3s16(-1,0,-1)).getContent() == CONTENT_GRASS);
563 EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,1,1)));
567 struct TestVoxelAlgorithms: public TestBase
569 void Run(INodeDefManager *ndef)
572 voxalgo::propagateSunlight
576 for(u16 z=0; z<3; z++)
577 for(u16 y=0; y<3; y++)
578 for(u16 x=0; x<3; x++)
581 v.setNodeNoRef(p, MapNode(CONTENT_AIR));
583 VoxelArea a(v3s16(0,0,0), v3s16(2,2,2));
585 core::map<v3s16, bool> light_sources;
586 voxalgo::setLight(v, a, 0, ndef);
587 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
588 v, a, true, light_sources, ndef);
589 //v.print(dstream, ndef, VOXELPRINT_LIGHT_DAY);
590 UASSERT(res.bottom_sunlight_valid == true);
591 UASSERT(v.getNode(v3s16(1,1,1)).getLight(LIGHTBANK_DAY, ndef)
594 v.setNodeNoRef(v3s16(0,0,0), MapNode(CONTENT_STONE));
596 core::map<v3s16, bool> light_sources;
597 voxalgo::setLight(v, a, 0, ndef);
598 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
599 v, a, true, light_sources, ndef);
600 UASSERT(res.bottom_sunlight_valid == true);
601 UASSERT(v.getNode(v3s16(1,1,1)).getLight(LIGHTBANK_DAY, ndef)
605 core::map<v3s16, bool> light_sources;
606 voxalgo::setLight(v, a, 0, ndef);
607 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
608 v, a, false, light_sources, ndef);
609 UASSERT(res.bottom_sunlight_valid == true);
610 UASSERT(v.getNode(v3s16(2,0,2)).getLight(LIGHTBANK_DAY, ndef)
613 v.setNodeNoRef(v3s16(1,3,2), MapNode(CONTENT_STONE));
615 core::map<v3s16, bool> light_sources;
616 voxalgo::setLight(v, a, 0, ndef);
617 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
618 v, a, true, light_sources, ndef);
619 UASSERT(res.bottom_sunlight_valid == true);
620 UASSERT(v.getNode(v3s16(1,1,2)).getLight(LIGHTBANK_DAY, ndef)
624 core::map<v3s16, bool> light_sources;
625 voxalgo::setLight(v, a, 0, ndef);
626 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
627 v, a, false, light_sources, ndef);
628 UASSERT(res.bottom_sunlight_valid == true);
629 UASSERT(v.getNode(v3s16(1,0,2)).getLight(LIGHTBANK_DAY, ndef)
633 MapNode n(CONTENT_AIR);
634 n.setLight(LIGHTBANK_DAY, 10, ndef);
635 v.setNodeNoRef(v3s16(1,-1,2), n);
638 core::map<v3s16, bool> light_sources;
639 voxalgo::setLight(v, a, 0, ndef);
640 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
641 v, a, true, light_sources, ndef);
642 UASSERT(res.bottom_sunlight_valid == true);
645 core::map<v3s16, bool> light_sources;
646 voxalgo::setLight(v, a, 0, ndef);
647 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
648 v, a, false, light_sources, ndef);
649 UASSERT(res.bottom_sunlight_valid == true);
652 MapNode n(CONTENT_AIR);
653 n.setLight(LIGHTBANK_DAY, LIGHT_SUN, ndef);
654 v.setNodeNoRef(v3s16(1,-1,2), n);
657 core::map<v3s16, bool> light_sources;
658 voxalgo::setLight(v, a, 0, ndef);
659 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
660 v, a, true, light_sources, ndef);
661 UASSERT(res.bottom_sunlight_valid == false);
664 core::map<v3s16, bool> light_sources;
665 voxalgo::setLight(v, a, 0, ndef);
666 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
667 v, a, false, light_sources, ndef);
668 UASSERT(res.bottom_sunlight_valid == false);
670 v.setNodeNoRef(v3s16(1,3,2), MapNode(CONTENT_IGNORE));
672 core::map<v3s16, bool> light_sources;
673 voxalgo::setLight(v, a, 0, ndef);
674 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
675 v, a, true, light_sources, ndef);
676 UASSERT(res.bottom_sunlight_valid == true);
680 voxalgo::clearLightAndCollectSources
684 for(u16 z=0; z<3; z++)
685 for(u16 y=0; y<3; y++)
686 for(u16 x=0; x<3; x++)
689 v.setNode(p, MapNode(CONTENT_AIR));
691 VoxelArea a(v3s16(0,0,0), v3s16(2,2,2));
692 v.setNodeNoRef(v3s16(0,0,0), MapNode(CONTENT_STONE));
693 v.setNodeNoRef(v3s16(1,1,1), MapNode(CONTENT_TORCH));
695 MapNode n(CONTENT_AIR);
696 n.setLight(LIGHTBANK_DAY, 1, ndef);
697 v.setNode(v3s16(1,1,2), n);
700 core::map<v3s16, bool> light_sources;
701 core::map<v3s16, u8> unlight_from;
702 voxalgo::clearLightAndCollectSources(v, a, LIGHTBANK_DAY,
703 ndef, light_sources, unlight_from);
704 //v.print(dstream, ndef, VOXELPRINT_LIGHT_DAY);
705 UASSERT(v.getNode(v3s16(0,1,1)).getLight(LIGHTBANK_DAY, ndef)
707 UASSERT(light_sources.find(v3s16(1,1,1)) != NULL);
708 UASSERT(light_sources.size() == 1);
709 UASSERT(unlight_from.find(v3s16(1,1,2)) != NULL);
710 UASSERT(unlight_from.size() == 1);
716 struct TestInventory: public TestBase
718 void Run(IItemDefManager *idef)
720 std::string serialized_inventory =
732 "Item default:cobble 61\n"
739 "Item default:dirt 71\n"
747 "Item default:dirt 99\n"
748 "Item default:cobble 38\n"
758 std::string serialized_inventory_2 =
770 "Item default:cobble 61\n"
777 "Item default:dirt 71\n"
785 "Item default:dirt 99\n"
786 "Item default:cobble 38\n"
797 std::istringstream is(serialized_inventory, std::ios::binary);
799 UASSERT(inv.getList("0"));
800 UASSERT(!inv.getList("main"));
801 inv.getList("0")->setName("main");
802 UASSERT(!inv.getList("0"));
803 UASSERT(inv.getList("main"));
804 UASSERT(inv.getList("main")->getWidth() == 3);
805 inv.getList("main")->setWidth(5);
806 std::ostringstream inv_os(std::ios::binary);
807 inv.serialize(inv_os);
808 UASSERT(inv_os.str() == serialized_inventory_2);
813 NOTE: These tests became non-working then NodeContainer was removed.
814 These should be redone, utilizing some kind of a virtual
815 interface for Map (IMap would be fine).
818 struct TestMapBlock: public TestBase
820 class TC : public NodeContainer
826 core::list<v3s16> validity_exceptions;
830 position_valid = true;
833 virtual bool isValidPosition(v3s16 p)
835 //return position_valid ^ (p==position_valid_exception);
836 bool exception = false;
837 for(core::list<v3s16>::Iterator i=validity_exceptions.begin();
838 i != validity_exceptions.end(); i++)
846 return exception ? !position_valid : position_valid;
849 virtual MapNode getNode(v3s16 p)
851 if(isValidPosition(p) == false)
852 throw InvalidPositionException();
856 virtual void setNode(v3s16 p, MapNode & n)
858 if(isValidPosition(p) == false)
859 throw InvalidPositionException();
862 virtual u16 nodeContainerId() const
872 MapBlock b(&parent, v3s16(1,1,1));
873 v3s16 relpos(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
875 UASSERT(b.getPosRelative() == relpos);
877 UASSERT(b.getBox().MinEdge.X == MAP_BLOCKSIZE);
878 UASSERT(b.getBox().MaxEdge.X == MAP_BLOCKSIZE*2-1);
879 UASSERT(b.getBox().MinEdge.Y == MAP_BLOCKSIZE);
880 UASSERT(b.getBox().MaxEdge.Y == MAP_BLOCKSIZE*2-1);
881 UASSERT(b.getBox().MinEdge.Z == MAP_BLOCKSIZE);
882 UASSERT(b.getBox().MaxEdge.Z == MAP_BLOCKSIZE*2-1);
884 UASSERT(b.isValidPosition(v3s16(0,0,0)) == true);
885 UASSERT(b.isValidPosition(v3s16(-1,0,0)) == false);
886 UASSERT(b.isValidPosition(v3s16(-1,-142,-2341)) == false);
887 UASSERT(b.isValidPosition(v3s16(-124,142,2341)) == false);
888 UASSERT(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
889 UASSERT(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE,MAP_BLOCKSIZE-1)) == false);
892 TODO: this method should probably be removed
893 if the block size isn't going to be set variable
895 /*UASSERT(b.getSizeNodes() == v3s16(MAP_BLOCKSIZE,
896 MAP_BLOCKSIZE, MAP_BLOCKSIZE));*/
898 // Changed flag should be initially set
899 UASSERT(b.getModified() == MOD_STATE_WRITE_NEEDED);
901 UASSERT(b.getModified() == MOD_STATE_CLEAN);
903 // All nodes should have been set to
904 // .d=CONTENT_IGNORE and .getLight() = 0
905 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
906 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
907 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
909 //UASSERT(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_AIR);
910 UASSERT(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_IGNORE);
911 UASSERT(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_DAY) == 0);
912 UASSERT(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_NIGHT) == 0);
916 MapNode n(CONTENT_AIR);
917 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
918 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
919 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
921 b.setNode(v3s16(x,y,z), n);
926 Parent fetch functions
928 parent.position_valid = false;
929 parent.node.setContent(5);
933 // Positions in the block should still be valid
934 UASSERT(b.isValidPositionParent(v3s16(0,0,0)) == true);
935 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
936 n = b.getNodeParent(v3s16(0,MAP_BLOCKSIZE-1,0));
937 UASSERT(n.getContent() == CONTENT_AIR);
939 // ...but outside the block they should be invalid
940 UASSERT(b.isValidPositionParent(v3s16(-121,2341,0)) == false);
941 UASSERT(b.isValidPositionParent(v3s16(-1,0,0)) == false);
942 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == false);
945 bool exception_thrown = false;
947 // This should throw an exception
948 MapNode n = b.getNodeParent(v3s16(0,0,-1));
950 catch(InvalidPositionException &e)
952 exception_thrown = true;
954 UASSERT(exception_thrown);
957 parent.position_valid = true;
958 // Now the positions outside should be valid
959 UASSERT(b.isValidPositionParent(v3s16(-121,2341,0)) == true);
960 UASSERT(b.isValidPositionParent(v3s16(-1,0,0)) == true);
961 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == true);
962 n = b.getNodeParent(v3s16(0,0,MAP_BLOCKSIZE));
963 UASSERT(n.getContent() == 5);
971 UASSERT(b.getNode(p).getContent() == 4);
972 //TODO: Update to new system
973 /*UASSERT(b.getNodeTile(p) == 4);
974 UASSERT(b.getNodeTile(v3s16(-1,-1,0)) == 5);*/
979 // Set lighting of all nodes to 0
980 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
981 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
982 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
983 MapNode n = b.getNode(v3s16(x,y,z));
984 n.setLight(LIGHTBANK_DAY, 0);
985 n.setLight(LIGHTBANK_NIGHT, 0);
986 b.setNode(v3s16(x,y,z), n);
992 Check how the block handles being a lonely sky block
994 parent.position_valid = true;
995 b.setIsUnderground(false);
996 parent.node.setContent(CONTENT_AIR);
997 parent.node.setLight(LIGHTBANK_DAY, LIGHT_SUN);
998 parent.node.setLight(LIGHTBANK_NIGHT, 0);
999 core::map<v3s16, bool> light_sources;
1000 // The bottom block is invalid, because we have a shadowing node
1001 UASSERT(b.propagateSunlight(light_sources) == false);
1002 UASSERT(b.getNode(v3s16(1,4,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
1003 UASSERT(b.getNode(v3s16(1,3,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
1004 UASSERT(b.getNode(v3s16(1,2,0)).getLight(LIGHTBANK_DAY) == 0);
1005 UASSERT(b.getNode(v3s16(1,1,0)).getLight(LIGHTBANK_DAY) == 0);
1006 UASSERT(b.getNode(v3s16(1,0,0)).getLight(LIGHTBANK_DAY) == 0);
1007 UASSERT(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
1008 UASSERT(b.getFaceLight2(1000, p, v3s16(0,1,0)) == LIGHT_SUN);
1009 UASSERT(b.getFaceLight2(1000, p, v3s16(0,-1,0)) == 0);
1010 UASSERT(b.getFaceLight2(0, p, v3s16(0,-1,0)) == 0);
1011 // According to MapBlock::getFaceLight,
1012 // The face on the z+ side should have double-diminished light
1013 //UASSERT(b.getFaceLight(p, v3s16(0,0,1)) == diminish_light(diminish_light(LIGHT_MAX)));
1014 // The face on the z+ side should have diminished light
1015 UASSERT(b.getFaceLight2(1000, p, v3s16(0,0,1)) == diminish_light(LIGHT_MAX));
1018 Check how the block handles being in between blocks with some non-sunlight
1019 while being underground
1022 // Make neighbours to exist and set some non-sunlight to them
1023 parent.position_valid = true;
1024 b.setIsUnderground(true);
1025 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
1026 core::map<v3s16, bool> light_sources;
1027 // The block below should be valid because there shouldn't be
1028 // sunlight in there either
1029 UASSERT(b.propagateSunlight(light_sources, true) == true);
1030 // Should not touch nodes that are not affected (that is, all of them)
1031 //UASSERT(b.getNode(v3s16(1,2,3)).getLight() == LIGHT_SUN);
1032 // Should set light of non-sunlighted blocks to 0.
1033 UASSERT(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == 0);
1036 Set up a situation where:
1037 - There is only air in this block
1038 - There is a valid non-sunlighted block at the bottom, and
1039 - Invalid blocks elsewhere.
1040 - the block is not underground.
1042 This should result in bottom block invalidity
1045 b.setIsUnderground(false);
1047 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
1048 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
1049 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
1051 n.setContent(CONTENT_AIR);
1052 n.setLight(LIGHTBANK_DAY, 0);
1053 b.setNode(v3s16(x,y,z), n);
1057 // Make neighbours invalid
1058 parent.position_valid = false;
1059 // Add exceptions to the top of the bottom block
1060 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
1061 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
1063 parent.validity_exceptions.push_back(v3s16(MAP_BLOCKSIZE+x, MAP_BLOCKSIZE-1, MAP_BLOCKSIZE+z));
1065 // Lighting value for the valid nodes
1066 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
1067 core::map<v3s16, bool> light_sources;
1068 // Bottom block is not valid
1069 UASSERT(b.propagateSunlight(light_sources) == false);
1074 struct TestMapSector: public TestBase
1076 class TC : public NodeContainer
1081 bool position_valid;
1085 position_valid = true;
1088 virtual bool isValidPosition(v3s16 p)
1090 return position_valid;
1093 virtual MapNode getNode(v3s16 p)
1095 if(position_valid == false)
1096 throw InvalidPositionException();
1100 virtual void setNode(v3s16 p, MapNode & n)
1102 if(position_valid == false)
1103 throw InvalidPositionException();
1106 virtual u16 nodeContainerId() const
1115 parent.position_valid = false;
1117 // Create one with no heightmaps
1118 ServerMapSector sector(&parent, v2s16(1,1));
1120 UASSERT(sector.getBlockNoCreateNoEx(0) == 0);
1121 UASSERT(sector.getBlockNoCreateNoEx(1) == 0);
1123 MapBlock * bref = sector.createBlankBlock(-2);
1125 UASSERT(sector.getBlockNoCreateNoEx(0) == 0);
1126 UASSERT(sector.getBlockNoCreateNoEx(-2) == bref);
1128 //TODO: Check for AlreadyExistsException
1130 /*bool exception_thrown = false;
1134 catch(InvalidPositionException &e){
1135 exception_thrown = true;
1137 UASSERT(exception_thrown);*/
1143 struct TestCollision: public TestBase
1148 axisAlignedCollision
1151 for(s16 bx = -3; bx <= 3; bx++)
1152 for(s16 by = -3; by <= 3; by++)
1153 for(s16 bz = -3; bz <= 3; bz++)
1157 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1158 aabb3f m(bx-2, by, bz, bx-1, by+1, bz+1);
1161 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1162 UASSERT(fabs(dtime - 1.000) < 0.001);
1165 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1166 aabb3f m(bx-2, by, bz, bx-1, by+1, bz+1);
1169 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == -1);
1172 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1173 aabb3f m(bx-2, by+1.5, bz, bx-1, by+2.5, bz-1);
1176 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == -1);
1179 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1180 aabb3f m(bx-2, by-1.5, bz, bx-1.5, by+0.5, bz+1);
1183 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1184 UASSERT(fabs(dtime - 3.000) < 0.001);
1187 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1188 aabb3f m(bx-2, by-1.5, bz, bx-1.5, by+0.5, bz+1);
1191 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1192 UASSERT(fabs(dtime - 3.000) < 0.001);
1197 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1198 aabb3f m(bx+2, by, bz, bx+3, by+1, bz+1);
1201 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1202 UASSERT(fabs(dtime - 1.000) < 0.001);
1205 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1206 aabb3f m(bx+2, by, bz, bx+3, by+1, bz+1);
1209 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == -1);
1212 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1213 aabb3f m(bx+2, by, bz+1.5, bx+3, by+1, bz+3.5);
1216 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == -1);
1219 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1220 aabb3f m(bx+2, by-1.5, bz, bx+2.5, by-0.5, bz+1);
1221 v3f v(-0.5, 0.2, 0);
1223 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 1); // Y, not X!
1224 UASSERT(fabs(dtime - 2.500) < 0.001);
1227 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1228 aabb3f m(bx+2, by-1.5, bz, bx+2.5, by-0.5, bz+1);
1229 v3f v(-0.5, 0.3, 0);
1231 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1232 UASSERT(fabs(dtime - 2.000) < 0.001);
1235 // TODO: Y-, Y+, Z-, Z+
1239 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1240 aabb3f m(bx+2.3, by+2.29, bz+2.29, bx+4.2, by+4.2, bz+4.2);
1241 v3f v(-1./3, -1./3, -1./3);
1243 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1244 UASSERT(fabs(dtime - 0.9) < 0.001);
1247 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1248 aabb3f m(bx+2.29, by+2.3, bz+2.29, bx+4.2, by+4.2, bz+4.2);
1249 v3f v(-1./3, -1./3, -1./3);
1251 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 1);
1252 UASSERT(fabs(dtime - 0.9) < 0.001);
1255 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1256 aabb3f m(bx+2.29, by+2.29, bz+2.3, bx+4.2, by+4.2, bz+4.2);
1257 v3f v(-1./3, -1./3, -1./3);
1259 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 2);
1260 UASSERT(fabs(dtime - 0.9) < 0.001);
1263 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1264 aabb3f m(bx-4.2, by-4.2, bz-4.2, bx-2.3, by-2.29, bz-2.29);
1265 v3f v(1./7, 1./7, 1./7);
1267 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1268 UASSERT(fabs(dtime - 16.1) < 0.001);
1271 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1272 aabb3f m(bx-4.2, by-4.2, bz-4.2, bx-2.29, by-2.3, bz-2.29);
1273 v3f v(1./7, 1./7, 1./7);
1275 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 1);
1276 UASSERT(fabs(dtime - 16.1) < 0.001);
1279 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1280 aabb3f m(bx-4.2, by-4.2, bz-4.2, bx-2.29, by-2.29, bz-2.3);
1281 v3f v(1./7, 1./7, 1./7);
1283 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 2);
1284 UASSERT(fabs(dtime - 16.1) < 0.001);
1290 struct TestSocket: public TestBase
1294 const int port = 30003;
1298 const char sendbuffer[] = "hello world!";
1299 socket.Send(Address(127,0,0,1,port), sendbuffer, sizeof(sendbuffer));
1303 char rcvbuffer[256];
1304 memset(rcvbuffer, 0, sizeof(rcvbuffer));
1308 int bytes_read = socket.Receive(sender, rcvbuffer, sizeof(rcvbuffer));
1312 //FIXME: This fails on some systems
1313 UASSERT(strncmp(sendbuffer, rcvbuffer, sizeof(sendbuffer))==0);
1314 UASSERT(sender.getAddress() == Address(127,0,0,1, 0).getAddress());
1318 struct TestConnection: public TestBase
1323 Test helper functions
1326 // Some constants for testing
1327 u32 proto_id = 0x12345678;
1330 SharedBuffer<u8> data1(1);
1332 Address a(127,0,0,1, 10);
1335 con::BufferedPacket p1 = con::makePacket(a, data1,
1336 proto_id, peer_id, channel);
1338 We should now have a packet with this data:
1341 [4] u16 sender_peer_id
1346 UASSERT(readU32(&p1.data[0]) == proto_id);
1347 UASSERT(readU16(&p1.data[4]) == peer_id);
1348 UASSERT(readU8(&p1.data[6]) == channel);
1349 UASSERT(readU8(&p1.data[7]) == data1[0]);
1351 //infostream<<"initial data1[0]="<<((u32)data1[0]&0xff)<<std::endl;
1353 SharedBuffer<u8> p2 = con::makeReliablePacket(data1, seqnum);
1355 /*infostream<<"p2.getSize()="<<p2.getSize()<<", data1.getSize()="
1356 <<data1.getSize()<<std::endl;
1357 infostream<<"readU8(&p2[3])="<<readU8(&p2[3])
1358 <<" p2[3]="<<((u32)p2[3]&0xff)<<std::endl;
1359 infostream<<"data1[0]="<<((u32)data1[0]&0xff)<<std::endl;*/
1361 UASSERT(p2.getSize() == 3 + data1.getSize());
1362 UASSERT(readU8(&p2[0]) == TYPE_RELIABLE);
1363 UASSERT(readU16(&p2[1]) == seqnum);
1364 UASSERT(readU8(&p2[3]) == data1[0]);
1367 struct Handler : public con::PeerHandler
1369 Handler(const char *a_name)
1375 void peerAdded(con::Peer *peer)
1377 infostream<<"Handler("<<name<<")::peerAdded(): "
1378 "id="<<peer->id<<std::endl;
1382 void deletingPeer(con::Peer *peer, bool timeout)
1384 infostream<<"Handler("<<name<<")::deletingPeer(): "
1386 <<", timeout="<<timeout<<std::endl;
1398 DSTACK("TestConnection::Run");
1403 Test some real connections
1405 NOTE: This mostly tests the legacy interface.
1408 u32 proto_id = 0xad26846a;
1410 Handler hand_server("server");
1411 Handler hand_client("client");
1413 infostream<<"** Creating server Connection"<<std::endl;
1414 con::Connection server(proto_id, 512, 5.0, &hand_server);
1415 server.Serve(30001);
1417 infostream<<"** Creating client Connection"<<std::endl;
1418 con::Connection client(proto_id, 512, 5.0, &hand_client);
1420 UASSERT(hand_server.count == 0);
1421 UASSERT(hand_client.count == 0);
1425 Address server_address(127,0,0,1, 30001);
1426 infostream<<"** running client.Connect()"<<std::endl;
1427 client.Connect(server_address);
1431 // Client should not have added client yet
1432 UASSERT(hand_client.count == 0);
1437 SharedBuffer<u8> data;
1438 infostream<<"** running client.Receive()"<<std::endl;
1439 u32 size = client.Receive(peer_id, data);
1440 infostream<<"** Client received: peer_id="<<peer_id
1444 catch(con::NoIncomingDataException &e)
1448 // Client should have added server now
1449 UASSERT(hand_client.count == 1);
1450 UASSERT(hand_client.last_id == 1);
1451 // Server should not have added client yet
1452 UASSERT(hand_server.count == 0);
1459 SharedBuffer<u8> data;
1460 infostream<<"** running server.Receive()"<<std::endl;
1461 u32 size = server.Receive(peer_id, data);
1462 infostream<<"** Server received: peer_id="<<peer_id
1466 catch(con::NoIncomingDataException &e)
1468 // No actual data received, but the client has
1469 // probably been connected
1472 // Client should be the same
1473 UASSERT(hand_client.count == 1);
1474 UASSERT(hand_client.last_id == 1);
1475 // Server should have the client
1476 UASSERT(hand_server.count == 1);
1477 UASSERT(hand_server.last_id == 2);
1481 while(client.Connected() == false)
1486 SharedBuffer<u8> data;
1487 infostream<<"** running client.Receive()"<<std::endl;
1488 u32 size = client.Receive(peer_id, data);
1489 infostream<<"** Client received: peer_id="<<peer_id
1493 catch(con::NoIncomingDataException &e)
1504 SharedBuffer<u8> data;
1505 infostream<<"** running server.Receive()"<<std::endl;
1506 u32 size = server.Receive(peer_id, data);
1507 infostream<<"** Server received: peer_id="<<peer_id
1511 catch(con::NoIncomingDataException &e)
1516 Simple send-receive test
1519 /*u8 data[] = "Hello World!";
1520 u32 datasize = sizeof(data);*/
1521 SharedBuffer<u8> data = SharedBufferFromString("Hello World!");
1523 infostream<<"** running client.Send()"<<std::endl;
1524 client.Send(PEER_ID_SERVER, 0, data, true);
1529 SharedBuffer<u8> recvdata;
1530 infostream<<"** running server.Receive()"<<std::endl;
1531 u32 size = server.Receive(peer_id, recvdata);
1532 infostream<<"** Server received: peer_id="<<peer_id
1536 UASSERT(memcmp(*data, *recvdata, data.getSize()) == 0);
1539 u16 peer_id_client = 2;
1542 Send consequent packets in different order
1543 Not compatible with new Connection, thus commented out.
1546 //u8 data1[] = "hello1";
1547 //u8 data2[] = "hello2";
1548 SharedBuffer<u8> data1 = SharedBufferFromString("hello1");
1549 SharedBuffer<u8> data2 = SharedBufferFromString("Hello2");
1551 Address client_address =
1552 server.GetPeerAddress(peer_id_client);
1554 infostream<<"*** Sending packets in wrong order (2,1,2)"
1558 con::Channel *ch = &server.getPeer(peer_id_client)->channels[chn];
1559 u16 sn = ch->next_outgoing_seqnum;
1560 ch->next_outgoing_seqnum = sn+1;
1561 server.Send(peer_id_client, chn, data2, true);
1562 ch->next_outgoing_seqnum = sn;
1563 server.Send(peer_id_client, chn, data1, true);
1564 ch->next_outgoing_seqnum = sn+1;
1565 server.Send(peer_id_client, chn, data2, true);
1569 infostream<<"*** Receiving the packets"<<std::endl;
1572 SharedBuffer<u8> recvdata;
1575 infostream<<"** running client.Receive()"<<std::endl;
1577 size = client.Receive(peer_id, recvdata);
1578 infostream<<"** Client received: peer_id="<<peer_id
1580 <<", data="<<*recvdata
1582 UASSERT(size == data1.getSize());
1583 UASSERT(memcmp(*data1, *recvdata, data1.getSize()) == 0);
1584 UASSERT(peer_id == PEER_ID_SERVER);
1586 infostream<<"** running client.Receive()"<<std::endl;
1588 size = client.Receive(peer_id, recvdata);
1589 infostream<<"** Client received: peer_id="<<peer_id
1591 <<", data="<<*recvdata
1593 UASSERT(size == data2.getSize());
1594 UASSERT(memcmp(*data2, *recvdata, data2.getSize()) == 0);
1595 UASSERT(peer_id == PEER_ID_SERVER);
1597 bool got_exception = false;
1600 infostream<<"** running client.Receive()"<<std::endl;
1602 size = client.Receive(peer_id, recvdata);
1603 infostream<<"** Client received: peer_id="<<peer_id
1605 <<", data="<<*recvdata
1608 catch(con::NoIncomingDataException &e)
1610 infostream<<"** No incoming data for client"<<std::endl;
1611 got_exception = true;
1613 UASSERT(got_exception);
1618 Send large amounts of packets (infinite test)
1619 Commented out because of infinity.
1622 infostream<<"Sending large amounts of packets (infinite test)"<<std::endl;
1625 int datasize = myrand_range(0,5)==0?myrand_range(100,10000):myrand_range(0,100);
1626 infostream<<"datasize="<<datasize<<std::endl;
1627 SharedBuffer<u8> data1(datasize);
1628 for(u16 i=0; i<datasize; i++)
1631 int sendtimes = myrand_range(1,10);
1632 for(int i=0; i<sendtimes; i++){
1633 server.Send(peer_id_client, 0, data1, true);
1636 infostream<<"sendcount="<<sendcount<<std::endl;
1638 //int receivetimes = myrand_range(1,20);
1639 int receivetimes = 20;
1640 for(int i=0; i<receivetimes; i++){
1641 SharedBuffer<u8> recvdata;
1644 bool received = false;
1646 size = client.Receive(peer_id, recvdata);
1648 }catch(con::NoIncomingDataException &e){
1658 const int datasize = 30000;
1659 SharedBuffer<u8> data1(datasize);
1660 for(u16 i=0; i<datasize; i++){
1664 infostream<<"Sending data (size="<<datasize<<"):";
1665 for(int i=0; i<datasize && i<20; i++){
1666 if(i%2==0) infostream<<" ";
1668 snprintf(buf, 10, "%.2X", ((int)((const char*)*data1)[i])&0xff);
1673 infostream<<std::endl;
1675 server.Send(peer_id_client, 0, data1, true);
1679 SharedBuffer<u8> recvdata;
1680 infostream<<"** running client.Receive()"<<std::endl;
1683 bool received = false;
1684 u32 timems0 = porting::getTimeMs();
1686 if(porting::getTimeMs() - timems0 > 5000 || received)
1689 size = client.Receive(peer_id, recvdata);
1691 }catch(con::NoIncomingDataException &e){
1696 infostream<<"** Client received: peer_id="<<peer_id
1700 infostream<<"Received data (size="<<size<<"): ";
1701 for(int i=0; i<size && i<20; i++){
1702 if(i%2==0) infostream<<" ";
1704 snprintf(buf, 10, "%.2X", ((int)(recvdata[i]))&0xff);
1709 infostream<<std::endl;
1711 UASSERT(memcmp(*data1, *recvdata, data1.getSize()) == 0);
1712 UASSERT(peer_id == PEER_ID_SERVER);
1715 // Check peer handlers
1716 UASSERT(hand_client.count == 1);
1717 UASSERT(hand_client.last_id == 1);
1718 UASSERT(hand_server.count == 1);
1719 UASSERT(hand_server.last_id == 2);
1728 infostream<<"Running " #X <<std::endl;\
1731 tests_failed += x.test_failed ? 1 : 0;\
1734 #define TESTPARAMS(X, ...)\
1737 infostream<<"Running " #X <<std::endl;\
1738 x.Run(__VA_ARGS__);\
1740 tests_failed += x.test_failed ? 1 : 0;\
1745 DSTACK(__FUNCTION_NAME);
1748 int tests_failed = 0;
1750 // Create item and node definitions
1751 IWritableItemDefManager *idef = createItemDefManager();
1752 IWritableNodeDefManager *ndef = createNodeDefManager();
1753 define_some_nodes(idef, ndef);
1755 infostream<<"run_tests() started"<<std::endl;
1756 TEST(TestUtilities);
1759 TEST(TestSerialization);
1760 TEST(TestNodedefSerialization);
1761 TESTPARAMS(TestMapNode, ndef);
1762 TESTPARAMS(TestVoxelManipulator, ndef);
1763 TESTPARAMS(TestVoxelAlgorithms, ndef);
1764 TESTPARAMS(TestInventory, idef);
1765 //TEST(TestMapBlock);
1766 //TEST(TestMapSector);
1767 TEST(TestCollision);
1768 if(INTERNET_SIMULATOR == false){
1770 dout_con<<"=== BEGIN RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
1771 TEST(TestConnection);
1772 dout_con<<"=== END RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
1774 if(tests_failed == 0){
1775 infostream<<"run_tests(): "<<tests_failed<<" / "<<tests_run<<" tests failed."<<std::endl;
1776 infostream<<"run_tests() passed."<<std::endl;
1779 errorstream<<"run_tests(): "<<tests_failed<<" / "<<tests_run<<" tests failed."<<std::endl;
1780 errorstream<<"run_tests() aborting."<<std::endl;