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 "common_irrlicht.h"
27 #include "connection.h"
28 #include "serialization.h"
32 #include "content_mapnode.h"
34 #include "mapsector.h"
37 #include "util/string.h"
38 #include "voxelalgorithms.h"
39 #include "inventory.h"
40 #include "util/numeric.h"
41 #include "util/serialize.h"
44 Asserts that the exception occurs
46 #define EXCEPTION_CHECK(EType, code)\
48 bool exception_thrown = false;\
50 catch(EType &e) { exception_thrown = true; }\
51 assert(exception_thrown);\
55 A few item and node definitions for those tests that need them
58 #define CONTENT_STONE 0
59 #define CONTENT_GRASS 0x800
60 #define CONTENT_TORCH 100
62 void define_some_nodes(IWritableItemDefManager *idef, IWritableNodeDefManager *ndef)
65 ItemDefinition itemdef;
72 itemdef = ItemDefinition();
73 itemdef.type = ITEM_NODE;
74 itemdef.name = "default:stone";
75 itemdef.description = "Stone";
76 itemdef.groups["cracky"] = 3;
77 itemdef.inventory_image = "[inventorycube"
81 f = ContentFeatures();
82 f.name = itemdef.name;
83 for(int i = 0; i < 6; i++)
84 f.tiledef[i].name = "default_stone.png";
85 f.is_ground_content = true;
86 idef->registerItem(itemdef);
93 itemdef = ItemDefinition();
94 itemdef.type = ITEM_NODE;
95 itemdef.name = "default:dirt_with_grass";
96 itemdef.description = "Dirt with grass";
97 itemdef.groups["crumbly"] = 3;
98 itemdef.inventory_image = "[inventorycube"
100 "{default_dirt.png&default_grass_side.png"
101 "{default_dirt.png&default_grass_side.png";
102 f = ContentFeatures();
103 f.name = itemdef.name;
104 f.tiledef[0].name = "default_grass.png";
105 f.tiledef[1].name = "default_dirt.png";
106 for(int i = 2; i < 6; i++)
107 f.tiledef[i].name = "default_dirt.png^default_grass_side.png";
108 f.is_ground_content = true;
109 idef->registerItem(itemdef);
113 Torch (minimal definition for lighting tests)
116 itemdef = ItemDefinition();
117 itemdef.type = ITEM_NODE;
118 itemdef.name = "default:torch";
119 f = ContentFeatures();
120 f.name = itemdef.name;
121 f.param_type = CPT_LIGHT;
122 f.light_propagates = true;
123 f.sunlight_propagates = true;
124 f.light_source = LIGHT_MAX-1;
125 idef->registerItem(itemdef);
133 /*infostream<<"wrapDegrees(100.0) = "<<wrapDegrees(100.0)<<std::endl;
134 infostream<<"wrapDegrees(720.5) = "<<wrapDegrees(720.5)<<std::endl;
135 infostream<<"wrapDegrees(-0.5) = "<<wrapDegrees(-0.5)<<std::endl;*/
136 assert(fabs(wrapDegrees(100.0) - 100.0) < 0.001);
137 assert(fabs(wrapDegrees(720.5) - 0.5) < 0.001);
138 assert(fabs(wrapDegrees(-0.5) - (-0.5)) < 0.001);
139 assert(fabs(wrapDegrees(-365.5) - (-5.5)) < 0.001);
140 assert(lowercase("Foo bAR") == "foo bar");
141 assert(is_yes("YeS") == true);
142 assert(is_yes("") == false);
143 assert(is_yes("FAlse") == false);
144 const char *ends[] = {"abc", "c", "bc", NULL};
145 assert(removeStringEnd("abc", ends) == "");
146 assert(removeStringEnd("bc", ends) == "b");
147 assert(removeStringEnd("12c", ends) == "12");
148 assert(removeStringEnd("foo", ends) == "");
157 // Test reading of settings
158 s.parseConfigLine("leet = 1337");
159 s.parseConfigLine("leetleet = 13371337");
160 s.parseConfigLine("leetleet_neg = -13371337");
161 s.parseConfigLine("floaty_thing = 1.1");
162 s.parseConfigLine("stringy_thing = asd /( ¤%&(/\" BLÖÄRP");
163 s.parseConfigLine("coord = (1, 2, 4.5)");
164 assert(s.getS32("leet") == 1337);
165 assert(s.getS16("leetleet") == 32767);
166 assert(s.getS16("leetleet_neg") == -32768);
167 // Not sure if 1.1 is an exact value as a float, but doesn't matter
168 assert(fabs(s.getFloat("floaty_thing") - 1.1) < 0.001);
169 assert(s.get("stringy_thing") == "asd /( ¤%&(/\" BLÖÄRP");
170 assert(fabs(s.getV3F("coord").X - 1.0) < 0.001);
171 assert(fabs(s.getV3F("coord").Y - 2.0) < 0.001);
172 assert(fabs(s.getV3F("coord").Z - 4.5) < 0.001);
173 // Test the setting of settings too
174 s.setFloat("floaty_thing_2", 1.2);
175 s.setV3F("coord2", v3f(1, 2, 3.3));
176 assert(s.get("floaty_thing_2").substr(0,3) == "1.2");
177 assert(fabs(s.getFloat("floaty_thing_2") - 1.2) < 0.001);
178 assert(fabs(s.getV3F("coord2").X - 1.0) < 0.001);
179 assert(fabs(s.getV3F("coord2").Y - 2.0) < 0.001);
180 assert(fabs(s.getV3F("coord2").Z - 3.3) < 0.001);
184 struct TestSerialization
186 // To be used like this:
187 // mkstr("Some\0string\0with\0embedded\0nuls")
188 // since std::string("...") doesn't work as expected in that case.
189 template<size_t N> std::string mkstr(const char (&s)[N])
191 return std::string(s, N - 1);
196 // Tests some serialization primitives
198 assert(serializeString("") == mkstr("\0\0"));
199 assert(serializeWideString(L"") == mkstr("\0\0"));
200 assert(serializeLongString("") == mkstr("\0\0\0\0"));
201 assert(serializeJsonString("") == "\"\"");
203 std::string teststring = "Hello world!";
204 assert(serializeString(teststring) ==
205 mkstr("\0\14Hello world!"));
206 assert(serializeWideString(narrow_to_wide(teststring)) ==
207 mkstr("\0\14\0H\0e\0l\0l\0o\0 \0w\0o\0r\0l\0d\0!"));
208 assert(serializeLongString(teststring) ==
209 mkstr("\0\0\0\14Hello world!"));
210 assert(serializeJsonString(teststring) ==
213 std::string teststring2;
214 std::wstring teststring2_w;
215 std::string teststring2_w_encoded;
217 std::ostringstream tmp_os;
218 std::wostringstream tmp_os_w;
219 std::ostringstream tmp_os_w_encoded;
220 for(int i = 0; i < 256; i++)
223 tmp_os_w<<(wchar_t)i;
224 tmp_os_w_encoded<<(char)0<<(char)i;
226 teststring2 = tmp_os.str();
227 teststring2_w = tmp_os_w.str();
228 teststring2_w_encoded = tmp_os_w_encoded.str();
230 assert(serializeString(teststring2) ==
231 mkstr("\1\0") + teststring2);
232 assert(serializeWideString(teststring2_w) ==
233 mkstr("\1\0") + teststring2_w_encoded);
234 assert(serializeLongString(teststring2) ==
235 mkstr("\0\0\1\0") + teststring2);
236 // MSVC fails when directly using "\\\\"
237 std::string backslash = "\\";
238 assert(serializeJsonString(teststring2) ==
240 "\\u0000\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007" +
241 "\\b\\t\\n\\u000b\\f\\r\\u000e\\u000f" +
242 "\\u0010\\u0011\\u0012\\u0013\\u0014\\u0015\\u0016\\u0017" +
243 "\\u0018\\u0019\\u001a\\u001b\\u001c\\u001d\\u001e\\u001f" +
244 " !\\\"" + teststring2.substr(0x23, 0x2f-0x23) +
245 "\\/" + teststring2.substr(0x30, 0x5c-0x30) +
246 backslash + backslash + teststring2.substr(0x5d, 0x7f-0x5d) + "\\u007f" +
247 "\\u0080\\u0081\\u0082\\u0083\\u0084\\u0085\\u0086\\u0087" +
248 "\\u0088\\u0089\\u008a\\u008b\\u008c\\u008d\\u008e\\u008f" +
249 "\\u0090\\u0091\\u0092\\u0093\\u0094\\u0095\\u0096\\u0097" +
250 "\\u0098\\u0099\\u009a\\u009b\\u009c\\u009d\\u009e\\u009f" +
251 "\\u00a0\\u00a1\\u00a2\\u00a3\\u00a4\\u00a5\\u00a6\\u00a7" +
252 "\\u00a8\\u00a9\\u00aa\\u00ab\\u00ac\\u00ad\\u00ae\\u00af" +
253 "\\u00b0\\u00b1\\u00b2\\u00b3\\u00b4\\u00b5\\u00b6\\u00b7" +
254 "\\u00b8\\u00b9\\u00ba\\u00bb\\u00bc\\u00bd\\u00be\\u00bf" +
255 "\\u00c0\\u00c1\\u00c2\\u00c3\\u00c4\\u00c5\\u00c6\\u00c7" +
256 "\\u00c8\\u00c9\\u00ca\\u00cb\\u00cc\\u00cd\\u00ce\\u00cf" +
257 "\\u00d0\\u00d1\\u00d2\\u00d3\\u00d4\\u00d5\\u00d6\\u00d7" +
258 "\\u00d8\\u00d9\\u00da\\u00db\\u00dc\\u00dd\\u00de\\u00df" +
259 "\\u00e0\\u00e1\\u00e2\\u00e3\\u00e4\\u00e5\\u00e6\\u00e7" +
260 "\\u00e8\\u00e9\\u00ea\\u00eb\\u00ec\\u00ed\\u00ee\\u00ef" +
261 "\\u00f0\\u00f1\\u00f2\\u00f3\\u00f4\\u00f5\\u00f6\\u00f7" +
262 "\\u00f8\\u00f9\\u00fa\\u00fb\\u00fc\\u00fd\\u00fe\\u00ff" +
266 std::istringstream is(serializeString(teststring2), std::ios::binary);
267 assert(deSerializeString(is) == teststring2);
273 std::istringstream is(serializeWideString(teststring2_w), std::ios::binary);
274 assert(deSerializeWideString(is) == teststring2_w);
280 std::istringstream is(serializeLongString(teststring2), std::ios::binary);
281 assert(deSerializeLongString(is) == teststring2);
287 std::istringstream is(serializeJsonString(teststring2), std::ios::binary);
288 //dstream<<serializeJsonString(deSerializeJsonString(is));
289 assert(deSerializeJsonString(is) == teststring2);
303 SharedBuffer<u8> fromdata(4);
309 std::ostringstream os(std::ios_base::binary);
310 compress(fromdata, os, 0);
312 std::string str_out = os.str();
314 infostream<<"str_out.size()="<<str_out.size()<<std::endl;
315 infostream<<"TestCompress: 1,5,5,1 -> ";
316 for(u32 i=0; i<str_out.size(); i++)
318 infostream<<(u32)str_out[i]<<",";
320 infostream<<std::endl;
322 assert(str_out.size() == 10);
324 assert(str_out[0] == 0);
325 assert(str_out[1] == 0);
326 assert(str_out[2] == 0);
327 assert(str_out[3] == 4);
328 assert(str_out[4] == 0);
329 assert(str_out[5] == 1);
330 assert(str_out[6] == 1);
331 assert(str_out[7] == 5);
332 assert(str_out[8] == 0);
333 assert(str_out[9] == 1);
335 std::istringstream is(str_out, std::ios_base::binary);
336 std::ostringstream os2(std::ios_base::binary);
338 decompress(is, os2, 0);
339 std::string str_out2 = os2.str();
341 infostream<<"decompress: ";
342 for(u32 i=0; i<str_out2.size(); i++)
344 infostream<<(u32)str_out2[i]<<",";
346 infostream<<std::endl;
348 assert(str_out2.size() == fromdata.getSize());
350 for(u32 i=0; i<str_out2.size(); i++)
352 assert(str_out2[i] == fromdata[i]);
359 SharedBuffer<u8> fromdata(4);
365 std::ostringstream os(std::ios_base::binary);
366 compress(fromdata, os, SER_FMT_VER_HIGHEST);
368 std::string str_out = os.str();
370 infostream<<"str_out.size()="<<str_out.size()<<std::endl;
371 infostream<<"TestCompress: 1,5,5,1 -> ";
372 for(u32 i=0; i<str_out.size(); i++)
374 infostream<<(u32)str_out[i]<<",";
376 infostream<<std::endl;
378 /*assert(str_out.size() == 10);
380 assert(str_out[0] == 0);
381 assert(str_out[1] == 0);
382 assert(str_out[2] == 0);
383 assert(str_out[3] == 4);
384 assert(str_out[4] == 0);
385 assert(str_out[5] == 1);
386 assert(str_out[6] == 1);
387 assert(str_out[7] == 5);
388 assert(str_out[8] == 0);
389 assert(str_out[9] == 1);*/
391 std::istringstream is(str_out, std::ios_base::binary);
392 std::ostringstream os2(std::ios_base::binary);
394 decompress(is, os2, SER_FMT_VER_HIGHEST);
395 std::string str_out2 = os2.str();
397 infostream<<"decompress: ";
398 for(u32 i=0; i<str_out2.size(); i++)
400 infostream<<(u32)str_out2[i]<<",";
402 infostream<<std::endl;
404 assert(str_out2.size() == fromdata.getSize());
406 for(u32 i=0; i<str_out2.size(); i++)
408 assert(str_out2[i] == fromdata[i]);
417 void Run(INodeDefManager *nodedef)
422 assert(n.getContent() == CONTENT_AIR);
423 assert(n.getLight(LIGHTBANK_DAY, nodedef) == 0);
424 assert(n.getLight(LIGHTBANK_NIGHT, nodedef) == 0);
427 n.setContent(CONTENT_AIR);
428 assert(nodedef->get(n).light_propagates == true);
429 n.setContent(LEGN(nodedef, "CONTENT_STONE"));
430 assert(nodedef->get(n).light_propagates == false);
434 struct TestVoxelManipulator
436 void Run(INodeDefManager *nodedef)
442 VoxelArea a(v3s16(-1,-1,-1), v3s16(1,1,1));
443 assert(a.index(0,0,0) == 1*3*3 + 1*3 + 1);
444 assert(a.index(-1,-1,-1) == 0);
446 VoxelArea c(v3s16(-2,-2,-2), v3s16(2,2,2));
447 // An area that is 1 bigger in x+ and z-
448 VoxelArea d(v3s16(-2,-2,-3), v3s16(3,2,2));
450 core::list<VoxelArea> aa;
454 core::array<VoxelArea> results;
455 results.push_back(VoxelArea(v3s16(-2,-2,-3),v3s16(3,2,-3)));
456 results.push_back(VoxelArea(v3s16(3,-2,-2),v3s16(3,2,2)));
458 assert(aa.size() == results.size());
460 infostream<<"Result of diff:"<<std::endl;
461 for(core::list<VoxelArea>::Iterator
462 i = aa.begin(); i != aa.end(); i++)
464 i->print(infostream);
465 infostream<<std::endl;
467 s32 j = results.linear_search(*i);
479 v.print(infostream, nodedef);
481 infostream<<"*** Setting (-1,0,-1)=2 ***"<<std::endl;
483 v.setNodeNoRef(v3s16(-1,0,-1), MapNode(CONTENT_GRASS));
485 v.print(infostream, nodedef);
487 assert(v.getNode(v3s16(-1,0,-1)).getContent() == CONTENT_GRASS);
489 infostream<<"*** Reading from inexistent (0,0,-1) ***"<<std::endl;
491 EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,0,-1)));
493 v.print(infostream, nodedef);
495 infostream<<"*** Adding area ***"<<std::endl;
499 v.print(infostream, nodedef);
501 assert(v.getNode(v3s16(-1,0,-1)).getContent() == CONTENT_GRASS);
502 EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,1,1)));
506 struct TestVoxelAlgorithms
508 void Run(INodeDefManager *ndef)
511 voxalgo::propagateSunlight
515 for(u16 z=0; z<3; z++)
516 for(u16 y=0; y<3; y++)
517 for(u16 x=0; x<3; x++)
520 v.setNodeNoRef(p, MapNode(CONTENT_AIR));
522 VoxelArea a(v3s16(0,0,0), v3s16(2,2,2));
524 core::map<v3s16, bool> light_sources;
525 voxalgo::setLight(v, a, 0, ndef);
526 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
527 v, a, true, light_sources, ndef);
528 //v.print(dstream, ndef, VOXELPRINT_LIGHT_DAY);
529 assert(res.bottom_sunlight_valid == true);
530 assert(v.getNode(v3s16(1,1,1)).getLight(LIGHTBANK_DAY, ndef)
533 v.setNodeNoRef(v3s16(0,0,0), MapNode(CONTENT_STONE));
535 core::map<v3s16, bool> light_sources;
536 voxalgo::setLight(v, a, 0, ndef);
537 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
538 v, a, true, light_sources, ndef);
539 assert(res.bottom_sunlight_valid == true);
540 assert(v.getNode(v3s16(1,1,1)).getLight(LIGHTBANK_DAY, ndef)
544 core::map<v3s16, bool> light_sources;
545 voxalgo::setLight(v, a, 0, ndef);
546 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
547 v, a, false, light_sources, ndef);
548 assert(res.bottom_sunlight_valid == true);
549 assert(v.getNode(v3s16(2,0,2)).getLight(LIGHTBANK_DAY, ndef)
552 v.setNodeNoRef(v3s16(1,3,2), MapNode(CONTENT_STONE));
554 core::map<v3s16, bool> light_sources;
555 voxalgo::setLight(v, a, 0, ndef);
556 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
557 v, a, true, light_sources, ndef);
558 assert(res.bottom_sunlight_valid == true);
559 assert(v.getNode(v3s16(1,1,2)).getLight(LIGHTBANK_DAY, ndef)
563 core::map<v3s16, bool> light_sources;
564 voxalgo::setLight(v, a, 0, ndef);
565 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
566 v, a, false, light_sources, ndef);
567 assert(res.bottom_sunlight_valid == true);
568 assert(v.getNode(v3s16(1,0,2)).getLight(LIGHTBANK_DAY, ndef)
572 MapNode n(CONTENT_AIR);
573 n.setLight(LIGHTBANK_DAY, 10, ndef);
574 v.setNodeNoRef(v3s16(1,-1,2), n);
577 core::map<v3s16, bool> light_sources;
578 voxalgo::setLight(v, a, 0, ndef);
579 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
580 v, a, true, light_sources, ndef);
581 assert(res.bottom_sunlight_valid == true);
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 assert(res.bottom_sunlight_valid == true);
591 MapNode n(CONTENT_AIR);
592 n.setLight(LIGHTBANK_DAY, LIGHT_SUN, ndef);
593 v.setNodeNoRef(v3s16(1,-1,2), n);
596 core::map<v3s16, bool> light_sources;
597 voxalgo::setLight(v, a, 0, ndef);
598 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
599 v, a, true, light_sources, ndef);
600 assert(res.bottom_sunlight_valid == false);
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 assert(res.bottom_sunlight_valid == false);
609 v.setNodeNoRef(v3s16(1,3,2), MapNode(CONTENT_IGNORE));
611 core::map<v3s16, bool> light_sources;
612 voxalgo::setLight(v, a, 0, ndef);
613 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
614 v, a, true, light_sources, ndef);
615 assert(res.bottom_sunlight_valid == true);
619 voxalgo::clearLightAndCollectSources
623 for(u16 z=0; z<3; z++)
624 for(u16 y=0; y<3; y++)
625 for(u16 x=0; x<3; x++)
628 v.setNode(p, MapNode(CONTENT_AIR));
630 VoxelArea a(v3s16(0,0,0), v3s16(2,2,2));
631 v.setNodeNoRef(v3s16(0,0,0), MapNode(CONTENT_STONE));
632 v.setNodeNoRef(v3s16(1,1,1), MapNode(CONTENT_TORCH));
634 MapNode n(CONTENT_AIR);
635 n.setLight(LIGHTBANK_DAY, 1, ndef);
636 v.setNode(v3s16(1,1,2), n);
639 core::map<v3s16, bool> light_sources;
640 core::map<v3s16, u8> unlight_from;
641 voxalgo::clearLightAndCollectSources(v, a, LIGHTBANK_DAY,
642 ndef, light_sources, unlight_from);
643 //v.print(dstream, ndef, VOXELPRINT_LIGHT_DAY);
644 assert(v.getNode(v3s16(0,1,1)).getLight(LIGHTBANK_DAY, ndef)
646 assert(light_sources.find(v3s16(1,1,1)) != NULL);
647 assert(light_sources.size() == 1);
648 assert(unlight_from.find(v3s16(1,1,2)) != NULL);
649 assert(unlight_from.size() == 1);
657 void Run(IItemDefManager *idef)
659 std::string serialized_inventory =
670 "Item default:cobble 61\n"
677 "Item default:dirt 71\n"
685 "Item default:dirt 99\n"
686 "Item default:cobble 38\n"
696 std::string serialized_inventory_2 =
707 "Item default:cobble 61\n"
714 "Item default:dirt 71\n"
722 "Item default:dirt 99\n"
723 "Item default:cobble 38\n"
734 std::istringstream is(serialized_inventory, std::ios::binary);
736 assert(inv.getList("0"));
737 assert(!inv.getList("main"));
738 inv.getList("0")->setName("main");
739 assert(!inv.getList("0"));
740 assert(inv.getList("main"));
741 std::ostringstream inv_os(std::ios::binary);
742 inv.serialize(inv_os);
743 assert(inv_os.str() == serialized_inventory_2);
748 NOTE: These tests became non-working then NodeContainer was removed.
749 These should be redone, utilizing some kind of a virtual
750 interface for Map (IMap would be fine).
755 class TC : public NodeContainer
761 core::list<v3s16> validity_exceptions;
765 position_valid = true;
768 virtual bool isValidPosition(v3s16 p)
770 //return position_valid ^ (p==position_valid_exception);
771 bool exception = false;
772 for(core::list<v3s16>::Iterator i=validity_exceptions.begin();
773 i != validity_exceptions.end(); i++)
781 return exception ? !position_valid : position_valid;
784 virtual MapNode getNode(v3s16 p)
786 if(isValidPosition(p) == false)
787 throw InvalidPositionException();
791 virtual void setNode(v3s16 p, MapNode & n)
793 if(isValidPosition(p) == false)
794 throw InvalidPositionException();
797 virtual u16 nodeContainerId() const
807 MapBlock b(&parent, v3s16(1,1,1));
808 v3s16 relpos(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
810 assert(b.getPosRelative() == relpos);
812 assert(b.getBox().MinEdge.X == MAP_BLOCKSIZE);
813 assert(b.getBox().MaxEdge.X == MAP_BLOCKSIZE*2-1);
814 assert(b.getBox().MinEdge.Y == MAP_BLOCKSIZE);
815 assert(b.getBox().MaxEdge.Y == MAP_BLOCKSIZE*2-1);
816 assert(b.getBox().MinEdge.Z == MAP_BLOCKSIZE);
817 assert(b.getBox().MaxEdge.Z == MAP_BLOCKSIZE*2-1);
819 assert(b.isValidPosition(v3s16(0,0,0)) == true);
820 assert(b.isValidPosition(v3s16(-1,0,0)) == false);
821 assert(b.isValidPosition(v3s16(-1,-142,-2341)) == false);
822 assert(b.isValidPosition(v3s16(-124,142,2341)) == false);
823 assert(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
824 assert(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE,MAP_BLOCKSIZE-1)) == false);
827 TODO: this method should probably be removed
828 if the block size isn't going to be set variable
830 /*assert(b.getSizeNodes() == v3s16(MAP_BLOCKSIZE,
831 MAP_BLOCKSIZE, MAP_BLOCKSIZE));*/
833 // Changed flag should be initially set
834 assert(b.getModified() == MOD_STATE_WRITE_NEEDED);
836 assert(b.getModified() == MOD_STATE_CLEAN);
838 // All nodes should have been set to
839 // .d=CONTENT_IGNORE and .getLight() = 0
840 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
841 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
842 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
844 //assert(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_AIR);
845 assert(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_IGNORE);
846 assert(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_DAY) == 0);
847 assert(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_NIGHT) == 0);
851 MapNode n(CONTENT_AIR);
852 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
853 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
854 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
856 b.setNode(v3s16(x,y,z), n);
861 Parent fetch functions
863 parent.position_valid = false;
864 parent.node.setContent(5);
868 // Positions in the block should still be valid
869 assert(b.isValidPositionParent(v3s16(0,0,0)) == true);
870 assert(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
871 n = b.getNodeParent(v3s16(0,MAP_BLOCKSIZE-1,0));
872 assert(n.getContent() == CONTENT_AIR);
874 // ...but outside the block they should be invalid
875 assert(b.isValidPositionParent(v3s16(-121,2341,0)) == false);
876 assert(b.isValidPositionParent(v3s16(-1,0,0)) == false);
877 assert(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == false);
880 bool exception_thrown = false;
882 // This should throw an exception
883 MapNode n = b.getNodeParent(v3s16(0,0,-1));
885 catch(InvalidPositionException &e)
887 exception_thrown = true;
889 assert(exception_thrown);
892 parent.position_valid = true;
893 // Now the positions outside should be valid
894 assert(b.isValidPositionParent(v3s16(-121,2341,0)) == true);
895 assert(b.isValidPositionParent(v3s16(-1,0,0)) == true);
896 assert(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == true);
897 n = b.getNodeParent(v3s16(0,0,MAP_BLOCKSIZE));
898 assert(n.getContent() == 5);
906 assert(b.getNode(p).getContent() == 4);
907 //TODO: Update to new system
908 /*assert(b.getNodeTile(p) == 4);
909 assert(b.getNodeTile(v3s16(-1,-1,0)) == 5);*/
914 // Set lighting of all nodes to 0
915 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
916 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
917 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
918 MapNode n = b.getNode(v3s16(x,y,z));
919 n.setLight(LIGHTBANK_DAY, 0);
920 n.setLight(LIGHTBANK_NIGHT, 0);
921 b.setNode(v3s16(x,y,z), n);
927 Check how the block handles being a lonely sky block
929 parent.position_valid = true;
930 b.setIsUnderground(false);
931 parent.node.setContent(CONTENT_AIR);
932 parent.node.setLight(LIGHTBANK_DAY, LIGHT_SUN);
933 parent.node.setLight(LIGHTBANK_NIGHT, 0);
934 core::map<v3s16, bool> light_sources;
935 // The bottom block is invalid, because we have a shadowing node
936 assert(b.propagateSunlight(light_sources) == false);
937 assert(b.getNode(v3s16(1,4,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
938 assert(b.getNode(v3s16(1,3,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
939 assert(b.getNode(v3s16(1,2,0)).getLight(LIGHTBANK_DAY) == 0);
940 assert(b.getNode(v3s16(1,1,0)).getLight(LIGHTBANK_DAY) == 0);
941 assert(b.getNode(v3s16(1,0,0)).getLight(LIGHTBANK_DAY) == 0);
942 assert(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
943 assert(b.getFaceLight2(1000, p, v3s16(0,1,0)) == LIGHT_SUN);
944 assert(b.getFaceLight2(1000, p, v3s16(0,-1,0)) == 0);
945 assert(b.getFaceLight2(0, p, v3s16(0,-1,0)) == 0);
946 // According to MapBlock::getFaceLight,
947 // The face on the z+ side should have double-diminished light
948 //assert(b.getFaceLight(p, v3s16(0,0,1)) == diminish_light(diminish_light(LIGHT_MAX)));
949 // The face on the z+ side should have diminished light
950 assert(b.getFaceLight2(1000, p, v3s16(0,0,1)) == diminish_light(LIGHT_MAX));
953 Check how the block handles being in between blocks with some non-sunlight
954 while being underground
957 // Make neighbours to exist and set some non-sunlight to them
958 parent.position_valid = true;
959 b.setIsUnderground(true);
960 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
961 core::map<v3s16, bool> light_sources;
962 // The block below should be valid because there shouldn't be
963 // sunlight in there either
964 assert(b.propagateSunlight(light_sources, true) == true);
965 // Should not touch nodes that are not affected (that is, all of them)
966 //assert(b.getNode(v3s16(1,2,3)).getLight() == LIGHT_SUN);
967 // Should set light of non-sunlighted blocks to 0.
968 assert(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == 0);
971 Set up a situation where:
972 - There is only air in this block
973 - There is a valid non-sunlighted block at the bottom, and
974 - Invalid blocks elsewhere.
975 - the block is not underground.
977 This should result in bottom block invalidity
980 b.setIsUnderground(false);
982 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
983 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
984 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
986 n.setContent(CONTENT_AIR);
987 n.setLight(LIGHTBANK_DAY, 0);
988 b.setNode(v3s16(x,y,z), n);
992 // Make neighbours invalid
993 parent.position_valid = false;
994 // Add exceptions to the top of the bottom block
995 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
996 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
998 parent.validity_exceptions.push_back(v3s16(MAP_BLOCKSIZE+x, MAP_BLOCKSIZE-1, MAP_BLOCKSIZE+z));
1000 // Lighting value for the valid nodes
1001 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
1002 core::map<v3s16, bool> light_sources;
1003 // Bottom block is not valid
1004 assert(b.propagateSunlight(light_sources) == false);
1009 struct TestMapSector
1011 class TC : public NodeContainer
1016 bool position_valid;
1020 position_valid = true;
1023 virtual bool isValidPosition(v3s16 p)
1025 return position_valid;
1028 virtual MapNode getNode(v3s16 p)
1030 if(position_valid == false)
1031 throw InvalidPositionException();
1035 virtual void setNode(v3s16 p, MapNode & n)
1037 if(position_valid == false)
1038 throw InvalidPositionException();
1041 virtual u16 nodeContainerId() const
1050 parent.position_valid = false;
1052 // Create one with no heightmaps
1053 ServerMapSector sector(&parent, v2s16(1,1));
1055 assert(sector.getBlockNoCreateNoEx(0) == 0);
1056 assert(sector.getBlockNoCreateNoEx(1) == 0);
1058 MapBlock * bref = sector.createBlankBlock(-2);
1060 assert(sector.getBlockNoCreateNoEx(0) == 0);
1061 assert(sector.getBlockNoCreateNoEx(-2) == bref);
1063 //TODO: Check for AlreadyExistsException
1065 /*bool exception_thrown = false;
1069 catch(InvalidPositionException &e){
1070 exception_thrown = true;
1072 assert(exception_thrown);*/
1082 const int port = 30003;
1086 const char sendbuffer[] = "hello world!";
1087 socket.Send(Address(127,0,0,1,port), sendbuffer, sizeof(sendbuffer));
1091 char rcvbuffer[256];
1092 memset(rcvbuffer, 0, sizeof(rcvbuffer));
1096 int bytes_read = socket.Receive(sender, rcvbuffer, sizeof(rcvbuffer));
1100 //FIXME: This fails on some systems
1101 assert(strncmp(sendbuffer, rcvbuffer, sizeof(sendbuffer))==0);
1102 assert(sender.getAddress() == Address(127,0,0,1, 0).getAddress());
1106 struct TestConnection
1111 Test helper functions
1114 // Some constants for testing
1115 u32 proto_id = 0x12345678;
1118 SharedBuffer<u8> data1(1);
1120 Address a(127,0,0,1, 10);
1123 con::BufferedPacket p1 = con::makePacket(a, data1,
1124 proto_id, peer_id, channel);
1126 We should now have a packet with this data:
1129 [4] u16 sender_peer_id
1134 assert(readU32(&p1.data[0]) == proto_id);
1135 assert(readU16(&p1.data[4]) == peer_id);
1136 assert(readU8(&p1.data[6]) == channel);
1137 assert(readU8(&p1.data[7]) == data1[0]);
1139 //infostream<<"initial data1[0]="<<((u32)data1[0]&0xff)<<std::endl;
1141 SharedBuffer<u8> p2 = con::makeReliablePacket(data1, seqnum);
1143 /*infostream<<"p2.getSize()="<<p2.getSize()<<", data1.getSize()="
1144 <<data1.getSize()<<std::endl;
1145 infostream<<"readU8(&p2[3])="<<readU8(&p2[3])
1146 <<" p2[3]="<<((u32)p2[3]&0xff)<<std::endl;
1147 infostream<<"data1[0]="<<((u32)data1[0]&0xff)<<std::endl;*/
1149 assert(p2.getSize() == 3 + data1.getSize());
1150 assert(readU8(&p2[0]) == TYPE_RELIABLE);
1151 assert(readU16(&p2[1]) == seqnum);
1152 assert(readU8(&p2[3]) == data1[0]);
1155 struct Handler : public con::PeerHandler
1157 Handler(const char *a_name)
1163 void peerAdded(con::Peer *peer)
1165 infostream<<"Handler("<<name<<")::peerAdded(): "
1166 "id="<<peer->id<<std::endl;
1170 void deletingPeer(con::Peer *peer, bool timeout)
1172 infostream<<"Handler("<<name<<")::deletingPeer(): "
1174 <<", timeout="<<timeout<<std::endl;
1186 DSTACK("TestConnection::Run");
1191 Test some real connections
1193 NOTE: This mostly tests the legacy interface.
1196 u32 proto_id = 0xad26846a;
1198 Handler hand_server("server");
1199 Handler hand_client("client");
1201 infostream<<"** Creating server Connection"<<std::endl;
1202 con::Connection server(proto_id, 512, 5.0, &hand_server);
1203 server.Serve(30001);
1205 infostream<<"** Creating client Connection"<<std::endl;
1206 con::Connection client(proto_id, 512, 5.0, &hand_client);
1208 assert(hand_server.count == 0);
1209 assert(hand_client.count == 0);
1213 Address server_address(127,0,0,1, 30001);
1214 infostream<<"** running client.Connect()"<<std::endl;
1215 client.Connect(server_address);
1219 // Client should not have added client yet
1220 assert(hand_client.count == 0);
1225 SharedBuffer<u8> data;
1226 infostream<<"** running client.Receive()"<<std::endl;
1227 u32 size = client.Receive(peer_id, data);
1228 infostream<<"** Client received: peer_id="<<peer_id
1232 catch(con::NoIncomingDataException &e)
1236 // Client should have added server now
1237 assert(hand_client.count == 1);
1238 assert(hand_client.last_id == 1);
1239 // Server should not have added client yet
1240 assert(hand_server.count == 0);
1247 SharedBuffer<u8> data;
1248 infostream<<"** running server.Receive()"<<std::endl;
1249 u32 size = server.Receive(peer_id, data);
1250 infostream<<"** Server received: peer_id="<<peer_id
1254 catch(con::NoIncomingDataException &e)
1256 // No actual data received, but the client has
1257 // probably been connected
1260 // Client should be the same
1261 assert(hand_client.count == 1);
1262 assert(hand_client.last_id == 1);
1263 // Server should have the client
1264 assert(hand_server.count == 1);
1265 assert(hand_server.last_id == 2);
1269 while(client.Connected() == false)
1274 SharedBuffer<u8> data;
1275 infostream<<"** running client.Receive()"<<std::endl;
1276 u32 size = client.Receive(peer_id, data);
1277 infostream<<"** Client received: peer_id="<<peer_id
1281 catch(con::NoIncomingDataException &e)
1292 SharedBuffer<u8> data;
1293 infostream<<"** running server.Receive()"<<std::endl;
1294 u32 size = server.Receive(peer_id, data);
1295 infostream<<"** Server received: peer_id="<<peer_id
1299 catch(con::NoIncomingDataException &e)
1304 Simple send-receive test
1307 /*u8 data[] = "Hello World!";
1308 u32 datasize = sizeof(data);*/
1309 SharedBuffer<u8> data = SharedBufferFromString("Hello World!");
1311 infostream<<"** running client.Send()"<<std::endl;
1312 client.Send(PEER_ID_SERVER, 0, data, true);
1317 SharedBuffer<u8> recvdata;
1318 infostream<<"** running server.Receive()"<<std::endl;
1319 u32 size = server.Receive(peer_id, recvdata);
1320 infostream<<"** Server received: peer_id="<<peer_id
1324 assert(memcmp(*data, *recvdata, data.getSize()) == 0);
1327 u16 peer_id_client = 2;
1330 Send consequent packets in different order
1331 Not compatible with new Connection, thus commented out.
1334 //u8 data1[] = "hello1";
1335 //u8 data2[] = "hello2";
1336 SharedBuffer<u8> data1 = SharedBufferFromString("hello1");
1337 SharedBuffer<u8> data2 = SharedBufferFromString("Hello2");
1339 Address client_address =
1340 server.GetPeerAddress(peer_id_client);
1342 infostream<<"*** Sending packets in wrong order (2,1,2)"
1346 con::Channel *ch = &server.getPeer(peer_id_client)->channels[chn];
1347 u16 sn = ch->next_outgoing_seqnum;
1348 ch->next_outgoing_seqnum = sn+1;
1349 server.Send(peer_id_client, chn, data2, true);
1350 ch->next_outgoing_seqnum = sn;
1351 server.Send(peer_id_client, chn, data1, true);
1352 ch->next_outgoing_seqnum = sn+1;
1353 server.Send(peer_id_client, chn, data2, true);
1357 infostream<<"*** Receiving the packets"<<std::endl;
1360 SharedBuffer<u8> recvdata;
1363 infostream<<"** running client.Receive()"<<std::endl;
1365 size = client.Receive(peer_id, recvdata);
1366 infostream<<"** Client received: peer_id="<<peer_id
1368 <<", data="<<*recvdata
1370 assert(size == data1.getSize());
1371 assert(memcmp(*data1, *recvdata, data1.getSize()) == 0);
1372 assert(peer_id == PEER_ID_SERVER);
1374 infostream<<"** running client.Receive()"<<std::endl;
1376 size = client.Receive(peer_id, recvdata);
1377 infostream<<"** Client received: peer_id="<<peer_id
1379 <<", data="<<*recvdata
1381 assert(size == data2.getSize());
1382 assert(memcmp(*data2, *recvdata, data2.getSize()) == 0);
1383 assert(peer_id == PEER_ID_SERVER);
1385 bool got_exception = false;
1388 infostream<<"** running client.Receive()"<<std::endl;
1390 size = client.Receive(peer_id, recvdata);
1391 infostream<<"** Client received: peer_id="<<peer_id
1393 <<", data="<<*recvdata
1396 catch(con::NoIncomingDataException &e)
1398 infostream<<"** No incoming data for client"<<std::endl;
1399 got_exception = true;
1401 assert(got_exception);
1406 Send large amounts of packets (infinite test)
1407 Commented out because of infinity.
1410 infostream<<"Sending large amounts of packets (infinite test)"<<std::endl;
1413 int datasize = myrand_range(0,5)==0?myrand_range(100,10000):myrand_range(0,100);
1414 infostream<<"datasize="<<datasize<<std::endl;
1415 SharedBuffer<u8> data1(datasize);
1416 for(u16 i=0; i<datasize; i++)
1419 int sendtimes = myrand_range(1,10);
1420 for(int i=0; i<sendtimes; i++){
1421 server.Send(peer_id_client, 0, data1, true);
1424 infostream<<"sendcount="<<sendcount<<std::endl;
1426 //int receivetimes = myrand_range(1,20);
1427 int receivetimes = 20;
1428 for(int i=0; i<receivetimes; i++){
1429 SharedBuffer<u8> recvdata;
1432 bool received = false;
1434 size = client.Receive(peer_id, recvdata);
1436 }catch(con::NoIncomingDataException &e){
1446 const int datasize = 30000;
1447 SharedBuffer<u8> data1(datasize);
1448 for(u16 i=0; i<datasize; i++){
1452 infostream<<"Sending data (size="<<datasize<<"):";
1453 for(int i=0; i<datasize && i<20; i++){
1454 if(i%2==0) infostream<<" ";
1456 snprintf(buf, 10, "%.2X", ((int)((const char*)*data1)[i])&0xff);
1461 infostream<<std::endl;
1463 server.Send(peer_id_client, 0, data1, true);
1467 SharedBuffer<u8> recvdata;
1468 infostream<<"** running client.Receive()"<<std::endl;
1471 bool received = false;
1472 u32 timems0 = porting::getTimeMs();
1474 if(porting::getTimeMs() - timems0 > 5000 || received)
1477 size = client.Receive(peer_id, recvdata);
1479 }catch(con::NoIncomingDataException &e){
1484 infostream<<"** Client received: peer_id="<<peer_id
1488 infostream<<"Received data (size="<<size<<"): ";
1489 for(int i=0; i<size && i<20; i++){
1490 if(i%2==0) infostream<<" ";
1492 snprintf(buf, 10, "%.2X", ((int)(recvdata[i]))&0xff);
1497 infostream<<std::endl;
1499 assert(memcmp(*data1, *recvdata, data1.getSize()) == 0);
1500 assert(peer_id == PEER_ID_SERVER);
1503 // Check peer handlers
1504 assert(hand_client.count == 1);
1505 assert(hand_client.last_id == 1);
1506 assert(hand_server.count == 1);
1507 assert(hand_server.last_id == 2);
1516 infostream<<"Running " #X <<std::endl;\
1520 #define TESTPARAMS(X, ...)\
1523 infostream<<"Running " #X <<std::endl;\
1524 x.Run(__VA_ARGS__);\
1529 DSTACK(__FUNCTION_NAME);
1531 // Create item and node definitions
1532 IWritableItemDefManager *idef = createItemDefManager();
1533 IWritableNodeDefManager *ndef = createNodeDefManager();
1534 define_some_nodes(idef, ndef);
1536 infostream<<"run_tests() started"<<std::endl;
1537 TEST(TestUtilities);
1540 TEST(TestSerialization);
1541 TESTPARAMS(TestMapNode, ndef);
1542 TESTPARAMS(TestVoxelManipulator, ndef);
1543 TESTPARAMS(TestVoxelAlgorithms, ndef);
1544 TESTPARAMS(TestInventory, idef);
1545 //TEST(TestMapBlock);
1546 //TEST(TestMapSector);
1547 if(INTERNET_SIMULATOR == false){
1549 dout_con<<"=== BEGIN RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
1550 TEST(TestConnection);
1551 dout_con<<"=== END RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
1553 infostream<<"run_tests() passed"<<std::endl;