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"
40 Asserts that the exception occurs
42 #define EXCEPTION_CHECK(EType, code)\
44 bool exception_thrown = false;\
46 catch(EType &e) { exception_thrown = true; }\
47 assert(exception_thrown);\
51 A few item and node definitions for those tests that need them
54 #define CONTENT_STONE 0
55 #define CONTENT_GRASS 0x800
57 void define_some_nodes(IWritableItemDefManager *idef, IWritableNodeDefManager *ndef)
60 ItemDefinition itemdef;
67 itemdef = ItemDefinition();
68 itemdef.type = ITEM_NODE;
69 itemdef.name = "default:stone";
70 itemdef.description = "Stone";
71 itemdef.groups["cracky"] = 3;
72 itemdef.inventory_image = "[inventorycube"
76 f = ContentFeatures();
77 f.name = itemdef.name;
78 for(int i = 0; i < 6; i++)
79 f.tname_tiles[i] = "default_stone.png";
80 f.is_ground_content = true;
81 idef->registerItem(itemdef);
88 itemdef = ItemDefinition();
89 itemdef.type = ITEM_NODE;
90 itemdef.name = "default:dirt_with_grass";
91 itemdef.description = "Dirt with grass";
92 itemdef.groups["crumbly"] = 3;
93 itemdef.inventory_image = "[inventorycube"
95 "{default_dirt.png&default_grass_side.png"
96 "{default_dirt.png&default_grass_side.png";
97 f = ContentFeatures();
98 f.name = itemdef.name;
99 f.tname_tiles[0] = "default_grass.png";
100 f.tname_tiles[1] = "default_dirt.png";
101 for(int i = 2; i < 6; i++)
102 f.tname_tiles[i] = "default_dirt.png^default_grass_side.png";
103 f.is_ground_content = true;
104 idef->registerItem(itemdef);
112 /*infostream<<"wrapDegrees(100.0) = "<<wrapDegrees(100.0)<<std::endl;
113 infostream<<"wrapDegrees(720.5) = "<<wrapDegrees(720.5)<<std::endl;
114 infostream<<"wrapDegrees(-0.5) = "<<wrapDegrees(-0.5)<<std::endl;*/
115 assert(fabs(wrapDegrees(100.0) - 100.0) < 0.001);
116 assert(fabs(wrapDegrees(720.5) - 0.5) < 0.001);
117 assert(fabs(wrapDegrees(-0.5) - (-0.5)) < 0.001);
118 assert(fabs(wrapDegrees(-365.5) - (-5.5)) < 0.001);
119 assert(lowercase("Foo bAR") == "foo bar");
120 assert(is_yes("YeS") == true);
121 assert(is_yes("") == false);
122 assert(is_yes("FAlse") == false);
131 // Test reading of settings
132 s.parseConfigLine("leet = 1337");
133 s.parseConfigLine("leetleet = 13371337");
134 s.parseConfigLine("leetleet_neg = -13371337");
135 s.parseConfigLine("floaty_thing = 1.1");
136 s.parseConfigLine("stringy_thing = asd /( ¤%&(/\" BLÖÄRP");
137 s.parseConfigLine("coord = (1, 2, 4.5)");
138 assert(s.getS32("leet") == 1337);
139 assert(s.getS16("leetleet") == 32767);
140 assert(s.getS16("leetleet_neg") == -32768);
141 // Not sure if 1.1 is an exact value as a float, but doesn't matter
142 assert(fabs(s.getFloat("floaty_thing") - 1.1) < 0.001);
143 assert(s.get("stringy_thing") == "asd /( ¤%&(/\" BLÖÄRP");
144 assert(fabs(s.getV3F("coord").X - 1.0) < 0.001);
145 assert(fabs(s.getV3F("coord").Y - 2.0) < 0.001);
146 assert(fabs(s.getV3F("coord").Z - 4.5) < 0.001);
147 // Test the setting of settings too
148 s.setFloat("floaty_thing_2", 1.2);
149 s.setV3F("coord2", v3f(1, 2, 3.3));
150 assert(s.get("floaty_thing_2").substr(0,3) == "1.2");
151 assert(fabs(s.getFloat("floaty_thing_2") - 1.2) < 0.001);
152 assert(fabs(s.getV3F("coord2").X - 1.0) < 0.001);
153 assert(fabs(s.getV3F("coord2").Y - 2.0) < 0.001);
154 assert(fabs(s.getV3F("coord2").Z - 3.3) < 0.001);
158 struct TestSerialization
160 // To be used like this:
161 // mkstr("Some\0string\0with\0embedded\0nuls")
162 // since std::string("...") doesn't work as expected in that case.
163 template<size_t N> std::string mkstr(const char (&s)[N])
165 return std::string(s, N - 1);
170 // Tests some serialization primitives
172 assert(serializeString("") == mkstr("\0\0"));
173 assert(serializeWideString(L"") == mkstr("\0\0"));
174 assert(serializeLongString("") == mkstr("\0\0\0\0"));
175 assert(serializeJsonString("") == "\"\"");
177 std::string teststring = "Hello world!";
178 assert(serializeString(teststring) ==
179 mkstr("\0\14Hello world!"));
180 assert(serializeWideString(narrow_to_wide(teststring)) ==
181 mkstr("\0\14\0H\0e\0l\0l\0o\0 \0w\0o\0r\0l\0d\0!"));
182 assert(serializeLongString(teststring) ==
183 mkstr("\0\0\0\14Hello world!"));
184 assert(serializeJsonString(teststring) ==
187 std::string teststring2;
188 std::wstring teststring2_w;
189 std::string teststring2_w_encoded;
191 std::ostringstream tmp_os;
192 std::wostringstream tmp_os_w;
193 std::ostringstream tmp_os_w_encoded;
194 for(int i = 0; i < 256; i++)
197 tmp_os_w<<(wchar_t)i;
198 tmp_os_w_encoded<<(char)0<<(char)i;
200 teststring2 = tmp_os.str();
201 teststring2_w = tmp_os_w.str();
202 teststring2_w_encoded = tmp_os_w_encoded.str();
204 assert(serializeString(teststring2) ==
205 mkstr("\1\0") + teststring2);
206 assert(serializeWideString(teststring2_w) ==
207 mkstr("\1\0") + teststring2_w_encoded);
208 assert(serializeLongString(teststring2) ==
209 mkstr("\0\0\1\0") + teststring2);
210 // MSVC fails when directly using "\\\\"
211 std::string backslash = "\\";
212 assert(serializeJsonString(teststring2) ==
214 "\\u0000\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007" +
215 "\\b\\t\\n\\u000b\\f\\r\\u000e\\u000f" +
216 "\\u0010\\u0011\\u0012\\u0013\\u0014\\u0015\\u0016\\u0017" +
217 "\\u0018\\u0019\\u001a\\u001b\\u001c\\u001d\\u001e\\u001f" +
218 " !\\\"" + teststring2.substr(0x23, 0x2f-0x23) +
219 "\\/" + teststring2.substr(0x30, 0x5c-0x30) +
220 backslash + backslash + teststring2.substr(0x5d, 0x7f-0x5d) + "\\u007f" +
221 "\\u0080\\u0081\\u0082\\u0083\\u0084\\u0085\\u0086\\u0087" +
222 "\\u0088\\u0089\\u008a\\u008b\\u008c\\u008d\\u008e\\u008f" +
223 "\\u0090\\u0091\\u0092\\u0093\\u0094\\u0095\\u0096\\u0097" +
224 "\\u0098\\u0099\\u009a\\u009b\\u009c\\u009d\\u009e\\u009f" +
225 "\\u00a0\\u00a1\\u00a2\\u00a3\\u00a4\\u00a5\\u00a6\\u00a7" +
226 "\\u00a8\\u00a9\\u00aa\\u00ab\\u00ac\\u00ad\\u00ae\\u00af" +
227 "\\u00b0\\u00b1\\u00b2\\u00b3\\u00b4\\u00b5\\u00b6\\u00b7" +
228 "\\u00b8\\u00b9\\u00ba\\u00bb\\u00bc\\u00bd\\u00be\\u00bf" +
229 "\\u00c0\\u00c1\\u00c2\\u00c3\\u00c4\\u00c5\\u00c6\\u00c7" +
230 "\\u00c8\\u00c9\\u00ca\\u00cb\\u00cc\\u00cd\\u00ce\\u00cf" +
231 "\\u00d0\\u00d1\\u00d2\\u00d3\\u00d4\\u00d5\\u00d6\\u00d7" +
232 "\\u00d8\\u00d9\\u00da\\u00db\\u00dc\\u00dd\\u00de\\u00df" +
233 "\\u00e0\\u00e1\\u00e2\\u00e3\\u00e4\\u00e5\\u00e6\\u00e7" +
234 "\\u00e8\\u00e9\\u00ea\\u00eb\\u00ec\\u00ed\\u00ee\\u00ef" +
235 "\\u00f0\\u00f1\\u00f2\\u00f3\\u00f4\\u00f5\\u00f6\\u00f7" +
236 "\\u00f8\\u00f9\\u00fa\\u00fb\\u00fc\\u00fd\\u00fe\\u00ff" +
240 std::istringstream is(serializeString(teststring2), std::ios::binary);
241 assert(deSerializeString(is) == teststring2);
247 std::istringstream is(serializeWideString(teststring2_w), std::ios::binary);
248 assert(deSerializeWideString(is) == teststring2_w);
254 std::istringstream is(serializeLongString(teststring2), std::ios::binary);
255 assert(deSerializeLongString(is) == teststring2);
261 std::istringstream is(serializeJsonString(teststring2), std::ios::binary);
262 //dstream<<serializeJsonString(deSerializeJsonString(is));
263 assert(deSerializeJsonString(is) == teststring2);
277 SharedBuffer<u8> fromdata(4);
283 std::ostringstream os(std::ios_base::binary);
284 compress(fromdata, os, 0);
286 std::string str_out = os.str();
288 infostream<<"str_out.size()="<<str_out.size()<<std::endl;
289 infostream<<"TestCompress: 1,5,5,1 -> ";
290 for(u32 i=0; i<str_out.size(); i++)
292 infostream<<(u32)str_out[i]<<",";
294 infostream<<std::endl;
296 assert(str_out.size() == 10);
298 assert(str_out[0] == 0);
299 assert(str_out[1] == 0);
300 assert(str_out[2] == 0);
301 assert(str_out[3] == 4);
302 assert(str_out[4] == 0);
303 assert(str_out[5] == 1);
304 assert(str_out[6] == 1);
305 assert(str_out[7] == 5);
306 assert(str_out[8] == 0);
307 assert(str_out[9] == 1);
309 std::istringstream is(str_out, std::ios_base::binary);
310 std::ostringstream os2(std::ios_base::binary);
312 decompress(is, os2, 0);
313 std::string str_out2 = os2.str();
315 infostream<<"decompress: ";
316 for(u32 i=0; i<str_out2.size(); i++)
318 infostream<<(u32)str_out2[i]<<",";
320 infostream<<std::endl;
322 assert(str_out2.size() == fromdata.getSize());
324 for(u32 i=0; i<str_out2.size(); i++)
326 assert(str_out2[i] == fromdata[i]);
333 SharedBuffer<u8> fromdata(4);
339 std::ostringstream os(std::ios_base::binary);
340 compress(fromdata, os, SER_FMT_VER_HIGHEST);
342 std::string str_out = os.str();
344 infostream<<"str_out.size()="<<str_out.size()<<std::endl;
345 infostream<<"TestCompress: 1,5,5,1 -> ";
346 for(u32 i=0; i<str_out.size(); i++)
348 infostream<<(u32)str_out[i]<<",";
350 infostream<<std::endl;
352 /*assert(str_out.size() == 10);
354 assert(str_out[0] == 0);
355 assert(str_out[1] == 0);
356 assert(str_out[2] == 0);
357 assert(str_out[3] == 4);
358 assert(str_out[4] == 0);
359 assert(str_out[5] == 1);
360 assert(str_out[6] == 1);
361 assert(str_out[7] == 5);
362 assert(str_out[8] == 0);
363 assert(str_out[9] == 1);*/
365 std::istringstream is(str_out, std::ios_base::binary);
366 std::ostringstream os2(std::ios_base::binary);
368 decompress(is, os2, SER_FMT_VER_HIGHEST);
369 std::string str_out2 = os2.str();
371 infostream<<"decompress: ";
372 for(u32 i=0; i<str_out2.size(); i++)
374 infostream<<(u32)str_out2[i]<<",";
376 infostream<<std::endl;
378 assert(str_out2.size() == fromdata.getSize());
380 for(u32 i=0; i<str_out2.size(); i++)
382 assert(str_out2[i] == fromdata[i]);
391 void Run(INodeDefManager *nodedef)
396 assert(n.getContent() == CONTENT_AIR);
397 assert(n.getLight(LIGHTBANK_DAY, nodedef) == 0);
398 assert(n.getLight(LIGHTBANK_NIGHT, nodedef) == 0);
401 n.setContent(CONTENT_AIR);
402 assert(nodedef->get(n).light_propagates == true);
403 n.setContent(LEGN(nodedef, "CONTENT_STONE"));
404 assert(nodedef->get(n).light_propagates == false);
408 struct TestVoxelManipulator
410 void Run(INodeDefManager *nodedef)
416 VoxelArea a(v3s16(-1,-1,-1), v3s16(1,1,1));
417 assert(a.index(0,0,0) == 1*3*3 + 1*3 + 1);
418 assert(a.index(-1,-1,-1) == 0);
420 VoxelArea c(v3s16(-2,-2,-2), v3s16(2,2,2));
421 // An area that is 1 bigger in x+ and z-
422 VoxelArea d(v3s16(-2,-2,-3), v3s16(3,2,2));
424 core::list<VoxelArea> aa;
428 core::array<VoxelArea> results;
429 results.push_back(VoxelArea(v3s16(-2,-2,-3),v3s16(3,2,-3)));
430 results.push_back(VoxelArea(v3s16(3,-2,-2),v3s16(3,2,2)));
432 assert(aa.size() == results.size());
434 infostream<<"Result of diff:"<<std::endl;
435 for(core::list<VoxelArea>::Iterator
436 i = aa.begin(); i != aa.end(); i++)
438 i->print(infostream);
439 infostream<<std::endl;
441 s32 j = results.linear_search(*i);
453 v.print(infostream, nodedef);
455 infostream<<"*** Setting (-1,0,-1)=2 ***"<<std::endl;
457 v.setNodeNoRef(v3s16(-1,0,-1), MapNode(CONTENT_GRASS));
459 v.print(infostream, nodedef);
461 assert(v.getNode(v3s16(-1,0,-1)).getContent() == CONTENT_GRASS);
463 infostream<<"*** Reading from inexistent (0,0,-1) ***"<<std::endl;
465 EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,0,-1)));
467 v.print(infostream, nodedef);
469 infostream<<"*** Adding area ***"<<std::endl;
473 v.print(infostream, nodedef);
475 assert(v.getNode(v3s16(-1,0,-1)).getContent() == CONTENT_GRASS);
476 EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,1,1)));
481 NOTE: These tests became non-working then NodeContainer was removed.
482 These should be redone, utilizing some kind of a virtual
483 interface for Map (IMap would be fine).
488 class TC : public NodeContainer
494 core::list<v3s16> validity_exceptions;
498 position_valid = true;
501 virtual bool isValidPosition(v3s16 p)
503 //return position_valid ^ (p==position_valid_exception);
504 bool exception = false;
505 for(core::list<v3s16>::Iterator i=validity_exceptions.begin();
506 i != validity_exceptions.end(); i++)
514 return exception ? !position_valid : position_valid;
517 virtual MapNode getNode(v3s16 p)
519 if(isValidPosition(p) == false)
520 throw InvalidPositionException();
524 virtual void setNode(v3s16 p, MapNode & n)
526 if(isValidPosition(p) == false)
527 throw InvalidPositionException();
530 virtual u16 nodeContainerId() const
540 MapBlock b(&parent, v3s16(1,1,1));
541 v3s16 relpos(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
543 assert(b.getPosRelative() == relpos);
545 assert(b.getBox().MinEdge.X == MAP_BLOCKSIZE);
546 assert(b.getBox().MaxEdge.X == MAP_BLOCKSIZE*2-1);
547 assert(b.getBox().MinEdge.Y == MAP_BLOCKSIZE);
548 assert(b.getBox().MaxEdge.Y == MAP_BLOCKSIZE*2-1);
549 assert(b.getBox().MinEdge.Z == MAP_BLOCKSIZE);
550 assert(b.getBox().MaxEdge.Z == MAP_BLOCKSIZE*2-1);
552 assert(b.isValidPosition(v3s16(0,0,0)) == true);
553 assert(b.isValidPosition(v3s16(-1,0,0)) == false);
554 assert(b.isValidPosition(v3s16(-1,-142,-2341)) == false);
555 assert(b.isValidPosition(v3s16(-124,142,2341)) == false);
556 assert(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
557 assert(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE,MAP_BLOCKSIZE-1)) == false);
560 TODO: this method should probably be removed
561 if the block size isn't going to be set variable
563 /*assert(b.getSizeNodes() == v3s16(MAP_BLOCKSIZE,
564 MAP_BLOCKSIZE, MAP_BLOCKSIZE));*/
566 // Changed flag should be initially set
567 assert(b.getModified() == MOD_STATE_WRITE_NEEDED);
569 assert(b.getModified() == MOD_STATE_CLEAN);
571 // All nodes should have been set to
572 // .d=CONTENT_IGNORE and .getLight() = 0
573 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
574 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
575 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
577 //assert(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_AIR);
578 assert(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_IGNORE);
579 assert(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_DAY) == 0);
580 assert(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_NIGHT) == 0);
584 MapNode n(CONTENT_AIR);
585 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
586 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
587 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
589 b.setNode(v3s16(x,y,z), n);
594 Parent fetch functions
596 parent.position_valid = false;
597 parent.node.setContent(5);
601 // Positions in the block should still be valid
602 assert(b.isValidPositionParent(v3s16(0,0,0)) == true);
603 assert(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
604 n = b.getNodeParent(v3s16(0,MAP_BLOCKSIZE-1,0));
605 assert(n.getContent() == CONTENT_AIR);
607 // ...but outside the block they should be invalid
608 assert(b.isValidPositionParent(v3s16(-121,2341,0)) == false);
609 assert(b.isValidPositionParent(v3s16(-1,0,0)) == false);
610 assert(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == false);
613 bool exception_thrown = false;
615 // This should throw an exception
616 MapNode n = b.getNodeParent(v3s16(0,0,-1));
618 catch(InvalidPositionException &e)
620 exception_thrown = true;
622 assert(exception_thrown);
625 parent.position_valid = true;
626 // Now the positions outside should be valid
627 assert(b.isValidPositionParent(v3s16(-121,2341,0)) == true);
628 assert(b.isValidPositionParent(v3s16(-1,0,0)) == true);
629 assert(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == true);
630 n = b.getNodeParent(v3s16(0,0,MAP_BLOCKSIZE));
631 assert(n.getContent() == 5);
639 assert(b.getNode(p).getContent() == 4);
640 //TODO: Update to new system
641 /*assert(b.getNodeTile(p) == 4);
642 assert(b.getNodeTile(v3s16(-1,-1,0)) == 5);*/
647 // Set lighting of all nodes to 0
648 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
649 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
650 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
651 MapNode n = b.getNode(v3s16(x,y,z));
652 n.setLight(LIGHTBANK_DAY, 0);
653 n.setLight(LIGHTBANK_NIGHT, 0);
654 b.setNode(v3s16(x,y,z), n);
660 Check how the block handles being a lonely sky block
662 parent.position_valid = true;
663 b.setIsUnderground(false);
664 parent.node.setContent(CONTENT_AIR);
665 parent.node.setLight(LIGHTBANK_DAY, LIGHT_SUN);
666 parent.node.setLight(LIGHTBANK_NIGHT, 0);
667 core::map<v3s16, bool> light_sources;
668 // The bottom block is invalid, because we have a shadowing node
669 assert(b.propagateSunlight(light_sources) == false);
670 assert(b.getNode(v3s16(1,4,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
671 assert(b.getNode(v3s16(1,3,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
672 assert(b.getNode(v3s16(1,2,0)).getLight(LIGHTBANK_DAY) == 0);
673 assert(b.getNode(v3s16(1,1,0)).getLight(LIGHTBANK_DAY) == 0);
674 assert(b.getNode(v3s16(1,0,0)).getLight(LIGHTBANK_DAY) == 0);
675 assert(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
676 assert(b.getFaceLight2(1000, p, v3s16(0,1,0)) == LIGHT_SUN);
677 assert(b.getFaceLight2(1000, p, v3s16(0,-1,0)) == 0);
678 assert(b.getFaceLight2(0, p, v3s16(0,-1,0)) == 0);
679 // According to MapBlock::getFaceLight,
680 // The face on the z+ side should have double-diminished light
681 //assert(b.getFaceLight(p, v3s16(0,0,1)) == diminish_light(diminish_light(LIGHT_MAX)));
682 // The face on the z+ side should have diminished light
683 assert(b.getFaceLight2(1000, p, v3s16(0,0,1)) == diminish_light(LIGHT_MAX));
686 Check how the block handles being in between blocks with some non-sunlight
687 while being underground
690 // Make neighbours to exist and set some non-sunlight to them
691 parent.position_valid = true;
692 b.setIsUnderground(true);
693 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
694 core::map<v3s16, bool> light_sources;
695 // The block below should be valid because there shouldn't be
696 // sunlight in there either
697 assert(b.propagateSunlight(light_sources, true) == true);
698 // Should not touch nodes that are not affected (that is, all of them)
699 //assert(b.getNode(v3s16(1,2,3)).getLight() == LIGHT_SUN);
700 // Should set light of non-sunlighted blocks to 0.
701 assert(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == 0);
704 Set up a situation where:
705 - There is only air in this block
706 - There is a valid non-sunlighted block at the bottom, and
707 - Invalid blocks elsewhere.
708 - the block is not underground.
710 This should result in bottom block invalidity
713 b.setIsUnderground(false);
715 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
716 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
717 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
719 n.setContent(CONTENT_AIR);
720 n.setLight(LIGHTBANK_DAY, 0);
721 b.setNode(v3s16(x,y,z), n);
725 // Make neighbours invalid
726 parent.position_valid = false;
727 // Add exceptions to the top of the bottom block
728 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
729 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
731 parent.validity_exceptions.push_back(v3s16(MAP_BLOCKSIZE+x, MAP_BLOCKSIZE-1, MAP_BLOCKSIZE+z));
733 // Lighting value for the valid nodes
734 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
735 core::map<v3s16, bool> light_sources;
736 // Bottom block is not valid
737 assert(b.propagateSunlight(light_sources) == false);
744 class TC : public NodeContainer
753 position_valid = true;
756 virtual bool isValidPosition(v3s16 p)
758 return position_valid;
761 virtual MapNode getNode(v3s16 p)
763 if(position_valid == false)
764 throw InvalidPositionException();
768 virtual void setNode(v3s16 p, MapNode & n)
770 if(position_valid == false)
771 throw InvalidPositionException();
774 virtual u16 nodeContainerId() const
783 parent.position_valid = false;
785 // Create one with no heightmaps
786 ServerMapSector sector(&parent, v2s16(1,1));
788 assert(sector.getBlockNoCreateNoEx(0) == 0);
789 assert(sector.getBlockNoCreateNoEx(1) == 0);
791 MapBlock * bref = sector.createBlankBlock(-2);
793 assert(sector.getBlockNoCreateNoEx(0) == 0);
794 assert(sector.getBlockNoCreateNoEx(-2) == bref);
796 //TODO: Check for AlreadyExistsException
798 /*bool exception_thrown = false;
802 catch(InvalidPositionException &e){
803 exception_thrown = true;
805 assert(exception_thrown);*/
815 const int port = 30003;
819 const char sendbuffer[] = "hello world!";
820 socket.Send(Address(127,0,0,1,port), sendbuffer, sizeof(sendbuffer));
825 memset(rcvbuffer, 0, sizeof(rcvbuffer));
829 int bytes_read = socket.Receive(sender, rcvbuffer, sizeof(rcvbuffer));
833 //FIXME: This fails on some systems
834 assert(strncmp(sendbuffer, rcvbuffer, sizeof(sendbuffer))==0);
835 assert(sender.getAddress() == Address(127,0,0,1, 0).getAddress());
839 struct TestConnection
844 Test helper functions
847 // Some constants for testing
848 u32 proto_id = 0x12345678;
851 SharedBuffer<u8> data1(1);
853 Address a(127,0,0,1, 10);
856 con::BufferedPacket p1 = con::makePacket(a, data1,
857 proto_id, peer_id, channel);
859 We should now have a packet with this data:
862 [4] u16 sender_peer_id
867 assert(readU32(&p1.data[0]) == proto_id);
868 assert(readU16(&p1.data[4]) == peer_id);
869 assert(readU8(&p1.data[6]) == channel);
870 assert(readU8(&p1.data[7]) == data1[0]);
872 //infostream<<"initial data1[0]="<<((u32)data1[0]&0xff)<<std::endl;
874 SharedBuffer<u8> p2 = con::makeReliablePacket(data1, seqnum);
876 /*infostream<<"p2.getSize()="<<p2.getSize()<<", data1.getSize()="
877 <<data1.getSize()<<std::endl;
878 infostream<<"readU8(&p2[3])="<<readU8(&p2[3])
879 <<" p2[3]="<<((u32)p2[3]&0xff)<<std::endl;
880 infostream<<"data1[0]="<<((u32)data1[0]&0xff)<<std::endl;*/
882 assert(p2.getSize() == 3 + data1.getSize());
883 assert(readU8(&p2[0]) == TYPE_RELIABLE);
884 assert(readU16(&p2[1]) == seqnum);
885 assert(readU8(&p2[3]) == data1[0]);
888 struct Handler : public con::PeerHandler
890 Handler(const char *a_name)
896 void peerAdded(con::Peer *peer)
898 infostream<<"Handler("<<name<<")::peerAdded(): "
899 "id="<<peer->id<<std::endl;
903 void deletingPeer(con::Peer *peer, bool timeout)
905 infostream<<"Handler("<<name<<")::deletingPeer(): "
907 <<", timeout="<<timeout<<std::endl;
919 DSTACK("TestConnection::Run");
924 Test some real connections
926 NOTE: This mostly tests the legacy interface.
929 u32 proto_id = 0xad26846a;
931 Handler hand_server("server");
932 Handler hand_client("client");
934 infostream<<"** Creating server Connection"<<std::endl;
935 con::Connection server(proto_id, 512, 5.0, &hand_server);
938 infostream<<"** Creating client Connection"<<std::endl;
939 con::Connection client(proto_id, 512, 5.0, &hand_client);
941 assert(hand_server.count == 0);
942 assert(hand_client.count == 0);
946 Address server_address(127,0,0,1, 30001);
947 infostream<<"** running client.Connect()"<<std::endl;
948 client.Connect(server_address);
952 // Client should not have added client yet
953 assert(hand_client.count == 0);
958 SharedBuffer<u8> data;
959 infostream<<"** running client.Receive()"<<std::endl;
960 u32 size = client.Receive(peer_id, data);
961 infostream<<"** Client received: peer_id="<<peer_id
965 catch(con::NoIncomingDataException &e)
969 // Client should have added server now
970 assert(hand_client.count == 1);
971 assert(hand_client.last_id == 1);
972 // Server should not have added client yet
973 assert(hand_server.count == 0);
980 SharedBuffer<u8> data;
981 infostream<<"** running server.Receive()"<<std::endl;
982 u32 size = server.Receive(peer_id, data);
983 infostream<<"** Server received: peer_id="<<peer_id
987 catch(con::NoIncomingDataException &e)
989 // No actual data received, but the client has
990 // probably been connected
993 // Client should be the same
994 assert(hand_client.count == 1);
995 assert(hand_client.last_id == 1);
996 // Server should have the client
997 assert(hand_server.count == 1);
998 assert(hand_server.last_id == 2);
1002 while(client.Connected() == false)
1007 SharedBuffer<u8> data;
1008 infostream<<"** running client.Receive()"<<std::endl;
1009 u32 size = client.Receive(peer_id, data);
1010 infostream<<"** Client received: peer_id="<<peer_id
1014 catch(con::NoIncomingDataException &e)
1025 SharedBuffer<u8> data;
1026 infostream<<"** running server.Receive()"<<std::endl;
1027 u32 size = server.Receive(peer_id, data);
1028 infostream<<"** Server received: peer_id="<<peer_id
1032 catch(con::NoIncomingDataException &e)
1037 Simple send-receive test
1040 /*u8 data[] = "Hello World!";
1041 u32 datasize = sizeof(data);*/
1042 SharedBuffer<u8> data = SharedBufferFromString("Hello World!");
1044 infostream<<"** running client.Send()"<<std::endl;
1045 client.Send(PEER_ID_SERVER, 0, data, true);
1050 SharedBuffer<u8> recvdata;
1051 infostream<<"** running server.Receive()"<<std::endl;
1052 u32 size = server.Receive(peer_id, recvdata);
1053 infostream<<"** Server received: peer_id="<<peer_id
1057 assert(memcmp(*data, *recvdata, data.getSize()) == 0);
1060 u16 peer_id_client = 2;
1063 Send consequent packets in different order
1064 Not compatible with new Connection, thus commented out.
1067 //u8 data1[] = "hello1";
1068 //u8 data2[] = "hello2";
1069 SharedBuffer<u8> data1 = SharedBufferFromString("hello1");
1070 SharedBuffer<u8> data2 = SharedBufferFromString("Hello2");
1072 Address client_address =
1073 server.GetPeerAddress(peer_id_client);
1075 infostream<<"*** Sending packets in wrong order (2,1,2)"
1079 con::Channel *ch = &server.getPeer(peer_id_client)->channels[chn];
1080 u16 sn = ch->next_outgoing_seqnum;
1081 ch->next_outgoing_seqnum = sn+1;
1082 server.Send(peer_id_client, chn, data2, true);
1083 ch->next_outgoing_seqnum = sn;
1084 server.Send(peer_id_client, chn, data1, true);
1085 ch->next_outgoing_seqnum = sn+1;
1086 server.Send(peer_id_client, chn, data2, true);
1090 infostream<<"*** Receiving the packets"<<std::endl;
1093 SharedBuffer<u8> recvdata;
1096 infostream<<"** running client.Receive()"<<std::endl;
1098 size = client.Receive(peer_id, recvdata);
1099 infostream<<"** Client received: peer_id="<<peer_id
1101 <<", data="<<*recvdata
1103 assert(size == data1.getSize());
1104 assert(memcmp(*data1, *recvdata, data1.getSize()) == 0);
1105 assert(peer_id == PEER_ID_SERVER);
1107 infostream<<"** running client.Receive()"<<std::endl;
1109 size = client.Receive(peer_id, recvdata);
1110 infostream<<"** Client received: peer_id="<<peer_id
1112 <<", data="<<*recvdata
1114 assert(size == data2.getSize());
1115 assert(memcmp(*data2, *recvdata, data2.getSize()) == 0);
1116 assert(peer_id == PEER_ID_SERVER);
1118 bool got_exception = false;
1121 infostream<<"** running client.Receive()"<<std::endl;
1123 size = client.Receive(peer_id, recvdata);
1124 infostream<<"** Client received: peer_id="<<peer_id
1126 <<", data="<<*recvdata
1129 catch(con::NoIncomingDataException &e)
1131 infostream<<"** No incoming data for client"<<std::endl;
1132 got_exception = true;
1134 assert(got_exception);
1139 Send large amounts of packets (infinite test)
1140 Commented out because of infinity.
1143 infostream<<"Sending large amounts of packets (infinite test)"<<std::endl;
1146 int datasize = myrand_range(0,5)==0?myrand_range(100,10000):myrand_range(0,100);
1147 infostream<<"datasize="<<datasize<<std::endl;
1148 SharedBuffer<u8> data1(datasize);
1149 for(u16 i=0; i<datasize; i++)
1152 int sendtimes = myrand_range(1,10);
1153 for(int i=0; i<sendtimes; i++){
1154 server.Send(peer_id_client, 0, data1, true);
1157 infostream<<"sendcount="<<sendcount<<std::endl;
1159 //int receivetimes = myrand_range(1,20);
1160 int receivetimes = 20;
1161 for(int i=0; i<receivetimes; i++){
1162 SharedBuffer<u8> recvdata;
1165 bool received = false;
1167 size = client.Receive(peer_id, recvdata);
1169 }catch(con::NoIncomingDataException &e){
1179 const int datasize = 30000;
1180 SharedBuffer<u8> data1(datasize);
1181 for(u16 i=0; i<datasize; i++){
1185 infostream<<"Sending data (size="<<datasize<<"):";
1186 for(int i=0; i<datasize && i<20; i++){
1187 if(i%2==0) infostream<<" ";
1189 snprintf(buf, 10, "%.2X", ((int)((const char*)*data1)[i])&0xff);
1194 infostream<<std::endl;
1196 server.Send(peer_id_client, 0, data1, true);
1200 SharedBuffer<u8> recvdata;
1201 infostream<<"** running client.Receive()"<<std::endl;
1204 bool received = false;
1205 u32 timems0 = porting::getTimeMs();
1207 if(porting::getTimeMs() - timems0 > 5000 || received)
1210 size = client.Receive(peer_id, recvdata);
1212 }catch(con::NoIncomingDataException &e){
1217 infostream<<"** Client received: peer_id="<<peer_id
1221 infostream<<"Received data (size="<<size<<"): ";
1222 for(int i=0; i<size && i<20; i++){
1223 if(i%2==0) infostream<<" ";
1225 snprintf(buf, 10, "%.2X", ((int)(recvdata[i]))&0xff);
1230 infostream<<std::endl;
1232 assert(memcmp(*data1, *recvdata, data1.getSize()) == 0);
1233 assert(peer_id == PEER_ID_SERVER);
1236 // Check peer handlers
1237 assert(hand_client.count == 1);
1238 assert(hand_client.last_id == 1);
1239 assert(hand_server.count == 1);
1240 assert(hand_server.last_id == 2);
1249 infostream<<"Running " #X <<std::endl;\
1253 #define TESTPARAMS(X, ...)\
1256 infostream<<"Running " #X <<std::endl;\
1257 x.Run(__VA_ARGS__);\
1262 DSTACK(__FUNCTION_NAME);
1264 // Create item and node definitions
1265 IWritableItemDefManager *idef = createItemDefManager();
1266 IWritableNodeDefManager *ndef = createNodeDefManager();
1267 define_some_nodes(idef, ndef);
1269 infostream<<"run_tests() started"<<std::endl;
1270 TEST(TestUtilities);
1273 TEST(TestSerialization);
1274 TESTPARAMS(TestMapNode, ndef);
1275 TESTPARAMS(TestVoxelManipulator, ndef);
1276 //TEST(TestMapBlock);
1277 //TEST(TestMapSector);
1278 if(INTERNET_SIMULATOR == false){
1280 dout_con<<"=== BEGIN RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
1281 TEST(TestConnection);
1282 dout_con<<"=== END RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
1284 infostream<<"run_tests() passed"<<std::endl;