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 General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
15 You should have received a copy of the GNU 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"
29 #include "serialization.h"
33 #include "content_mapnode.h"
35 #include "mapsector.h"
38 #include "utility_string.h"
39 #include "voxelalgorithms.h"
40 #include "inventory.h"
43 Asserts that the exception occurs
45 #define EXCEPTION_CHECK(EType, code)\
47 bool exception_thrown = false;\
49 catch(EType &e) { exception_thrown = true; }\
50 assert(exception_thrown);\
54 A few item and node definitions for those tests that need them
57 #define CONTENT_STONE 0
58 #define CONTENT_GRASS 0x800
59 #define CONTENT_TORCH 100
61 void define_some_nodes(IWritableItemDefManager *idef, IWritableNodeDefManager *ndef)
64 ItemDefinition itemdef;
71 itemdef = ItemDefinition();
72 itemdef.type = ITEM_NODE;
73 itemdef.name = "default:stone";
74 itemdef.description = "Stone";
75 itemdef.groups["cracky"] = 3;
76 itemdef.inventory_image = "[inventorycube"
80 f = ContentFeatures();
81 f.name = itemdef.name;
82 for(int i = 0; i < 6; i++)
83 f.tname_tiles[i] = "default_stone.png";
84 f.is_ground_content = true;
85 idef->registerItem(itemdef);
92 itemdef = ItemDefinition();
93 itemdef.type = ITEM_NODE;
94 itemdef.name = "default:dirt_with_grass";
95 itemdef.description = "Dirt with grass";
96 itemdef.groups["crumbly"] = 3;
97 itemdef.inventory_image = "[inventorycube"
99 "{default_dirt.png&default_grass_side.png"
100 "{default_dirt.png&default_grass_side.png";
101 f = ContentFeatures();
102 f.name = itemdef.name;
103 f.tname_tiles[0] = "default_grass.png";
104 f.tname_tiles[1] = "default_dirt.png";
105 for(int i = 2; i < 6; i++)
106 f.tname_tiles[i] = "default_dirt.png^default_grass_side.png";
107 f.is_ground_content = true;
108 idef->registerItem(itemdef);
112 Torch (minimal definition for lighting tests)
115 itemdef = ItemDefinition();
116 itemdef.type = ITEM_NODE;
117 itemdef.name = "default:torch";
118 f = ContentFeatures();
119 f.name = itemdef.name;
120 f.param_type = CPT_LIGHT;
121 f.light_propagates = true;
122 f.sunlight_propagates = true;
123 f.light_source = LIGHT_MAX-1;
124 idef->registerItem(itemdef);
132 /*infostream<<"wrapDegrees(100.0) = "<<wrapDegrees(100.0)<<std::endl;
133 infostream<<"wrapDegrees(720.5) = "<<wrapDegrees(720.5)<<std::endl;
134 infostream<<"wrapDegrees(-0.5) = "<<wrapDegrees(-0.5)<<std::endl;*/
135 assert(fabs(wrapDegrees(100.0) - 100.0) < 0.001);
136 assert(fabs(wrapDegrees(720.5) - 0.5) < 0.001);
137 assert(fabs(wrapDegrees(-0.5) - (-0.5)) < 0.001);
138 assert(fabs(wrapDegrees(-365.5) - (-5.5)) < 0.001);
139 assert(lowercase("Foo bAR") == "foo bar");
140 assert(is_yes("YeS") == true);
141 assert(is_yes("") == false);
142 assert(is_yes("FAlse") == false);
143 const char *ends[] = {"abc", "c", "bc", NULL};
144 assert(removeStringEnd("abc", ends) == "");
145 assert(removeStringEnd("bc", ends) == "b");
146 assert(removeStringEnd("12c", ends) == "12");
147 assert(removeStringEnd("foo", ends) == "");
156 // Test reading of settings
157 s.parseConfigLine("leet = 1337");
158 s.parseConfigLine("leetleet = 13371337");
159 s.parseConfigLine("leetleet_neg = -13371337");
160 s.parseConfigLine("floaty_thing = 1.1");
161 s.parseConfigLine("stringy_thing = asd /( ¤%&(/\" BLÖÄRP");
162 s.parseConfigLine("coord = (1, 2, 4.5)");
163 assert(s.getS32("leet") == 1337);
164 assert(s.getS16("leetleet") == 32767);
165 assert(s.getS16("leetleet_neg") == -32768);
166 // Not sure if 1.1 is an exact value as a float, but doesn't matter
167 assert(fabs(s.getFloat("floaty_thing") - 1.1) < 0.001);
168 assert(s.get("stringy_thing") == "asd /( ¤%&(/\" BLÖÄRP");
169 assert(fabs(s.getV3F("coord").X - 1.0) < 0.001);
170 assert(fabs(s.getV3F("coord").Y - 2.0) < 0.001);
171 assert(fabs(s.getV3F("coord").Z - 4.5) < 0.001);
172 // Test the setting of settings too
173 s.setFloat("floaty_thing_2", 1.2);
174 s.setV3F("coord2", v3f(1, 2, 3.3));
175 assert(s.get("floaty_thing_2").substr(0,3) == "1.2");
176 assert(fabs(s.getFloat("floaty_thing_2") - 1.2) < 0.001);
177 assert(fabs(s.getV3F("coord2").X - 1.0) < 0.001);
178 assert(fabs(s.getV3F("coord2").Y - 2.0) < 0.001);
179 assert(fabs(s.getV3F("coord2").Z - 3.3) < 0.001);
183 struct TestSerialization
185 // To be used like this:
186 // mkstr("Some\0string\0with\0embedded\0nuls")
187 // since std::string("...") doesn't work as expected in that case.
188 template<size_t N> std::string mkstr(const char (&s)[N])
190 return std::string(s, N - 1);
195 // Tests some serialization primitives
197 assert(serializeString("") == mkstr("\0\0"));
198 assert(serializeWideString(L"") == mkstr("\0\0"));
199 assert(serializeLongString("") == mkstr("\0\0\0\0"));
200 assert(serializeJsonString("") == "\"\"");
202 std::string teststring = "Hello world!";
203 assert(serializeString(teststring) ==
204 mkstr("\0\14Hello world!"));
205 assert(serializeWideString(narrow_to_wide(teststring)) ==
206 mkstr("\0\14\0H\0e\0l\0l\0o\0 \0w\0o\0r\0l\0d\0!"));
207 assert(serializeLongString(teststring) ==
208 mkstr("\0\0\0\14Hello world!"));
209 assert(serializeJsonString(teststring) ==
212 std::string teststring2;
213 std::wstring teststring2_w;
214 std::string teststring2_w_encoded;
216 std::ostringstream tmp_os;
217 std::wostringstream tmp_os_w;
218 std::ostringstream tmp_os_w_encoded;
219 for(int i = 0; i < 256; i++)
222 tmp_os_w<<(wchar_t)i;
223 tmp_os_w_encoded<<(char)0<<(char)i;
225 teststring2 = tmp_os.str();
226 teststring2_w = tmp_os_w.str();
227 teststring2_w_encoded = tmp_os_w_encoded.str();
229 assert(serializeString(teststring2) ==
230 mkstr("\1\0") + teststring2);
231 assert(serializeWideString(teststring2_w) ==
232 mkstr("\1\0") + teststring2_w_encoded);
233 assert(serializeLongString(teststring2) ==
234 mkstr("\0\0\1\0") + teststring2);
235 // MSVC fails when directly using "\\\\"
236 std::string backslash = "\\";
237 assert(serializeJsonString(teststring2) ==
239 "\\u0000\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007" +
240 "\\b\\t\\n\\u000b\\f\\r\\u000e\\u000f" +
241 "\\u0010\\u0011\\u0012\\u0013\\u0014\\u0015\\u0016\\u0017" +
242 "\\u0018\\u0019\\u001a\\u001b\\u001c\\u001d\\u001e\\u001f" +
243 " !\\\"" + teststring2.substr(0x23, 0x2f-0x23) +
244 "\\/" + teststring2.substr(0x30, 0x5c-0x30) +
245 backslash + backslash + teststring2.substr(0x5d, 0x7f-0x5d) + "\\u007f" +
246 "\\u0080\\u0081\\u0082\\u0083\\u0084\\u0085\\u0086\\u0087" +
247 "\\u0088\\u0089\\u008a\\u008b\\u008c\\u008d\\u008e\\u008f" +
248 "\\u0090\\u0091\\u0092\\u0093\\u0094\\u0095\\u0096\\u0097" +
249 "\\u0098\\u0099\\u009a\\u009b\\u009c\\u009d\\u009e\\u009f" +
250 "\\u00a0\\u00a1\\u00a2\\u00a3\\u00a4\\u00a5\\u00a6\\u00a7" +
251 "\\u00a8\\u00a9\\u00aa\\u00ab\\u00ac\\u00ad\\u00ae\\u00af" +
252 "\\u00b0\\u00b1\\u00b2\\u00b3\\u00b4\\u00b5\\u00b6\\u00b7" +
253 "\\u00b8\\u00b9\\u00ba\\u00bb\\u00bc\\u00bd\\u00be\\u00bf" +
254 "\\u00c0\\u00c1\\u00c2\\u00c3\\u00c4\\u00c5\\u00c6\\u00c7" +
255 "\\u00c8\\u00c9\\u00ca\\u00cb\\u00cc\\u00cd\\u00ce\\u00cf" +
256 "\\u00d0\\u00d1\\u00d2\\u00d3\\u00d4\\u00d5\\u00d6\\u00d7" +
257 "\\u00d8\\u00d9\\u00da\\u00db\\u00dc\\u00dd\\u00de\\u00df" +
258 "\\u00e0\\u00e1\\u00e2\\u00e3\\u00e4\\u00e5\\u00e6\\u00e7" +
259 "\\u00e8\\u00e9\\u00ea\\u00eb\\u00ec\\u00ed\\u00ee\\u00ef" +
260 "\\u00f0\\u00f1\\u00f2\\u00f3\\u00f4\\u00f5\\u00f6\\u00f7" +
261 "\\u00f8\\u00f9\\u00fa\\u00fb\\u00fc\\u00fd\\u00fe\\u00ff" +
265 std::istringstream is(serializeString(teststring2), std::ios::binary);
266 assert(deSerializeString(is) == teststring2);
272 std::istringstream is(serializeWideString(teststring2_w), std::ios::binary);
273 assert(deSerializeWideString(is) == teststring2_w);
279 std::istringstream is(serializeLongString(teststring2), std::ios::binary);
280 assert(deSerializeLongString(is) == teststring2);
286 std::istringstream is(serializeJsonString(teststring2), std::ios::binary);
287 //dstream<<serializeJsonString(deSerializeJsonString(is));
288 assert(deSerializeJsonString(is) == teststring2);
302 SharedBuffer<u8> fromdata(4);
308 std::ostringstream os(std::ios_base::binary);
309 compress(fromdata, os, 0);
311 std::string str_out = os.str();
313 infostream<<"str_out.size()="<<str_out.size()<<std::endl;
314 infostream<<"TestCompress: 1,5,5,1 -> ";
315 for(u32 i=0; i<str_out.size(); i++)
317 infostream<<(u32)str_out[i]<<",";
319 infostream<<std::endl;
321 assert(str_out.size() == 10);
323 assert(str_out[0] == 0);
324 assert(str_out[1] == 0);
325 assert(str_out[2] == 0);
326 assert(str_out[3] == 4);
327 assert(str_out[4] == 0);
328 assert(str_out[5] == 1);
329 assert(str_out[6] == 1);
330 assert(str_out[7] == 5);
331 assert(str_out[8] == 0);
332 assert(str_out[9] == 1);
334 std::istringstream is(str_out, std::ios_base::binary);
335 std::ostringstream os2(std::ios_base::binary);
337 decompress(is, os2, 0);
338 std::string str_out2 = os2.str();
340 infostream<<"decompress: ";
341 for(u32 i=0; i<str_out2.size(); i++)
343 infostream<<(u32)str_out2[i]<<",";
345 infostream<<std::endl;
347 assert(str_out2.size() == fromdata.getSize());
349 for(u32 i=0; i<str_out2.size(); i++)
351 assert(str_out2[i] == fromdata[i]);
358 SharedBuffer<u8> fromdata(4);
364 std::ostringstream os(std::ios_base::binary);
365 compress(fromdata, os, SER_FMT_VER_HIGHEST);
367 std::string str_out = os.str();
369 infostream<<"str_out.size()="<<str_out.size()<<std::endl;
370 infostream<<"TestCompress: 1,5,5,1 -> ";
371 for(u32 i=0; i<str_out.size(); i++)
373 infostream<<(u32)str_out[i]<<",";
375 infostream<<std::endl;
377 /*assert(str_out.size() == 10);
379 assert(str_out[0] == 0);
380 assert(str_out[1] == 0);
381 assert(str_out[2] == 0);
382 assert(str_out[3] == 4);
383 assert(str_out[4] == 0);
384 assert(str_out[5] == 1);
385 assert(str_out[6] == 1);
386 assert(str_out[7] == 5);
387 assert(str_out[8] == 0);
388 assert(str_out[9] == 1);*/
390 std::istringstream is(str_out, std::ios_base::binary);
391 std::ostringstream os2(std::ios_base::binary);
393 decompress(is, os2, SER_FMT_VER_HIGHEST);
394 std::string str_out2 = os2.str();
396 infostream<<"decompress: ";
397 for(u32 i=0; i<str_out2.size(); i++)
399 infostream<<(u32)str_out2[i]<<",";
401 infostream<<std::endl;
403 assert(str_out2.size() == fromdata.getSize());
405 for(u32 i=0; i<str_out2.size(); i++)
407 assert(str_out2[i] == fromdata[i]);
416 void Run(INodeDefManager *nodedef)
421 assert(n.getContent() == CONTENT_AIR);
422 assert(n.getLight(LIGHTBANK_DAY, nodedef) == 0);
423 assert(n.getLight(LIGHTBANK_NIGHT, nodedef) == 0);
426 n.setContent(CONTENT_AIR);
427 assert(nodedef->get(n).light_propagates == true);
428 n.setContent(LEGN(nodedef, "CONTENT_STONE"));
429 assert(nodedef->get(n).light_propagates == false);
433 struct TestVoxelManipulator
435 void Run(INodeDefManager *nodedef)
441 VoxelArea a(v3s16(-1,-1,-1), v3s16(1,1,1));
442 assert(a.index(0,0,0) == 1*3*3 + 1*3 + 1);
443 assert(a.index(-1,-1,-1) == 0);
445 VoxelArea c(v3s16(-2,-2,-2), v3s16(2,2,2));
446 // An area that is 1 bigger in x+ and z-
447 VoxelArea d(v3s16(-2,-2,-3), v3s16(3,2,2));
449 core::list<VoxelArea> aa;
453 core::array<VoxelArea> results;
454 results.push_back(VoxelArea(v3s16(-2,-2,-3),v3s16(3,2,-3)));
455 results.push_back(VoxelArea(v3s16(3,-2,-2),v3s16(3,2,2)));
457 assert(aa.size() == results.size());
459 infostream<<"Result of diff:"<<std::endl;
460 for(core::list<VoxelArea>::Iterator
461 i = aa.begin(); i != aa.end(); i++)
463 i->print(infostream);
464 infostream<<std::endl;
466 s32 j = results.linear_search(*i);
478 v.print(infostream, nodedef);
480 infostream<<"*** Setting (-1,0,-1)=2 ***"<<std::endl;
482 v.setNodeNoRef(v3s16(-1,0,-1), MapNode(CONTENT_GRASS));
484 v.print(infostream, nodedef);
486 assert(v.getNode(v3s16(-1,0,-1)).getContent() == CONTENT_GRASS);
488 infostream<<"*** Reading from inexistent (0,0,-1) ***"<<std::endl;
490 EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,0,-1)));
492 v.print(infostream, nodedef);
494 infostream<<"*** Adding area ***"<<std::endl;
498 v.print(infostream, nodedef);
500 assert(v.getNode(v3s16(-1,0,-1)).getContent() == CONTENT_GRASS);
501 EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,1,1)));
505 struct TestVoxelAlgorithms
507 void Run(INodeDefManager *ndef)
510 voxalgo::propagateSunlight
514 for(u16 z=0; z<3; z++)
515 for(u16 y=0; y<3; y++)
516 for(u16 x=0; x<3; x++)
519 v.setNodeNoRef(p, MapNode(CONTENT_AIR));
521 VoxelArea a(v3s16(0,0,0), v3s16(2,2,2));
523 core::map<v3s16, bool> light_sources;
524 voxalgo::setLight(v, a, 0, ndef);
525 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
526 v, a, true, light_sources, ndef);
527 //v.print(dstream, ndef, VOXELPRINT_LIGHT_DAY);
528 assert(res.bottom_sunlight_valid == true);
529 assert(v.getNode(v3s16(1,1,1)).getLight(LIGHTBANK_DAY, ndef)
532 v.setNodeNoRef(v3s16(0,0,0), MapNode(CONTENT_STONE));
534 core::map<v3s16, bool> light_sources;
535 voxalgo::setLight(v, a, 0, ndef);
536 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
537 v, a, true, light_sources, ndef);
538 assert(res.bottom_sunlight_valid == true);
539 assert(v.getNode(v3s16(1,1,1)).getLight(LIGHTBANK_DAY, ndef)
543 core::map<v3s16, bool> light_sources;
544 voxalgo::setLight(v, a, 0, ndef);
545 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
546 v, a, false, light_sources, ndef);
547 assert(res.bottom_sunlight_valid == true);
548 assert(v.getNode(v3s16(2,0,2)).getLight(LIGHTBANK_DAY, ndef)
551 v.setNodeNoRef(v3s16(1,3,2), MapNode(CONTENT_STONE));
553 core::map<v3s16, bool> light_sources;
554 voxalgo::setLight(v, a, 0, ndef);
555 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
556 v, a, true, light_sources, ndef);
557 assert(res.bottom_sunlight_valid == true);
558 assert(v.getNode(v3s16(1,1,2)).getLight(LIGHTBANK_DAY, ndef)
562 core::map<v3s16, bool> light_sources;
563 voxalgo::setLight(v, a, 0, ndef);
564 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
565 v, a, false, light_sources, ndef);
566 assert(res.bottom_sunlight_valid == true);
567 assert(v.getNode(v3s16(1,0,2)).getLight(LIGHTBANK_DAY, ndef)
571 MapNode n(CONTENT_AIR);
572 n.setLight(LIGHTBANK_DAY, 10, ndef);
573 v.setNodeNoRef(v3s16(1,-1,2), n);
576 core::map<v3s16, bool> light_sources;
577 voxalgo::setLight(v, a, 0, ndef);
578 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
579 v, a, true, light_sources, ndef);
580 assert(res.bottom_sunlight_valid == true);
583 core::map<v3s16, bool> light_sources;
584 voxalgo::setLight(v, a, 0, ndef);
585 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
586 v, a, false, light_sources, ndef);
587 assert(res.bottom_sunlight_valid == true);
590 MapNode n(CONTENT_AIR);
591 n.setLight(LIGHTBANK_DAY, LIGHT_SUN, ndef);
592 v.setNodeNoRef(v3s16(1,-1,2), n);
595 core::map<v3s16, bool> light_sources;
596 voxalgo::setLight(v, a, 0, ndef);
597 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
598 v, a, true, light_sources, ndef);
599 assert(res.bottom_sunlight_valid == false);
602 core::map<v3s16, bool> light_sources;
603 voxalgo::setLight(v, a, 0, ndef);
604 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
605 v, a, false, light_sources, ndef);
606 assert(res.bottom_sunlight_valid == false);
608 v.setNodeNoRef(v3s16(1,3,2), MapNode(CONTENT_IGNORE));
610 core::map<v3s16, bool> light_sources;
611 voxalgo::setLight(v, a, 0, ndef);
612 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
613 v, a, true, light_sources, ndef);
614 assert(res.bottom_sunlight_valid == true);
618 voxalgo::clearLightAndCollectSources
622 for(u16 z=0; z<3; z++)
623 for(u16 y=0; y<3; y++)
624 for(u16 x=0; x<3; x++)
627 v.setNode(p, MapNode(CONTENT_AIR));
629 VoxelArea a(v3s16(0,0,0), v3s16(2,2,2));
630 v.setNodeNoRef(v3s16(0,0,0), MapNode(CONTENT_STONE));
631 v.setNodeNoRef(v3s16(1,1,1), MapNode(CONTENT_TORCH));
633 MapNode n(CONTENT_AIR);
634 n.setLight(LIGHTBANK_DAY, 1, ndef);
635 v.setNode(v3s16(1,1,2), n);
638 core::map<v3s16, bool> light_sources;
639 core::map<v3s16, u8> unlight_from;
640 voxalgo::clearLightAndCollectSources(v, a, LIGHTBANK_DAY,
641 ndef, light_sources, unlight_from);
642 //v.print(dstream, ndef, VOXELPRINT_LIGHT_DAY);
643 assert(v.getNode(v3s16(0,1,1)).getLight(LIGHTBANK_DAY, ndef)
645 assert(light_sources.find(v3s16(1,1,1)) != NULL);
646 assert(light_sources.size() == 1);
647 assert(unlight_from.find(v3s16(1,1,2)) != NULL);
648 assert(unlight_from.size() == 1);
656 void Run(IItemDefManager *idef)
658 std::string serialized_inventory =
669 "Item default:cobble 61\n"
676 "Item default:dirt 71\n"
684 "Item default:dirt 99\n"
685 "Item default:cobble 38\n"
695 std::string serialized_inventory_2 =
706 "Item default:cobble 61\n"
713 "Item default:dirt 71\n"
721 "Item default:dirt 99\n"
722 "Item default:cobble 38\n"
733 std::istringstream is(serialized_inventory, std::ios::binary);
735 assert(inv.getList("0"));
736 assert(!inv.getList("main"));
737 inv.getList("0")->setName("main");
738 assert(!inv.getList("0"));
739 assert(inv.getList("main"));
740 std::ostringstream inv_os(std::ios::binary);
741 inv.serialize(inv_os);
742 assert(inv_os.str() == serialized_inventory_2);
747 NOTE: These tests became non-working then NodeContainer was removed.
748 These should be redone, utilizing some kind of a virtual
749 interface for Map (IMap would be fine).
754 class TC : public NodeContainer
760 core::list<v3s16> validity_exceptions;
764 position_valid = true;
767 virtual bool isValidPosition(v3s16 p)
769 //return position_valid ^ (p==position_valid_exception);
770 bool exception = false;
771 for(core::list<v3s16>::Iterator i=validity_exceptions.begin();
772 i != validity_exceptions.end(); i++)
780 return exception ? !position_valid : position_valid;
783 virtual MapNode getNode(v3s16 p)
785 if(isValidPosition(p) == false)
786 throw InvalidPositionException();
790 virtual void setNode(v3s16 p, MapNode & n)
792 if(isValidPosition(p) == false)
793 throw InvalidPositionException();
796 virtual u16 nodeContainerId() const
806 MapBlock b(&parent, v3s16(1,1,1));
807 v3s16 relpos(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
809 assert(b.getPosRelative() == relpos);
811 assert(b.getBox().MinEdge.X == MAP_BLOCKSIZE);
812 assert(b.getBox().MaxEdge.X == MAP_BLOCKSIZE*2-1);
813 assert(b.getBox().MinEdge.Y == MAP_BLOCKSIZE);
814 assert(b.getBox().MaxEdge.Y == MAP_BLOCKSIZE*2-1);
815 assert(b.getBox().MinEdge.Z == MAP_BLOCKSIZE);
816 assert(b.getBox().MaxEdge.Z == MAP_BLOCKSIZE*2-1);
818 assert(b.isValidPosition(v3s16(0,0,0)) == true);
819 assert(b.isValidPosition(v3s16(-1,0,0)) == false);
820 assert(b.isValidPosition(v3s16(-1,-142,-2341)) == false);
821 assert(b.isValidPosition(v3s16(-124,142,2341)) == false);
822 assert(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
823 assert(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE,MAP_BLOCKSIZE-1)) == false);
826 TODO: this method should probably be removed
827 if the block size isn't going to be set variable
829 /*assert(b.getSizeNodes() == v3s16(MAP_BLOCKSIZE,
830 MAP_BLOCKSIZE, MAP_BLOCKSIZE));*/
832 // Changed flag should be initially set
833 assert(b.getModified() == MOD_STATE_WRITE_NEEDED);
835 assert(b.getModified() == MOD_STATE_CLEAN);
837 // All nodes should have been set to
838 // .d=CONTENT_IGNORE and .getLight() = 0
839 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
840 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
841 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
843 //assert(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_AIR);
844 assert(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_IGNORE);
845 assert(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_DAY) == 0);
846 assert(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_NIGHT) == 0);
850 MapNode n(CONTENT_AIR);
851 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
852 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
853 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
855 b.setNode(v3s16(x,y,z), n);
860 Parent fetch functions
862 parent.position_valid = false;
863 parent.node.setContent(5);
867 // Positions in the block should still be valid
868 assert(b.isValidPositionParent(v3s16(0,0,0)) == true);
869 assert(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
870 n = b.getNodeParent(v3s16(0,MAP_BLOCKSIZE-1,0));
871 assert(n.getContent() == CONTENT_AIR);
873 // ...but outside the block they should be invalid
874 assert(b.isValidPositionParent(v3s16(-121,2341,0)) == false);
875 assert(b.isValidPositionParent(v3s16(-1,0,0)) == false);
876 assert(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == false);
879 bool exception_thrown = false;
881 // This should throw an exception
882 MapNode n = b.getNodeParent(v3s16(0,0,-1));
884 catch(InvalidPositionException &e)
886 exception_thrown = true;
888 assert(exception_thrown);
891 parent.position_valid = true;
892 // Now the positions outside should be valid
893 assert(b.isValidPositionParent(v3s16(-121,2341,0)) == true);
894 assert(b.isValidPositionParent(v3s16(-1,0,0)) == true);
895 assert(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == true);
896 n = b.getNodeParent(v3s16(0,0,MAP_BLOCKSIZE));
897 assert(n.getContent() == 5);
905 assert(b.getNode(p).getContent() == 4);
906 //TODO: Update to new system
907 /*assert(b.getNodeTile(p) == 4);
908 assert(b.getNodeTile(v3s16(-1,-1,0)) == 5);*/
913 // Set lighting of all nodes to 0
914 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
915 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
916 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
917 MapNode n = b.getNode(v3s16(x,y,z));
918 n.setLight(LIGHTBANK_DAY, 0);
919 n.setLight(LIGHTBANK_NIGHT, 0);
920 b.setNode(v3s16(x,y,z), n);
926 Check how the block handles being a lonely sky block
928 parent.position_valid = true;
929 b.setIsUnderground(false);
930 parent.node.setContent(CONTENT_AIR);
931 parent.node.setLight(LIGHTBANK_DAY, LIGHT_SUN);
932 parent.node.setLight(LIGHTBANK_NIGHT, 0);
933 core::map<v3s16, bool> light_sources;
934 // The bottom block is invalid, because we have a shadowing node
935 assert(b.propagateSunlight(light_sources) == false);
936 assert(b.getNode(v3s16(1,4,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
937 assert(b.getNode(v3s16(1,3,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
938 assert(b.getNode(v3s16(1,2,0)).getLight(LIGHTBANK_DAY) == 0);
939 assert(b.getNode(v3s16(1,1,0)).getLight(LIGHTBANK_DAY) == 0);
940 assert(b.getNode(v3s16(1,0,0)).getLight(LIGHTBANK_DAY) == 0);
941 assert(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
942 assert(b.getFaceLight2(1000, p, v3s16(0,1,0)) == LIGHT_SUN);
943 assert(b.getFaceLight2(1000, p, v3s16(0,-1,0)) == 0);
944 assert(b.getFaceLight2(0, p, v3s16(0,-1,0)) == 0);
945 // According to MapBlock::getFaceLight,
946 // The face on the z+ side should have double-diminished light
947 //assert(b.getFaceLight(p, v3s16(0,0,1)) == diminish_light(diminish_light(LIGHT_MAX)));
948 // The face on the z+ side should have diminished light
949 assert(b.getFaceLight2(1000, p, v3s16(0,0,1)) == diminish_light(LIGHT_MAX));
952 Check how the block handles being in between blocks with some non-sunlight
953 while being underground
956 // Make neighbours to exist and set some non-sunlight to them
957 parent.position_valid = true;
958 b.setIsUnderground(true);
959 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
960 core::map<v3s16, bool> light_sources;
961 // The block below should be valid because there shouldn't be
962 // sunlight in there either
963 assert(b.propagateSunlight(light_sources, true) == true);
964 // Should not touch nodes that are not affected (that is, all of them)
965 //assert(b.getNode(v3s16(1,2,3)).getLight() == LIGHT_SUN);
966 // Should set light of non-sunlighted blocks to 0.
967 assert(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == 0);
970 Set up a situation where:
971 - There is only air in this block
972 - There is a valid non-sunlighted block at the bottom, and
973 - Invalid blocks elsewhere.
974 - the block is not underground.
976 This should result in bottom block invalidity
979 b.setIsUnderground(false);
981 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
982 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
983 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
985 n.setContent(CONTENT_AIR);
986 n.setLight(LIGHTBANK_DAY, 0);
987 b.setNode(v3s16(x,y,z), n);
991 // Make neighbours invalid
992 parent.position_valid = false;
993 // Add exceptions to the top of the bottom block
994 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
995 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
997 parent.validity_exceptions.push_back(v3s16(MAP_BLOCKSIZE+x, MAP_BLOCKSIZE-1, MAP_BLOCKSIZE+z));
999 // Lighting value for the valid nodes
1000 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
1001 core::map<v3s16, bool> light_sources;
1002 // Bottom block is not valid
1003 assert(b.propagateSunlight(light_sources) == false);
1008 struct TestMapSector
1010 class TC : public NodeContainer
1015 bool position_valid;
1019 position_valid = true;
1022 virtual bool isValidPosition(v3s16 p)
1024 return position_valid;
1027 virtual MapNode getNode(v3s16 p)
1029 if(position_valid == false)
1030 throw InvalidPositionException();
1034 virtual void setNode(v3s16 p, MapNode & n)
1036 if(position_valid == false)
1037 throw InvalidPositionException();
1040 virtual u16 nodeContainerId() const
1049 parent.position_valid = false;
1051 // Create one with no heightmaps
1052 ServerMapSector sector(&parent, v2s16(1,1));
1054 assert(sector.getBlockNoCreateNoEx(0) == 0);
1055 assert(sector.getBlockNoCreateNoEx(1) == 0);
1057 MapBlock * bref = sector.createBlankBlock(-2);
1059 assert(sector.getBlockNoCreateNoEx(0) == 0);
1060 assert(sector.getBlockNoCreateNoEx(-2) == bref);
1062 //TODO: Check for AlreadyExistsException
1064 /*bool exception_thrown = false;
1068 catch(InvalidPositionException &e){
1069 exception_thrown = true;
1071 assert(exception_thrown);*/
1081 const int port = 30003;
1085 const char sendbuffer[] = "hello world!";
1086 socket.Send(Address(127,0,0,1,port), sendbuffer, sizeof(sendbuffer));
1090 char rcvbuffer[256];
1091 memset(rcvbuffer, 0, sizeof(rcvbuffer));
1095 int bytes_read = socket.Receive(sender, rcvbuffer, sizeof(rcvbuffer));
1099 //FIXME: This fails on some systems
1100 assert(strncmp(sendbuffer, rcvbuffer, sizeof(sendbuffer))==0);
1101 assert(sender.getAddress() == Address(127,0,0,1, 0).getAddress());
1105 struct TestConnection
1110 Test helper functions
1113 // Some constants for testing
1114 u32 proto_id = 0x12345678;
1117 SharedBuffer<u8> data1(1);
1119 Address a(127,0,0,1, 10);
1122 con::BufferedPacket p1 = con::makePacket(a, data1,
1123 proto_id, peer_id, channel);
1125 We should now have a packet with this data:
1128 [4] u16 sender_peer_id
1133 assert(readU32(&p1.data[0]) == proto_id);
1134 assert(readU16(&p1.data[4]) == peer_id);
1135 assert(readU8(&p1.data[6]) == channel);
1136 assert(readU8(&p1.data[7]) == data1[0]);
1138 //infostream<<"initial data1[0]="<<((u32)data1[0]&0xff)<<std::endl;
1140 SharedBuffer<u8> p2 = con::makeReliablePacket(data1, seqnum);
1142 /*infostream<<"p2.getSize()="<<p2.getSize()<<", data1.getSize()="
1143 <<data1.getSize()<<std::endl;
1144 infostream<<"readU8(&p2[3])="<<readU8(&p2[3])
1145 <<" p2[3]="<<((u32)p2[3]&0xff)<<std::endl;
1146 infostream<<"data1[0]="<<((u32)data1[0]&0xff)<<std::endl;*/
1148 assert(p2.getSize() == 3 + data1.getSize());
1149 assert(readU8(&p2[0]) == TYPE_RELIABLE);
1150 assert(readU16(&p2[1]) == seqnum);
1151 assert(readU8(&p2[3]) == data1[0]);
1154 struct Handler : public con::PeerHandler
1156 Handler(const char *a_name)
1162 void peerAdded(con::Peer *peer)
1164 infostream<<"Handler("<<name<<")::peerAdded(): "
1165 "id="<<peer->id<<std::endl;
1169 void deletingPeer(con::Peer *peer, bool timeout)
1171 infostream<<"Handler("<<name<<")::deletingPeer(): "
1173 <<", timeout="<<timeout<<std::endl;
1185 DSTACK("TestConnection::Run");
1190 Test some real connections
1192 NOTE: This mostly tests the legacy interface.
1195 u32 proto_id = 0xad26846a;
1197 Handler hand_server("server");
1198 Handler hand_client("client");
1200 infostream<<"** Creating server Connection"<<std::endl;
1201 con::Connection server(proto_id, 512, 5.0, &hand_server);
1202 server.Serve(30001);
1204 infostream<<"** Creating client Connection"<<std::endl;
1205 con::Connection client(proto_id, 512, 5.0, &hand_client);
1207 assert(hand_server.count == 0);
1208 assert(hand_client.count == 0);
1212 Address server_address(127,0,0,1, 30001);
1213 infostream<<"** running client.Connect()"<<std::endl;
1214 client.Connect(server_address);
1218 // Client should not have added client yet
1219 assert(hand_client.count == 0);
1224 SharedBuffer<u8> data;
1225 infostream<<"** running client.Receive()"<<std::endl;
1226 u32 size = client.Receive(peer_id, data);
1227 infostream<<"** Client received: peer_id="<<peer_id
1231 catch(con::NoIncomingDataException &e)
1235 // Client should have added server now
1236 assert(hand_client.count == 1);
1237 assert(hand_client.last_id == 1);
1238 // Server should not have added client yet
1239 assert(hand_server.count == 0);
1246 SharedBuffer<u8> data;
1247 infostream<<"** running server.Receive()"<<std::endl;
1248 u32 size = server.Receive(peer_id, data);
1249 infostream<<"** Server received: peer_id="<<peer_id
1253 catch(con::NoIncomingDataException &e)
1255 // No actual data received, but the client has
1256 // probably been connected
1259 // Client should be the same
1260 assert(hand_client.count == 1);
1261 assert(hand_client.last_id == 1);
1262 // Server should have the client
1263 assert(hand_server.count == 1);
1264 assert(hand_server.last_id == 2);
1268 while(client.Connected() == false)
1273 SharedBuffer<u8> data;
1274 infostream<<"** running client.Receive()"<<std::endl;
1275 u32 size = client.Receive(peer_id, data);
1276 infostream<<"** Client received: peer_id="<<peer_id
1280 catch(con::NoIncomingDataException &e)
1291 SharedBuffer<u8> data;
1292 infostream<<"** running server.Receive()"<<std::endl;
1293 u32 size = server.Receive(peer_id, data);
1294 infostream<<"** Server received: peer_id="<<peer_id
1298 catch(con::NoIncomingDataException &e)
1303 Simple send-receive test
1306 /*u8 data[] = "Hello World!";
1307 u32 datasize = sizeof(data);*/
1308 SharedBuffer<u8> data = SharedBufferFromString("Hello World!");
1310 infostream<<"** running client.Send()"<<std::endl;
1311 client.Send(PEER_ID_SERVER, 0, data, true);
1316 SharedBuffer<u8> recvdata;
1317 infostream<<"** running server.Receive()"<<std::endl;
1318 u32 size = server.Receive(peer_id, recvdata);
1319 infostream<<"** Server received: peer_id="<<peer_id
1323 assert(memcmp(*data, *recvdata, data.getSize()) == 0);
1326 u16 peer_id_client = 2;
1329 Send consequent packets in different order
1330 Not compatible with new Connection, thus commented out.
1333 //u8 data1[] = "hello1";
1334 //u8 data2[] = "hello2";
1335 SharedBuffer<u8> data1 = SharedBufferFromString("hello1");
1336 SharedBuffer<u8> data2 = SharedBufferFromString("Hello2");
1338 Address client_address =
1339 server.GetPeerAddress(peer_id_client);
1341 infostream<<"*** Sending packets in wrong order (2,1,2)"
1345 con::Channel *ch = &server.getPeer(peer_id_client)->channels[chn];
1346 u16 sn = ch->next_outgoing_seqnum;
1347 ch->next_outgoing_seqnum = sn+1;
1348 server.Send(peer_id_client, chn, data2, true);
1349 ch->next_outgoing_seqnum = sn;
1350 server.Send(peer_id_client, chn, data1, true);
1351 ch->next_outgoing_seqnum = sn+1;
1352 server.Send(peer_id_client, chn, data2, true);
1356 infostream<<"*** Receiving the packets"<<std::endl;
1359 SharedBuffer<u8> recvdata;
1362 infostream<<"** running client.Receive()"<<std::endl;
1364 size = client.Receive(peer_id, recvdata);
1365 infostream<<"** Client received: peer_id="<<peer_id
1367 <<", data="<<*recvdata
1369 assert(size == data1.getSize());
1370 assert(memcmp(*data1, *recvdata, data1.getSize()) == 0);
1371 assert(peer_id == PEER_ID_SERVER);
1373 infostream<<"** running client.Receive()"<<std::endl;
1375 size = client.Receive(peer_id, recvdata);
1376 infostream<<"** Client received: peer_id="<<peer_id
1378 <<", data="<<*recvdata
1380 assert(size == data2.getSize());
1381 assert(memcmp(*data2, *recvdata, data2.getSize()) == 0);
1382 assert(peer_id == PEER_ID_SERVER);
1384 bool got_exception = false;
1387 infostream<<"** running client.Receive()"<<std::endl;
1389 size = client.Receive(peer_id, recvdata);
1390 infostream<<"** Client received: peer_id="<<peer_id
1392 <<", data="<<*recvdata
1395 catch(con::NoIncomingDataException &e)
1397 infostream<<"** No incoming data for client"<<std::endl;
1398 got_exception = true;
1400 assert(got_exception);
1405 Send large amounts of packets (infinite test)
1406 Commented out because of infinity.
1409 infostream<<"Sending large amounts of packets (infinite test)"<<std::endl;
1412 int datasize = myrand_range(0,5)==0?myrand_range(100,10000):myrand_range(0,100);
1413 infostream<<"datasize="<<datasize<<std::endl;
1414 SharedBuffer<u8> data1(datasize);
1415 for(u16 i=0; i<datasize; i++)
1418 int sendtimes = myrand_range(1,10);
1419 for(int i=0; i<sendtimes; i++){
1420 server.Send(peer_id_client, 0, data1, true);
1423 infostream<<"sendcount="<<sendcount<<std::endl;
1425 //int receivetimes = myrand_range(1,20);
1426 int receivetimes = 20;
1427 for(int i=0; i<receivetimes; i++){
1428 SharedBuffer<u8> recvdata;
1431 bool received = false;
1433 size = client.Receive(peer_id, recvdata);
1435 }catch(con::NoIncomingDataException &e){
1445 const int datasize = 30000;
1446 SharedBuffer<u8> data1(datasize);
1447 for(u16 i=0; i<datasize; i++){
1451 infostream<<"Sending data (size="<<datasize<<"):";
1452 for(int i=0; i<datasize && i<20; i++){
1453 if(i%2==0) infostream<<" ";
1455 snprintf(buf, 10, "%.2X", ((int)((const char*)*data1)[i])&0xff);
1460 infostream<<std::endl;
1462 server.Send(peer_id_client, 0, data1, true);
1466 SharedBuffer<u8> recvdata;
1467 infostream<<"** running client.Receive()"<<std::endl;
1470 bool received = false;
1471 u32 timems0 = porting::getTimeMs();
1473 if(porting::getTimeMs() - timems0 > 5000 || received)
1476 size = client.Receive(peer_id, recvdata);
1478 }catch(con::NoIncomingDataException &e){
1483 infostream<<"** Client received: peer_id="<<peer_id
1487 infostream<<"Received data (size="<<size<<"): ";
1488 for(int i=0; i<size && i<20; i++){
1489 if(i%2==0) infostream<<" ";
1491 snprintf(buf, 10, "%.2X", ((int)(recvdata[i]))&0xff);
1496 infostream<<std::endl;
1498 assert(memcmp(*data1, *recvdata, data1.getSize()) == 0);
1499 assert(peer_id == PEER_ID_SERVER);
1502 // Check peer handlers
1503 assert(hand_client.count == 1);
1504 assert(hand_client.last_id == 1);
1505 assert(hand_server.count == 1);
1506 assert(hand_server.last_id == 2);
1515 infostream<<"Running " #X <<std::endl;\
1519 #define TESTPARAMS(X, ...)\
1522 infostream<<"Running " #X <<std::endl;\
1523 x.Run(__VA_ARGS__);\
1528 DSTACK(__FUNCTION_NAME);
1530 // Create item and node definitions
1531 IWritableItemDefManager *idef = createItemDefManager();
1532 IWritableNodeDefManager *ndef = createNodeDefManager();
1533 define_some_nodes(idef, ndef);
1535 infostream<<"run_tests() started"<<std::endl;
1536 TEST(TestUtilities);
1539 TEST(TestSerialization);
1540 TESTPARAMS(TestMapNode, ndef);
1541 TESTPARAMS(TestVoxelManipulator, ndef);
1542 TESTPARAMS(TestVoxelAlgorithms, ndef);
1543 TESTPARAMS(TestInventory, idef);
1544 //TEST(TestMapBlock);
1545 //TEST(TestMapSector);
1546 if(INTERNET_SIMULATOR == false){
1548 dout_con<<"=== BEGIN RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
1549 TEST(TestConnection);
1550 dout_con<<"=== END RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
1552 infostream<<"run_tests() passed"<<std::endl;