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.is_ground_content = true;
80 f.material.diggability = DIGGABLE_NORMAL;
81 f.material.weight = 5.0;
82 f.material.crackiness = 1.0;
83 f.material.crumbliness = -0.1;
84 f.material.cuttability = -0.2;
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.inventory_image = "[inventorycube"
98 "{default_dirt.png&default_grass_side.png"
99 "{default_dirt.png&default_grass_side.png";
100 f = ContentFeatures();
101 f.name = itemdef.name;
102 f.tname_tiles[0] = "default_grass.png";
103 f.tname_tiles[1] = "default_dirt.png";
104 for(int i = 2; i < 6; i++)
105 f.tname_tiles[i] = "default_dirt.png^default_grass_side.png";
106 f.is_ground_content = true;
107 f.material.diggability = DIGGABLE_NORMAL;
108 f.material.weight = 1.2;
109 f.material.crackiness = 0.0;
110 f.material.crumbliness = 1.2;
111 f.material.cuttability = -0.4;
112 idef->registerItem(itemdef);
120 /*infostream<<"wrapDegrees(100.0) = "<<wrapDegrees(100.0)<<std::endl;
121 infostream<<"wrapDegrees(720.5) = "<<wrapDegrees(720.5)<<std::endl;
122 infostream<<"wrapDegrees(-0.5) = "<<wrapDegrees(-0.5)<<std::endl;*/
123 assert(fabs(wrapDegrees(100.0) - 100.0) < 0.001);
124 assert(fabs(wrapDegrees(720.5) - 0.5) < 0.001);
125 assert(fabs(wrapDegrees(-0.5) - (-0.5)) < 0.001);
126 assert(fabs(wrapDegrees(-365.5) - (-5.5)) < 0.001);
127 assert(lowercase("Foo bAR") == "foo bar");
128 assert(is_yes("YeS") == true);
129 assert(is_yes("") == false);
130 assert(is_yes("FAlse") == false);
139 // Test reading of settings
140 s.parseConfigLine("leet = 1337");
141 s.parseConfigLine("leetleet = 13371337");
142 s.parseConfigLine("leetleet_neg = -13371337");
143 s.parseConfigLine("floaty_thing = 1.1");
144 s.parseConfigLine("stringy_thing = asd /( ¤%&(/\" BLÖÄRP");
145 s.parseConfigLine("coord = (1, 2, 4.5)");
146 assert(s.getS32("leet") == 1337);
147 assert(s.getS16("leetleet") == 32767);
148 assert(s.getS16("leetleet_neg") == -32768);
149 // Not sure if 1.1 is an exact value as a float, but doesn't matter
150 assert(fabs(s.getFloat("floaty_thing") - 1.1) < 0.001);
151 assert(s.get("stringy_thing") == "asd /( ¤%&(/\" BLÖÄRP");
152 assert(fabs(s.getV3F("coord").X - 1.0) < 0.001);
153 assert(fabs(s.getV3F("coord").Y - 2.0) < 0.001);
154 assert(fabs(s.getV3F("coord").Z - 4.5) < 0.001);
155 // Test the setting of settings too
156 s.setFloat("floaty_thing_2", 1.2);
157 s.setV3F("coord2", v3f(1, 2, 3.3));
158 assert(s.get("floaty_thing_2").substr(0,3) == "1.2");
159 assert(fabs(s.getFloat("floaty_thing_2") - 1.2) < 0.001);
160 assert(fabs(s.getV3F("coord2").X - 1.0) < 0.001);
161 assert(fabs(s.getV3F("coord2").Y - 2.0) < 0.001);
162 assert(fabs(s.getV3F("coord2").Z - 3.3) < 0.001);
166 struct TestSerialization
168 // To be used like this:
169 // mkstr("Some\0string\0with\0embedded\0nuls")
170 // since std::string("...") doesn't work as expected in that case.
171 template<size_t N> std::string mkstr(const char (&s)[N])
173 return std::string(s, N - 1);
178 // Tests some serialization primitives
180 assert(serializeString("") == mkstr("\0\0"));
181 assert(serializeWideString(L"") == mkstr("\0\0"));
182 assert(serializeLongString("") == mkstr("\0\0\0\0"));
183 assert(serializeJsonString("") == "\"\"");
185 std::string teststring = "Hello world!";
186 assert(serializeString(teststring) ==
187 mkstr("\0\14Hello world!"));
188 assert(serializeWideString(narrow_to_wide(teststring)) ==
189 mkstr("\0\14\0H\0e\0l\0l\0o\0 \0w\0o\0r\0l\0d\0!"));
190 assert(serializeLongString(teststring) ==
191 mkstr("\0\0\0\14Hello world!"));
192 assert(serializeJsonString(teststring) ==
195 std::string teststring2;
196 std::wstring teststring2_w;
197 std::string teststring2_w_encoded;
199 std::ostringstream tmp_os;
200 std::wostringstream tmp_os_w;
201 std::ostringstream tmp_os_w_encoded;
202 for(int i = 0; i < 256; i++)
205 tmp_os_w<<(wchar_t)i;
206 tmp_os_w_encoded<<(char)0<<(char)i;
208 teststring2 = tmp_os.str();
209 teststring2_w = tmp_os_w.str();
210 teststring2_w_encoded = tmp_os_w_encoded.str();
212 assert(serializeString(teststring2) ==
213 mkstr("\1\0") + teststring2);
214 assert(serializeWideString(teststring2_w) ==
215 mkstr("\1\0") + teststring2_w_encoded);
216 assert(serializeLongString(teststring2) ==
217 mkstr("\0\0\1\0") + teststring2);
218 // MSVC fails when directly using "\\\\"
219 std::string backslash = "\\";
220 assert(serializeJsonString(teststring2) ==
222 "\\u0000\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007" +
223 "\\b\\t\\n\\u000b\\f\\r\\u000e\\u000f" +
224 "\\u0010\\u0011\\u0012\\u0013\\u0014\\u0015\\u0016\\u0017" +
225 "\\u0018\\u0019\\u001a\\u001b\\u001c\\u001d\\u001e\\u001f" +
226 " !\\\"" + teststring2.substr(0x23, 0x2f-0x23) +
227 "\\/" + teststring2.substr(0x30, 0x5c-0x30) +
228 backslash + backslash + teststring2.substr(0x5d, 0x7f-0x5d) + "\\u007f" +
229 "\\u0080\\u0081\\u0082\\u0083\\u0084\\u0085\\u0086\\u0087" +
230 "\\u0088\\u0089\\u008a\\u008b\\u008c\\u008d\\u008e\\u008f" +
231 "\\u0090\\u0091\\u0092\\u0093\\u0094\\u0095\\u0096\\u0097" +
232 "\\u0098\\u0099\\u009a\\u009b\\u009c\\u009d\\u009e\\u009f" +
233 "\\u00a0\\u00a1\\u00a2\\u00a3\\u00a4\\u00a5\\u00a6\\u00a7" +
234 "\\u00a8\\u00a9\\u00aa\\u00ab\\u00ac\\u00ad\\u00ae\\u00af" +
235 "\\u00b0\\u00b1\\u00b2\\u00b3\\u00b4\\u00b5\\u00b6\\u00b7" +
236 "\\u00b8\\u00b9\\u00ba\\u00bb\\u00bc\\u00bd\\u00be\\u00bf" +
237 "\\u00c0\\u00c1\\u00c2\\u00c3\\u00c4\\u00c5\\u00c6\\u00c7" +
238 "\\u00c8\\u00c9\\u00ca\\u00cb\\u00cc\\u00cd\\u00ce\\u00cf" +
239 "\\u00d0\\u00d1\\u00d2\\u00d3\\u00d4\\u00d5\\u00d6\\u00d7" +
240 "\\u00d8\\u00d9\\u00da\\u00db\\u00dc\\u00dd\\u00de\\u00df" +
241 "\\u00e0\\u00e1\\u00e2\\u00e3\\u00e4\\u00e5\\u00e6\\u00e7" +
242 "\\u00e8\\u00e9\\u00ea\\u00eb\\u00ec\\u00ed\\u00ee\\u00ef" +
243 "\\u00f0\\u00f1\\u00f2\\u00f3\\u00f4\\u00f5\\u00f6\\u00f7" +
244 "\\u00f8\\u00f9\\u00fa\\u00fb\\u00fc\\u00fd\\u00fe\\u00ff" +
248 std::istringstream is(serializeString(teststring2), std::ios::binary);
249 assert(deSerializeString(is) == teststring2);
255 std::istringstream is(serializeWideString(teststring2_w), std::ios::binary);
256 assert(deSerializeWideString(is) == teststring2_w);
262 std::istringstream is(serializeLongString(teststring2), std::ios::binary);
263 assert(deSerializeLongString(is) == teststring2);
269 std::istringstream is(serializeJsonString(teststring2), std::ios::binary);
270 //dstream<<serializeJsonString(deSerializeJsonString(is));
271 assert(deSerializeJsonString(is) == teststring2);
285 SharedBuffer<u8> fromdata(4);
291 std::ostringstream os(std::ios_base::binary);
292 compress(fromdata, os, 0);
294 std::string str_out = os.str();
296 infostream<<"str_out.size()="<<str_out.size()<<std::endl;
297 infostream<<"TestCompress: 1,5,5,1 -> ";
298 for(u32 i=0; i<str_out.size(); i++)
300 infostream<<(u32)str_out[i]<<",";
302 infostream<<std::endl;
304 assert(str_out.size() == 10);
306 assert(str_out[0] == 0);
307 assert(str_out[1] == 0);
308 assert(str_out[2] == 0);
309 assert(str_out[3] == 4);
310 assert(str_out[4] == 0);
311 assert(str_out[5] == 1);
312 assert(str_out[6] == 1);
313 assert(str_out[7] == 5);
314 assert(str_out[8] == 0);
315 assert(str_out[9] == 1);
317 std::istringstream is(str_out, std::ios_base::binary);
318 std::ostringstream os2(std::ios_base::binary);
320 decompress(is, os2, 0);
321 std::string str_out2 = os2.str();
323 infostream<<"decompress: ";
324 for(u32 i=0; i<str_out2.size(); i++)
326 infostream<<(u32)str_out2[i]<<",";
328 infostream<<std::endl;
330 assert(str_out2.size() == fromdata.getSize());
332 for(u32 i=0; i<str_out2.size(); i++)
334 assert(str_out2[i] == fromdata[i]);
341 SharedBuffer<u8> fromdata(4);
347 std::ostringstream os(std::ios_base::binary);
348 compress(fromdata, os, SER_FMT_VER_HIGHEST);
350 std::string str_out = os.str();
352 infostream<<"str_out.size()="<<str_out.size()<<std::endl;
353 infostream<<"TestCompress: 1,5,5,1 -> ";
354 for(u32 i=0; i<str_out.size(); i++)
356 infostream<<(u32)str_out[i]<<",";
358 infostream<<std::endl;
360 /*assert(str_out.size() == 10);
362 assert(str_out[0] == 0);
363 assert(str_out[1] == 0);
364 assert(str_out[2] == 0);
365 assert(str_out[3] == 4);
366 assert(str_out[4] == 0);
367 assert(str_out[5] == 1);
368 assert(str_out[6] == 1);
369 assert(str_out[7] == 5);
370 assert(str_out[8] == 0);
371 assert(str_out[9] == 1);*/
373 std::istringstream is(str_out, std::ios_base::binary);
374 std::ostringstream os2(std::ios_base::binary);
376 decompress(is, os2, SER_FMT_VER_HIGHEST);
377 std::string str_out2 = os2.str();
379 infostream<<"decompress: ";
380 for(u32 i=0; i<str_out2.size(); i++)
382 infostream<<(u32)str_out2[i]<<",";
384 infostream<<std::endl;
386 assert(str_out2.size() == fromdata.getSize());
388 for(u32 i=0; i<str_out2.size(); i++)
390 assert(str_out2[i] == fromdata[i]);
399 void Run(INodeDefManager *nodedef)
404 assert(n.getContent() == CONTENT_AIR);
405 assert(n.getLight(LIGHTBANK_DAY, nodedef) == 0);
406 assert(n.getLight(LIGHTBANK_NIGHT, nodedef) == 0);
409 n.setContent(CONTENT_AIR);
410 assert(nodedef->get(n).light_propagates == true);
411 n.setContent(LEGN(nodedef, "CONTENT_STONE"));
412 assert(nodedef->get(n).light_propagates == false);
416 struct TestVoxelManipulator
418 void Run(INodeDefManager *nodedef)
424 VoxelArea a(v3s16(-1,-1,-1), v3s16(1,1,1));
425 assert(a.index(0,0,0) == 1*3*3 + 1*3 + 1);
426 assert(a.index(-1,-1,-1) == 0);
428 VoxelArea c(v3s16(-2,-2,-2), v3s16(2,2,2));
429 // An area that is 1 bigger in x+ and z-
430 VoxelArea d(v3s16(-2,-2,-3), v3s16(3,2,2));
432 core::list<VoxelArea> aa;
436 core::array<VoxelArea> results;
437 results.push_back(VoxelArea(v3s16(-2,-2,-3),v3s16(3,2,-3)));
438 results.push_back(VoxelArea(v3s16(3,-2,-2),v3s16(3,2,2)));
440 assert(aa.size() == results.size());
442 infostream<<"Result of diff:"<<std::endl;
443 for(core::list<VoxelArea>::Iterator
444 i = aa.begin(); i != aa.end(); i++)
446 i->print(infostream);
447 infostream<<std::endl;
449 s32 j = results.linear_search(*i);
461 v.print(infostream, nodedef);
463 infostream<<"*** Setting (-1,0,-1)=2 ***"<<std::endl;
465 v.setNodeNoRef(v3s16(-1,0,-1), MapNode(CONTENT_GRASS));
467 v.print(infostream, nodedef);
469 assert(v.getNode(v3s16(-1,0,-1)).getContent() == CONTENT_GRASS);
471 infostream<<"*** Reading from inexistent (0,0,-1) ***"<<std::endl;
473 EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,0,-1)));
475 v.print(infostream, nodedef);
477 infostream<<"*** Adding area ***"<<std::endl;
481 v.print(infostream, nodedef);
483 assert(v.getNode(v3s16(-1,0,-1)).getContent() == CONTENT_GRASS);
484 EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,1,1)));
489 NOTE: These tests became non-working then NodeContainer was removed.
490 These should be redone, utilizing some kind of a virtual
491 interface for Map (IMap would be fine).
496 class TC : public NodeContainer
502 core::list<v3s16> validity_exceptions;
506 position_valid = true;
509 virtual bool isValidPosition(v3s16 p)
511 //return position_valid ^ (p==position_valid_exception);
512 bool exception = false;
513 for(core::list<v3s16>::Iterator i=validity_exceptions.begin();
514 i != validity_exceptions.end(); i++)
522 return exception ? !position_valid : position_valid;
525 virtual MapNode getNode(v3s16 p)
527 if(isValidPosition(p) == false)
528 throw InvalidPositionException();
532 virtual void setNode(v3s16 p, MapNode & n)
534 if(isValidPosition(p) == false)
535 throw InvalidPositionException();
538 virtual u16 nodeContainerId() const
548 MapBlock b(&parent, v3s16(1,1,1));
549 v3s16 relpos(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
551 assert(b.getPosRelative() == relpos);
553 assert(b.getBox().MinEdge.X == MAP_BLOCKSIZE);
554 assert(b.getBox().MaxEdge.X == MAP_BLOCKSIZE*2-1);
555 assert(b.getBox().MinEdge.Y == MAP_BLOCKSIZE);
556 assert(b.getBox().MaxEdge.Y == MAP_BLOCKSIZE*2-1);
557 assert(b.getBox().MinEdge.Z == MAP_BLOCKSIZE);
558 assert(b.getBox().MaxEdge.Z == MAP_BLOCKSIZE*2-1);
560 assert(b.isValidPosition(v3s16(0,0,0)) == true);
561 assert(b.isValidPosition(v3s16(-1,0,0)) == false);
562 assert(b.isValidPosition(v3s16(-1,-142,-2341)) == false);
563 assert(b.isValidPosition(v3s16(-124,142,2341)) == false);
564 assert(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
565 assert(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE,MAP_BLOCKSIZE-1)) == false);
568 TODO: this method should probably be removed
569 if the block size isn't going to be set variable
571 /*assert(b.getSizeNodes() == v3s16(MAP_BLOCKSIZE,
572 MAP_BLOCKSIZE, MAP_BLOCKSIZE));*/
574 // Changed flag should be initially set
575 assert(b.getModified() == MOD_STATE_WRITE_NEEDED);
577 assert(b.getModified() == MOD_STATE_CLEAN);
579 // All nodes should have been set to
580 // .d=CONTENT_IGNORE and .getLight() = 0
581 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
582 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
583 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
585 //assert(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_AIR);
586 assert(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_IGNORE);
587 assert(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_DAY) == 0);
588 assert(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_NIGHT) == 0);
592 MapNode n(CONTENT_AIR);
593 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
594 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
595 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
597 b.setNode(v3s16(x,y,z), n);
602 Parent fetch functions
604 parent.position_valid = false;
605 parent.node.setContent(5);
609 // Positions in the block should still be valid
610 assert(b.isValidPositionParent(v3s16(0,0,0)) == true);
611 assert(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
612 n = b.getNodeParent(v3s16(0,MAP_BLOCKSIZE-1,0));
613 assert(n.getContent() == CONTENT_AIR);
615 // ...but outside the block they should be invalid
616 assert(b.isValidPositionParent(v3s16(-121,2341,0)) == false);
617 assert(b.isValidPositionParent(v3s16(-1,0,0)) == false);
618 assert(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == false);
621 bool exception_thrown = false;
623 // This should throw an exception
624 MapNode n = b.getNodeParent(v3s16(0,0,-1));
626 catch(InvalidPositionException &e)
628 exception_thrown = true;
630 assert(exception_thrown);
633 parent.position_valid = true;
634 // Now the positions outside should be valid
635 assert(b.isValidPositionParent(v3s16(-121,2341,0)) == true);
636 assert(b.isValidPositionParent(v3s16(-1,0,0)) == true);
637 assert(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == true);
638 n = b.getNodeParent(v3s16(0,0,MAP_BLOCKSIZE));
639 assert(n.getContent() == 5);
647 assert(b.getNode(p).getContent() == 4);
648 //TODO: Update to new system
649 /*assert(b.getNodeTile(p) == 4);
650 assert(b.getNodeTile(v3s16(-1,-1,0)) == 5);*/
655 // Set lighting of all nodes to 0
656 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
657 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
658 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
659 MapNode n = b.getNode(v3s16(x,y,z));
660 n.setLight(LIGHTBANK_DAY, 0);
661 n.setLight(LIGHTBANK_NIGHT, 0);
662 b.setNode(v3s16(x,y,z), n);
668 Check how the block handles being a lonely sky block
670 parent.position_valid = true;
671 b.setIsUnderground(false);
672 parent.node.setContent(CONTENT_AIR);
673 parent.node.setLight(LIGHTBANK_DAY, LIGHT_SUN);
674 parent.node.setLight(LIGHTBANK_NIGHT, 0);
675 core::map<v3s16, bool> light_sources;
676 // The bottom block is invalid, because we have a shadowing node
677 assert(b.propagateSunlight(light_sources) == false);
678 assert(b.getNode(v3s16(1,4,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
679 assert(b.getNode(v3s16(1,3,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
680 assert(b.getNode(v3s16(1,2,0)).getLight(LIGHTBANK_DAY) == 0);
681 assert(b.getNode(v3s16(1,1,0)).getLight(LIGHTBANK_DAY) == 0);
682 assert(b.getNode(v3s16(1,0,0)).getLight(LIGHTBANK_DAY) == 0);
683 assert(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
684 assert(b.getFaceLight2(1000, p, v3s16(0,1,0)) == LIGHT_SUN);
685 assert(b.getFaceLight2(1000, p, v3s16(0,-1,0)) == 0);
686 assert(b.getFaceLight2(0, p, v3s16(0,-1,0)) == 0);
687 // According to MapBlock::getFaceLight,
688 // The face on the z+ side should have double-diminished light
689 //assert(b.getFaceLight(p, v3s16(0,0,1)) == diminish_light(diminish_light(LIGHT_MAX)));
690 // The face on the z+ side should have diminished light
691 assert(b.getFaceLight2(1000, p, v3s16(0,0,1)) == diminish_light(LIGHT_MAX));
694 Check how the block handles being in between blocks with some non-sunlight
695 while being underground
698 // Make neighbours to exist and set some non-sunlight to them
699 parent.position_valid = true;
700 b.setIsUnderground(true);
701 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
702 core::map<v3s16, bool> light_sources;
703 // The block below should be valid because there shouldn't be
704 // sunlight in there either
705 assert(b.propagateSunlight(light_sources, true) == true);
706 // Should not touch nodes that are not affected (that is, all of them)
707 //assert(b.getNode(v3s16(1,2,3)).getLight() == LIGHT_SUN);
708 // Should set light of non-sunlighted blocks to 0.
709 assert(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == 0);
712 Set up a situation where:
713 - There is only air in this block
714 - There is a valid non-sunlighted block at the bottom, and
715 - Invalid blocks elsewhere.
716 - the block is not underground.
718 This should result in bottom block invalidity
721 b.setIsUnderground(false);
723 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
724 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
725 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
727 n.setContent(CONTENT_AIR);
728 n.setLight(LIGHTBANK_DAY, 0);
729 b.setNode(v3s16(x,y,z), n);
733 // Make neighbours invalid
734 parent.position_valid = false;
735 // Add exceptions to the top of the bottom block
736 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
737 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
739 parent.validity_exceptions.push_back(v3s16(MAP_BLOCKSIZE+x, MAP_BLOCKSIZE-1, MAP_BLOCKSIZE+z));
741 // Lighting value for the valid nodes
742 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
743 core::map<v3s16, bool> light_sources;
744 // Bottom block is not valid
745 assert(b.propagateSunlight(light_sources) == false);
752 class TC : public NodeContainer
761 position_valid = true;
764 virtual bool isValidPosition(v3s16 p)
766 return position_valid;
769 virtual MapNode getNode(v3s16 p)
771 if(position_valid == false)
772 throw InvalidPositionException();
776 virtual void setNode(v3s16 p, MapNode & n)
778 if(position_valid == false)
779 throw InvalidPositionException();
782 virtual u16 nodeContainerId() const
791 parent.position_valid = false;
793 // Create one with no heightmaps
794 ServerMapSector sector(&parent, v2s16(1,1));
796 assert(sector.getBlockNoCreateNoEx(0) == 0);
797 assert(sector.getBlockNoCreateNoEx(1) == 0);
799 MapBlock * bref = sector.createBlankBlock(-2);
801 assert(sector.getBlockNoCreateNoEx(0) == 0);
802 assert(sector.getBlockNoCreateNoEx(-2) == bref);
804 //TODO: Check for AlreadyExistsException
806 /*bool exception_thrown = false;
810 catch(InvalidPositionException &e){
811 exception_thrown = true;
813 assert(exception_thrown);*/
823 const int port = 30003;
827 const char sendbuffer[] = "hello world!";
828 socket.Send(Address(127,0,0,1,port), sendbuffer, sizeof(sendbuffer));
833 memset(rcvbuffer, 0, sizeof(rcvbuffer));
837 int bytes_read = socket.Receive(sender, rcvbuffer, sizeof(rcvbuffer));
841 //FIXME: This fails on some systems
842 assert(strncmp(sendbuffer, rcvbuffer, sizeof(sendbuffer))==0);
843 assert(sender.getAddress() == Address(127,0,0,1, 0).getAddress());
847 struct TestConnection
852 Test helper functions
855 // Some constants for testing
856 u32 proto_id = 0x12345678;
859 SharedBuffer<u8> data1(1);
861 Address a(127,0,0,1, 10);
864 con::BufferedPacket p1 = con::makePacket(a, data1,
865 proto_id, peer_id, channel);
867 We should now have a packet with this data:
870 [4] u16 sender_peer_id
875 assert(readU32(&p1.data[0]) == proto_id);
876 assert(readU16(&p1.data[4]) == peer_id);
877 assert(readU8(&p1.data[6]) == channel);
878 assert(readU8(&p1.data[7]) == data1[0]);
880 //infostream<<"initial data1[0]="<<((u32)data1[0]&0xff)<<std::endl;
882 SharedBuffer<u8> p2 = con::makeReliablePacket(data1, seqnum);
884 /*infostream<<"p2.getSize()="<<p2.getSize()<<", data1.getSize()="
885 <<data1.getSize()<<std::endl;
886 infostream<<"readU8(&p2[3])="<<readU8(&p2[3])
887 <<" p2[3]="<<((u32)p2[3]&0xff)<<std::endl;
888 infostream<<"data1[0]="<<((u32)data1[0]&0xff)<<std::endl;*/
890 assert(p2.getSize() == 3 + data1.getSize());
891 assert(readU8(&p2[0]) == TYPE_RELIABLE);
892 assert(readU16(&p2[1]) == seqnum);
893 assert(readU8(&p2[3]) == data1[0]);
896 struct Handler : public con::PeerHandler
898 Handler(const char *a_name)
904 void peerAdded(con::Peer *peer)
906 infostream<<"Handler("<<name<<")::peerAdded(): "
907 "id="<<peer->id<<std::endl;
911 void deletingPeer(con::Peer *peer, bool timeout)
913 infostream<<"Handler("<<name<<")::deletingPeer(): "
915 <<", timeout="<<timeout<<std::endl;
927 DSTACK("TestConnection::Run");
932 Test some real connections
934 NOTE: This mostly tests the legacy interface.
937 u32 proto_id = 0xad26846a;
939 Handler hand_server("server");
940 Handler hand_client("client");
942 infostream<<"** Creating server Connection"<<std::endl;
943 con::Connection server(proto_id, 512, 5.0, &hand_server);
946 infostream<<"** Creating client Connection"<<std::endl;
947 con::Connection client(proto_id, 512, 5.0, &hand_client);
949 assert(hand_server.count == 0);
950 assert(hand_client.count == 0);
954 Address server_address(127,0,0,1, 30001);
955 infostream<<"** running client.Connect()"<<std::endl;
956 client.Connect(server_address);
960 // Client should not have added client yet
961 assert(hand_client.count == 0);
966 SharedBuffer<u8> data;
967 infostream<<"** running client.Receive()"<<std::endl;
968 u32 size = client.Receive(peer_id, data);
969 infostream<<"** Client received: peer_id="<<peer_id
973 catch(con::NoIncomingDataException &e)
977 // Client should have added server now
978 assert(hand_client.count == 1);
979 assert(hand_client.last_id == 1);
980 // Server should not have added client yet
981 assert(hand_server.count == 0);
988 SharedBuffer<u8> data;
989 infostream<<"** running server.Receive()"<<std::endl;
990 u32 size = server.Receive(peer_id, data);
991 infostream<<"** Server received: peer_id="<<peer_id
995 catch(con::NoIncomingDataException &e)
997 // No actual data received, but the client has
998 // probably been connected
1001 // Client should be the same
1002 assert(hand_client.count == 1);
1003 assert(hand_client.last_id == 1);
1004 // Server should have the client
1005 assert(hand_server.count == 1);
1006 assert(hand_server.last_id == 2);
1010 while(client.Connected() == false)
1015 SharedBuffer<u8> data;
1016 infostream<<"** running client.Receive()"<<std::endl;
1017 u32 size = client.Receive(peer_id, data);
1018 infostream<<"** Client received: peer_id="<<peer_id
1022 catch(con::NoIncomingDataException &e)
1033 SharedBuffer<u8> data;
1034 infostream<<"** running server.Receive()"<<std::endl;
1035 u32 size = server.Receive(peer_id, data);
1036 infostream<<"** Server received: peer_id="<<peer_id
1040 catch(con::NoIncomingDataException &e)
1045 Simple send-receive test
1048 /*u8 data[] = "Hello World!";
1049 u32 datasize = sizeof(data);*/
1050 SharedBuffer<u8> data = SharedBufferFromString("Hello World!");
1052 infostream<<"** running client.Send()"<<std::endl;
1053 client.Send(PEER_ID_SERVER, 0, data, true);
1058 SharedBuffer<u8> recvdata;
1059 infostream<<"** running server.Receive()"<<std::endl;
1060 u32 size = server.Receive(peer_id, recvdata);
1061 infostream<<"** Server received: peer_id="<<peer_id
1065 assert(memcmp(*data, *recvdata, data.getSize()) == 0);
1068 u16 peer_id_client = 2;
1071 Send consequent packets in different order
1072 Not compatible with new Connection, thus commented out.
1075 //u8 data1[] = "hello1";
1076 //u8 data2[] = "hello2";
1077 SharedBuffer<u8> data1 = SharedBufferFromString("hello1");
1078 SharedBuffer<u8> data2 = SharedBufferFromString("Hello2");
1080 Address client_address =
1081 server.GetPeerAddress(peer_id_client);
1083 infostream<<"*** Sending packets in wrong order (2,1,2)"
1087 con::Channel *ch = &server.getPeer(peer_id_client)->channels[chn];
1088 u16 sn = ch->next_outgoing_seqnum;
1089 ch->next_outgoing_seqnum = sn+1;
1090 server.Send(peer_id_client, chn, data2, true);
1091 ch->next_outgoing_seqnum = sn;
1092 server.Send(peer_id_client, chn, data1, true);
1093 ch->next_outgoing_seqnum = sn+1;
1094 server.Send(peer_id_client, chn, data2, true);
1098 infostream<<"*** Receiving the packets"<<std::endl;
1101 SharedBuffer<u8> recvdata;
1104 infostream<<"** running client.Receive()"<<std::endl;
1106 size = client.Receive(peer_id, recvdata);
1107 infostream<<"** Client received: peer_id="<<peer_id
1109 <<", data="<<*recvdata
1111 assert(size == data1.getSize());
1112 assert(memcmp(*data1, *recvdata, data1.getSize()) == 0);
1113 assert(peer_id == PEER_ID_SERVER);
1115 infostream<<"** running client.Receive()"<<std::endl;
1117 size = client.Receive(peer_id, recvdata);
1118 infostream<<"** Client received: peer_id="<<peer_id
1120 <<", data="<<*recvdata
1122 assert(size == data2.getSize());
1123 assert(memcmp(*data2, *recvdata, data2.getSize()) == 0);
1124 assert(peer_id == PEER_ID_SERVER);
1126 bool got_exception = false;
1129 infostream<<"** running client.Receive()"<<std::endl;
1131 size = client.Receive(peer_id, recvdata);
1132 infostream<<"** Client received: peer_id="<<peer_id
1134 <<", data="<<*recvdata
1137 catch(con::NoIncomingDataException &e)
1139 infostream<<"** No incoming data for client"<<std::endl;
1140 got_exception = true;
1142 assert(got_exception);
1147 Send large amounts of packets (infinite test)
1148 Commented out because of infinity.
1151 infostream<<"Sending large amounts of packets (infinite test)"<<std::endl;
1154 int datasize = myrand_range(0,5)==0?myrand_range(100,10000):myrand_range(0,100);
1155 infostream<<"datasize="<<datasize<<std::endl;
1156 SharedBuffer<u8> data1(datasize);
1157 for(u16 i=0; i<datasize; i++)
1160 int sendtimes = myrand_range(1,10);
1161 for(int i=0; i<sendtimes; i++){
1162 server.Send(peer_id_client, 0, data1, true);
1165 infostream<<"sendcount="<<sendcount<<std::endl;
1167 //int receivetimes = myrand_range(1,20);
1168 int receivetimes = 20;
1169 for(int i=0; i<receivetimes; i++){
1170 SharedBuffer<u8> recvdata;
1173 bool received = false;
1175 size = client.Receive(peer_id, recvdata);
1177 }catch(con::NoIncomingDataException &e){
1187 const int datasize = 30000;
1188 SharedBuffer<u8> data1(datasize);
1189 for(u16 i=0; i<datasize; i++){
1193 infostream<<"Sending data (size="<<datasize<<"):";
1194 for(int i=0; i<datasize && i<20; i++){
1195 if(i%2==0) DEBUGPRINT(" ");
1196 DEBUGPRINT("%.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) DEBUGPRINT(" ");
1230 DEBUGPRINT("%.2X", ((int)(recvdata[i]))&0xff);
1234 infostream<<std::endl;
1236 assert(memcmp(*data1, *recvdata, data1.getSize()) == 0);
1237 assert(peer_id == PEER_ID_SERVER);
1240 // Check peer handlers
1241 assert(hand_client.count == 1);
1242 assert(hand_client.last_id == 1);
1243 assert(hand_server.count == 1);
1244 assert(hand_server.last_id == 2);
1253 infostream<<"Running " #X <<std::endl;\
1257 #define TESTPARAMS(X, ...)\
1260 infostream<<"Running " #X <<std::endl;\
1261 x.Run(__VA_ARGS__);\
1266 DSTACK(__FUNCTION_NAME);
1268 // Create item and node definitions
1269 IWritableItemDefManager *idef = createItemDefManager();
1270 IWritableNodeDefManager *ndef = createNodeDefManager();
1271 define_some_nodes(idef, ndef);
1273 infostream<<"run_tests() started"<<std::endl;
1274 TEST(TestUtilities);
1277 TEST(TestSerialization);
1278 TESTPARAMS(TestMapNode, ndef);
1279 TESTPARAMS(TestVoxelManipulator, ndef);
1280 //TEST(TestMapBlock);
1281 //TEST(TestMapSector);
1282 if(INTERNET_SIMULATOR == false){
1284 dout_con<<"=== BEGIN RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
1285 TEST(TestConnection);
1286 dout_con<<"=== END RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
1288 infostream<<"run_tests() passed"<<std::endl;