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"
41 Asserts that the exception occurs
43 #define EXCEPTION_CHECK(EType, code)\
45 bool exception_thrown = false;\
47 catch(EType &e) { exception_thrown = true; }\
48 assert(exception_thrown);\
52 A few item and node definitions for those tests that need them
55 #define CONTENT_STONE 0
56 #define CONTENT_GRASS 0x800
58 void define_some_nodes(IWritableItemDefManager *idef, IWritableNodeDefManager *ndef)
61 ItemDefinition itemdef;
68 itemdef = ItemDefinition();
69 itemdef.type = ITEM_NODE;
70 itemdef.name = "default:stone";
71 itemdef.description = "Stone";
72 itemdef.groups["cracky"] = 3;
73 itemdef.inventory_image = "[inventorycube"
77 f = ContentFeatures();
78 f.name = itemdef.name;
79 for(int i = 0; i < 6; i++)
80 f.tname_tiles[i] = "default_stone.png";
81 f.is_ground_content = true;
82 idef->registerItem(itemdef);
89 itemdef = ItemDefinition();
90 itemdef.type = ITEM_NODE;
91 itemdef.name = "default:dirt_with_grass";
92 itemdef.description = "Dirt with grass";
93 itemdef.groups["crumbly"] = 3;
94 itemdef.inventory_image = "[inventorycube"
96 "{default_dirt.png&default_grass_side.png"
97 "{default_dirt.png&default_grass_side.png";
98 f = ContentFeatures();
99 f.name = itemdef.name;
100 f.tname_tiles[0] = "default_grass.png";
101 f.tname_tiles[1] = "default_dirt.png";
102 for(int i = 2; i < 6; i++)
103 f.tname_tiles[i] = "default_dirt.png^default_grass_side.png";
104 f.is_ground_content = true;
105 idef->registerItem(itemdef);
113 /*infostream<<"wrapDegrees(100.0) = "<<wrapDegrees(100.0)<<std::endl;
114 infostream<<"wrapDegrees(720.5) = "<<wrapDegrees(720.5)<<std::endl;
115 infostream<<"wrapDegrees(-0.5) = "<<wrapDegrees(-0.5)<<std::endl;*/
116 assert(fabs(wrapDegrees(100.0) - 100.0) < 0.001);
117 assert(fabs(wrapDegrees(720.5) - 0.5) < 0.001);
118 assert(fabs(wrapDegrees(-0.5) - (-0.5)) < 0.001);
119 assert(fabs(wrapDegrees(-365.5) - (-5.5)) < 0.001);
120 assert(lowercase("Foo bAR") == "foo bar");
121 assert(is_yes("YeS") == true);
122 assert(is_yes("") == false);
123 assert(is_yes("FAlse") == false);
124 const char *ends[] = {"abc", "c", "bc", NULL};
125 assert(removeStringEnd("abc", ends) == "");
126 assert(removeStringEnd("bc", ends) == "b");
127 assert(removeStringEnd("12c", ends) == "12");
128 assert(removeStringEnd("foo", ends) == "");
137 // Test reading of settings
138 s.parseConfigLine("leet = 1337");
139 s.parseConfigLine("leetleet = 13371337");
140 s.parseConfigLine("leetleet_neg = -13371337");
141 s.parseConfigLine("floaty_thing = 1.1");
142 s.parseConfigLine("stringy_thing = asd /( ¤%&(/\" BLÖÄRP");
143 s.parseConfigLine("coord = (1, 2, 4.5)");
144 assert(s.getS32("leet") == 1337);
145 assert(s.getS16("leetleet") == 32767);
146 assert(s.getS16("leetleet_neg") == -32768);
147 // Not sure if 1.1 is an exact value as a float, but doesn't matter
148 assert(fabs(s.getFloat("floaty_thing") - 1.1) < 0.001);
149 assert(s.get("stringy_thing") == "asd /( ¤%&(/\" BLÖÄRP");
150 assert(fabs(s.getV3F("coord").X - 1.0) < 0.001);
151 assert(fabs(s.getV3F("coord").Y - 2.0) < 0.001);
152 assert(fabs(s.getV3F("coord").Z - 4.5) < 0.001);
153 // Test the setting of settings too
154 s.setFloat("floaty_thing_2", 1.2);
155 s.setV3F("coord2", v3f(1, 2, 3.3));
156 assert(s.get("floaty_thing_2").substr(0,3) == "1.2");
157 assert(fabs(s.getFloat("floaty_thing_2") - 1.2) < 0.001);
158 assert(fabs(s.getV3F("coord2").X - 1.0) < 0.001);
159 assert(fabs(s.getV3F("coord2").Y - 2.0) < 0.001);
160 assert(fabs(s.getV3F("coord2").Z - 3.3) < 0.001);
164 struct TestSerialization
166 // To be used like this:
167 // mkstr("Some\0string\0with\0embedded\0nuls")
168 // since std::string("...") doesn't work as expected in that case.
169 template<size_t N> std::string mkstr(const char (&s)[N])
171 return std::string(s, N - 1);
176 // Tests some serialization primitives
178 assert(serializeString("") == mkstr("\0\0"));
179 assert(serializeWideString(L"") == mkstr("\0\0"));
180 assert(serializeLongString("") == mkstr("\0\0\0\0"));
181 assert(serializeJsonString("") == "\"\"");
183 std::string teststring = "Hello world!";
184 assert(serializeString(teststring) ==
185 mkstr("\0\14Hello world!"));
186 assert(serializeWideString(narrow_to_wide(teststring)) ==
187 mkstr("\0\14\0H\0e\0l\0l\0o\0 \0w\0o\0r\0l\0d\0!"));
188 assert(serializeLongString(teststring) ==
189 mkstr("\0\0\0\14Hello world!"));
190 assert(serializeJsonString(teststring) ==
193 std::string teststring2;
194 std::wstring teststring2_w;
195 std::string teststring2_w_encoded;
197 std::ostringstream tmp_os;
198 std::wostringstream tmp_os_w;
199 std::ostringstream tmp_os_w_encoded;
200 for(int i = 0; i < 256; i++)
203 tmp_os_w<<(wchar_t)i;
204 tmp_os_w_encoded<<(char)0<<(char)i;
206 teststring2 = tmp_os.str();
207 teststring2_w = tmp_os_w.str();
208 teststring2_w_encoded = tmp_os_w_encoded.str();
210 assert(serializeString(teststring2) ==
211 mkstr("\1\0") + teststring2);
212 assert(serializeWideString(teststring2_w) ==
213 mkstr("\1\0") + teststring2_w_encoded);
214 assert(serializeLongString(teststring2) ==
215 mkstr("\0\0\1\0") + teststring2);
216 // MSVC fails when directly using "\\\\"
217 std::string backslash = "\\";
218 assert(serializeJsonString(teststring2) ==
220 "\\u0000\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007" +
221 "\\b\\t\\n\\u000b\\f\\r\\u000e\\u000f" +
222 "\\u0010\\u0011\\u0012\\u0013\\u0014\\u0015\\u0016\\u0017" +
223 "\\u0018\\u0019\\u001a\\u001b\\u001c\\u001d\\u001e\\u001f" +
224 " !\\\"" + teststring2.substr(0x23, 0x2f-0x23) +
225 "\\/" + teststring2.substr(0x30, 0x5c-0x30) +
226 backslash + backslash + teststring2.substr(0x5d, 0x7f-0x5d) + "\\u007f" +
227 "\\u0080\\u0081\\u0082\\u0083\\u0084\\u0085\\u0086\\u0087" +
228 "\\u0088\\u0089\\u008a\\u008b\\u008c\\u008d\\u008e\\u008f" +
229 "\\u0090\\u0091\\u0092\\u0093\\u0094\\u0095\\u0096\\u0097" +
230 "\\u0098\\u0099\\u009a\\u009b\\u009c\\u009d\\u009e\\u009f" +
231 "\\u00a0\\u00a1\\u00a2\\u00a3\\u00a4\\u00a5\\u00a6\\u00a7" +
232 "\\u00a8\\u00a9\\u00aa\\u00ab\\u00ac\\u00ad\\u00ae\\u00af" +
233 "\\u00b0\\u00b1\\u00b2\\u00b3\\u00b4\\u00b5\\u00b6\\u00b7" +
234 "\\u00b8\\u00b9\\u00ba\\u00bb\\u00bc\\u00bd\\u00be\\u00bf" +
235 "\\u00c0\\u00c1\\u00c2\\u00c3\\u00c4\\u00c5\\u00c6\\u00c7" +
236 "\\u00c8\\u00c9\\u00ca\\u00cb\\u00cc\\u00cd\\u00ce\\u00cf" +
237 "\\u00d0\\u00d1\\u00d2\\u00d3\\u00d4\\u00d5\\u00d6\\u00d7" +
238 "\\u00d8\\u00d9\\u00da\\u00db\\u00dc\\u00dd\\u00de\\u00df" +
239 "\\u00e0\\u00e1\\u00e2\\u00e3\\u00e4\\u00e5\\u00e6\\u00e7" +
240 "\\u00e8\\u00e9\\u00ea\\u00eb\\u00ec\\u00ed\\u00ee\\u00ef" +
241 "\\u00f0\\u00f1\\u00f2\\u00f3\\u00f4\\u00f5\\u00f6\\u00f7" +
242 "\\u00f8\\u00f9\\u00fa\\u00fb\\u00fc\\u00fd\\u00fe\\u00ff" +
246 std::istringstream is(serializeString(teststring2), std::ios::binary);
247 assert(deSerializeString(is) == teststring2);
253 std::istringstream is(serializeWideString(teststring2_w), std::ios::binary);
254 assert(deSerializeWideString(is) == teststring2_w);
260 std::istringstream is(serializeLongString(teststring2), std::ios::binary);
261 assert(deSerializeLongString(is) == teststring2);
267 std::istringstream is(serializeJsonString(teststring2), std::ios::binary);
268 //dstream<<serializeJsonString(deSerializeJsonString(is));
269 assert(deSerializeJsonString(is) == teststring2);
283 SharedBuffer<u8> fromdata(4);
289 std::ostringstream os(std::ios_base::binary);
290 compress(fromdata, os, 0);
292 std::string str_out = os.str();
294 infostream<<"str_out.size()="<<str_out.size()<<std::endl;
295 infostream<<"TestCompress: 1,5,5,1 -> ";
296 for(u32 i=0; i<str_out.size(); i++)
298 infostream<<(u32)str_out[i]<<",";
300 infostream<<std::endl;
302 assert(str_out.size() == 10);
304 assert(str_out[0] == 0);
305 assert(str_out[1] == 0);
306 assert(str_out[2] == 0);
307 assert(str_out[3] == 4);
308 assert(str_out[4] == 0);
309 assert(str_out[5] == 1);
310 assert(str_out[6] == 1);
311 assert(str_out[7] == 5);
312 assert(str_out[8] == 0);
313 assert(str_out[9] == 1);
315 std::istringstream is(str_out, std::ios_base::binary);
316 std::ostringstream os2(std::ios_base::binary);
318 decompress(is, os2, 0);
319 std::string str_out2 = os2.str();
321 infostream<<"decompress: ";
322 for(u32 i=0; i<str_out2.size(); i++)
324 infostream<<(u32)str_out2[i]<<",";
326 infostream<<std::endl;
328 assert(str_out2.size() == fromdata.getSize());
330 for(u32 i=0; i<str_out2.size(); i++)
332 assert(str_out2[i] == fromdata[i]);
339 SharedBuffer<u8> fromdata(4);
345 std::ostringstream os(std::ios_base::binary);
346 compress(fromdata, os, SER_FMT_VER_HIGHEST);
348 std::string str_out = os.str();
350 infostream<<"str_out.size()="<<str_out.size()<<std::endl;
351 infostream<<"TestCompress: 1,5,5,1 -> ";
352 for(u32 i=0; i<str_out.size(); i++)
354 infostream<<(u32)str_out[i]<<",";
356 infostream<<std::endl;
358 /*assert(str_out.size() == 10);
360 assert(str_out[0] == 0);
361 assert(str_out[1] == 0);
362 assert(str_out[2] == 0);
363 assert(str_out[3] == 4);
364 assert(str_out[4] == 0);
365 assert(str_out[5] == 1);
366 assert(str_out[6] == 1);
367 assert(str_out[7] == 5);
368 assert(str_out[8] == 0);
369 assert(str_out[9] == 1);*/
371 std::istringstream is(str_out, std::ios_base::binary);
372 std::ostringstream os2(std::ios_base::binary);
374 decompress(is, os2, SER_FMT_VER_HIGHEST);
375 std::string str_out2 = os2.str();
377 infostream<<"decompress: ";
378 for(u32 i=0; i<str_out2.size(); i++)
380 infostream<<(u32)str_out2[i]<<",";
382 infostream<<std::endl;
384 assert(str_out2.size() == fromdata.getSize());
386 for(u32 i=0; i<str_out2.size(); i++)
388 assert(str_out2[i] == fromdata[i]);
397 void Run(INodeDefManager *nodedef)
402 assert(n.getContent() == CONTENT_AIR);
403 assert(n.getLight(LIGHTBANK_DAY, nodedef) == 0);
404 assert(n.getLight(LIGHTBANK_NIGHT, nodedef) == 0);
407 n.setContent(CONTENT_AIR);
408 assert(nodedef->get(n).light_propagates == true);
409 n.setContent(LEGN(nodedef, "CONTENT_STONE"));
410 assert(nodedef->get(n).light_propagates == false);
414 struct TestVoxelManipulator
416 void Run(INodeDefManager *nodedef)
422 VoxelArea a(v3s16(-1,-1,-1), v3s16(1,1,1));
423 assert(a.index(0,0,0) == 1*3*3 + 1*3 + 1);
424 assert(a.index(-1,-1,-1) == 0);
426 VoxelArea c(v3s16(-2,-2,-2), v3s16(2,2,2));
427 // An area that is 1 bigger in x+ and z-
428 VoxelArea d(v3s16(-2,-2,-3), v3s16(3,2,2));
430 core::list<VoxelArea> aa;
434 core::array<VoxelArea> results;
435 results.push_back(VoxelArea(v3s16(-2,-2,-3),v3s16(3,2,-3)));
436 results.push_back(VoxelArea(v3s16(3,-2,-2),v3s16(3,2,2)));
438 assert(aa.size() == results.size());
440 infostream<<"Result of diff:"<<std::endl;
441 for(core::list<VoxelArea>::Iterator
442 i = aa.begin(); i != aa.end(); i++)
444 i->print(infostream);
445 infostream<<std::endl;
447 s32 j = results.linear_search(*i);
459 v.print(infostream, nodedef);
461 infostream<<"*** Setting (-1,0,-1)=2 ***"<<std::endl;
463 v.setNodeNoRef(v3s16(-1,0,-1), MapNode(CONTENT_GRASS));
465 v.print(infostream, nodedef);
467 assert(v.getNode(v3s16(-1,0,-1)).getContent() == CONTENT_GRASS);
469 infostream<<"*** Reading from inexistent (0,0,-1) ***"<<std::endl;
471 EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,0,-1)));
473 v.print(infostream, nodedef);
475 infostream<<"*** Adding area ***"<<std::endl;
479 v.print(infostream, nodedef);
481 assert(v.getNode(v3s16(-1,0,-1)).getContent() == CONTENT_GRASS);
482 EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,1,1)));
487 NOTE: These tests became non-working then NodeContainer was removed.
488 These should be redone, utilizing some kind of a virtual
489 interface for Map (IMap would be fine).
494 class TC : public NodeContainer
500 core::list<v3s16> validity_exceptions;
504 position_valid = true;
507 virtual bool isValidPosition(v3s16 p)
509 //return position_valid ^ (p==position_valid_exception);
510 bool exception = false;
511 for(core::list<v3s16>::Iterator i=validity_exceptions.begin();
512 i != validity_exceptions.end(); i++)
520 return exception ? !position_valid : position_valid;
523 virtual MapNode getNode(v3s16 p)
525 if(isValidPosition(p) == false)
526 throw InvalidPositionException();
530 virtual void setNode(v3s16 p, MapNode & n)
532 if(isValidPosition(p) == false)
533 throw InvalidPositionException();
536 virtual u16 nodeContainerId() const
546 MapBlock b(&parent, v3s16(1,1,1));
547 v3s16 relpos(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
549 assert(b.getPosRelative() == relpos);
551 assert(b.getBox().MinEdge.X == MAP_BLOCKSIZE);
552 assert(b.getBox().MaxEdge.X == MAP_BLOCKSIZE*2-1);
553 assert(b.getBox().MinEdge.Y == MAP_BLOCKSIZE);
554 assert(b.getBox().MaxEdge.Y == MAP_BLOCKSIZE*2-1);
555 assert(b.getBox().MinEdge.Z == MAP_BLOCKSIZE);
556 assert(b.getBox().MaxEdge.Z == MAP_BLOCKSIZE*2-1);
558 assert(b.isValidPosition(v3s16(0,0,0)) == true);
559 assert(b.isValidPosition(v3s16(-1,0,0)) == false);
560 assert(b.isValidPosition(v3s16(-1,-142,-2341)) == false);
561 assert(b.isValidPosition(v3s16(-124,142,2341)) == false);
562 assert(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
563 assert(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE,MAP_BLOCKSIZE-1)) == false);
566 TODO: this method should probably be removed
567 if the block size isn't going to be set variable
569 /*assert(b.getSizeNodes() == v3s16(MAP_BLOCKSIZE,
570 MAP_BLOCKSIZE, MAP_BLOCKSIZE));*/
572 // Changed flag should be initially set
573 assert(b.getModified() == MOD_STATE_WRITE_NEEDED);
575 assert(b.getModified() == MOD_STATE_CLEAN);
577 // All nodes should have been set to
578 // .d=CONTENT_IGNORE and .getLight() = 0
579 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
580 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
581 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
583 //assert(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_AIR);
584 assert(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_IGNORE);
585 assert(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_DAY) == 0);
586 assert(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_NIGHT) == 0);
590 MapNode n(CONTENT_AIR);
591 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
592 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
593 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
595 b.setNode(v3s16(x,y,z), n);
600 Parent fetch functions
602 parent.position_valid = false;
603 parent.node.setContent(5);
607 // Positions in the block should still be valid
608 assert(b.isValidPositionParent(v3s16(0,0,0)) == true);
609 assert(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
610 n = b.getNodeParent(v3s16(0,MAP_BLOCKSIZE-1,0));
611 assert(n.getContent() == CONTENT_AIR);
613 // ...but outside the block they should be invalid
614 assert(b.isValidPositionParent(v3s16(-121,2341,0)) == false);
615 assert(b.isValidPositionParent(v3s16(-1,0,0)) == false);
616 assert(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == false);
619 bool exception_thrown = false;
621 // This should throw an exception
622 MapNode n = b.getNodeParent(v3s16(0,0,-1));
624 catch(InvalidPositionException &e)
626 exception_thrown = true;
628 assert(exception_thrown);
631 parent.position_valid = true;
632 // Now the positions outside should be valid
633 assert(b.isValidPositionParent(v3s16(-121,2341,0)) == true);
634 assert(b.isValidPositionParent(v3s16(-1,0,0)) == true);
635 assert(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == true);
636 n = b.getNodeParent(v3s16(0,0,MAP_BLOCKSIZE));
637 assert(n.getContent() == 5);
645 assert(b.getNode(p).getContent() == 4);
646 //TODO: Update to new system
647 /*assert(b.getNodeTile(p) == 4);
648 assert(b.getNodeTile(v3s16(-1,-1,0)) == 5);*/
653 // Set lighting of all nodes to 0
654 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
655 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
656 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
657 MapNode n = b.getNode(v3s16(x,y,z));
658 n.setLight(LIGHTBANK_DAY, 0);
659 n.setLight(LIGHTBANK_NIGHT, 0);
660 b.setNode(v3s16(x,y,z), n);
666 Check how the block handles being a lonely sky block
668 parent.position_valid = true;
669 b.setIsUnderground(false);
670 parent.node.setContent(CONTENT_AIR);
671 parent.node.setLight(LIGHTBANK_DAY, LIGHT_SUN);
672 parent.node.setLight(LIGHTBANK_NIGHT, 0);
673 core::map<v3s16, bool> light_sources;
674 // The bottom block is invalid, because we have a shadowing node
675 assert(b.propagateSunlight(light_sources) == false);
676 assert(b.getNode(v3s16(1,4,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
677 assert(b.getNode(v3s16(1,3,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
678 assert(b.getNode(v3s16(1,2,0)).getLight(LIGHTBANK_DAY) == 0);
679 assert(b.getNode(v3s16(1,1,0)).getLight(LIGHTBANK_DAY) == 0);
680 assert(b.getNode(v3s16(1,0,0)).getLight(LIGHTBANK_DAY) == 0);
681 assert(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
682 assert(b.getFaceLight2(1000, p, v3s16(0,1,0)) == LIGHT_SUN);
683 assert(b.getFaceLight2(1000, p, v3s16(0,-1,0)) == 0);
684 assert(b.getFaceLight2(0, p, v3s16(0,-1,0)) == 0);
685 // According to MapBlock::getFaceLight,
686 // The face on the z+ side should have double-diminished light
687 //assert(b.getFaceLight(p, v3s16(0,0,1)) == diminish_light(diminish_light(LIGHT_MAX)));
688 // The face on the z+ side should have diminished light
689 assert(b.getFaceLight2(1000, p, v3s16(0,0,1)) == diminish_light(LIGHT_MAX));
692 Check how the block handles being in between blocks with some non-sunlight
693 while being underground
696 // Make neighbours to exist and set some non-sunlight to them
697 parent.position_valid = true;
698 b.setIsUnderground(true);
699 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
700 core::map<v3s16, bool> light_sources;
701 // The block below should be valid because there shouldn't be
702 // sunlight in there either
703 assert(b.propagateSunlight(light_sources, true) == true);
704 // Should not touch nodes that are not affected (that is, all of them)
705 //assert(b.getNode(v3s16(1,2,3)).getLight() == LIGHT_SUN);
706 // Should set light of non-sunlighted blocks to 0.
707 assert(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == 0);
710 Set up a situation where:
711 - There is only air in this block
712 - There is a valid non-sunlighted block at the bottom, and
713 - Invalid blocks elsewhere.
714 - the block is not underground.
716 This should result in bottom block invalidity
719 b.setIsUnderground(false);
721 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
722 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
723 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
725 n.setContent(CONTENT_AIR);
726 n.setLight(LIGHTBANK_DAY, 0);
727 b.setNode(v3s16(x,y,z), n);
731 // Make neighbours invalid
732 parent.position_valid = false;
733 // Add exceptions to the top of the bottom block
734 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
735 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
737 parent.validity_exceptions.push_back(v3s16(MAP_BLOCKSIZE+x, MAP_BLOCKSIZE-1, MAP_BLOCKSIZE+z));
739 // Lighting value for the valid nodes
740 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
741 core::map<v3s16, bool> light_sources;
742 // Bottom block is not valid
743 assert(b.propagateSunlight(light_sources) == false);
750 class TC : public NodeContainer
759 position_valid = true;
762 virtual bool isValidPosition(v3s16 p)
764 return position_valid;
767 virtual MapNode getNode(v3s16 p)
769 if(position_valid == false)
770 throw InvalidPositionException();
774 virtual void setNode(v3s16 p, MapNode & n)
776 if(position_valid == false)
777 throw InvalidPositionException();
780 virtual u16 nodeContainerId() const
789 parent.position_valid = false;
791 // Create one with no heightmaps
792 ServerMapSector sector(&parent, v2s16(1,1));
794 assert(sector.getBlockNoCreateNoEx(0) == 0);
795 assert(sector.getBlockNoCreateNoEx(1) == 0);
797 MapBlock * bref = sector.createBlankBlock(-2);
799 assert(sector.getBlockNoCreateNoEx(0) == 0);
800 assert(sector.getBlockNoCreateNoEx(-2) == bref);
802 //TODO: Check for AlreadyExistsException
804 /*bool exception_thrown = false;
808 catch(InvalidPositionException &e){
809 exception_thrown = true;
811 assert(exception_thrown);*/
821 const int port = 30003;
825 const char sendbuffer[] = "hello world!";
826 socket.Send(Address(127,0,0,1,port), sendbuffer, sizeof(sendbuffer));
831 memset(rcvbuffer, 0, sizeof(rcvbuffer));
835 int bytes_read = socket.Receive(sender, rcvbuffer, sizeof(rcvbuffer));
839 //FIXME: This fails on some systems
840 assert(strncmp(sendbuffer, rcvbuffer, sizeof(sendbuffer))==0);
841 assert(sender.getAddress() == Address(127,0,0,1, 0).getAddress());
845 struct TestConnection
850 Test helper functions
853 // Some constants for testing
854 u32 proto_id = 0x12345678;
857 SharedBuffer<u8> data1(1);
859 Address a(127,0,0,1, 10);
862 con::BufferedPacket p1 = con::makePacket(a, data1,
863 proto_id, peer_id, channel);
865 We should now have a packet with this data:
868 [4] u16 sender_peer_id
873 assert(readU32(&p1.data[0]) == proto_id);
874 assert(readU16(&p1.data[4]) == peer_id);
875 assert(readU8(&p1.data[6]) == channel);
876 assert(readU8(&p1.data[7]) == data1[0]);
878 //infostream<<"initial data1[0]="<<((u32)data1[0]&0xff)<<std::endl;
880 SharedBuffer<u8> p2 = con::makeReliablePacket(data1, seqnum);
882 /*infostream<<"p2.getSize()="<<p2.getSize()<<", data1.getSize()="
883 <<data1.getSize()<<std::endl;
884 infostream<<"readU8(&p2[3])="<<readU8(&p2[3])
885 <<" p2[3]="<<((u32)p2[3]&0xff)<<std::endl;
886 infostream<<"data1[0]="<<((u32)data1[0]&0xff)<<std::endl;*/
888 assert(p2.getSize() == 3 + data1.getSize());
889 assert(readU8(&p2[0]) == TYPE_RELIABLE);
890 assert(readU16(&p2[1]) == seqnum);
891 assert(readU8(&p2[3]) == data1[0]);
894 struct Handler : public con::PeerHandler
896 Handler(const char *a_name)
902 void peerAdded(con::Peer *peer)
904 infostream<<"Handler("<<name<<")::peerAdded(): "
905 "id="<<peer->id<<std::endl;
909 void deletingPeer(con::Peer *peer, bool timeout)
911 infostream<<"Handler("<<name<<")::deletingPeer(): "
913 <<", timeout="<<timeout<<std::endl;
925 DSTACK("TestConnection::Run");
930 Test some real connections
932 NOTE: This mostly tests the legacy interface.
935 u32 proto_id = 0xad26846a;
937 Handler hand_server("server");
938 Handler hand_client("client");
940 infostream<<"** Creating server Connection"<<std::endl;
941 con::Connection server(proto_id, 512, 5.0, &hand_server);
944 infostream<<"** Creating client Connection"<<std::endl;
945 con::Connection client(proto_id, 512, 5.0, &hand_client);
947 assert(hand_server.count == 0);
948 assert(hand_client.count == 0);
952 Address server_address(127,0,0,1, 30001);
953 infostream<<"** running client.Connect()"<<std::endl;
954 client.Connect(server_address);
958 // Client should not have added client yet
959 assert(hand_client.count == 0);
964 SharedBuffer<u8> data;
965 infostream<<"** running client.Receive()"<<std::endl;
966 u32 size = client.Receive(peer_id, data);
967 infostream<<"** Client received: peer_id="<<peer_id
971 catch(con::NoIncomingDataException &e)
975 // Client should have added server now
976 assert(hand_client.count == 1);
977 assert(hand_client.last_id == 1);
978 // Server should not have added client yet
979 assert(hand_server.count == 0);
986 SharedBuffer<u8> data;
987 infostream<<"** running server.Receive()"<<std::endl;
988 u32 size = server.Receive(peer_id, data);
989 infostream<<"** Server received: peer_id="<<peer_id
993 catch(con::NoIncomingDataException &e)
995 // No actual data received, but the client has
996 // probably been connected
999 // Client should be the same
1000 assert(hand_client.count == 1);
1001 assert(hand_client.last_id == 1);
1002 // Server should have the client
1003 assert(hand_server.count == 1);
1004 assert(hand_server.last_id == 2);
1008 while(client.Connected() == false)
1013 SharedBuffer<u8> data;
1014 infostream<<"** running client.Receive()"<<std::endl;
1015 u32 size = client.Receive(peer_id, data);
1016 infostream<<"** Client received: peer_id="<<peer_id
1020 catch(con::NoIncomingDataException &e)
1031 SharedBuffer<u8> data;
1032 infostream<<"** running server.Receive()"<<std::endl;
1033 u32 size = server.Receive(peer_id, data);
1034 infostream<<"** Server received: peer_id="<<peer_id
1038 catch(con::NoIncomingDataException &e)
1043 Simple send-receive test
1046 /*u8 data[] = "Hello World!";
1047 u32 datasize = sizeof(data);*/
1048 SharedBuffer<u8> data = SharedBufferFromString("Hello World!");
1050 infostream<<"** running client.Send()"<<std::endl;
1051 client.Send(PEER_ID_SERVER, 0, data, true);
1056 SharedBuffer<u8> recvdata;
1057 infostream<<"** running server.Receive()"<<std::endl;
1058 u32 size = server.Receive(peer_id, recvdata);
1059 infostream<<"** Server received: peer_id="<<peer_id
1063 assert(memcmp(*data, *recvdata, data.getSize()) == 0);
1066 u16 peer_id_client = 2;
1069 Send consequent packets in different order
1070 Not compatible with new Connection, thus commented out.
1073 //u8 data1[] = "hello1";
1074 //u8 data2[] = "hello2";
1075 SharedBuffer<u8> data1 = SharedBufferFromString("hello1");
1076 SharedBuffer<u8> data2 = SharedBufferFromString("Hello2");
1078 Address client_address =
1079 server.GetPeerAddress(peer_id_client);
1081 infostream<<"*** Sending packets in wrong order (2,1,2)"
1085 con::Channel *ch = &server.getPeer(peer_id_client)->channels[chn];
1086 u16 sn = ch->next_outgoing_seqnum;
1087 ch->next_outgoing_seqnum = sn+1;
1088 server.Send(peer_id_client, chn, data2, true);
1089 ch->next_outgoing_seqnum = sn;
1090 server.Send(peer_id_client, chn, data1, true);
1091 ch->next_outgoing_seqnum = sn+1;
1092 server.Send(peer_id_client, chn, data2, true);
1096 infostream<<"*** Receiving the packets"<<std::endl;
1099 SharedBuffer<u8> recvdata;
1102 infostream<<"** running client.Receive()"<<std::endl;
1104 size = client.Receive(peer_id, recvdata);
1105 infostream<<"** Client received: peer_id="<<peer_id
1107 <<", data="<<*recvdata
1109 assert(size == data1.getSize());
1110 assert(memcmp(*data1, *recvdata, data1.getSize()) == 0);
1111 assert(peer_id == PEER_ID_SERVER);
1113 infostream<<"** running client.Receive()"<<std::endl;
1115 size = client.Receive(peer_id, recvdata);
1116 infostream<<"** Client received: peer_id="<<peer_id
1118 <<", data="<<*recvdata
1120 assert(size == data2.getSize());
1121 assert(memcmp(*data2, *recvdata, data2.getSize()) == 0);
1122 assert(peer_id == PEER_ID_SERVER);
1124 bool got_exception = false;
1127 infostream<<"** running client.Receive()"<<std::endl;
1129 size = client.Receive(peer_id, recvdata);
1130 infostream<<"** Client received: peer_id="<<peer_id
1132 <<", data="<<*recvdata
1135 catch(con::NoIncomingDataException &e)
1137 infostream<<"** No incoming data for client"<<std::endl;
1138 got_exception = true;
1140 assert(got_exception);
1145 Send large amounts of packets (infinite test)
1146 Commented out because of infinity.
1149 infostream<<"Sending large amounts of packets (infinite test)"<<std::endl;
1152 int datasize = myrand_range(0,5)==0?myrand_range(100,10000):myrand_range(0,100);
1153 infostream<<"datasize="<<datasize<<std::endl;
1154 SharedBuffer<u8> data1(datasize);
1155 for(u16 i=0; i<datasize; i++)
1158 int sendtimes = myrand_range(1,10);
1159 for(int i=0; i<sendtimes; i++){
1160 server.Send(peer_id_client, 0, data1, true);
1163 infostream<<"sendcount="<<sendcount<<std::endl;
1165 //int receivetimes = myrand_range(1,20);
1166 int receivetimes = 20;
1167 for(int i=0; i<receivetimes; i++){
1168 SharedBuffer<u8> recvdata;
1171 bool received = false;
1173 size = client.Receive(peer_id, recvdata);
1175 }catch(con::NoIncomingDataException &e){
1185 const int datasize = 30000;
1186 SharedBuffer<u8> data1(datasize);
1187 for(u16 i=0; i<datasize; i++){
1191 infostream<<"Sending data (size="<<datasize<<"):";
1192 for(int i=0; i<datasize && i<20; i++){
1193 if(i%2==0) infostream<<" ";
1195 snprintf(buf, 10, "%.2X", ((int)((const char*)*data1)[i])&0xff);
1200 infostream<<std::endl;
1202 server.Send(peer_id_client, 0, data1, true);
1206 SharedBuffer<u8> recvdata;
1207 infostream<<"** running client.Receive()"<<std::endl;
1210 bool received = false;
1211 u32 timems0 = porting::getTimeMs();
1213 if(porting::getTimeMs() - timems0 > 5000 || received)
1216 size = client.Receive(peer_id, recvdata);
1218 }catch(con::NoIncomingDataException &e){
1223 infostream<<"** Client received: peer_id="<<peer_id
1227 infostream<<"Received data (size="<<size<<"): ";
1228 for(int i=0; i<size && i<20; i++){
1229 if(i%2==0) infostream<<" ";
1231 snprintf(buf, 10, "%.2X", ((int)(recvdata[i]))&0xff);
1236 infostream<<std::endl;
1238 assert(memcmp(*data1, *recvdata, data1.getSize()) == 0);
1239 assert(peer_id == PEER_ID_SERVER);
1242 // Check peer handlers
1243 assert(hand_client.count == 1);
1244 assert(hand_client.last_id == 1);
1245 assert(hand_server.count == 1);
1246 assert(hand_server.last_id == 2);
1255 infostream<<"Running " #X <<std::endl;\
1259 #define TESTPARAMS(X, ...)\
1262 infostream<<"Running " #X <<std::endl;\
1263 x.Run(__VA_ARGS__);\
1268 DSTACK(__FUNCTION_NAME);
1270 // Create item and node definitions
1271 IWritableItemDefManager *idef = createItemDefManager();
1272 IWritableNodeDefManager *ndef = createNodeDefManager();
1273 define_some_nodes(idef, ndef);
1275 infostream<<"run_tests() started"<<std::endl;
1276 TEST(TestUtilities);
1279 TEST(TestSerialization);
1280 TESTPARAMS(TestMapNode, ndef);
1281 TESTPARAMS(TestVoxelManipulator, ndef);
1282 //TEST(TestMapBlock);
1283 //TEST(TestMapSector);
1284 if(INTERNET_SIMULATOR == false){
1286 dout_con<<"=== BEGIN RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
1287 TEST(TestConnection);
1288 dout_con<<"=== END RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
1290 infostream<<"run_tests() passed"<<std::endl;