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 TestCompress: public TestBase
323 SharedBuffer<u8> fromdata(4);
329 std::ostringstream os(std::ios_base::binary);
330 compress(fromdata, os, 0);
332 std::string str_out = os.str();
334 infostream<<"str_out.size()="<<str_out.size()<<std::endl;
335 infostream<<"TestCompress: 1,5,5,1 -> ";
336 for(u32 i=0; i<str_out.size(); i++)
338 infostream<<(u32)str_out[i]<<",";
340 infostream<<std::endl;
342 UASSERT(str_out.size() == 10);
344 UASSERT(str_out[0] == 0);
345 UASSERT(str_out[1] == 0);
346 UASSERT(str_out[2] == 0);
347 UASSERT(str_out[3] == 4);
348 UASSERT(str_out[4] == 0);
349 UASSERT(str_out[5] == 1);
350 UASSERT(str_out[6] == 1);
351 UASSERT(str_out[7] == 5);
352 UASSERT(str_out[8] == 0);
353 UASSERT(str_out[9] == 1);
355 std::istringstream is(str_out, std::ios_base::binary);
356 std::ostringstream os2(std::ios_base::binary);
358 decompress(is, os2, 0);
359 std::string str_out2 = os2.str();
361 infostream<<"decompress: ";
362 for(u32 i=0; i<str_out2.size(); i++)
364 infostream<<(u32)str_out2[i]<<",";
366 infostream<<std::endl;
368 UASSERT(str_out2.size() == fromdata.getSize());
370 for(u32 i=0; i<str_out2.size(); i++)
372 UASSERT(str_out2[i] == fromdata[i]);
379 SharedBuffer<u8> fromdata(4);
385 std::ostringstream os(std::ios_base::binary);
386 compress(fromdata, os, SER_FMT_VER_HIGHEST);
388 std::string str_out = os.str();
390 infostream<<"str_out.size()="<<str_out.size()<<std::endl;
391 infostream<<"TestCompress: 1,5,5,1 -> ";
392 for(u32 i=0; i<str_out.size(); i++)
394 infostream<<(u32)str_out[i]<<",";
396 infostream<<std::endl;
398 std::istringstream is(str_out, std::ios_base::binary);
399 std::ostringstream os2(std::ios_base::binary);
401 decompress(is, os2, SER_FMT_VER_HIGHEST);
402 std::string str_out2 = os2.str();
404 infostream<<"decompress: ";
405 for(u32 i=0; i<str_out2.size(); i++)
407 infostream<<(u32)str_out2[i]<<",";
409 infostream<<std::endl;
411 UASSERT(str_out2.size() == fromdata.getSize());
413 for(u32 i=0; i<str_out2.size(); i++)
415 UASSERT(str_out2[i] == fromdata[i]);
420 // Test zlib wrapper with large amounts of data (larger than its
423 infostream<<"Test: Testing zlib wrappers with a large amount "
424 <<"of pseudorandom data"<<std::endl;
426 infostream<<"Test: Input size of large compressZlib is "
429 data_in.resize(size);
430 PseudoRandom pseudorandom(9420);
431 for(u32 i=0; i<size; i++)
432 data_in[i] = pseudorandom.range(0,255);
433 std::ostringstream os_compressed(std::ios::binary);
434 compressZlib(data_in, os_compressed);
435 infostream<<"Test: Output size of large compressZlib is "
436 <<os_compressed.str().size()<<std::endl;
437 std::istringstream is_compressed(os_compressed.str(), std::ios::binary);
438 std::ostringstream os_decompressed(std::ios::binary);
439 decompressZlib(is_compressed, os_decompressed);
440 infostream<<"Test: Output size of large decompressZlib is "
441 <<os_decompressed.str().size()<<std::endl;
442 std::string str_decompressed = os_decompressed.str();
443 UTEST(str_decompressed.size() == data_in.size(), "Output size not"
444 " equal (output: %i, input: %i)",
445 str_decompressed.size(), data_in.size());
446 for(u32 i=0; i<size && i<str_decompressed.size(); i++){
447 UTEST(str_decompressed[i] == data_in[i],
448 "index out[%i]=%i differs from in[%i]=%i",
449 i, str_decompressed[i], i, data_in[i]);
455 struct TestMapNode: public TestBase
457 void Run(INodeDefManager *nodedef)
462 UASSERT(n.getContent() == CONTENT_AIR);
463 UASSERT(n.getLight(LIGHTBANK_DAY, nodedef) == 0);
464 UASSERT(n.getLight(LIGHTBANK_NIGHT, nodedef) == 0);
467 n.setContent(CONTENT_AIR);
468 UASSERT(nodedef->get(n).light_propagates == true);
469 n.setContent(LEGN(nodedef, "CONTENT_STONE"));
470 UASSERT(nodedef->get(n).light_propagates == false);
474 struct TestVoxelManipulator: public TestBase
476 void Run(INodeDefManager *nodedef)
482 VoxelArea a(v3s16(-1,-1,-1), v3s16(1,1,1));
483 UASSERT(a.index(0,0,0) == 1*3*3 + 1*3 + 1);
484 UASSERT(a.index(-1,-1,-1) == 0);
486 VoxelArea c(v3s16(-2,-2,-2), v3s16(2,2,2));
487 // An area that is 1 bigger in x+ and z-
488 VoxelArea d(v3s16(-2,-2,-3), v3s16(3,2,2));
490 core::list<VoxelArea> aa;
494 core::array<VoxelArea> results;
495 results.push_back(VoxelArea(v3s16(-2,-2,-3),v3s16(3,2,-3)));
496 results.push_back(VoxelArea(v3s16(3,-2,-2),v3s16(3,2,2)));
498 UASSERT(aa.size() == results.size());
500 infostream<<"Result of diff:"<<std::endl;
501 for(core::list<VoxelArea>::Iterator
502 i = aa.begin(); i != aa.end(); i++)
504 i->print(infostream);
505 infostream<<std::endl;
507 s32 j = results.linear_search(*i);
519 v.print(infostream, nodedef);
521 infostream<<"*** Setting (-1,0,-1)=2 ***"<<std::endl;
523 v.setNodeNoRef(v3s16(-1,0,-1), MapNode(CONTENT_GRASS));
525 v.print(infostream, nodedef);
527 UASSERT(v.getNode(v3s16(-1,0,-1)).getContent() == CONTENT_GRASS);
529 infostream<<"*** Reading from inexistent (0,0,-1) ***"<<std::endl;
531 EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,0,-1)));
533 v.print(infostream, nodedef);
535 infostream<<"*** Adding area ***"<<std::endl;
539 v.print(infostream, nodedef);
541 UASSERT(v.getNode(v3s16(-1,0,-1)).getContent() == CONTENT_GRASS);
542 EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,1,1)));
546 struct TestVoxelAlgorithms: public TestBase
548 void Run(INodeDefManager *ndef)
551 voxalgo::propagateSunlight
555 for(u16 z=0; z<3; z++)
556 for(u16 y=0; y<3; y++)
557 for(u16 x=0; x<3; x++)
560 v.setNodeNoRef(p, MapNode(CONTENT_AIR));
562 VoxelArea a(v3s16(0,0,0), v3s16(2,2,2));
564 core::map<v3s16, bool> light_sources;
565 voxalgo::setLight(v, a, 0, ndef);
566 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
567 v, a, true, light_sources, ndef);
568 //v.print(dstream, ndef, VOXELPRINT_LIGHT_DAY);
569 UASSERT(res.bottom_sunlight_valid == true);
570 UASSERT(v.getNode(v3s16(1,1,1)).getLight(LIGHTBANK_DAY, ndef)
573 v.setNodeNoRef(v3s16(0,0,0), MapNode(CONTENT_STONE));
575 core::map<v3s16, bool> light_sources;
576 voxalgo::setLight(v, a, 0, ndef);
577 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
578 v, a, true, light_sources, ndef);
579 UASSERT(res.bottom_sunlight_valid == true);
580 UASSERT(v.getNode(v3s16(1,1,1)).getLight(LIGHTBANK_DAY, ndef)
584 core::map<v3s16, bool> light_sources;
585 voxalgo::setLight(v, a, 0, ndef);
586 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
587 v, a, false, light_sources, ndef);
588 UASSERT(res.bottom_sunlight_valid == true);
589 UASSERT(v.getNode(v3s16(2,0,2)).getLight(LIGHTBANK_DAY, ndef)
592 v.setNodeNoRef(v3s16(1,3,2), MapNode(CONTENT_STONE));
594 core::map<v3s16, bool> light_sources;
595 voxalgo::setLight(v, a, 0, ndef);
596 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
597 v, a, true, light_sources, ndef);
598 UASSERT(res.bottom_sunlight_valid == true);
599 UASSERT(v.getNode(v3s16(1,1,2)).getLight(LIGHTBANK_DAY, ndef)
603 core::map<v3s16, bool> light_sources;
604 voxalgo::setLight(v, a, 0, ndef);
605 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
606 v, a, false, light_sources, ndef);
607 UASSERT(res.bottom_sunlight_valid == true);
608 UASSERT(v.getNode(v3s16(1,0,2)).getLight(LIGHTBANK_DAY, ndef)
612 MapNode n(CONTENT_AIR);
613 n.setLight(LIGHTBANK_DAY, 10, ndef);
614 v.setNodeNoRef(v3s16(1,-1,2), n);
617 core::map<v3s16, bool> light_sources;
618 voxalgo::setLight(v, a, 0, ndef);
619 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
620 v, a, true, light_sources, ndef);
621 UASSERT(res.bottom_sunlight_valid == true);
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);
631 MapNode n(CONTENT_AIR);
632 n.setLight(LIGHTBANK_DAY, LIGHT_SUN, ndef);
633 v.setNodeNoRef(v3s16(1,-1,2), n);
636 core::map<v3s16, bool> light_sources;
637 voxalgo::setLight(v, a, 0, ndef);
638 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
639 v, a, true, light_sources, ndef);
640 UASSERT(res.bottom_sunlight_valid == false);
643 core::map<v3s16, bool> light_sources;
644 voxalgo::setLight(v, a, 0, ndef);
645 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
646 v, a, false, light_sources, ndef);
647 UASSERT(res.bottom_sunlight_valid == false);
649 v.setNodeNoRef(v3s16(1,3,2), MapNode(CONTENT_IGNORE));
651 core::map<v3s16, bool> light_sources;
652 voxalgo::setLight(v, a, 0, ndef);
653 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
654 v, a, true, light_sources, ndef);
655 UASSERT(res.bottom_sunlight_valid == true);
659 voxalgo::clearLightAndCollectSources
663 for(u16 z=0; z<3; z++)
664 for(u16 y=0; y<3; y++)
665 for(u16 x=0; x<3; x++)
668 v.setNode(p, MapNode(CONTENT_AIR));
670 VoxelArea a(v3s16(0,0,0), v3s16(2,2,2));
671 v.setNodeNoRef(v3s16(0,0,0), MapNode(CONTENT_STONE));
672 v.setNodeNoRef(v3s16(1,1,1), MapNode(CONTENT_TORCH));
674 MapNode n(CONTENT_AIR);
675 n.setLight(LIGHTBANK_DAY, 1, ndef);
676 v.setNode(v3s16(1,1,2), n);
679 core::map<v3s16, bool> light_sources;
680 core::map<v3s16, u8> unlight_from;
681 voxalgo::clearLightAndCollectSources(v, a, LIGHTBANK_DAY,
682 ndef, light_sources, unlight_from);
683 //v.print(dstream, ndef, VOXELPRINT_LIGHT_DAY);
684 UASSERT(v.getNode(v3s16(0,1,1)).getLight(LIGHTBANK_DAY, ndef)
686 UASSERT(light_sources.find(v3s16(1,1,1)) != NULL);
687 UASSERT(light_sources.size() == 1);
688 UASSERT(unlight_from.find(v3s16(1,1,2)) != NULL);
689 UASSERT(unlight_from.size() == 1);
695 struct TestInventory: public TestBase
697 void Run(IItemDefManager *idef)
699 std::string serialized_inventory =
711 "Item default:cobble 61\n"
718 "Item default:dirt 71\n"
726 "Item default:dirt 99\n"
727 "Item default:cobble 38\n"
737 std::string serialized_inventory_2 =
749 "Item default:cobble 61\n"
756 "Item default:dirt 71\n"
764 "Item default:dirt 99\n"
765 "Item default:cobble 38\n"
776 std::istringstream is(serialized_inventory, std::ios::binary);
778 UASSERT(inv.getList("0"));
779 UASSERT(!inv.getList("main"));
780 inv.getList("0")->setName("main");
781 UASSERT(!inv.getList("0"));
782 UASSERT(inv.getList("main"));
783 UASSERT(inv.getList("main")->getWidth() == 3);
784 inv.getList("main")->setWidth(5);
785 std::ostringstream inv_os(std::ios::binary);
786 inv.serialize(inv_os);
787 UASSERT(inv_os.str() == serialized_inventory_2);
792 NOTE: These tests became non-working then NodeContainer was removed.
793 These should be redone, utilizing some kind of a virtual
794 interface for Map (IMap would be fine).
797 struct TestMapBlock: public TestBase
799 class TC : public NodeContainer
805 core::list<v3s16> validity_exceptions;
809 position_valid = true;
812 virtual bool isValidPosition(v3s16 p)
814 //return position_valid ^ (p==position_valid_exception);
815 bool exception = false;
816 for(core::list<v3s16>::Iterator i=validity_exceptions.begin();
817 i != validity_exceptions.end(); i++)
825 return exception ? !position_valid : position_valid;
828 virtual MapNode getNode(v3s16 p)
830 if(isValidPosition(p) == false)
831 throw InvalidPositionException();
835 virtual void setNode(v3s16 p, MapNode & n)
837 if(isValidPosition(p) == false)
838 throw InvalidPositionException();
841 virtual u16 nodeContainerId() const
851 MapBlock b(&parent, v3s16(1,1,1));
852 v3s16 relpos(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
854 UASSERT(b.getPosRelative() == relpos);
856 UASSERT(b.getBox().MinEdge.X == MAP_BLOCKSIZE);
857 UASSERT(b.getBox().MaxEdge.X == MAP_BLOCKSIZE*2-1);
858 UASSERT(b.getBox().MinEdge.Y == MAP_BLOCKSIZE);
859 UASSERT(b.getBox().MaxEdge.Y == MAP_BLOCKSIZE*2-1);
860 UASSERT(b.getBox().MinEdge.Z == MAP_BLOCKSIZE);
861 UASSERT(b.getBox().MaxEdge.Z == MAP_BLOCKSIZE*2-1);
863 UASSERT(b.isValidPosition(v3s16(0,0,0)) == true);
864 UASSERT(b.isValidPosition(v3s16(-1,0,0)) == false);
865 UASSERT(b.isValidPosition(v3s16(-1,-142,-2341)) == false);
866 UASSERT(b.isValidPosition(v3s16(-124,142,2341)) == false);
867 UASSERT(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
868 UASSERT(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE,MAP_BLOCKSIZE-1)) == false);
871 TODO: this method should probably be removed
872 if the block size isn't going to be set variable
874 /*UASSERT(b.getSizeNodes() == v3s16(MAP_BLOCKSIZE,
875 MAP_BLOCKSIZE, MAP_BLOCKSIZE));*/
877 // Changed flag should be initially set
878 UASSERT(b.getModified() == MOD_STATE_WRITE_NEEDED);
880 UASSERT(b.getModified() == MOD_STATE_CLEAN);
882 // All nodes should have been set to
883 // .d=CONTENT_IGNORE and .getLight() = 0
884 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
885 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
886 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
888 //UASSERT(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_AIR);
889 UASSERT(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_IGNORE);
890 UASSERT(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_DAY) == 0);
891 UASSERT(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_NIGHT) == 0);
895 MapNode n(CONTENT_AIR);
896 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
897 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
898 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
900 b.setNode(v3s16(x,y,z), n);
905 Parent fetch functions
907 parent.position_valid = false;
908 parent.node.setContent(5);
912 // Positions in the block should still be valid
913 UASSERT(b.isValidPositionParent(v3s16(0,0,0)) == true);
914 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
915 n = b.getNodeParent(v3s16(0,MAP_BLOCKSIZE-1,0));
916 UASSERT(n.getContent() == CONTENT_AIR);
918 // ...but outside the block they should be invalid
919 UASSERT(b.isValidPositionParent(v3s16(-121,2341,0)) == false);
920 UASSERT(b.isValidPositionParent(v3s16(-1,0,0)) == false);
921 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == false);
924 bool exception_thrown = false;
926 // This should throw an exception
927 MapNode n = b.getNodeParent(v3s16(0,0,-1));
929 catch(InvalidPositionException &e)
931 exception_thrown = true;
933 UASSERT(exception_thrown);
936 parent.position_valid = true;
937 // Now the positions outside should be valid
938 UASSERT(b.isValidPositionParent(v3s16(-121,2341,0)) == true);
939 UASSERT(b.isValidPositionParent(v3s16(-1,0,0)) == true);
940 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == true);
941 n = b.getNodeParent(v3s16(0,0,MAP_BLOCKSIZE));
942 UASSERT(n.getContent() == 5);
950 UASSERT(b.getNode(p).getContent() == 4);
951 //TODO: Update to new system
952 /*UASSERT(b.getNodeTile(p) == 4);
953 UASSERT(b.getNodeTile(v3s16(-1,-1,0)) == 5);*/
958 // Set lighting of all nodes to 0
959 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
960 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
961 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
962 MapNode n = b.getNode(v3s16(x,y,z));
963 n.setLight(LIGHTBANK_DAY, 0);
964 n.setLight(LIGHTBANK_NIGHT, 0);
965 b.setNode(v3s16(x,y,z), n);
971 Check how the block handles being a lonely sky block
973 parent.position_valid = true;
974 b.setIsUnderground(false);
975 parent.node.setContent(CONTENT_AIR);
976 parent.node.setLight(LIGHTBANK_DAY, LIGHT_SUN);
977 parent.node.setLight(LIGHTBANK_NIGHT, 0);
978 core::map<v3s16, bool> light_sources;
979 // The bottom block is invalid, because we have a shadowing node
980 UASSERT(b.propagateSunlight(light_sources) == false);
981 UASSERT(b.getNode(v3s16(1,4,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
982 UASSERT(b.getNode(v3s16(1,3,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
983 UASSERT(b.getNode(v3s16(1,2,0)).getLight(LIGHTBANK_DAY) == 0);
984 UASSERT(b.getNode(v3s16(1,1,0)).getLight(LIGHTBANK_DAY) == 0);
985 UASSERT(b.getNode(v3s16(1,0,0)).getLight(LIGHTBANK_DAY) == 0);
986 UASSERT(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
987 UASSERT(b.getFaceLight2(1000, p, v3s16(0,1,0)) == LIGHT_SUN);
988 UASSERT(b.getFaceLight2(1000, p, v3s16(0,-1,0)) == 0);
989 UASSERT(b.getFaceLight2(0, p, v3s16(0,-1,0)) == 0);
990 // According to MapBlock::getFaceLight,
991 // The face on the z+ side should have double-diminished light
992 //UASSERT(b.getFaceLight(p, v3s16(0,0,1)) == diminish_light(diminish_light(LIGHT_MAX)));
993 // The face on the z+ side should have diminished light
994 UASSERT(b.getFaceLight2(1000, p, v3s16(0,0,1)) == diminish_light(LIGHT_MAX));
997 Check how the block handles being in between blocks with some non-sunlight
998 while being underground
1001 // Make neighbours to exist and set some non-sunlight to them
1002 parent.position_valid = true;
1003 b.setIsUnderground(true);
1004 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
1005 core::map<v3s16, bool> light_sources;
1006 // The block below should be valid because there shouldn't be
1007 // sunlight in there either
1008 UASSERT(b.propagateSunlight(light_sources, true) == true);
1009 // Should not touch nodes that are not affected (that is, all of them)
1010 //UASSERT(b.getNode(v3s16(1,2,3)).getLight() == LIGHT_SUN);
1011 // Should set light of non-sunlighted blocks to 0.
1012 UASSERT(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == 0);
1015 Set up a situation where:
1016 - There is only air in this block
1017 - There is a valid non-sunlighted block at the bottom, and
1018 - Invalid blocks elsewhere.
1019 - the block is not underground.
1021 This should result in bottom block invalidity
1024 b.setIsUnderground(false);
1026 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
1027 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
1028 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
1030 n.setContent(CONTENT_AIR);
1031 n.setLight(LIGHTBANK_DAY, 0);
1032 b.setNode(v3s16(x,y,z), n);
1036 // Make neighbours invalid
1037 parent.position_valid = false;
1038 // Add exceptions to the top of the bottom block
1039 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
1040 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
1042 parent.validity_exceptions.push_back(v3s16(MAP_BLOCKSIZE+x, MAP_BLOCKSIZE-1, MAP_BLOCKSIZE+z));
1044 // Lighting value for the valid nodes
1045 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
1046 core::map<v3s16, bool> light_sources;
1047 // Bottom block is not valid
1048 UASSERT(b.propagateSunlight(light_sources) == false);
1053 struct TestMapSector: public TestBase
1055 class TC : public NodeContainer
1060 bool position_valid;
1064 position_valid = true;
1067 virtual bool isValidPosition(v3s16 p)
1069 return position_valid;
1072 virtual MapNode getNode(v3s16 p)
1074 if(position_valid == false)
1075 throw InvalidPositionException();
1079 virtual void setNode(v3s16 p, MapNode & n)
1081 if(position_valid == false)
1082 throw InvalidPositionException();
1085 virtual u16 nodeContainerId() const
1094 parent.position_valid = false;
1096 // Create one with no heightmaps
1097 ServerMapSector sector(&parent, v2s16(1,1));
1099 UASSERT(sector.getBlockNoCreateNoEx(0) == 0);
1100 UASSERT(sector.getBlockNoCreateNoEx(1) == 0);
1102 MapBlock * bref = sector.createBlankBlock(-2);
1104 UASSERT(sector.getBlockNoCreateNoEx(0) == 0);
1105 UASSERT(sector.getBlockNoCreateNoEx(-2) == bref);
1107 //TODO: Check for AlreadyExistsException
1109 /*bool exception_thrown = false;
1113 catch(InvalidPositionException &e){
1114 exception_thrown = true;
1116 UASSERT(exception_thrown);*/
1122 struct TestCollision: public TestBase
1127 axisAlignedCollision
1130 for(s16 bx = -3; bx <= 3; bx++)
1131 for(s16 by = -3; by <= 3; by++)
1132 for(s16 bz = -3; bz <= 3; bz++)
1136 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1137 aabb3f m(bx-2, by, bz, bx-1, by+1, bz+1);
1140 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1141 UASSERT(fabs(dtime - 1.000) < 0.001);
1144 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1145 aabb3f m(bx-2, by, bz, bx-1, by+1, bz+1);
1148 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == -1);
1151 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1152 aabb3f m(bx-2, by+1.5, bz, bx-1, by+2.5, bz-1);
1155 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == -1);
1158 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1159 aabb3f m(bx-2, by-1.5, bz, bx-1.5, by+0.5, bz+1);
1162 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1163 UASSERT(fabs(dtime - 3.000) < 0.001);
1166 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1167 aabb3f m(bx-2, by-1.5, bz, bx-1.5, by+0.5, bz+1);
1170 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1171 UASSERT(fabs(dtime - 3.000) < 0.001);
1176 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1177 aabb3f m(bx+2, by, bz, bx+3, by+1, bz+1);
1180 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1181 UASSERT(fabs(dtime - 1.000) < 0.001);
1184 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1185 aabb3f m(bx+2, by, bz, bx+3, by+1, bz+1);
1188 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == -1);
1191 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1192 aabb3f m(bx+2, by, bz+1.5, bx+3, by+1, bz+3.5);
1195 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == -1);
1198 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1199 aabb3f m(bx+2, by-1.5, bz, bx+2.5, by-0.5, bz+1);
1200 v3f v(-0.5, 0.2, 0);
1202 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 1); // Y, not X!
1203 UASSERT(fabs(dtime - 2.500) < 0.001);
1206 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1207 aabb3f m(bx+2, by-1.5, bz, bx+2.5, by-0.5, bz+1);
1208 v3f v(-0.5, 0.3, 0);
1210 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1211 UASSERT(fabs(dtime - 2.000) < 0.001);
1214 // TODO: Y-, Y+, Z-, Z+
1218 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1219 aabb3f m(bx+2.3, by+2.29, bz+2.29, bx+4.2, by+4.2, bz+4.2);
1220 v3f v(-1./3, -1./3, -1./3);
1222 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1223 UASSERT(fabs(dtime - 0.9) < 0.001);
1226 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1227 aabb3f m(bx+2.29, by+2.3, bz+2.29, bx+4.2, by+4.2, bz+4.2);
1228 v3f v(-1./3, -1./3, -1./3);
1230 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 1);
1231 UASSERT(fabs(dtime - 0.9) < 0.001);
1234 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1235 aabb3f m(bx+2.29, by+2.29, bz+2.3, bx+4.2, by+4.2, bz+4.2);
1236 v3f v(-1./3, -1./3, -1./3);
1238 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 2);
1239 UASSERT(fabs(dtime - 0.9) < 0.001);
1242 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1243 aabb3f m(bx-4.2, by-4.2, bz-4.2, bx-2.3, by-2.29, bz-2.29);
1244 v3f v(1./7, 1./7, 1./7);
1246 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1247 UASSERT(fabs(dtime - 16.1) < 0.001);
1250 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1251 aabb3f m(bx-4.2, by-4.2, bz-4.2, bx-2.29, by-2.3, bz-2.29);
1252 v3f v(1./7, 1./7, 1./7);
1254 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 1);
1255 UASSERT(fabs(dtime - 16.1) < 0.001);
1258 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1259 aabb3f m(bx-4.2, by-4.2, bz-4.2, bx-2.29, by-2.29, bz-2.3);
1260 v3f v(1./7, 1./7, 1./7);
1262 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 2);
1263 UASSERT(fabs(dtime - 16.1) < 0.001);
1269 struct TestSocket: public TestBase
1273 const int port = 30003;
1277 const char sendbuffer[] = "hello world!";
1278 socket.Send(Address(127,0,0,1,port), sendbuffer, sizeof(sendbuffer));
1282 char rcvbuffer[256];
1283 memset(rcvbuffer, 0, sizeof(rcvbuffer));
1287 int bytes_read = socket.Receive(sender, rcvbuffer, sizeof(rcvbuffer));
1291 //FIXME: This fails on some systems
1292 UASSERT(strncmp(sendbuffer, rcvbuffer, sizeof(sendbuffer))==0);
1293 UASSERT(sender.getAddress() == Address(127,0,0,1, 0).getAddress());
1297 struct TestConnection: public TestBase
1302 Test helper functions
1305 // Some constants for testing
1306 u32 proto_id = 0x12345678;
1309 SharedBuffer<u8> data1(1);
1311 Address a(127,0,0,1, 10);
1314 con::BufferedPacket p1 = con::makePacket(a, data1,
1315 proto_id, peer_id, channel);
1317 We should now have a packet with this data:
1320 [4] u16 sender_peer_id
1325 UASSERT(readU32(&p1.data[0]) == proto_id);
1326 UASSERT(readU16(&p1.data[4]) == peer_id);
1327 UASSERT(readU8(&p1.data[6]) == channel);
1328 UASSERT(readU8(&p1.data[7]) == data1[0]);
1330 //infostream<<"initial data1[0]="<<((u32)data1[0]&0xff)<<std::endl;
1332 SharedBuffer<u8> p2 = con::makeReliablePacket(data1, seqnum);
1334 /*infostream<<"p2.getSize()="<<p2.getSize()<<", data1.getSize()="
1335 <<data1.getSize()<<std::endl;
1336 infostream<<"readU8(&p2[3])="<<readU8(&p2[3])
1337 <<" p2[3]="<<((u32)p2[3]&0xff)<<std::endl;
1338 infostream<<"data1[0]="<<((u32)data1[0]&0xff)<<std::endl;*/
1340 UASSERT(p2.getSize() == 3 + data1.getSize());
1341 UASSERT(readU8(&p2[0]) == TYPE_RELIABLE);
1342 UASSERT(readU16(&p2[1]) == seqnum);
1343 UASSERT(readU8(&p2[3]) == data1[0]);
1346 struct Handler : public con::PeerHandler
1348 Handler(const char *a_name)
1354 void peerAdded(con::Peer *peer)
1356 infostream<<"Handler("<<name<<")::peerAdded(): "
1357 "id="<<peer->id<<std::endl;
1361 void deletingPeer(con::Peer *peer, bool timeout)
1363 infostream<<"Handler("<<name<<")::deletingPeer(): "
1365 <<", timeout="<<timeout<<std::endl;
1377 DSTACK("TestConnection::Run");
1382 Test some real connections
1384 NOTE: This mostly tests the legacy interface.
1387 u32 proto_id = 0xad26846a;
1389 Handler hand_server("server");
1390 Handler hand_client("client");
1392 infostream<<"** Creating server Connection"<<std::endl;
1393 con::Connection server(proto_id, 512, 5.0, &hand_server);
1394 server.Serve(30001);
1396 infostream<<"** Creating client Connection"<<std::endl;
1397 con::Connection client(proto_id, 512, 5.0, &hand_client);
1399 UASSERT(hand_server.count == 0);
1400 UASSERT(hand_client.count == 0);
1404 Address server_address(127,0,0,1, 30001);
1405 infostream<<"** running client.Connect()"<<std::endl;
1406 client.Connect(server_address);
1410 // Client should not have added client yet
1411 UASSERT(hand_client.count == 0);
1416 SharedBuffer<u8> data;
1417 infostream<<"** running client.Receive()"<<std::endl;
1418 u32 size = client.Receive(peer_id, data);
1419 infostream<<"** Client received: peer_id="<<peer_id
1423 catch(con::NoIncomingDataException &e)
1427 // Client should have added server now
1428 UASSERT(hand_client.count == 1);
1429 UASSERT(hand_client.last_id == 1);
1430 // Server should not have added client yet
1431 UASSERT(hand_server.count == 0);
1438 SharedBuffer<u8> data;
1439 infostream<<"** running server.Receive()"<<std::endl;
1440 u32 size = server.Receive(peer_id, data);
1441 infostream<<"** Server received: peer_id="<<peer_id
1445 catch(con::NoIncomingDataException &e)
1447 // No actual data received, but the client has
1448 // probably been connected
1451 // Client should be the same
1452 UASSERT(hand_client.count == 1);
1453 UASSERT(hand_client.last_id == 1);
1454 // Server should have the client
1455 UASSERT(hand_server.count == 1);
1456 UASSERT(hand_server.last_id == 2);
1460 while(client.Connected() == false)
1465 SharedBuffer<u8> data;
1466 infostream<<"** running client.Receive()"<<std::endl;
1467 u32 size = client.Receive(peer_id, data);
1468 infostream<<"** Client received: peer_id="<<peer_id
1472 catch(con::NoIncomingDataException &e)
1483 SharedBuffer<u8> data;
1484 infostream<<"** running server.Receive()"<<std::endl;
1485 u32 size = server.Receive(peer_id, data);
1486 infostream<<"** Server received: peer_id="<<peer_id
1490 catch(con::NoIncomingDataException &e)
1495 Simple send-receive test
1498 /*u8 data[] = "Hello World!";
1499 u32 datasize = sizeof(data);*/
1500 SharedBuffer<u8> data = SharedBufferFromString("Hello World!");
1502 infostream<<"** running client.Send()"<<std::endl;
1503 client.Send(PEER_ID_SERVER, 0, data, true);
1508 SharedBuffer<u8> recvdata;
1509 infostream<<"** running server.Receive()"<<std::endl;
1510 u32 size = server.Receive(peer_id, recvdata);
1511 infostream<<"** Server received: peer_id="<<peer_id
1515 UASSERT(memcmp(*data, *recvdata, data.getSize()) == 0);
1518 u16 peer_id_client = 2;
1521 Send consequent packets in different order
1522 Not compatible with new Connection, thus commented out.
1525 //u8 data1[] = "hello1";
1526 //u8 data2[] = "hello2";
1527 SharedBuffer<u8> data1 = SharedBufferFromString("hello1");
1528 SharedBuffer<u8> data2 = SharedBufferFromString("Hello2");
1530 Address client_address =
1531 server.GetPeerAddress(peer_id_client);
1533 infostream<<"*** Sending packets in wrong order (2,1,2)"
1537 con::Channel *ch = &server.getPeer(peer_id_client)->channels[chn];
1538 u16 sn = ch->next_outgoing_seqnum;
1539 ch->next_outgoing_seqnum = sn+1;
1540 server.Send(peer_id_client, chn, data2, true);
1541 ch->next_outgoing_seqnum = sn;
1542 server.Send(peer_id_client, chn, data1, true);
1543 ch->next_outgoing_seqnum = sn+1;
1544 server.Send(peer_id_client, chn, data2, true);
1548 infostream<<"*** Receiving the packets"<<std::endl;
1551 SharedBuffer<u8> recvdata;
1554 infostream<<"** running client.Receive()"<<std::endl;
1556 size = client.Receive(peer_id, recvdata);
1557 infostream<<"** Client received: peer_id="<<peer_id
1559 <<", data="<<*recvdata
1561 UASSERT(size == data1.getSize());
1562 UASSERT(memcmp(*data1, *recvdata, data1.getSize()) == 0);
1563 UASSERT(peer_id == PEER_ID_SERVER);
1565 infostream<<"** running client.Receive()"<<std::endl;
1567 size = client.Receive(peer_id, recvdata);
1568 infostream<<"** Client received: peer_id="<<peer_id
1570 <<", data="<<*recvdata
1572 UASSERT(size == data2.getSize());
1573 UASSERT(memcmp(*data2, *recvdata, data2.getSize()) == 0);
1574 UASSERT(peer_id == PEER_ID_SERVER);
1576 bool got_exception = false;
1579 infostream<<"** running client.Receive()"<<std::endl;
1581 size = client.Receive(peer_id, recvdata);
1582 infostream<<"** Client received: peer_id="<<peer_id
1584 <<", data="<<*recvdata
1587 catch(con::NoIncomingDataException &e)
1589 infostream<<"** No incoming data for client"<<std::endl;
1590 got_exception = true;
1592 UASSERT(got_exception);
1597 Send large amounts of packets (infinite test)
1598 Commented out because of infinity.
1601 infostream<<"Sending large amounts of packets (infinite test)"<<std::endl;
1604 int datasize = myrand_range(0,5)==0?myrand_range(100,10000):myrand_range(0,100);
1605 infostream<<"datasize="<<datasize<<std::endl;
1606 SharedBuffer<u8> data1(datasize);
1607 for(u16 i=0; i<datasize; i++)
1610 int sendtimes = myrand_range(1,10);
1611 for(int i=0; i<sendtimes; i++){
1612 server.Send(peer_id_client, 0, data1, true);
1615 infostream<<"sendcount="<<sendcount<<std::endl;
1617 //int receivetimes = myrand_range(1,20);
1618 int receivetimes = 20;
1619 for(int i=0; i<receivetimes; i++){
1620 SharedBuffer<u8> recvdata;
1623 bool received = false;
1625 size = client.Receive(peer_id, recvdata);
1627 }catch(con::NoIncomingDataException &e){
1637 const int datasize = 30000;
1638 SharedBuffer<u8> data1(datasize);
1639 for(u16 i=0; i<datasize; i++){
1643 infostream<<"Sending data (size="<<datasize<<"):";
1644 for(int i=0; i<datasize && i<20; i++){
1645 if(i%2==0) infostream<<" ";
1647 snprintf(buf, 10, "%.2X", ((int)((const char*)*data1)[i])&0xff);
1652 infostream<<std::endl;
1654 server.Send(peer_id_client, 0, data1, true);
1658 SharedBuffer<u8> recvdata;
1659 infostream<<"** running client.Receive()"<<std::endl;
1662 bool received = false;
1663 u32 timems0 = porting::getTimeMs();
1665 if(porting::getTimeMs() - timems0 > 5000 || received)
1668 size = client.Receive(peer_id, recvdata);
1670 }catch(con::NoIncomingDataException &e){
1675 infostream<<"** Client received: peer_id="<<peer_id
1679 infostream<<"Received data (size="<<size<<"): ";
1680 for(int i=0; i<size && i<20; i++){
1681 if(i%2==0) infostream<<" ";
1683 snprintf(buf, 10, "%.2X", ((int)(recvdata[i]))&0xff);
1688 infostream<<std::endl;
1690 UASSERT(memcmp(*data1, *recvdata, data1.getSize()) == 0);
1691 UASSERT(peer_id == PEER_ID_SERVER);
1694 // Check peer handlers
1695 UASSERT(hand_client.count == 1);
1696 UASSERT(hand_client.last_id == 1);
1697 UASSERT(hand_server.count == 1);
1698 UASSERT(hand_server.last_id == 2);
1707 infostream<<"Running " #X <<std::endl;\
1710 tests_failed += x.test_failed ? 1 : 0;\
1713 #define TESTPARAMS(X, ...)\
1716 infostream<<"Running " #X <<std::endl;\
1717 x.Run(__VA_ARGS__);\
1719 tests_failed += x.test_failed ? 1 : 0;\
1724 DSTACK(__FUNCTION_NAME);
1727 int tests_failed = 0;
1729 // Create item and node definitions
1730 IWritableItemDefManager *idef = createItemDefManager();
1731 IWritableNodeDefManager *ndef = createNodeDefManager();
1732 define_some_nodes(idef, ndef);
1734 infostream<<"run_tests() started"<<std::endl;
1735 TEST(TestUtilities);
1738 TEST(TestSerialization);
1739 TESTPARAMS(TestMapNode, ndef);
1740 TESTPARAMS(TestVoxelManipulator, ndef);
1741 TESTPARAMS(TestVoxelAlgorithms, ndef);
1742 TESTPARAMS(TestInventory, idef);
1743 //TEST(TestMapBlock);
1744 //TEST(TestMapSector);
1745 TEST(TestCollision);
1746 if(INTERNET_SIMULATOR == false){
1748 dout_con<<"=== BEGIN RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
1749 TEST(TestConnection);
1750 dout_con<<"=== END RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
1752 if(tests_failed == 0){
1753 infostream<<"run_tests(): "<<tests_failed<<" / "<<tests_run<<" tests failed."<<std::endl;
1754 infostream<<"run_tests() passed."<<std::endl;
1757 errorstream<<"run_tests(): "<<tests_failed<<" / "<<tests_run<<" tests failed."<<std::endl;
1758 errorstream<<"run_tests() aborting."<<std::endl;