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"
43 #include "noise.h" // PseudoRandom used for random data for compression
46 Asserts that the exception occurs
48 #define EXCEPTION_CHECK(EType, code)\
50 bool exception_thrown = false;\
52 catch(EType &e) { exception_thrown = true; }\
53 UASSERT(exception_thrown);\
56 #define UTEST(x, fmt, ...)\
59 LOGLINEF(LMT_ERROR, "Test (%s) failed: " fmt, #x, ##__VA_ARGS__);\
64 #define UASSERT(x) UTEST(x, "UASSERT")
67 A few item and node definitions for those tests that need them
70 #define CONTENT_STONE 0
71 #define CONTENT_GRASS 0x800
72 #define CONTENT_TORCH 100
74 void define_some_nodes(IWritableItemDefManager *idef, IWritableNodeDefManager *ndef)
77 ItemDefinition itemdef;
84 itemdef = ItemDefinition();
85 itemdef.type = ITEM_NODE;
86 itemdef.name = "default:stone";
87 itemdef.description = "Stone";
88 itemdef.groups["cracky"] = 3;
89 itemdef.inventory_image = "[inventorycube"
93 f = ContentFeatures();
94 f.name = itemdef.name;
95 for(int i = 0; i < 6; i++)
96 f.tiledef[i].name = "default_stone.png";
97 f.is_ground_content = true;
98 idef->registerItem(itemdef);
105 itemdef = ItemDefinition();
106 itemdef.type = ITEM_NODE;
107 itemdef.name = "default:dirt_with_grass";
108 itemdef.description = "Dirt with grass";
109 itemdef.groups["crumbly"] = 3;
110 itemdef.inventory_image = "[inventorycube"
112 "{default_dirt.png&default_grass_side.png"
113 "{default_dirt.png&default_grass_side.png";
114 f = ContentFeatures();
115 f.name = itemdef.name;
116 f.tiledef[0].name = "default_grass.png";
117 f.tiledef[1].name = "default_dirt.png";
118 for(int i = 2; i < 6; i++)
119 f.tiledef[i].name = "default_dirt.png^default_grass_side.png";
120 f.is_ground_content = true;
121 idef->registerItem(itemdef);
125 Torch (minimal definition for lighting tests)
128 itemdef = ItemDefinition();
129 itemdef.type = ITEM_NODE;
130 itemdef.name = "default:torch";
131 f = ContentFeatures();
132 f.name = itemdef.name;
133 f.param_type = CPT_LIGHT;
134 f.light_propagates = true;
135 f.sunlight_propagates = true;
136 f.light_source = LIGHT_MAX-1;
137 idef->registerItem(itemdef);
149 struct TestUtilities: public TestBase
153 /*infostream<<"wrapDegrees(100.0) = "<<wrapDegrees(100.0)<<std::endl;
154 infostream<<"wrapDegrees(720.5) = "<<wrapDegrees(720.5)<<std::endl;
155 infostream<<"wrapDegrees(-0.5) = "<<wrapDegrees(-0.5)<<std::endl;*/
156 UASSERT(fabs(wrapDegrees(100.0) - 100.0) < 0.001);
157 UASSERT(fabs(wrapDegrees(720.5) - 0.5) < 0.001);
158 UASSERT(fabs(wrapDegrees(-0.5) - (-0.5)) < 0.001);
159 UASSERT(fabs(wrapDegrees(-365.5) - (-5.5)) < 0.001);
160 UASSERT(lowercase("Foo bAR") == "foo bar");
161 UASSERT(is_yes("YeS") == true);
162 UASSERT(is_yes("") == false);
163 UASSERT(is_yes("FAlse") == false);
164 const char *ends[] = {"abc", "c", "bc", NULL};
165 UASSERT(removeStringEnd("abc", ends) == "");
166 UASSERT(removeStringEnd("bc", ends) == "b");
167 UASSERT(removeStringEnd("12c", ends) == "12");
168 UASSERT(removeStringEnd("foo", ends) == "");
172 struct TestSettings: public TestBase
177 // Test reading of settings
178 s.parseConfigLine("leet = 1337");
179 s.parseConfigLine("leetleet = 13371337");
180 s.parseConfigLine("leetleet_neg = -13371337");
181 s.parseConfigLine("floaty_thing = 1.1");
182 s.parseConfigLine("stringy_thing = asd /( ¤%&(/\" BLÖÄRP");
183 s.parseConfigLine("coord = (1, 2, 4.5)");
184 UASSERT(s.getS32("leet") == 1337);
185 UASSERT(s.getS16("leetleet") == 32767);
186 UASSERT(s.getS16("leetleet_neg") == -32768);
187 // Not sure if 1.1 is an exact value as a float, but doesn't matter
188 UASSERT(fabs(s.getFloat("floaty_thing") - 1.1) < 0.001);
189 UASSERT(s.get("stringy_thing") == "asd /( ¤%&(/\" BLÖÄRP");
190 UASSERT(fabs(s.getV3F("coord").X - 1.0) < 0.001);
191 UASSERT(fabs(s.getV3F("coord").Y - 2.0) < 0.001);
192 UASSERT(fabs(s.getV3F("coord").Z - 4.5) < 0.001);
193 // Test the setting of settings too
194 s.setFloat("floaty_thing_2", 1.2);
195 s.setV3F("coord2", v3f(1, 2, 3.3));
196 UASSERT(s.get("floaty_thing_2").substr(0,3) == "1.2");
197 UASSERT(fabs(s.getFloat("floaty_thing_2") - 1.2) < 0.001);
198 UASSERT(fabs(s.getV3F("coord2").X - 1.0) < 0.001);
199 UASSERT(fabs(s.getV3F("coord2").Y - 2.0) < 0.001);
200 UASSERT(fabs(s.getV3F("coord2").Z - 3.3) < 0.001);
204 struct TestSerialization: public TestBase
206 // To be used like this:
207 // mkstr("Some\0string\0with\0embedded\0nuls")
208 // since std::string("...") doesn't work as expected in that case.
209 template<size_t N> std::string mkstr(const char (&s)[N])
211 return std::string(s, N - 1);
216 // Tests some serialization primitives
218 UASSERT(serializeString("") == mkstr("\0\0"));
219 UASSERT(serializeWideString(L"") == mkstr("\0\0"));
220 UASSERT(serializeLongString("") == mkstr("\0\0\0\0"));
221 UASSERT(serializeJsonString("") == "\"\"");
223 std::string teststring = "Hello world!";
224 UASSERT(serializeString(teststring) ==
225 mkstr("\0\14Hello world!"));
226 UASSERT(serializeWideString(narrow_to_wide(teststring)) ==
227 mkstr("\0\14\0H\0e\0l\0l\0o\0 \0w\0o\0r\0l\0d\0!"));
228 UASSERT(serializeLongString(teststring) ==
229 mkstr("\0\0\0\14Hello world!"));
230 UASSERT(serializeJsonString(teststring) ==
233 std::string teststring2;
234 std::wstring teststring2_w;
235 std::string teststring2_w_encoded;
237 std::ostringstream tmp_os;
238 std::wostringstream tmp_os_w;
239 std::ostringstream tmp_os_w_encoded;
240 for(int i = 0; i < 256; i++)
243 tmp_os_w<<(wchar_t)i;
244 tmp_os_w_encoded<<(char)0<<(char)i;
246 teststring2 = tmp_os.str();
247 teststring2_w = tmp_os_w.str();
248 teststring2_w_encoded = tmp_os_w_encoded.str();
250 UASSERT(serializeString(teststring2) ==
251 mkstr("\1\0") + teststring2);
252 UASSERT(serializeWideString(teststring2_w) ==
253 mkstr("\1\0") + teststring2_w_encoded);
254 UASSERT(serializeLongString(teststring2) ==
255 mkstr("\0\0\1\0") + teststring2);
256 // MSVC fails when directly using "\\\\"
257 std::string backslash = "\\";
258 UASSERT(serializeJsonString(teststring2) ==
260 "\\u0000\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007" +
261 "\\b\\t\\n\\u000b\\f\\r\\u000e\\u000f" +
262 "\\u0010\\u0011\\u0012\\u0013\\u0014\\u0015\\u0016\\u0017" +
263 "\\u0018\\u0019\\u001a\\u001b\\u001c\\u001d\\u001e\\u001f" +
264 " !\\\"" + teststring2.substr(0x23, 0x2f-0x23) +
265 "\\/" + teststring2.substr(0x30, 0x5c-0x30) +
266 backslash + backslash + teststring2.substr(0x5d, 0x7f-0x5d) + "\\u007f" +
267 "\\u0080\\u0081\\u0082\\u0083\\u0084\\u0085\\u0086\\u0087" +
268 "\\u0088\\u0089\\u008a\\u008b\\u008c\\u008d\\u008e\\u008f" +
269 "\\u0090\\u0091\\u0092\\u0093\\u0094\\u0095\\u0096\\u0097" +
270 "\\u0098\\u0099\\u009a\\u009b\\u009c\\u009d\\u009e\\u009f" +
271 "\\u00a0\\u00a1\\u00a2\\u00a3\\u00a4\\u00a5\\u00a6\\u00a7" +
272 "\\u00a8\\u00a9\\u00aa\\u00ab\\u00ac\\u00ad\\u00ae\\u00af" +
273 "\\u00b0\\u00b1\\u00b2\\u00b3\\u00b4\\u00b5\\u00b6\\u00b7" +
274 "\\u00b8\\u00b9\\u00ba\\u00bb\\u00bc\\u00bd\\u00be\\u00bf" +
275 "\\u00c0\\u00c1\\u00c2\\u00c3\\u00c4\\u00c5\\u00c6\\u00c7" +
276 "\\u00c8\\u00c9\\u00ca\\u00cb\\u00cc\\u00cd\\u00ce\\u00cf" +
277 "\\u00d0\\u00d1\\u00d2\\u00d3\\u00d4\\u00d5\\u00d6\\u00d7" +
278 "\\u00d8\\u00d9\\u00da\\u00db\\u00dc\\u00dd\\u00de\\u00df" +
279 "\\u00e0\\u00e1\\u00e2\\u00e3\\u00e4\\u00e5\\u00e6\\u00e7" +
280 "\\u00e8\\u00e9\\u00ea\\u00eb\\u00ec\\u00ed\\u00ee\\u00ef" +
281 "\\u00f0\\u00f1\\u00f2\\u00f3\\u00f4\\u00f5\\u00f6\\u00f7" +
282 "\\u00f8\\u00f9\\u00fa\\u00fb\\u00fc\\u00fd\\u00fe\\u00ff" +
286 std::istringstream is(serializeString(teststring2), std::ios::binary);
287 UASSERT(deSerializeString(is) == teststring2);
293 std::istringstream is(serializeWideString(teststring2_w), std::ios::binary);
294 UASSERT(deSerializeWideString(is) == teststring2_w);
300 std::istringstream is(serializeLongString(teststring2), std::ios::binary);
301 UASSERT(deSerializeLongString(is) == teststring2);
307 std::istringstream is(serializeJsonString(teststring2), std::ios::binary);
308 //dstream<<serializeJsonString(deSerializeJsonString(is));
309 UASSERT(deSerializeJsonString(is) == teststring2);
317 struct TestNodedefSerialization: public TestBase
322 f.name = "default:stone";
323 for(int i = 0; i < 6; i++)
324 f.tiledef[i].name = "default_stone.png";
325 f.is_ground_content = true;
326 std::ostringstream os(std::ios::binary);
328 verbosestream<<"Test ContentFeatures size: "<<os.str().size()<<std::endl;
329 std::istringstream is(os.str(), std::ios::binary);
332 UASSERT(f.walkable == f2.walkable);
333 UASSERT(f.node_box.type == f2.node_box.type);
337 struct TestCompress: public TestBase
343 SharedBuffer<u8> fromdata(4);
349 std::ostringstream os(std::ios_base::binary);
350 compress(fromdata, os, 0);
352 std::string str_out = os.str();
354 infostream<<"str_out.size()="<<str_out.size()<<std::endl;
355 infostream<<"TestCompress: 1,5,5,1 -> ";
356 for(u32 i=0; i<str_out.size(); i++)
358 infostream<<(u32)str_out[i]<<",";
360 infostream<<std::endl;
362 UASSERT(str_out.size() == 10);
364 UASSERT(str_out[0] == 0);
365 UASSERT(str_out[1] == 0);
366 UASSERT(str_out[2] == 0);
367 UASSERT(str_out[3] == 4);
368 UASSERT(str_out[4] == 0);
369 UASSERT(str_out[5] == 1);
370 UASSERT(str_out[6] == 1);
371 UASSERT(str_out[7] == 5);
372 UASSERT(str_out[8] == 0);
373 UASSERT(str_out[9] == 1);
375 std::istringstream is(str_out, std::ios_base::binary);
376 std::ostringstream os2(std::ios_base::binary);
378 decompress(is, os2, 0);
379 std::string str_out2 = os2.str();
381 infostream<<"decompress: ";
382 for(u32 i=0; i<str_out2.size(); i++)
384 infostream<<(u32)str_out2[i]<<",";
386 infostream<<std::endl;
388 UASSERT(str_out2.size() == fromdata.getSize());
390 for(u32 i=0; i<str_out2.size(); i++)
392 UASSERT(str_out2[i] == fromdata[i]);
399 SharedBuffer<u8> fromdata(4);
405 std::ostringstream os(std::ios_base::binary);
406 compress(fromdata, os, SER_FMT_VER_HIGHEST);
408 std::string str_out = os.str();
410 infostream<<"str_out.size()="<<str_out.size()<<std::endl;
411 infostream<<"TestCompress: 1,5,5,1 -> ";
412 for(u32 i=0; i<str_out.size(); i++)
414 infostream<<(u32)str_out[i]<<",";
416 infostream<<std::endl;
418 std::istringstream is(str_out, std::ios_base::binary);
419 std::ostringstream os2(std::ios_base::binary);
421 decompress(is, os2, SER_FMT_VER_HIGHEST);
422 std::string str_out2 = os2.str();
424 infostream<<"decompress: ";
425 for(u32 i=0; i<str_out2.size(); i++)
427 infostream<<(u32)str_out2[i]<<",";
429 infostream<<std::endl;
431 UASSERT(str_out2.size() == fromdata.getSize());
433 for(u32 i=0; i<str_out2.size(); i++)
435 UASSERT(str_out2[i] == fromdata[i]);
440 // Test zlib wrapper with large amounts of data (larger than its
443 infostream<<"Test: Testing zlib wrappers with a large amount "
444 <<"of pseudorandom data"<<std::endl;
446 infostream<<"Test: Input size of large compressZlib is "
449 data_in.resize(size);
450 PseudoRandom pseudorandom(9420);
451 for(u32 i=0; i<size; i++)
452 data_in[i] = pseudorandom.range(0,255);
453 std::ostringstream os_compressed(std::ios::binary);
454 compressZlib(data_in, os_compressed);
455 infostream<<"Test: Output size of large compressZlib is "
456 <<os_compressed.str().size()<<std::endl;
457 std::istringstream is_compressed(os_compressed.str(), std::ios::binary);
458 std::ostringstream os_decompressed(std::ios::binary);
459 decompressZlib(is_compressed, os_decompressed);
460 infostream<<"Test: Output size of large decompressZlib is "
461 <<os_decompressed.str().size()<<std::endl;
462 std::string str_decompressed = os_decompressed.str();
463 UTEST(str_decompressed.size() == data_in.size(), "Output size not"
464 " equal (output: %i, input: %i)",
465 str_decompressed.size(), data_in.size());
466 for(u32 i=0; i<size && i<str_decompressed.size(); i++){
467 UTEST(str_decompressed[i] == data_in[i],
468 "index out[%i]=%i differs from in[%i]=%i",
469 i, str_decompressed[i], i, data_in[i]);
475 struct TestMapNode: public TestBase
477 void Run(INodeDefManager *nodedef)
482 UASSERT(n.getContent() == CONTENT_AIR);
483 UASSERT(n.getLight(LIGHTBANK_DAY, nodedef) == 0);
484 UASSERT(n.getLight(LIGHTBANK_NIGHT, nodedef) == 0);
487 n.setContent(CONTENT_AIR);
488 UASSERT(nodedef->get(n).light_propagates == true);
489 n.setContent(LEGN(nodedef, "CONTENT_STONE"));
490 UASSERT(nodedef->get(n).light_propagates == false);
494 struct TestVoxelManipulator: public TestBase
496 void Run(INodeDefManager *nodedef)
502 VoxelArea a(v3s16(-1,-1,-1), v3s16(1,1,1));
503 UASSERT(a.index(0,0,0) == 1*3*3 + 1*3 + 1);
504 UASSERT(a.index(-1,-1,-1) == 0);
506 VoxelArea c(v3s16(-2,-2,-2), v3s16(2,2,2));
507 // An area that is 1 bigger in x+ and z-
508 VoxelArea d(v3s16(-2,-2,-3), v3s16(3,2,2));
510 core::list<VoxelArea> aa;
514 core::array<VoxelArea> results;
515 results.push_back(VoxelArea(v3s16(-2,-2,-3),v3s16(3,2,-3)));
516 results.push_back(VoxelArea(v3s16(3,-2,-2),v3s16(3,2,2)));
518 UASSERT(aa.size() == results.size());
520 infostream<<"Result of diff:"<<std::endl;
521 for(core::list<VoxelArea>::Iterator
522 i = aa.begin(); i != aa.end(); i++)
524 i->print(infostream);
525 infostream<<std::endl;
527 s32 j = results.linear_search(*i);
539 v.print(infostream, nodedef);
541 infostream<<"*** Setting (-1,0,-1)=2 ***"<<std::endl;
543 v.setNodeNoRef(v3s16(-1,0,-1), MapNode(CONTENT_GRASS));
545 v.print(infostream, nodedef);
547 UASSERT(v.getNode(v3s16(-1,0,-1)).getContent() == CONTENT_GRASS);
549 infostream<<"*** Reading from inexistent (0,0,-1) ***"<<std::endl;
551 EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,0,-1)));
553 v.print(infostream, nodedef);
555 infostream<<"*** Adding area ***"<<std::endl;
559 v.print(infostream, nodedef);
561 UASSERT(v.getNode(v3s16(-1,0,-1)).getContent() == CONTENT_GRASS);
562 EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,1,1)));
566 struct TestVoxelAlgorithms: public TestBase
568 void Run(INodeDefManager *ndef)
571 voxalgo::propagateSunlight
575 for(u16 z=0; z<3; z++)
576 for(u16 y=0; y<3; y++)
577 for(u16 x=0; x<3; x++)
580 v.setNodeNoRef(p, MapNode(CONTENT_AIR));
582 VoxelArea a(v3s16(0,0,0), v3s16(2,2,2));
584 core::map<v3s16, bool> light_sources;
585 voxalgo::setLight(v, a, 0, ndef);
586 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
587 v, a, true, light_sources, ndef);
588 //v.print(dstream, ndef, VOXELPRINT_LIGHT_DAY);
589 UASSERT(res.bottom_sunlight_valid == true);
590 UASSERT(v.getNode(v3s16(1,1,1)).getLight(LIGHTBANK_DAY, ndef)
593 v.setNodeNoRef(v3s16(0,0,0), MapNode(CONTENT_STONE));
595 core::map<v3s16, bool> light_sources;
596 voxalgo::setLight(v, a, 0, ndef);
597 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
598 v, a, true, light_sources, ndef);
599 UASSERT(res.bottom_sunlight_valid == true);
600 UASSERT(v.getNode(v3s16(1,1,1)).getLight(LIGHTBANK_DAY, ndef)
604 core::map<v3s16, bool> light_sources;
605 voxalgo::setLight(v, a, 0, ndef);
606 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
607 v, a, false, light_sources, ndef);
608 UASSERT(res.bottom_sunlight_valid == true);
609 UASSERT(v.getNode(v3s16(2,0,2)).getLight(LIGHTBANK_DAY, ndef)
612 v.setNodeNoRef(v3s16(1,3,2), MapNode(CONTENT_STONE));
614 core::map<v3s16, bool> light_sources;
615 voxalgo::setLight(v, a, 0, ndef);
616 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
617 v, a, true, light_sources, ndef);
618 UASSERT(res.bottom_sunlight_valid == true);
619 UASSERT(v.getNode(v3s16(1,1,2)).getLight(LIGHTBANK_DAY, ndef)
623 core::map<v3s16, bool> light_sources;
624 voxalgo::setLight(v, a, 0, ndef);
625 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
626 v, a, false, light_sources, ndef);
627 UASSERT(res.bottom_sunlight_valid == true);
628 UASSERT(v.getNode(v3s16(1,0,2)).getLight(LIGHTBANK_DAY, ndef)
632 MapNode n(CONTENT_AIR);
633 n.setLight(LIGHTBANK_DAY, 10, ndef);
634 v.setNodeNoRef(v3s16(1,-1,2), n);
637 core::map<v3s16, bool> light_sources;
638 voxalgo::setLight(v, a, 0, ndef);
639 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
640 v, a, true, light_sources, ndef);
641 UASSERT(res.bottom_sunlight_valid == true);
644 core::map<v3s16, bool> light_sources;
645 voxalgo::setLight(v, a, 0, ndef);
646 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
647 v, a, false, light_sources, ndef);
648 UASSERT(res.bottom_sunlight_valid == true);
651 MapNode n(CONTENT_AIR);
652 n.setLight(LIGHTBANK_DAY, LIGHT_SUN, ndef);
653 v.setNodeNoRef(v3s16(1,-1,2), n);
656 core::map<v3s16, bool> light_sources;
657 voxalgo::setLight(v, a, 0, ndef);
658 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
659 v, a, true, light_sources, ndef);
660 UASSERT(res.bottom_sunlight_valid == false);
663 core::map<v3s16, bool> light_sources;
664 voxalgo::setLight(v, a, 0, ndef);
665 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
666 v, a, false, light_sources, ndef);
667 UASSERT(res.bottom_sunlight_valid == false);
669 v.setNodeNoRef(v3s16(1,3,2), MapNode(CONTENT_IGNORE));
671 core::map<v3s16, bool> light_sources;
672 voxalgo::setLight(v, a, 0, ndef);
673 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
674 v, a, true, light_sources, ndef);
675 UASSERT(res.bottom_sunlight_valid == true);
679 voxalgo::clearLightAndCollectSources
683 for(u16 z=0; z<3; z++)
684 for(u16 y=0; y<3; y++)
685 for(u16 x=0; x<3; x++)
688 v.setNode(p, MapNode(CONTENT_AIR));
690 VoxelArea a(v3s16(0,0,0), v3s16(2,2,2));
691 v.setNodeNoRef(v3s16(0,0,0), MapNode(CONTENT_STONE));
692 v.setNodeNoRef(v3s16(1,1,1), MapNode(CONTENT_TORCH));
694 MapNode n(CONTENT_AIR);
695 n.setLight(LIGHTBANK_DAY, 1, ndef);
696 v.setNode(v3s16(1,1,2), n);
699 core::map<v3s16, bool> light_sources;
700 core::map<v3s16, u8> unlight_from;
701 voxalgo::clearLightAndCollectSources(v, a, LIGHTBANK_DAY,
702 ndef, light_sources, unlight_from);
703 //v.print(dstream, ndef, VOXELPRINT_LIGHT_DAY);
704 UASSERT(v.getNode(v3s16(0,1,1)).getLight(LIGHTBANK_DAY, ndef)
706 UASSERT(light_sources.find(v3s16(1,1,1)) != NULL);
707 UASSERT(light_sources.size() == 1);
708 UASSERT(unlight_from.find(v3s16(1,1,2)) != NULL);
709 UASSERT(unlight_from.size() == 1);
715 struct TestInventory: public TestBase
717 void Run(IItemDefManager *idef)
719 std::string serialized_inventory =
731 "Item default:cobble 61\n"
738 "Item default:dirt 71\n"
746 "Item default:dirt 99\n"
747 "Item default:cobble 38\n"
757 std::string serialized_inventory_2 =
769 "Item default:cobble 61\n"
776 "Item default:dirt 71\n"
784 "Item default:dirt 99\n"
785 "Item default:cobble 38\n"
796 std::istringstream is(serialized_inventory, std::ios::binary);
798 UASSERT(inv.getList("0"));
799 UASSERT(!inv.getList("main"));
800 inv.getList("0")->setName("main");
801 UASSERT(!inv.getList("0"));
802 UASSERT(inv.getList("main"));
803 UASSERT(inv.getList("main")->getWidth() == 3);
804 inv.getList("main")->setWidth(5);
805 std::ostringstream inv_os(std::ios::binary);
806 inv.serialize(inv_os);
807 UASSERT(inv_os.str() == serialized_inventory_2);
812 NOTE: These tests became non-working then NodeContainer was removed.
813 These should be redone, utilizing some kind of a virtual
814 interface for Map (IMap would be fine).
817 struct TestMapBlock: public TestBase
819 class TC : public NodeContainer
825 core::list<v3s16> validity_exceptions;
829 position_valid = true;
832 virtual bool isValidPosition(v3s16 p)
834 //return position_valid ^ (p==position_valid_exception);
835 bool exception = false;
836 for(core::list<v3s16>::Iterator i=validity_exceptions.begin();
837 i != validity_exceptions.end(); i++)
845 return exception ? !position_valid : position_valid;
848 virtual MapNode getNode(v3s16 p)
850 if(isValidPosition(p) == false)
851 throw InvalidPositionException();
855 virtual void setNode(v3s16 p, MapNode & n)
857 if(isValidPosition(p) == false)
858 throw InvalidPositionException();
861 virtual u16 nodeContainerId() const
871 MapBlock b(&parent, v3s16(1,1,1));
872 v3s16 relpos(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
874 UASSERT(b.getPosRelative() == relpos);
876 UASSERT(b.getBox().MinEdge.X == MAP_BLOCKSIZE);
877 UASSERT(b.getBox().MaxEdge.X == MAP_BLOCKSIZE*2-1);
878 UASSERT(b.getBox().MinEdge.Y == MAP_BLOCKSIZE);
879 UASSERT(b.getBox().MaxEdge.Y == MAP_BLOCKSIZE*2-1);
880 UASSERT(b.getBox().MinEdge.Z == MAP_BLOCKSIZE);
881 UASSERT(b.getBox().MaxEdge.Z == MAP_BLOCKSIZE*2-1);
883 UASSERT(b.isValidPosition(v3s16(0,0,0)) == true);
884 UASSERT(b.isValidPosition(v3s16(-1,0,0)) == false);
885 UASSERT(b.isValidPosition(v3s16(-1,-142,-2341)) == false);
886 UASSERT(b.isValidPosition(v3s16(-124,142,2341)) == false);
887 UASSERT(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
888 UASSERT(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE,MAP_BLOCKSIZE-1)) == false);
891 TODO: this method should probably be removed
892 if the block size isn't going to be set variable
894 /*UASSERT(b.getSizeNodes() == v3s16(MAP_BLOCKSIZE,
895 MAP_BLOCKSIZE, MAP_BLOCKSIZE));*/
897 // Changed flag should be initially set
898 UASSERT(b.getModified() == MOD_STATE_WRITE_NEEDED);
900 UASSERT(b.getModified() == MOD_STATE_CLEAN);
902 // All nodes should have been set to
903 // .d=CONTENT_IGNORE and .getLight() = 0
904 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
905 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
906 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
908 //UASSERT(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_AIR);
909 UASSERT(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_IGNORE);
910 UASSERT(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_DAY) == 0);
911 UASSERT(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_NIGHT) == 0);
915 MapNode n(CONTENT_AIR);
916 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
917 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
918 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
920 b.setNode(v3s16(x,y,z), n);
925 Parent fetch functions
927 parent.position_valid = false;
928 parent.node.setContent(5);
932 // Positions in the block should still be valid
933 UASSERT(b.isValidPositionParent(v3s16(0,0,0)) == true);
934 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
935 n = b.getNodeParent(v3s16(0,MAP_BLOCKSIZE-1,0));
936 UASSERT(n.getContent() == CONTENT_AIR);
938 // ...but outside the block they should be invalid
939 UASSERT(b.isValidPositionParent(v3s16(-121,2341,0)) == false);
940 UASSERT(b.isValidPositionParent(v3s16(-1,0,0)) == false);
941 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == false);
944 bool exception_thrown = false;
946 // This should throw an exception
947 MapNode n = b.getNodeParent(v3s16(0,0,-1));
949 catch(InvalidPositionException &e)
951 exception_thrown = true;
953 UASSERT(exception_thrown);
956 parent.position_valid = true;
957 // Now the positions outside should be valid
958 UASSERT(b.isValidPositionParent(v3s16(-121,2341,0)) == true);
959 UASSERT(b.isValidPositionParent(v3s16(-1,0,0)) == true);
960 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == true);
961 n = b.getNodeParent(v3s16(0,0,MAP_BLOCKSIZE));
962 UASSERT(n.getContent() == 5);
970 UASSERT(b.getNode(p).getContent() == 4);
971 //TODO: Update to new system
972 /*UASSERT(b.getNodeTile(p) == 4);
973 UASSERT(b.getNodeTile(v3s16(-1,-1,0)) == 5);*/
978 // Set lighting of all nodes to 0
979 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
980 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
981 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
982 MapNode n = b.getNode(v3s16(x,y,z));
983 n.setLight(LIGHTBANK_DAY, 0);
984 n.setLight(LIGHTBANK_NIGHT, 0);
985 b.setNode(v3s16(x,y,z), n);
991 Check how the block handles being a lonely sky block
993 parent.position_valid = true;
994 b.setIsUnderground(false);
995 parent.node.setContent(CONTENT_AIR);
996 parent.node.setLight(LIGHTBANK_DAY, LIGHT_SUN);
997 parent.node.setLight(LIGHTBANK_NIGHT, 0);
998 core::map<v3s16, bool> light_sources;
999 // The bottom block is invalid, because we have a shadowing node
1000 UASSERT(b.propagateSunlight(light_sources) == false);
1001 UASSERT(b.getNode(v3s16(1,4,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
1002 UASSERT(b.getNode(v3s16(1,3,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
1003 UASSERT(b.getNode(v3s16(1,2,0)).getLight(LIGHTBANK_DAY) == 0);
1004 UASSERT(b.getNode(v3s16(1,1,0)).getLight(LIGHTBANK_DAY) == 0);
1005 UASSERT(b.getNode(v3s16(1,0,0)).getLight(LIGHTBANK_DAY) == 0);
1006 UASSERT(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
1007 UASSERT(b.getFaceLight2(1000, p, v3s16(0,1,0)) == LIGHT_SUN);
1008 UASSERT(b.getFaceLight2(1000, p, v3s16(0,-1,0)) == 0);
1009 UASSERT(b.getFaceLight2(0, p, v3s16(0,-1,0)) == 0);
1010 // According to MapBlock::getFaceLight,
1011 // The face on the z+ side should have double-diminished light
1012 //UASSERT(b.getFaceLight(p, v3s16(0,0,1)) == diminish_light(diminish_light(LIGHT_MAX)));
1013 // The face on the z+ side should have diminished light
1014 UASSERT(b.getFaceLight2(1000, p, v3s16(0,0,1)) == diminish_light(LIGHT_MAX));
1017 Check how the block handles being in between blocks with some non-sunlight
1018 while being underground
1021 // Make neighbours to exist and set some non-sunlight to them
1022 parent.position_valid = true;
1023 b.setIsUnderground(true);
1024 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
1025 core::map<v3s16, bool> light_sources;
1026 // The block below should be valid because there shouldn't be
1027 // sunlight in there either
1028 UASSERT(b.propagateSunlight(light_sources, true) == true);
1029 // Should not touch nodes that are not affected (that is, all of them)
1030 //UASSERT(b.getNode(v3s16(1,2,3)).getLight() == LIGHT_SUN);
1031 // Should set light of non-sunlighted blocks to 0.
1032 UASSERT(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == 0);
1035 Set up a situation where:
1036 - There is only air in this block
1037 - There is a valid non-sunlighted block at the bottom, and
1038 - Invalid blocks elsewhere.
1039 - the block is not underground.
1041 This should result in bottom block invalidity
1044 b.setIsUnderground(false);
1046 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
1047 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
1048 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
1050 n.setContent(CONTENT_AIR);
1051 n.setLight(LIGHTBANK_DAY, 0);
1052 b.setNode(v3s16(x,y,z), n);
1056 // Make neighbours invalid
1057 parent.position_valid = false;
1058 // Add exceptions to the top of the bottom block
1059 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
1060 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
1062 parent.validity_exceptions.push_back(v3s16(MAP_BLOCKSIZE+x, MAP_BLOCKSIZE-1, MAP_BLOCKSIZE+z));
1064 // Lighting value for the valid nodes
1065 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
1066 core::map<v3s16, bool> light_sources;
1067 // Bottom block is not valid
1068 UASSERT(b.propagateSunlight(light_sources) == false);
1073 struct TestMapSector: public TestBase
1075 class TC : public NodeContainer
1080 bool position_valid;
1084 position_valid = true;
1087 virtual bool isValidPosition(v3s16 p)
1089 return position_valid;
1092 virtual MapNode getNode(v3s16 p)
1094 if(position_valid == false)
1095 throw InvalidPositionException();
1099 virtual void setNode(v3s16 p, MapNode & n)
1101 if(position_valid == false)
1102 throw InvalidPositionException();
1105 virtual u16 nodeContainerId() const
1114 parent.position_valid = false;
1116 // Create one with no heightmaps
1117 ServerMapSector sector(&parent, v2s16(1,1));
1119 UASSERT(sector.getBlockNoCreateNoEx(0) == 0);
1120 UASSERT(sector.getBlockNoCreateNoEx(1) == 0);
1122 MapBlock * bref = sector.createBlankBlock(-2);
1124 UASSERT(sector.getBlockNoCreateNoEx(0) == 0);
1125 UASSERT(sector.getBlockNoCreateNoEx(-2) == bref);
1127 //TODO: Check for AlreadyExistsException
1129 /*bool exception_thrown = false;
1133 catch(InvalidPositionException &e){
1134 exception_thrown = true;
1136 UASSERT(exception_thrown);*/
1142 struct TestCollision: public TestBase
1147 axisAlignedCollision
1150 for(s16 bx = -3; bx <= 3; bx++)
1151 for(s16 by = -3; by <= 3; by++)
1152 for(s16 bz = -3; bz <= 3; bz++)
1156 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1157 aabb3f m(bx-2, by, bz, bx-1, by+1, bz+1);
1160 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1161 UASSERT(fabs(dtime - 1.000) < 0.001);
1164 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1165 aabb3f m(bx-2, by, bz, bx-1, by+1, bz+1);
1168 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == -1);
1171 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1172 aabb3f m(bx-2, by+1.5, bz, bx-1, by+2.5, bz-1);
1175 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == -1);
1178 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1179 aabb3f m(bx-2, by-1.5, bz, bx-1.5, by+0.5, bz+1);
1182 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1183 UASSERT(fabs(dtime - 3.000) < 0.001);
1186 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1187 aabb3f m(bx-2, by-1.5, bz, bx-1.5, by+0.5, bz+1);
1190 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1191 UASSERT(fabs(dtime - 3.000) < 0.001);
1196 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1197 aabb3f m(bx+2, by, bz, bx+3, by+1, bz+1);
1200 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1201 UASSERT(fabs(dtime - 1.000) < 0.001);
1204 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1205 aabb3f m(bx+2, by, bz, bx+3, by+1, bz+1);
1208 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == -1);
1211 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1212 aabb3f m(bx+2, by, bz+1.5, bx+3, by+1, bz+3.5);
1215 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == -1);
1218 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1219 aabb3f m(bx+2, by-1.5, bz, bx+2.5, by-0.5, bz+1);
1220 v3f v(-0.5, 0.2, 0);
1222 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 1); // Y, not X!
1223 UASSERT(fabs(dtime - 2.500) < 0.001);
1226 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1227 aabb3f m(bx+2, by-1.5, bz, bx+2.5, by-0.5, bz+1);
1228 v3f v(-0.5, 0.3, 0);
1230 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1231 UASSERT(fabs(dtime - 2.000) < 0.001);
1234 // TODO: Y-, Y+, Z-, Z+
1238 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1239 aabb3f m(bx+2.3, by+2.29, bz+2.29, bx+4.2, by+4.2, bz+4.2);
1240 v3f v(-1./3, -1./3, -1./3);
1242 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1243 UASSERT(fabs(dtime - 0.9) < 0.001);
1246 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1247 aabb3f m(bx+2.29, by+2.3, bz+2.29, bx+4.2, by+4.2, bz+4.2);
1248 v3f v(-1./3, -1./3, -1./3);
1250 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 1);
1251 UASSERT(fabs(dtime - 0.9) < 0.001);
1254 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1255 aabb3f m(bx+2.29, by+2.29, bz+2.3, bx+4.2, by+4.2, bz+4.2);
1256 v3f v(-1./3, -1./3, -1./3);
1258 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 2);
1259 UASSERT(fabs(dtime - 0.9) < 0.001);
1262 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1263 aabb3f m(bx-4.2, by-4.2, bz-4.2, bx-2.3, by-2.29, bz-2.29);
1264 v3f v(1./7, 1./7, 1./7);
1266 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1267 UASSERT(fabs(dtime - 16.1) < 0.001);
1270 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1271 aabb3f m(bx-4.2, by-4.2, bz-4.2, bx-2.29, by-2.3, bz-2.29);
1272 v3f v(1./7, 1./7, 1./7);
1274 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 1);
1275 UASSERT(fabs(dtime - 16.1) < 0.001);
1278 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1279 aabb3f m(bx-4.2, by-4.2, bz-4.2, bx-2.29, by-2.29, bz-2.3);
1280 v3f v(1./7, 1./7, 1./7);
1282 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 2);
1283 UASSERT(fabs(dtime - 16.1) < 0.001);
1289 struct TestSocket: public TestBase
1293 const int port = 30003;
1297 const char sendbuffer[] = "hello world!";
1298 socket.Send(Address(127,0,0,1,port), sendbuffer, sizeof(sendbuffer));
1302 char rcvbuffer[256];
1303 memset(rcvbuffer, 0, sizeof(rcvbuffer));
1307 int bytes_read = socket.Receive(sender, rcvbuffer, sizeof(rcvbuffer));
1311 //FIXME: This fails on some systems
1312 UASSERT(strncmp(sendbuffer, rcvbuffer, sizeof(sendbuffer))==0);
1313 UASSERT(sender.getAddress() == Address(127,0,0,1, 0).getAddress());
1317 struct TestConnection: public TestBase
1322 Test helper functions
1325 // Some constants for testing
1326 u32 proto_id = 0x12345678;
1329 SharedBuffer<u8> data1(1);
1331 Address a(127,0,0,1, 10);
1334 con::BufferedPacket p1 = con::makePacket(a, data1,
1335 proto_id, peer_id, channel);
1337 We should now have a packet with this data:
1340 [4] u16 sender_peer_id
1345 UASSERT(readU32(&p1.data[0]) == proto_id);
1346 UASSERT(readU16(&p1.data[4]) == peer_id);
1347 UASSERT(readU8(&p1.data[6]) == channel);
1348 UASSERT(readU8(&p1.data[7]) == data1[0]);
1350 //infostream<<"initial data1[0]="<<((u32)data1[0]&0xff)<<std::endl;
1352 SharedBuffer<u8> p2 = con::makeReliablePacket(data1, seqnum);
1354 /*infostream<<"p2.getSize()="<<p2.getSize()<<", data1.getSize()="
1355 <<data1.getSize()<<std::endl;
1356 infostream<<"readU8(&p2[3])="<<readU8(&p2[3])
1357 <<" p2[3]="<<((u32)p2[3]&0xff)<<std::endl;
1358 infostream<<"data1[0]="<<((u32)data1[0]&0xff)<<std::endl;*/
1360 UASSERT(p2.getSize() == 3 + data1.getSize());
1361 UASSERT(readU8(&p2[0]) == TYPE_RELIABLE);
1362 UASSERT(readU16(&p2[1]) == seqnum);
1363 UASSERT(readU8(&p2[3]) == data1[0]);
1366 struct Handler : public con::PeerHandler
1368 Handler(const char *a_name)
1374 void peerAdded(con::Peer *peer)
1376 infostream<<"Handler("<<name<<")::peerAdded(): "
1377 "id="<<peer->id<<std::endl;
1381 void deletingPeer(con::Peer *peer, bool timeout)
1383 infostream<<"Handler("<<name<<")::deletingPeer(): "
1385 <<", timeout="<<timeout<<std::endl;
1397 DSTACK("TestConnection::Run");
1402 Test some real connections
1404 NOTE: This mostly tests the legacy interface.
1407 u32 proto_id = 0xad26846a;
1409 Handler hand_server("server");
1410 Handler hand_client("client");
1412 infostream<<"** Creating server Connection"<<std::endl;
1413 con::Connection server(proto_id, 512, 5.0, &hand_server);
1414 server.Serve(30001);
1416 infostream<<"** Creating client Connection"<<std::endl;
1417 con::Connection client(proto_id, 512, 5.0, &hand_client);
1419 UASSERT(hand_server.count == 0);
1420 UASSERT(hand_client.count == 0);
1424 Address server_address(127,0,0,1, 30001);
1425 infostream<<"** running client.Connect()"<<std::endl;
1426 client.Connect(server_address);
1430 // Client should not have added client yet
1431 UASSERT(hand_client.count == 0);
1436 SharedBuffer<u8> data;
1437 infostream<<"** running client.Receive()"<<std::endl;
1438 u32 size = client.Receive(peer_id, data);
1439 infostream<<"** Client received: peer_id="<<peer_id
1443 catch(con::NoIncomingDataException &e)
1447 // Client should have added server now
1448 UASSERT(hand_client.count == 1);
1449 UASSERT(hand_client.last_id == 1);
1450 // Server should not have added client yet
1451 UASSERT(hand_server.count == 0);
1458 SharedBuffer<u8> data;
1459 infostream<<"** running server.Receive()"<<std::endl;
1460 u32 size = server.Receive(peer_id, data);
1461 infostream<<"** Server received: peer_id="<<peer_id
1465 catch(con::NoIncomingDataException &e)
1467 // No actual data received, but the client has
1468 // probably been connected
1471 // Client should be the same
1472 UASSERT(hand_client.count == 1);
1473 UASSERT(hand_client.last_id == 1);
1474 // Server should have the client
1475 UASSERT(hand_server.count == 1);
1476 UASSERT(hand_server.last_id == 2);
1480 while(client.Connected() == false)
1485 SharedBuffer<u8> data;
1486 infostream<<"** running client.Receive()"<<std::endl;
1487 u32 size = client.Receive(peer_id, data);
1488 infostream<<"** Client received: peer_id="<<peer_id
1492 catch(con::NoIncomingDataException &e)
1503 SharedBuffer<u8> data;
1504 infostream<<"** running server.Receive()"<<std::endl;
1505 u32 size = server.Receive(peer_id, data);
1506 infostream<<"** Server received: peer_id="<<peer_id
1510 catch(con::NoIncomingDataException &e)
1515 Simple send-receive test
1518 /*u8 data[] = "Hello World!";
1519 u32 datasize = sizeof(data);*/
1520 SharedBuffer<u8> data = SharedBufferFromString("Hello World!");
1522 infostream<<"** running client.Send()"<<std::endl;
1523 client.Send(PEER_ID_SERVER, 0, data, true);
1528 SharedBuffer<u8> recvdata;
1529 infostream<<"** running server.Receive()"<<std::endl;
1530 u32 size = server.Receive(peer_id, recvdata);
1531 infostream<<"** Server received: peer_id="<<peer_id
1535 UASSERT(memcmp(*data, *recvdata, data.getSize()) == 0);
1538 u16 peer_id_client = 2;
1541 Send consequent packets in different order
1542 Not compatible with new Connection, thus commented out.
1545 //u8 data1[] = "hello1";
1546 //u8 data2[] = "hello2";
1547 SharedBuffer<u8> data1 = SharedBufferFromString("hello1");
1548 SharedBuffer<u8> data2 = SharedBufferFromString("Hello2");
1550 Address client_address =
1551 server.GetPeerAddress(peer_id_client);
1553 infostream<<"*** Sending packets in wrong order (2,1,2)"
1557 con::Channel *ch = &server.getPeer(peer_id_client)->channels[chn];
1558 u16 sn = ch->next_outgoing_seqnum;
1559 ch->next_outgoing_seqnum = sn+1;
1560 server.Send(peer_id_client, chn, data2, true);
1561 ch->next_outgoing_seqnum = sn;
1562 server.Send(peer_id_client, chn, data1, true);
1563 ch->next_outgoing_seqnum = sn+1;
1564 server.Send(peer_id_client, chn, data2, true);
1568 infostream<<"*** Receiving the packets"<<std::endl;
1571 SharedBuffer<u8> recvdata;
1574 infostream<<"** running client.Receive()"<<std::endl;
1576 size = client.Receive(peer_id, recvdata);
1577 infostream<<"** Client received: peer_id="<<peer_id
1579 <<", data="<<*recvdata
1581 UASSERT(size == data1.getSize());
1582 UASSERT(memcmp(*data1, *recvdata, data1.getSize()) == 0);
1583 UASSERT(peer_id == PEER_ID_SERVER);
1585 infostream<<"** running client.Receive()"<<std::endl;
1587 size = client.Receive(peer_id, recvdata);
1588 infostream<<"** Client received: peer_id="<<peer_id
1590 <<", data="<<*recvdata
1592 UASSERT(size == data2.getSize());
1593 UASSERT(memcmp(*data2, *recvdata, data2.getSize()) == 0);
1594 UASSERT(peer_id == PEER_ID_SERVER);
1596 bool got_exception = false;
1599 infostream<<"** running client.Receive()"<<std::endl;
1601 size = client.Receive(peer_id, recvdata);
1602 infostream<<"** Client received: peer_id="<<peer_id
1604 <<", data="<<*recvdata
1607 catch(con::NoIncomingDataException &e)
1609 infostream<<"** No incoming data for client"<<std::endl;
1610 got_exception = true;
1612 UASSERT(got_exception);
1617 Send large amounts of packets (infinite test)
1618 Commented out because of infinity.
1621 infostream<<"Sending large amounts of packets (infinite test)"<<std::endl;
1624 int datasize = myrand_range(0,5)==0?myrand_range(100,10000):myrand_range(0,100);
1625 infostream<<"datasize="<<datasize<<std::endl;
1626 SharedBuffer<u8> data1(datasize);
1627 for(u16 i=0; i<datasize; i++)
1630 int sendtimes = myrand_range(1,10);
1631 for(int i=0; i<sendtimes; i++){
1632 server.Send(peer_id_client, 0, data1, true);
1635 infostream<<"sendcount="<<sendcount<<std::endl;
1637 //int receivetimes = myrand_range(1,20);
1638 int receivetimes = 20;
1639 for(int i=0; i<receivetimes; i++){
1640 SharedBuffer<u8> recvdata;
1643 bool received = false;
1645 size = client.Receive(peer_id, recvdata);
1647 }catch(con::NoIncomingDataException &e){
1657 const int datasize = 30000;
1658 SharedBuffer<u8> data1(datasize);
1659 for(u16 i=0; i<datasize; i++){
1663 infostream<<"Sending data (size="<<datasize<<"):";
1664 for(int i=0; i<datasize && i<20; i++){
1665 if(i%2==0) infostream<<" ";
1667 snprintf(buf, 10, "%.2X", ((int)((const char*)*data1)[i])&0xff);
1672 infostream<<std::endl;
1674 server.Send(peer_id_client, 0, data1, true);
1678 SharedBuffer<u8> recvdata;
1679 infostream<<"** running client.Receive()"<<std::endl;
1682 bool received = false;
1683 u32 timems0 = porting::getTimeMs();
1685 if(porting::getTimeMs() - timems0 > 5000 || received)
1688 size = client.Receive(peer_id, recvdata);
1690 }catch(con::NoIncomingDataException &e){
1695 infostream<<"** Client received: peer_id="<<peer_id
1699 infostream<<"Received data (size="<<size<<"): ";
1700 for(int i=0; i<size && i<20; i++){
1701 if(i%2==0) infostream<<" ";
1703 snprintf(buf, 10, "%.2X", ((int)(recvdata[i]))&0xff);
1708 infostream<<std::endl;
1710 UASSERT(memcmp(*data1, *recvdata, data1.getSize()) == 0);
1711 UASSERT(peer_id == PEER_ID_SERVER);
1714 // Check peer handlers
1715 UASSERT(hand_client.count == 1);
1716 UASSERT(hand_client.last_id == 1);
1717 UASSERT(hand_server.count == 1);
1718 UASSERT(hand_server.last_id == 2);
1727 infostream<<"Running " #X <<std::endl;\
1730 tests_failed += x.test_failed ? 1 : 0;\
1733 #define TESTPARAMS(X, ...)\
1736 infostream<<"Running " #X <<std::endl;\
1737 x.Run(__VA_ARGS__);\
1739 tests_failed += x.test_failed ? 1 : 0;\
1744 DSTACK(__FUNCTION_NAME);
1747 int tests_failed = 0;
1749 // Create item and node definitions
1750 IWritableItemDefManager *idef = createItemDefManager();
1751 IWritableNodeDefManager *ndef = createNodeDefManager();
1752 define_some_nodes(idef, ndef);
1754 infostream<<"run_tests() started"<<std::endl;
1755 TEST(TestUtilities);
1758 TEST(TestSerialization);
1759 TEST(TestNodedefSerialization);
1760 TESTPARAMS(TestMapNode, ndef);
1761 TESTPARAMS(TestVoxelManipulator, ndef);
1762 TESTPARAMS(TestVoxelAlgorithms, ndef);
1763 TESTPARAMS(TestInventory, idef);
1764 //TEST(TestMapBlock);
1765 //TEST(TestMapSector);
1766 TEST(TestCollision);
1767 if(INTERNET_SIMULATOR == false){
1769 dout_con<<"=== BEGIN RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
1770 TEST(TestConnection);
1771 dout_con<<"=== END RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
1773 if(tests_failed == 0){
1774 infostream<<"run_tests(): "<<tests_failed<<" / "<<tests_run<<" tests failed."<<std::endl;
1775 infostream<<"run_tests() passed."<<std::endl;
1778 errorstream<<"run_tests(): "<<tests_failed<<" / "<<tests_run<<" tests failed."<<std::endl;
1779 errorstream<<"run_tests() aborting."<<std::endl;