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 it's
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 =
710 "Item default:cobble 61\n"
717 "Item default:dirt 71\n"
725 "Item default:dirt 99\n"
726 "Item default:cobble 38\n"
736 std::string serialized_inventory_2 =
747 "Item default:cobble 61\n"
754 "Item default:dirt 71\n"
762 "Item default:dirt 99\n"
763 "Item default:cobble 38\n"
774 std::istringstream is(serialized_inventory, std::ios::binary);
776 UASSERT(inv.getList("0"));
777 UASSERT(!inv.getList("main"));
778 inv.getList("0")->setName("main");
779 UASSERT(!inv.getList("0"));
780 UASSERT(inv.getList("main"));
781 std::ostringstream inv_os(std::ios::binary);
782 inv.serialize(inv_os);
783 UASSERT(inv_os.str() == serialized_inventory_2);
788 NOTE: These tests became non-working then NodeContainer was removed.
789 These should be redone, utilizing some kind of a virtual
790 interface for Map (IMap would be fine).
793 struct TestMapBlock: public TestBase
795 class TC : public NodeContainer
801 core::list<v3s16> validity_exceptions;
805 position_valid = true;
808 virtual bool isValidPosition(v3s16 p)
810 //return position_valid ^ (p==position_valid_exception);
811 bool exception = false;
812 for(core::list<v3s16>::Iterator i=validity_exceptions.begin();
813 i != validity_exceptions.end(); i++)
821 return exception ? !position_valid : position_valid;
824 virtual MapNode getNode(v3s16 p)
826 if(isValidPosition(p) == false)
827 throw InvalidPositionException();
831 virtual void setNode(v3s16 p, MapNode & n)
833 if(isValidPosition(p) == false)
834 throw InvalidPositionException();
837 virtual u16 nodeContainerId() const
847 MapBlock b(&parent, v3s16(1,1,1));
848 v3s16 relpos(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
850 UASSERT(b.getPosRelative() == relpos);
852 UASSERT(b.getBox().MinEdge.X == MAP_BLOCKSIZE);
853 UASSERT(b.getBox().MaxEdge.X == MAP_BLOCKSIZE*2-1);
854 UASSERT(b.getBox().MinEdge.Y == MAP_BLOCKSIZE);
855 UASSERT(b.getBox().MaxEdge.Y == MAP_BLOCKSIZE*2-1);
856 UASSERT(b.getBox().MinEdge.Z == MAP_BLOCKSIZE);
857 UASSERT(b.getBox().MaxEdge.Z == MAP_BLOCKSIZE*2-1);
859 UASSERT(b.isValidPosition(v3s16(0,0,0)) == true);
860 UASSERT(b.isValidPosition(v3s16(-1,0,0)) == false);
861 UASSERT(b.isValidPosition(v3s16(-1,-142,-2341)) == false);
862 UASSERT(b.isValidPosition(v3s16(-124,142,2341)) == false);
863 UASSERT(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
864 UASSERT(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE,MAP_BLOCKSIZE-1)) == false);
867 TODO: this method should probably be removed
868 if the block size isn't going to be set variable
870 /*UASSERT(b.getSizeNodes() == v3s16(MAP_BLOCKSIZE,
871 MAP_BLOCKSIZE, MAP_BLOCKSIZE));*/
873 // Changed flag should be initially set
874 UASSERT(b.getModified() == MOD_STATE_WRITE_NEEDED);
876 UASSERT(b.getModified() == MOD_STATE_CLEAN);
878 // All nodes should have been set to
879 // .d=CONTENT_IGNORE and .getLight() = 0
880 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
881 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
882 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
884 //UASSERT(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_AIR);
885 UASSERT(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_IGNORE);
886 UASSERT(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_DAY) == 0);
887 UASSERT(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_NIGHT) == 0);
891 MapNode n(CONTENT_AIR);
892 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
893 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
894 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
896 b.setNode(v3s16(x,y,z), n);
901 Parent fetch functions
903 parent.position_valid = false;
904 parent.node.setContent(5);
908 // Positions in the block should still be valid
909 UASSERT(b.isValidPositionParent(v3s16(0,0,0)) == true);
910 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
911 n = b.getNodeParent(v3s16(0,MAP_BLOCKSIZE-1,0));
912 UASSERT(n.getContent() == CONTENT_AIR);
914 // ...but outside the block they should be invalid
915 UASSERT(b.isValidPositionParent(v3s16(-121,2341,0)) == false);
916 UASSERT(b.isValidPositionParent(v3s16(-1,0,0)) == false);
917 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == false);
920 bool exception_thrown = false;
922 // This should throw an exception
923 MapNode n = b.getNodeParent(v3s16(0,0,-1));
925 catch(InvalidPositionException &e)
927 exception_thrown = true;
929 UASSERT(exception_thrown);
932 parent.position_valid = true;
933 // Now the positions outside should be valid
934 UASSERT(b.isValidPositionParent(v3s16(-121,2341,0)) == true);
935 UASSERT(b.isValidPositionParent(v3s16(-1,0,0)) == true);
936 UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == true);
937 n = b.getNodeParent(v3s16(0,0,MAP_BLOCKSIZE));
938 UASSERT(n.getContent() == 5);
946 UASSERT(b.getNode(p).getContent() == 4);
947 //TODO: Update to new system
948 /*UASSERT(b.getNodeTile(p) == 4);
949 UASSERT(b.getNodeTile(v3s16(-1,-1,0)) == 5);*/
954 // Set lighting of all nodes to 0
955 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
956 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
957 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
958 MapNode n = b.getNode(v3s16(x,y,z));
959 n.setLight(LIGHTBANK_DAY, 0);
960 n.setLight(LIGHTBANK_NIGHT, 0);
961 b.setNode(v3s16(x,y,z), n);
967 Check how the block handles being a lonely sky block
969 parent.position_valid = true;
970 b.setIsUnderground(false);
971 parent.node.setContent(CONTENT_AIR);
972 parent.node.setLight(LIGHTBANK_DAY, LIGHT_SUN);
973 parent.node.setLight(LIGHTBANK_NIGHT, 0);
974 core::map<v3s16, bool> light_sources;
975 // The bottom block is invalid, because we have a shadowing node
976 UASSERT(b.propagateSunlight(light_sources) == false);
977 UASSERT(b.getNode(v3s16(1,4,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
978 UASSERT(b.getNode(v3s16(1,3,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
979 UASSERT(b.getNode(v3s16(1,2,0)).getLight(LIGHTBANK_DAY) == 0);
980 UASSERT(b.getNode(v3s16(1,1,0)).getLight(LIGHTBANK_DAY) == 0);
981 UASSERT(b.getNode(v3s16(1,0,0)).getLight(LIGHTBANK_DAY) == 0);
982 UASSERT(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
983 UASSERT(b.getFaceLight2(1000, p, v3s16(0,1,0)) == LIGHT_SUN);
984 UASSERT(b.getFaceLight2(1000, p, v3s16(0,-1,0)) == 0);
985 UASSERT(b.getFaceLight2(0, p, v3s16(0,-1,0)) == 0);
986 // According to MapBlock::getFaceLight,
987 // The face on the z+ side should have double-diminished light
988 //UASSERT(b.getFaceLight(p, v3s16(0,0,1)) == diminish_light(diminish_light(LIGHT_MAX)));
989 // The face on the z+ side should have diminished light
990 UASSERT(b.getFaceLight2(1000, p, v3s16(0,0,1)) == diminish_light(LIGHT_MAX));
993 Check how the block handles being in between blocks with some non-sunlight
994 while being underground
997 // Make neighbours to exist and set some non-sunlight to them
998 parent.position_valid = true;
999 b.setIsUnderground(true);
1000 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
1001 core::map<v3s16, bool> light_sources;
1002 // The block below should be valid because there shouldn't be
1003 // sunlight in there either
1004 UASSERT(b.propagateSunlight(light_sources, true) == true);
1005 // Should not touch nodes that are not affected (that is, all of them)
1006 //UASSERT(b.getNode(v3s16(1,2,3)).getLight() == LIGHT_SUN);
1007 // Should set light of non-sunlighted blocks to 0.
1008 UASSERT(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == 0);
1011 Set up a situation where:
1012 - There is only air in this block
1013 - There is a valid non-sunlighted block at the bottom, and
1014 - Invalid blocks elsewhere.
1015 - the block is not underground.
1017 This should result in bottom block invalidity
1020 b.setIsUnderground(false);
1022 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
1023 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
1024 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
1026 n.setContent(CONTENT_AIR);
1027 n.setLight(LIGHTBANK_DAY, 0);
1028 b.setNode(v3s16(x,y,z), n);
1032 // Make neighbours invalid
1033 parent.position_valid = false;
1034 // Add exceptions to the top of the bottom block
1035 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
1036 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
1038 parent.validity_exceptions.push_back(v3s16(MAP_BLOCKSIZE+x, MAP_BLOCKSIZE-1, MAP_BLOCKSIZE+z));
1040 // Lighting value for the valid nodes
1041 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
1042 core::map<v3s16, bool> light_sources;
1043 // Bottom block is not valid
1044 UASSERT(b.propagateSunlight(light_sources) == false);
1049 struct TestMapSector: public TestBase
1051 class TC : public NodeContainer
1056 bool position_valid;
1060 position_valid = true;
1063 virtual bool isValidPosition(v3s16 p)
1065 return position_valid;
1068 virtual MapNode getNode(v3s16 p)
1070 if(position_valid == false)
1071 throw InvalidPositionException();
1075 virtual void setNode(v3s16 p, MapNode & n)
1077 if(position_valid == false)
1078 throw InvalidPositionException();
1081 virtual u16 nodeContainerId() const
1090 parent.position_valid = false;
1092 // Create one with no heightmaps
1093 ServerMapSector sector(&parent, v2s16(1,1));
1095 UASSERT(sector.getBlockNoCreateNoEx(0) == 0);
1096 UASSERT(sector.getBlockNoCreateNoEx(1) == 0);
1098 MapBlock * bref = sector.createBlankBlock(-2);
1100 UASSERT(sector.getBlockNoCreateNoEx(0) == 0);
1101 UASSERT(sector.getBlockNoCreateNoEx(-2) == bref);
1103 //TODO: Check for AlreadyExistsException
1105 /*bool exception_thrown = false;
1109 catch(InvalidPositionException &e){
1110 exception_thrown = true;
1112 UASSERT(exception_thrown);*/
1118 struct TestCollision: public TestBase
1123 axisAlignedCollision
1126 for(s16 bx = -3; bx <= 3; bx++)
1127 for(s16 by = -3; by <= 3; by++)
1128 for(s16 bz = -3; bz <= 3; bz++)
1132 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1133 aabb3f m(bx-2, by, bz, bx-1, by+1, bz+1);
1136 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1137 UASSERT(fabs(dtime - 1.000) < 0.001);
1140 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1141 aabb3f m(bx-2, by, bz, bx-1, by+1, bz+1);
1144 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == -1);
1147 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1148 aabb3f m(bx-2, by+1.5, bz, bx-1, by+2.5, bz-1);
1151 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == -1);
1154 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1155 aabb3f m(bx-2, by-1.5, bz, bx-1.5, by+0.5, bz+1);
1158 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1159 UASSERT(fabs(dtime - 3.000) < 0.001);
1162 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1163 aabb3f m(bx-2, by-1.5, bz, bx-1.5, by+0.5, bz+1);
1166 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1167 UASSERT(fabs(dtime - 3.000) < 0.001);
1172 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1173 aabb3f m(bx+2, by, bz, bx+3, by+1, bz+1);
1176 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1177 UASSERT(fabs(dtime - 1.000) < 0.001);
1180 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1181 aabb3f m(bx+2, by, bz, bx+3, by+1, bz+1);
1184 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == -1);
1187 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1188 aabb3f m(bx+2, by, bz+1.5, bx+3, by+1, bz+3.5);
1191 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == -1);
1194 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1195 aabb3f m(bx+2, by-1.5, bz, bx+2.5, by-0.5, bz+1);
1196 v3f v(-0.5, 0.2, 0);
1198 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 1); // Y, not X!
1199 UASSERT(fabs(dtime - 2.500) < 0.001);
1202 aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
1203 aabb3f m(bx+2, by-1.5, bz, bx+2.5, by-0.5, bz+1);
1204 v3f v(-0.5, 0.3, 0);
1206 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1207 UASSERT(fabs(dtime - 2.000) < 0.001);
1210 // TODO: Y-, Y+, Z-, Z+
1214 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1215 aabb3f m(bx+2.3, by+2.29, bz+2.29, bx+4.2, by+4.2, bz+4.2);
1216 v3f v(-1./3, -1./3, -1./3);
1218 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1219 UASSERT(fabs(dtime - 0.9) < 0.001);
1222 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1223 aabb3f m(bx+2.29, by+2.3, bz+2.29, bx+4.2, by+4.2, bz+4.2);
1224 v3f v(-1./3, -1./3, -1./3);
1226 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 1);
1227 UASSERT(fabs(dtime - 0.9) < 0.001);
1230 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1231 aabb3f m(bx+2.29, by+2.29, bz+2.3, bx+4.2, by+4.2, bz+4.2);
1232 v3f v(-1./3, -1./3, -1./3);
1234 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 2);
1235 UASSERT(fabs(dtime - 0.9) < 0.001);
1238 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1239 aabb3f m(bx-4.2, by-4.2, bz-4.2, bx-2.3, by-2.29, bz-2.29);
1240 v3f v(1./7, 1./7, 1./7);
1242 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 0);
1243 UASSERT(fabs(dtime - 16.1) < 0.001);
1246 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1247 aabb3f m(bx-4.2, by-4.2, bz-4.2, bx-2.29, by-2.3, bz-2.29);
1248 v3f v(1./7, 1./7, 1./7);
1250 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 1);
1251 UASSERT(fabs(dtime - 16.1) < 0.001);
1254 aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
1255 aabb3f m(bx-4.2, by-4.2, bz-4.2, bx-2.29, by-2.29, bz-2.3);
1256 v3f v(1./7, 1./7, 1./7);
1258 UASSERT(axisAlignedCollision(s, m, v, 0, dtime) == 2);
1259 UASSERT(fabs(dtime - 16.1) < 0.001);
1265 struct TestSocket: public TestBase
1269 const int port = 30003;
1273 const char sendbuffer[] = "hello world!";
1274 socket.Send(Address(127,0,0,1,port), sendbuffer, sizeof(sendbuffer));
1278 char rcvbuffer[256];
1279 memset(rcvbuffer, 0, sizeof(rcvbuffer));
1283 int bytes_read = socket.Receive(sender, rcvbuffer, sizeof(rcvbuffer));
1287 //FIXME: This fails on some systems
1288 UASSERT(strncmp(sendbuffer, rcvbuffer, sizeof(sendbuffer))==0);
1289 UASSERT(sender.getAddress() == Address(127,0,0,1, 0).getAddress());
1293 struct TestConnection: public TestBase
1298 Test helper functions
1301 // Some constants for testing
1302 u32 proto_id = 0x12345678;
1305 SharedBuffer<u8> data1(1);
1307 Address a(127,0,0,1, 10);
1310 con::BufferedPacket p1 = con::makePacket(a, data1,
1311 proto_id, peer_id, channel);
1313 We should now have a packet with this data:
1316 [4] u16 sender_peer_id
1321 UASSERT(readU32(&p1.data[0]) == proto_id);
1322 UASSERT(readU16(&p1.data[4]) == peer_id);
1323 UASSERT(readU8(&p1.data[6]) == channel);
1324 UASSERT(readU8(&p1.data[7]) == data1[0]);
1326 //infostream<<"initial data1[0]="<<((u32)data1[0]&0xff)<<std::endl;
1328 SharedBuffer<u8> p2 = con::makeReliablePacket(data1, seqnum);
1330 /*infostream<<"p2.getSize()="<<p2.getSize()<<", data1.getSize()="
1331 <<data1.getSize()<<std::endl;
1332 infostream<<"readU8(&p2[3])="<<readU8(&p2[3])
1333 <<" p2[3]="<<((u32)p2[3]&0xff)<<std::endl;
1334 infostream<<"data1[0]="<<((u32)data1[0]&0xff)<<std::endl;*/
1336 UASSERT(p2.getSize() == 3 + data1.getSize());
1337 UASSERT(readU8(&p2[0]) == TYPE_RELIABLE);
1338 UASSERT(readU16(&p2[1]) == seqnum);
1339 UASSERT(readU8(&p2[3]) == data1[0]);
1342 struct Handler : public con::PeerHandler
1344 Handler(const char *a_name)
1350 void peerAdded(con::Peer *peer)
1352 infostream<<"Handler("<<name<<")::peerAdded(): "
1353 "id="<<peer->id<<std::endl;
1357 void deletingPeer(con::Peer *peer, bool timeout)
1359 infostream<<"Handler("<<name<<")::deletingPeer(): "
1361 <<", timeout="<<timeout<<std::endl;
1373 DSTACK("TestConnection::Run");
1378 Test some real connections
1380 NOTE: This mostly tests the legacy interface.
1383 u32 proto_id = 0xad26846a;
1385 Handler hand_server("server");
1386 Handler hand_client("client");
1388 infostream<<"** Creating server Connection"<<std::endl;
1389 con::Connection server(proto_id, 512, 5.0, &hand_server);
1390 server.Serve(30001);
1392 infostream<<"** Creating client Connection"<<std::endl;
1393 con::Connection client(proto_id, 512, 5.0, &hand_client);
1395 UASSERT(hand_server.count == 0);
1396 UASSERT(hand_client.count == 0);
1400 Address server_address(127,0,0,1, 30001);
1401 infostream<<"** running client.Connect()"<<std::endl;
1402 client.Connect(server_address);
1406 // Client should not have added client yet
1407 UASSERT(hand_client.count == 0);
1412 SharedBuffer<u8> data;
1413 infostream<<"** running client.Receive()"<<std::endl;
1414 u32 size = client.Receive(peer_id, data);
1415 infostream<<"** Client received: peer_id="<<peer_id
1419 catch(con::NoIncomingDataException &e)
1423 // Client should have added server now
1424 UASSERT(hand_client.count == 1);
1425 UASSERT(hand_client.last_id == 1);
1426 // Server should not have added client yet
1427 UASSERT(hand_server.count == 0);
1434 SharedBuffer<u8> data;
1435 infostream<<"** running server.Receive()"<<std::endl;
1436 u32 size = server.Receive(peer_id, data);
1437 infostream<<"** Server received: peer_id="<<peer_id
1441 catch(con::NoIncomingDataException &e)
1443 // No actual data received, but the client has
1444 // probably been connected
1447 // Client should be the same
1448 UASSERT(hand_client.count == 1);
1449 UASSERT(hand_client.last_id == 1);
1450 // Server should have the client
1451 UASSERT(hand_server.count == 1);
1452 UASSERT(hand_server.last_id == 2);
1456 while(client.Connected() == false)
1461 SharedBuffer<u8> data;
1462 infostream<<"** running client.Receive()"<<std::endl;
1463 u32 size = client.Receive(peer_id, data);
1464 infostream<<"** Client received: peer_id="<<peer_id
1468 catch(con::NoIncomingDataException &e)
1479 SharedBuffer<u8> data;
1480 infostream<<"** running server.Receive()"<<std::endl;
1481 u32 size = server.Receive(peer_id, data);
1482 infostream<<"** Server received: peer_id="<<peer_id
1486 catch(con::NoIncomingDataException &e)
1491 Simple send-receive test
1494 /*u8 data[] = "Hello World!";
1495 u32 datasize = sizeof(data);*/
1496 SharedBuffer<u8> data = SharedBufferFromString("Hello World!");
1498 infostream<<"** running client.Send()"<<std::endl;
1499 client.Send(PEER_ID_SERVER, 0, data, true);
1504 SharedBuffer<u8> recvdata;
1505 infostream<<"** running server.Receive()"<<std::endl;
1506 u32 size = server.Receive(peer_id, recvdata);
1507 infostream<<"** Server received: peer_id="<<peer_id
1511 UASSERT(memcmp(*data, *recvdata, data.getSize()) == 0);
1514 u16 peer_id_client = 2;
1517 Send consequent packets in different order
1518 Not compatible with new Connection, thus commented out.
1521 //u8 data1[] = "hello1";
1522 //u8 data2[] = "hello2";
1523 SharedBuffer<u8> data1 = SharedBufferFromString("hello1");
1524 SharedBuffer<u8> data2 = SharedBufferFromString("Hello2");
1526 Address client_address =
1527 server.GetPeerAddress(peer_id_client);
1529 infostream<<"*** Sending packets in wrong order (2,1,2)"
1533 con::Channel *ch = &server.getPeer(peer_id_client)->channels[chn];
1534 u16 sn = ch->next_outgoing_seqnum;
1535 ch->next_outgoing_seqnum = sn+1;
1536 server.Send(peer_id_client, chn, data2, true);
1537 ch->next_outgoing_seqnum = sn;
1538 server.Send(peer_id_client, chn, data1, true);
1539 ch->next_outgoing_seqnum = sn+1;
1540 server.Send(peer_id_client, chn, data2, true);
1544 infostream<<"*** Receiving the packets"<<std::endl;
1547 SharedBuffer<u8> recvdata;
1550 infostream<<"** running client.Receive()"<<std::endl;
1552 size = client.Receive(peer_id, recvdata);
1553 infostream<<"** Client received: peer_id="<<peer_id
1555 <<", data="<<*recvdata
1557 UASSERT(size == data1.getSize());
1558 UASSERT(memcmp(*data1, *recvdata, data1.getSize()) == 0);
1559 UASSERT(peer_id == PEER_ID_SERVER);
1561 infostream<<"** running client.Receive()"<<std::endl;
1563 size = client.Receive(peer_id, recvdata);
1564 infostream<<"** Client received: peer_id="<<peer_id
1566 <<", data="<<*recvdata
1568 UASSERT(size == data2.getSize());
1569 UASSERT(memcmp(*data2, *recvdata, data2.getSize()) == 0);
1570 UASSERT(peer_id == PEER_ID_SERVER);
1572 bool got_exception = false;
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
1583 catch(con::NoIncomingDataException &e)
1585 infostream<<"** No incoming data for client"<<std::endl;
1586 got_exception = true;
1588 UASSERT(got_exception);
1593 Send large amounts of packets (infinite test)
1594 Commented out because of infinity.
1597 infostream<<"Sending large amounts of packets (infinite test)"<<std::endl;
1600 int datasize = myrand_range(0,5)==0?myrand_range(100,10000):myrand_range(0,100);
1601 infostream<<"datasize="<<datasize<<std::endl;
1602 SharedBuffer<u8> data1(datasize);
1603 for(u16 i=0; i<datasize; i++)
1606 int sendtimes = myrand_range(1,10);
1607 for(int i=0; i<sendtimes; i++){
1608 server.Send(peer_id_client, 0, data1, true);
1611 infostream<<"sendcount="<<sendcount<<std::endl;
1613 //int receivetimes = myrand_range(1,20);
1614 int receivetimes = 20;
1615 for(int i=0; i<receivetimes; i++){
1616 SharedBuffer<u8> recvdata;
1619 bool received = false;
1621 size = client.Receive(peer_id, recvdata);
1623 }catch(con::NoIncomingDataException &e){
1633 const int datasize = 30000;
1634 SharedBuffer<u8> data1(datasize);
1635 for(u16 i=0; i<datasize; i++){
1639 infostream<<"Sending data (size="<<datasize<<"):";
1640 for(int i=0; i<datasize && i<20; i++){
1641 if(i%2==0) infostream<<" ";
1643 snprintf(buf, 10, "%.2X", ((int)((const char*)*data1)[i])&0xff);
1648 infostream<<std::endl;
1650 server.Send(peer_id_client, 0, data1, true);
1654 SharedBuffer<u8> recvdata;
1655 infostream<<"** running client.Receive()"<<std::endl;
1658 bool received = false;
1659 u32 timems0 = porting::getTimeMs();
1661 if(porting::getTimeMs() - timems0 > 5000 || received)
1664 size = client.Receive(peer_id, recvdata);
1666 }catch(con::NoIncomingDataException &e){
1671 infostream<<"** Client received: peer_id="<<peer_id
1675 infostream<<"Received data (size="<<size<<"): ";
1676 for(int i=0; i<size && i<20; i++){
1677 if(i%2==0) infostream<<" ";
1679 snprintf(buf, 10, "%.2X", ((int)(recvdata[i]))&0xff);
1684 infostream<<std::endl;
1686 UASSERT(memcmp(*data1, *recvdata, data1.getSize()) == 0);
1687 UASSERT(peer_id == PEER_ID_SERVER);
1690 // Check peer handlers
1691 UASSERT(hand_client.count == 1);
1692 UASSERT(hand_client.last_id == 1);
1693 UASSERT(hand_server.count == 1);
1694 UASSERT(hand_server.last_id == 2);
1703 infostream<<"Running " #X <<std::endl;\
1706 tests_failed += x.test_failed ? 1 : 0;\
1709 #define TESTPARAMS(X, ...)\
1712 infostream<<"Running " #X <<std::endl;\
1713 x.Run(__VA_ARGS__);\
1715 tests_failed += x.test_failed ? 1 : 0;\
1720 DSTACK(__FUNCTION_NAME);
1723 int tests_failed = 0;
1725 // Create item and node definitions
1726 IWritableItemDefManager *idef = createItemDefManager();
1727 IWritableNodeDefManager *ndef = createNodeDefManager();
1728 define_some_nodes(idef, ndef);
1730 infostream<<"run_tests() started"<<std::endl;
1731 TEST(TestUtilities);
1734 TEST(TestSerialization);
1735 TESTPARAMS(TestMapNode, ndef);
1736 TESTPARAMS(TestVoxelManipulator, ndef);
1737 TESTPARAMS(TestVoxelAlgorithms, ndef);
1738 TESTPARAMS(TestInventory, idef);
1739 //TEST(TestMapBlock);
1740 //TEST(TestMapSector);
1741 TEST(TestCollision);
1742 if(INTERNET_SIMULATOR == false){
1744 dout_con<<"=== BEGIN RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
1745 TEST(TestConnection);
1746 dout_con<<"=== END RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
1748 if(tests_failed == 0){
1749 infostream<<"run_tests(): "<<tests_failed<<" / "<<tests_run<<" tests failed."<<std::endl;
1750 infostream<<"run_tests() passed."<<std::endl;
1753 errorstream<<"run_tests(): "<<tests_failed<<" / "<<tests_run<<" tests failed."<<std::endl;
1754 errorstream<<"run_tests() aborting."<<std::endl;