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.inventory_image = "[inventorycube"
75 f = ContentFeatures();
76 f.name = itemdef.name;
77 for(int i = 0; i < 6; i++)
78 f.tname_tiles[i] = "default_stone.png";
79 f.param_type = CPT_MINERAL;
80 f.is_ground_content = true;
81 f.dug_item = itemdef.name;
82 f.material.diggability = DIGGABLE_NORMAL;
83 f.material.weight = 5.0;
84 f.material.crackiness = 1.0;
85 f.material.crumbliness = -0.1;
86 f.material.cuttability = -0.2;
87 idef->registerItem(itemdef);
94 itemdef = ItemDefinition();
95 itemdef.type = ITEM_NODE;
96 itemdef.name = "default:dirt_with_grass";
97 itemdef.description = "Dirt with grass";
98 itemdef.inventory_image = "[inventorycube"
100 "{default_dirt.png&default_grass_side.png"
101 "{default_dirt.png&default_grass_side.png";
102 f = ContentFeatures();
103 f.name = itemdef.name;
104 f.tname_tiles[0] = "default_grass.png";
105 f.tname_tiles[1] = "default_dirt.png";
106 for(int i = 2; i < 6; i++)
107 f.tname_tiles[i] = "default_dirt.png^default_grass_side.png";
108 f.is_ground_content = true;
109 f.dug_item = itemdef.name;
110 f.material.diggability = DIGGABLE_NORMAL;
111 f.material.weight = 1.2;
112 f.material.crackiness = 0.0;
113 f.material.crumbliness = 1.2;
114 f.material.cuttability = -0.4;
115 idef->registerItem(itemdef);
123 /*infostream<<"wrapDegrees(100.0) = "<<wrapDegrees(100.0)<<std::endl;
124 infostream<<"wrapDegrees(720.5) = "<<wrapDegrees(720.5)<<std::endl;
125 infostream<<"wrapDegrees(-0.5) = "<<wrapDegrees(-0.5)<<std::endl;*/
126 assert(fabs(wrapDegrees(100.0) - 100.0) < 0.001);
127 assert(fabs(wrapDegrees(720.5) - 0.5) < 0.001);
128 assert(fabs(wrapDegrees(-0.5) - (-0.5)) < 0.001);
129 assert(fabs(wrapDegrees(-365.5) - (-5.5)) < 0.001);
130 assert(lowercase("Foo bAR") == "foo bar");
131 assert(is_yes("YeS") == true);
132 assert(is_yes("") == false);
133 assert(is_yes("FAlse") == false);
142 // Test reading of settings
143 s.parseConfigLine("leet = 1337");
144 s.parseConfigLine("leetleet = 13371337");
145 s.parseConfigLine("leetleet_neg = -13371337");
146 s.parseConfigLine("floaty_thing = 1.1");
147 s.parseConfigLine("stringy_thing = asd /( ¤%&(/\" BLÖÄRP");
148 s.parseConfigLine("coord = (1, 2, 4.5)");
149 assert(s.getS32("leet") == 1337);
150 assert(s.getS16("leetleet") == 32767);
151 assert(s.getS16("leetleet_neg") == -32768);
152 // Not sure if 1.1 is an exact value as a float, but doesn't matter
153 assert(fabs(s.getFloat("floaty_thing") - 1.1) < 0.001);
154 assert(s.get("stringy_thing") == "asd /( ¤%&(/\" BLÖÄRP");
155 assert(fabs(s.getV3F("coord").X - 1.0) < 0.001);
156 assert(fabs(s.getV3F("coord").Y - 2.0) < 0.001);
157 assert(fabs(s.getV3F("coord").Z - 4.5) < 0.001);
158 // Test the setting of settings too
159 s.setFloat("floaty_thing_2", 1.2);
160 s.setV3F("coord2", v3f(1, 2, 3.3));
161 assert(s.get("floaty_thing_2").substr(0,3) == "1.2");
162 assert(fabs(s.getFloat("floaty_thing_2") - 1.2) < 0.001);
163 assert(fabs(s.getV3F("coord2").X - 1.0) < 0.001);
164 assert(fabs(s.getV3F("coord2").Y - 2.0) < 0.001);
165 assert(fabs(s.getV3F("coord2").Z - 3.3) < 0.001);
169 struct TestSerialization
171 // To be used like this:
172 // mkstr("Some\0string\0with\0embedded\0nuls")
173 // since std::string("...") doesn't work as expected in that case.
174 template<size_t N> std::string mkstr(const char (&s)[N])
176 return std::string(s, N - 1);
181 // Tests some serialization primitives
183 assert(serializeString("") == mkstr("\0\0"));
184 assert(serializeWideString(L"") == mkstr("\0\0"));
185 assert(serializeLongString("") == mkstr("\0\0\0\0"));
186 assert(serializeJsonString("") == "\"\"");
188 std::string teststring = "Hello world!";
189 assert(serializeString(teststring) ==
190 mkstr("\0\14Hello world!"));
191 assert(serializeWideString(narrow_to_wide(teststring)) ==
192 mkstr("\0\14\0H\0e\0l\0l\0o\0 \0w\0o\0r\0l\0d\0!"));
193 assert(serializeLongString(teststring) ==
194 mkstr("\0\0\0\14Hello world!"));
195 assert(serializeJsonString(teststring) ==
198 std::string teststring2;
199 std::wstring teststring2_w;
200 std::string teststring2_w_encoded;
202 std::ostringstream tmp_os;
203 std::wostringstream tmp_os_w;
204 std::ostringstream tmp_os_w_encoded;
205 for(int i = 0; i < 256; i++)
208 tmp_os_w<<(wchar_t)i;
209 tmp_os_w_encoded<<(char)0<<(char)i;
211 teststring2 = tmp_os.str();
212 teststring2_w = tmp_os_w.str();
213 teststring2_w_encoded = tmp_os_w_encoded.str();
215 assert(serializeString(teststring2) ==
216 mkstr("\1\0") + teststring2);
217 assert(serializeWideString(teststring2_w) ==
218 mkstr("\1\0") + teststring2_w_encoded);
219 assert(serializeLongString(teststring2) ==
220 mkstr("\0\0\1\0") + teststring2);
221 assert(serializeJsonString(teststring2) ==
223 "\\u0000\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007" +
224 "\\b\\t\\n\\u000b\\f\\r\\u000e\\u000f" +
225 "\\u0010\\u0011\\u0012\\u0013\\u0014\\u0015\\u0016\\u0017" +
226 "\\u0018\\u0019\\u001a\\u001b\\u001c\\u001d\\u001e\\u001f" +
227 " !\\\"" + teststring2.substr(0x23, 0x2f-0x23) +
228 "\\/" + teststring2.substr(0x30, 0x5c-0x30) +
229 "\\\\" + teststring2.substr(0x5d, 0x7f-0x5d) + "\\u007f" +
230 "\\u0080\\u0081\\u0082\\u0083\\u0084\\u0085\\u0086\\u0087" +
231 "\\u0088\\u0089\\u008a\\u008b\\u008c\\u008d\\u008e\\u008f" +
232 "\\u0090\\u0091\\u0092\\u0093\\u0094\\u0095\\u0096\\u0097" +
233 "\\u0098\\u0099\\u009a\\u009b\\u009c\\u009d\\u009e\\u009f" +
234 "\\u00a0\\u00a1\\u00a2\\u00a3\\u00a4\\u00a5\\u00a6\\u00a7" +
235 "\\u00a8\\u00a9\\u00aa\\u00ab\\u00ac\\u00ad\\u00ae\\u00af" +
236 "\\u00b0\\u00b1\\u00b2\\u00b3\\u00b4\\u00b5\\u00b6\\u00b7" +
237 "\\u00b8\\u00b9\\u00ba\\u00bb\\u00bc\\u00bd\\u00be\\u00bf" +
238 "\\u00c0\\u00c1\\u00c2\\u00c3\\u00c4\\u00c5\\u00c6\\u00c7" +
239 "\\u00c8\\u00c9\\u00ca\\u00cb\\u00cc\\u00cd\\u00ce\\u00cf" +
240 "\\u00d0\\u00d1\\u00d2\\u00d3\\u00d4\\u00d5\\u00d6\\u00d7" +
241 "\\u00d8\\u00d9\\u00da\\u00db\\u00dc\\u00dd\\u00de\\u00df" +
242 "\\u00e0\\u00e1\\u00e2\\u00e3\\u00e4\\u00e5\\u00e6\\u00e7" +
243 "\\u00e8\\u00e9\\u00ea\\u00eb\\u00ec\\u00ed\\u00ee\\u00ef" +
244 "\\u00f0\\u00f1\\u00f2\\u00f3\\u00f4\\u00f5\\u00f6\\u00f7" +
245 "\\u00f8\\u00f9\\u00fa\\u00fb\\u00fc\\u00fd\\u00fe\\u00ff" +
249 std::istringstream is(serializeString(teststring2), std::ios::binary);
250 assert(deSerializeString(is) == teststring2);
256 std::istringstream is(serializeWideString(teststring2_w), std::ios::binary);
257 assert(deSerializeWideString(is) == teststring2_w);
263 std::istringstream is(serializeLongString(teststring2), std::ios::binary);
264 assert(deSerializeLongString(is) == teststring2);
270 std::istringstream is(serializeJsonString(teststring2), std::ios::binary);
271 //dstream<<serializeJsonString(deSerializeJsonString(is));
272 assert(deSerializeJsonString(is) == teststring2);
286 SharedBuffer<u8> fromdata(4);
292 std::ostringstream os(std::ios_base::binary);
293 compress(fromdata, os, 0);
295 std::string str_out = os.str();
297 infostream<<"str_out.size()="<<str_out.size()<<std::endl;
298 infostream<<"TestCompress: 1,5,5,1 -> ";
299 for(u32 i=0; i<str_out.size(); i++)
301 infostream<<(u32)str_out[i]<<",";
303 infostream<<std::endl;
305 assert(str_out.size() == 10);
307 assert(str_out[0] == 0);
308 assert(str_out[1] == 0);
309 assert(str_out[2] == 0);
310 assert(str_out[3] == 4);
311 assert(str_out[4] == 0);
312 assert(str_out[5] == 1);
313 assert(str_out[6] == 1);
314 assert(str_out[7] == 5);
315 assert(str_out[8] == 0);
316 assert(str_out[9] == 1);
318 std::istringstream is(str_out, std::ios_base::binary);
319 std::ostringstream os2(std::ios_base::binary);
321 decompress(is, os2, 0);
322 std::string str_out2 = os2.str();
324 infostream<<"decompress: ";
325 for(u32 i=0; i<str_out2.size(); i++)
327 infostream<<(u32)str_out2[i]<<",";
329 infostream<<std::endl;
331 assert(str_out2.size() == fromdata.getSize());
333 for(u32 i=0; i<str_out2.size(); i++)
335 assert(str_out2[i] == fromdata[i]);
342 SharedBuffer<u8> fromdata(4);
348 std::ostringstream os(std::ios_base::binary);
349 compress(fromdata, os, SER_FMT_VER_HIGHEST);
351 std::string str_out = os.str();
353 infostream<<"str_out.size()="<<str_out.size()<<std::endl;
354 infostream<<"TestCompress: 1,5,5,1 -> ";
355 for(u32 i=0; i<str_out.size(); i++)
357 infostream<<(u32)str_out[i]<<",";
359 infostream<<std::endl;
361 /*assert(str_out.size() == 10);
363 assert(str_out[0] == 0);
364 assert(str_out[1] == 0);
365 assert(str_out[2] == 0);
366 assert(str_out[3] == 4);
367 assert(str_out[4] == 0);
368 assert(str_out[5] == 1);
369 assert(str_out[6] == 1);
370 assert(str_out[7] == 5);
371 assert(str_out[8] == 0);
372 assert(str_out[9] == 1);*/
374 std::istringstream is(str_out, std::ios_base::binary);
375 std::ostringstream os2(std::ios_base::binary);
377 decompress(is, os2, SER_FMT_VER_HIGHEST);
378 std::string str_out2 = os2.str();
380 infostream<<"decompress: ";
381 for(u32 i=0; i<str_out2.size(); i++)
383 infostream<<(u32)str_out2[i]<<",";
385 infostream<<std::endl;
387 assert(str_out2.size() == fromdata.getSize());
389 for(u32 i=0; i<str_out2.size(); i++)
391 assert(str_out2[i] == fromdata[i]);
400 void Run(INodeDefManager *nodedef)
405 assert(n.getContent() == CONTENT_AIR);
406 assert(n.getLight(LIGHTBANK_DAY, nodedef) == 0);
407 assert(n.getLight(LIGHTBANK_NIGHT, nodedef) == 0);
410 n.setContent(CONTENT_AIR);
411 assert(nodedef->get(n).light_propagates == true);
412 n.setContent(LEGN(nodedef, "CONTENT_STONE"));
413 assert(nodedef->get(n).light_propagates == false);
417 struct TestVoxelManipulator
419 void Run(INodeDefManager *nodedef)
425 VoxelArea a(v3s16(-1,-1,-1), v3s16(1,1,1));
426 assert(a.index(0,0,0) == 1*3*3 + 1*3 + 1);
427 assert(a.index(-1,-1,-1) == 0);
429 VoxelArea c(v3s16(-2,-2,-2), v3s16(2,2,2));
430 // An area that is 1 bigger in x+ and z-
431 VoxelArea d(v3s16(-2,-2,-3), v3s16(3,2,2));
433 core::list<VoxelArea> aa;
437 core::array<VoxelArea> results;
438 results.push_back(VoxelArea(v3s16(-2,-2,-3),v3s16(3,2,-3)));
439 results.push_back(VoxelArea(v3s16(3,-2,-2),v3s16(3,2,2)));
441 assert(aa.size() == results.size());
443 infostream<<"Result of diff:"<<std::endl;
444 for(core::list<VoxelArea>::Iterator
445 i = aa.begin(); i != aa.end(); i++)
447 i->print(infostream);
448 infostream<<std::endl;
450 s32 j = results.linear_search(*i);
462 v.print(infostream, nodedef);
464 infostream<<"*** Setting (-1,0,-1)=2 ***"<<std::endl;
466 v.setNodeNoRef(v3s16(-1,0,-1), MapNode(CONTENT_GRASS));
468 v.print(infostream, nodedef);
470 assert(v.getNode(v3s16(-1,0,-1)).getContent() == CONTENT_GRASS);
472 infostream<<"*** Reading from inexistent (0,0,-1) ***"<<std::endl;
474 EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,0,-1)));
476 v.print(infostream, nodedef);
478 infostream<<"*** Adding area ***"<<std::endl;
482 v.print(infostream, nodedef);
484 assert(v.getNode(v3s16(-1,0,-1)).getContent() == CONTENT_GRASS);
485 EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,1,1)));
490 NOTE: These tests became non-working then NodeContainer was removed.
491 These should be redone, utilizing some kind of a virtual
492 interface for Map (IMap would be fine).
497 class TC : public NodeContainer
503 core::list<v3s16> validity_exceptions;
507 position_valid = true;
510 virtual bool isValidPosition(v3s16 p)
512 //return position_valid ^ (p==position_valid_exception);
513 bool exception = false;
514 for(core::list<v3s16>::Iterator i=validity_exceptions.begin();
515 i != validity_exceptions.end(); i++)
523 return exception ? !position_valid : position_valid;
526 virtual MapNode getNode(v3s16 p)
528 if(isValidPosition(p) == false)
529 throw InvalidPositionException();
533 virtual void setNode(v3s16 p, MapNode & n)
535 if(isValidPosition(p) == false)
536 throw InvalidPositionException();
539 virtual u16 nodeContainerId() const
549 MapBlock b(&parent, v3s16(1,1,1));
550 v3s16 relpos(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
552 assert(b.getPosRelative() == relpos);
554 assert(b.getBox().MinEdge.X == MAP_BLOCKSIZE);
555 assert(b.getBox().MaxEdge.X == MAP_BLOCKSIZE*2-1);
556 assert(b.getBox().MinEdge.Y == MAP_BLOCKSIZE);
557 assert(b.getBox().MaxEdge.Y == MAP_BLOCKSIZE*2-1);
558 assert(b.getBox().MinEdge.Z == MAP_BLOCKSIZE);
559 assert(b.getBox().MaxEdge.Z == MAP_BLOCKSIZE*2-1);
561 assert(b.isValidPosition(v3s16(0,0,0)) == true);
562 assert(b.isValidPosition(v3s16(-1,0,0)) == false);
563 assert(b.isValidPosition(v3s16(-1,-142,-2341)) == false);
564 assert(b.isValidPosition(v3s16(-124,142,2341)) == false);
565 assert(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
566 assert(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE,MAP_BLOCKSIZE-1)) == false);
569 TODO: this method should probably be removed
570 if the block size isn't going to be set variable
572 /*assert(b.getSizeNodes() == v3s16(MAP_BLOCKSIZE,
573 MAP_BLOCKSIZE, MAP_BLOCKSIZE));*/
575 // Changed flag should be initially set
576 assert(b.getModified() == MOD_STATE_WRITE_NEEDED);
578 assert(b.getModified() == MOD_STATE_CLEAN);
580 // All nodes should have been set to
581 // .d=CONTENT_IGNORE and .getLight() = 0
582 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
583 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
584 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
586 //assert(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_AIR);
587 assert(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_IGNORE);
588 assert(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_DAY) == 0);
589 assert(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_NIGHT) == 0);
593 MapNode n(CONTENT_AIR);
594 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
595 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
596 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
598 b.setNode(v3s16(x,y,z), n);
603 Parent fetch functions
605 parent.position_valid = false;
606 parent.node.setContent(5);
610 // Positions in the block should still be valid
611 assert(b.isValidPositionParent(v3s16(0,0,0)) == true);
612 assert(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
613 n = b.getNodeParent(v3s16(0,MAP_BLOCKSIZE-1,0));
614 assert(n.getContent() == CONTENT_AIR);
616 // ...but outside the block they should be invalid
617 assert(b.isValidPositionParent(v3s16(-121,2341,0)) == false);
618 assert(b.isValidPositionParent(v3s16(-1,0,0)) == false);
619 assert(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == false);
622 bool exception_thrown = false;
624 // This should throw an exception
625 MapNode n = b.getNodeParent(v3s16(0,0,-1));
627 catch(InvalidPositionException &e)
629 exception_thrown = true;
631 assert(exception_thrown);
634 parent.position_valid = true;
635 // Now the positions outside should be valid
636 assert(b.isValidPositionParent(v3s16(-121,2341,0)) == true);
637 assert(b.isValidPositionParent(v3s16(-1,0,0)) == true);
638 assert(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == true);
639 n = b.getNodeParent(v3s16(0,0,MAP_BLOCKSIZE));
640 assert(n.getContent() == 5);
648 assert(b.getNode(p).getContent() == 4);
649 //TODO: Update to new system
650 /*assert(b.getNodeTile(p) == 4);
651 assert(b.getNodeTile(v3s16(-1,-1,0)) == 5);*/
656 // Set lighting of all nodes to 0
657 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
658 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
659 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
660 MapNode n = b.getNode(v3s16(x,y,z));
661 n.setLight(LIGHTBANK_DAY, 0);
662 n.setLight(LIGHTBANK_NIGHT, 0);
663 b.setNode(v3s16(x,y,z), n);
669 Check how the block handles being a lonely sky block
671 parent.position_valid = true;
672 b.setIsUnderground(false);
673 parent.node.setContent(CONTENT_AIR);
674 parent.node.setLight(LIGHTBANK_DAY, LIGHT_SUN);
675 parent.node.setLight(LIGHTBANK_NIGHT, 0);
676 core::map<v3s16, bool> light_sources;
677 // The bottom block is invalid, because we have a shadowing node
678 assert(b.propagateSunlight(light_sources) == false);
679 assert(b.getNode(v3s16(1,4,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
680 assert(b.getNode(v3s16(1,3,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
681 assert(b.getNode(v3s16(1,2,0)).getLight(LIGHTBANK_DAY) == 0);
682 assert(b.getNode(v3s16(1,1,0)).getLight(LIGHTBANK_DAY) == 0);
683 assert(b.getNode(v3s16(1,0,0)).getLight(LIGHTBANK_DAY) == 0);
684 assert(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
685 assert(b.getFaceLight2(1000, p, v3s16(0,1,0)) == LIGHT_SUN);
686 assert(b.getFaceLight2(1000, p, v3s16(0,-1,0)) == 0);
687 assert(b.getFaceLight2(0, p, v3s16(0,-1,0)) == 0);
688 // According to MapBlock::getFaceLight,
689 // The face on the z+ side should have double-diminished light
690 //assert(b.getFaceLight(p, v3s16(0,0,1)) == diminish_light(diminish_light(LIGHT_MAX)));
691 // The face on the z+ side should have diminished light
692 assert(b.getFaceLight2(1000, p, v3s16(0,0,1)) == diminish_light(LIGHT_MAX));
695 Check how the block handles being in between blocks with some non-sunlight
696 while being underground
699 // Make neighbours to exist and set some non-sunlight to them
700 parent.position_valid = true;
701 b.setIsUnderground(true);
702 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
703 core::map<v3s16, bool> light_sources;
704 // The block below should be valid because there shouldn't be
705 // sunlight in there either
706 assert(b.propagateSunlight(light_sources, true) == true);
707 // Should not touch nodes that are not affected (that is, all of them)
708 //assert(b.getNode(v3s16(1,2,3)).getLight() == LIGHT_SUN);
709 // Should set light of non-sunlighted blocks to 0.
710 assert(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == 0);
713 Set up a situation where:
714 - There is only air in this block
715 - There is a valid non-sunlighted block at the bottom, and
716 - Invalid blocks elsewhere.
717 - the block is not underground.
719 This should result in bottom block invalidity
722 b.setIsUnderground(false);
724 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
725 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
726 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
728 n.setContent(CONTENT_AIR);
729 n.setLight(LIGHTBANK_DAY, 0);
730 b.setNode(v3s16(x,y,z), n);
734 // Make neighbours invalid
735 parent.position_valid = false;
736 // Add exceptions to the top of the bottom block
737 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
738 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
740 parent.validity_exceptions.push_back(v3s16(MAP_BLOCKSIZE+x, MAP_BLOCKSIZE-1, MAP_BLOCKSIZE+z));
742 // Lighting value for the valid nodes
743 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
744 core::map<v3s16, bool> light_sources;
745 // Bottom block is not valid
746 assert(b.propagateSunlight(light_sources) == false);
753 class TC : public NodeContainer
762 position_valid = true;
765 virtual bool isValidPosition(v3s16 p)
767 return position_valid;
770 virtual MapNode getNode(v3s16 p)
772 if(position_valid == false)
773 throw InvalidPositionException();
777 virtual void setNode(v3s16 p, MapNode & n)
779 if(position_valid == false)
780 throw InvalidPositionException();
783 virtual u16 nodeContainerId() const
792 parent.position_valid = false;
794 // Create one with no heightmaps
795 ServerMapSector sector(&parent, v2s16(1,1));
797 assert(sector.getBlockNoCreateNoEx(0) == 0);
798 assert(sector.getBlockNoCreateNoEx(1) == 0);
800 MapBlock * bref = sector.createBlankBlock(-2);
802 assert(sector.getBlockNoCreateNoEx(0) == 0);
803 assert(sector.getBlockNoCreateNoEx(-2) == bref);
805 //TODO: Check for AlreadyExistsException
807 /*bool exception_thrown = false;
811 catch(InvalidPositionException &e){
812 exception_thrown = true;
814 assert(exception_thrown);*/
824 const int port = 30003;
828 const char sendbuffer[] = "hello world!";
829 socket.Send(Address(127,0,0,1,port), sendbuffer, sizeof(sendbuffer));
834 memset(rcvbuffer, 0, sizeof(rcvbuffer));
838 int bytes_read = socket.Receive(sender, rcvbuffer, sizeof(rcvbuffer));
842 //FIXME: This fails on some systems
843 assert(strncmp(sendbuffer, rcvbuffer, sizeof(sendbuffer))==0);
844 assert(sender.getAddress() == Address(127,0,0,1, 0).getAddress());
848 struct TestConnection
853 Test helper functions
856 // Some constants for testing
857 u32 proto_id = 0x12345678;
860 SharedBuffer<u8> data1(1);
862 Address a(127,0,0,1, 10);
865 con::BufferedPacket p1 = con::makePacket(a, data1,
866 proto_id, peer_id, channel);
868 We should now have a packet with this data:
871 [4] u16 sender_peer_id
876 assert(readU32(&p1.data[0]) == proto_id);
877 assert(readU16(&p1.data[4]) == peer_id);
878 assert(readU8(&p1.data[6]) == channel);
879 assert(readU8(&p1.data[7]) == data1[0]);
881 //infostream<<"initial data1[0]="<<((u32)data1[0]&0xff)<<std::endl;
883 SharedBuffer<u8> p2 = con::makeReliablePacket(data1, seqnum);
885 /*infostream<<"p2.getSize()="<<p2.getSize()<<", data1.getSize()="
886 <<data1.getSize()<<std::endl;
887 infostream<<"readU8(&p2[3])="<<readU8(&p2[3])
888 <<" p2[3]="<<((u32)p2[3]&0xff)<<std::endl;
889 infostream<<"data1[0]="<<((u32)data1[0]&0xff)<<std::endl;*/
891 assert(p2.getSize() == 3 + data1.getSize());
892 assert(readU8(&p2[0]) == TYPE_RELIABLE);
893 assert(readU16(&p2[1]) == seqnum);
894 assert(readU8(&p2[3]) == data1[0]);
897 struct Handler : public con::PeerHandler
899 Handler(const char *a_name)
905 void peerAdded(con::Peer *peer)
907 infostream<<"Handler("<<name<<")::peerAdded(): "
908 "id="<<peer->id<<std::endl;
912 void deletingPeer(con::Peer *peer, bool timeout)
914 infostream<<"Handler("<<name<<")::deletingPeer(): "
916 <<", timeout="<<timeout<<std::endl;
928 DSTACK("TestConnection::Run");
933 Test some real connections
935 NOTE: This mostly tests the legacy interface.
938 u32 proto_id = 0xad26846a;
940 Handler hand_server("server");
941 Handler hand_client("client");
943 infostream<<"** Creating server Connection"<<std::endl;
944 con::Connection server(proto_id, 512, 5.0, &hand_server);
947 infostream<<"** Creating client Connection"<<std::endl;
948 con::Connection client(proto_id, 512, 5.0, &hand_client);
950 assert(hand_server.count == 0);
951 assert(hand_client.count == 0);
955 Address server_address(127,0,0,1, 30001);
956 infostream<<"** running client.Connect()"<<std::endl;
957 client.Connect(server_address);
961 // Client should not have added client yet
962 assert(hand_client.count == 0);
967 SharedBuffer<u8> data;
968 infostream<<"** running client.Receive()"<<std::endl;
969 u32 size = client.Receive(peer_id, data);
970 infostream<<"** Client received: peer_id="<<peer_id
974 catch(con::NoIncomingDataException &e)
978 // Client should have added server now
979 assert(hand_client.count == 1);
980 assert(hand_client.last_id == 1);
981 // Server should not have added client yet
982 assert(hand_server.count == 0);
989 SharedBuffer<u8> data;
990 infostream<<"** running server.Receive()"<<std::endl;
991 u32 size = server.Receive(peer_id, data);
992 infostream<<"** Server received: peer_id="<<peer_id
996 catch(con::NoIncomingDataException &e)
998 // No actual data received, but the client has
999 // probably been connected
1002 // Client should be the same
1003 assert(hand_client.count == 1);
1004 assert(hand_client.last_id == 1);
1005 // Server should have the client
1006 assert(hand_server.count == 1);
1007 assert(hand_server.last_id == 2);
1011 while(client.Connected() == false)
1016 SharedBuffer<u8> data;
1017 infostream<<"** running client.Receive()"<<std::endl;
1018 u32 size = client.Receive(peer_id, data);
1019 infostream<<"** Client received: peer_id="<<peer_id
1023 catch(con::NoIncomingDataException &e)
1034 SharedBuffer<u8> data;
1035 infostream<<"** running server.Receive()"<<std::endl;
1036 u32 size = server.Receive(peer_id, data);
1037 infostream<<"** Server received: peer_id="<<peer_id
1041 catch(con::NoIncomingDataException &e)
1046 Simple send-receive test
1049 /*u8 data[] = "Hello World!";
1050 u32 datasize = sizeof(data);*/
1051 SharedBuffer<u8> data = SharedBufferFromString("Hello World!");
1053 infostream<<"** running client.Send()"<<std::endl;
1054 client.Send(PEER_ID_SERVER, 0, data, true);
1059 SharedBuffer<u8> recvdata;
1060 infostream<<"** running server.Receive()"<<std::endl;
1061 u32 size = server.Receive(peer_id, recvdata);
1062 infostream<<"** Server received: peer_id="<<peer_id
1066 assert(memcmp(*data, *recvdata, data.getSize()) == 0);
1069 u16 peer_id_client = 2;
1072 Send consequent packets in different order
1073 Not compatible with new Connection, thus commented out.
1076 //u8 data1[] = "hello1";
1077 //u8 data2[] = "hello2";
1078 SharedBuffer<u8> data1 = SharedBufferFromString("hello1");
1079 SharedBuffer<u8> data2 = SharedBufferFromString("Hello2");
1081 Address client_address =
1082 server.GetPeerAddress(peer_id_client);
1084 infostream<<"*** Sending packets in wrong order (2,1,2)"
1088 con::Channel *ch = &server.getPeer(peer_id_client)->channels[chn];
1089 u16 sn = ch->next_outgoing_seqnum;
1090 ch->next_outgoing_seqnum = sn+1;
1091 server.Send(peer_id_client, chn, data2, true);
1092 ch->next_outgoing_seqnum = sn;
1093 server.Send(peer_id_client, chn, data1, true);
1094 ch->next_outgoing_seqnum = sn+1;
1095 server.Send(peer_id_client, chn, data2, true);
1099 infostream<<"*** Receiving the packets"<<std::endl;
1102 SharedBuffer<u8> recvdata;
1105 infostream<<"** running client.Receive()"<<std::endl;
1107 size = client.Receive(peer_id, recvdata);
1108 infostream<<"** Client received: peer_id="<<peer_id
1110 <<", data="<<*recvdata
1112 assert(size == data1.getSize());
1113 assert(memcmp(*data1, *recvdata, data1.getSize()) == 0);
1114 assert(peer_id == PEER_ID_SERVER);
1116 infostream<<"** running client.Receive()"<<std::endl;
1118 size = client.Receive(peer_id, recvdata);
1119 infostream<<"** Client received: peer_id="<<peer_id
1121 <<", data="<<*recvdata
1123 assert(size == data2.getSize());
1124 assert(memcmp(*data2, *recvdata, data2.getSize()) == 0);
1125 assert(peer_id == PEER_ID_SERVER);
1127 bool got_exception = false;
1130 infostream<<"** running client.Receive()"<<std::endl;
1132 size = client.Receive(peer_id, recvdata);
1133 infostream<<"** Client received: peer_id="<<peer_id
1135 <<", data="<<*recvdata
1138 catch(con::NoIncomingDataException &e)
1140 infostream<<"** No incoming data for client"<<std::endl;
1141 got_exception = true;
1143 assert(got_exception);
1148 Send large amounts of packets (infinite test)
1149 Commented out because of infinity.
1152 infostream<<"Sending large amounts of packets (infinite test)"<<std::endl;
1155 int datasize = myrand_range(0,5)==0?myrand_range(100,10000):myrand_range(0,100);
1156 infostream<<"datasize="<<datasize<<std::endl;
1157 SharedBuffer<u8> data1(datasize);
1158 for(u16 i=0; i<datasize; i++)
1161 int sendtimes = myrand_range(1,10);
1162 for(int i=0; i<sendtimes; i++){
1163 server.Send(peer_id_client, 0, data1, true);
1166 infostream<<"sendcount="<<sendcount<<std::endl;
1168 //int receivetimes = myrand_range(1,20);
1169 int receivetimes = 20;
1170 for(int i=0; i<receivetimes; i++){
1171 SharedBuffer<u8> recvdata;
1174 bool received = false;
1176 size = client.Receive(peer_id, recvdata);
1178 }catch(con::NoIncomingDataException &e){
1188 const int datasize = 30000;
1189 SharedBuffer<u8> data1(datasize);
1190 for(u16 i=0; i<datasize; i++){
1194 infostream<<"Sending data (size="<<datasize<<"):";
1195 for(int i=0; i<datasize && i<20; i++){
1196 if(i%2==0) DEBUGPRINT(" ");
1197 DEBUGPRINT("%.2X", ((int)((const char*)*data1)[i])&0xff);
1201 infostream<<std::endl;
1203 server.Send(peer_id_client, 0, data1, true);
1207 SharedBuffer<u8> recvdata;
1208 infostream<<"** running client.Receive()"<<std::endl;
1211 bool received = false;
1212 u32 timems0 = porting::getTimeMs();
1214 if(porting::getTimeMs() - timems0 > 5000)
1217 size = client.Receive(peer_id, recvdata);
1219 }catch(con::NoIncomingDataException &e){
1224 infostream<<"** Client received: peer_id="<<peer_id
1228 infostream<<"Received data (size="<<size<<"):";
1229 for(int i=0; i<size && i<20; i++){
1230 if(i%2==0) DEBUGPRINT(" ");
1231 DEBUGPRINT("%.2X", ((int)(recvdata[i]))&0xff);
1235 infostream<<std::endl;
1237 assert(memcmp(*data1, *recvdata, data1.getSize()) == 0);
1238 assert(peer_id == PEER_ID_SERVER);
1241 // Check peer handlers
1242 assert(hand_client.count == 1);
1243 assert(hand_client.last_id == 1);
1244 assert(hand_server.count == 1);
1245 assert(hand_server.last_id == 2);
1254 infostream<<"Running " #X <<std::endl;\
1258 #define TESTPARAMS(X, ...)\
1261 infostream<<"Running " #X <<std::endl;\
1262 x.Run(__VA_ARGS__);\
1267 DSTACK(__FUNCTION_NAME);
1269 // Create item and node definitions
1270 IWritableItemDefManager *idef = createItemDefManager();
1271 IWritableNodeDefManager *ndef = createNodeDefManager();
1272 define_some_nodes(idef, ndef);
1274 infostream<<"run_tests() started"<<std::endl;
1275 TEST(TestUtilities);
1278 TEST(TestSerialization);
1279 TESTPARAMS(TestMapNode, ndef);
1280 TESTPARAMS(TestVoxelManipulator, ndef);
1281 //TEST(TestMapBlock);
1282 //TEST(TestMapSector);
1283 if(INTERNET_SIMULATOR == false){
1285 dout_con<<"=== BEGIN RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
1286 TEST(TestConnection);
1287 dout_con<<"=== END RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
1289 infostream<<"run_tests() passed"<<std::endl;