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"
26 #include "heightmap.h"
28 #include "connection.h"
30 #include "serialization.h"
36 #define sleep_ms(x) Sleep(x)
39 #define sleep_ms(x) usleep(x*1000)
43 Asserts that the exception occurs
45 #define EXCEPTION_CHECK(EType, code)\
47 bool exception_thrown = false;\
49 catch(EType &e) { exception_thrown = true; }\
50 assert(exception_thrown);\
57 /*dstream<<"wrapDegrees(100.0) = "<<wrapDegrees(100.0)<<std::endl;
58 dstream<<"wrapDegrees(720.5) = "<<wrapDegrees(720.5)<<std::endl;
59 dstream<<"wrapDegrees(-0.5) = "<<wrapDegrees(-0.5)<<std::endl;*/
60 assert(fabs(wrapDegrees(100.0) - 100.0) < 0.001);
61 assert(fabs(wrapDegrees(720.5) - 0.5) < 0.001);
62 assert(fabs(wrapDegrees(-0.5) - (-0.5)) < 0.001);
63 assert(fabs(wrapDegrees(-365.5) - (-5.5)) < 0.001);
64 assert(lowercase("Foo bAR") == "foo bar");
65 assert(is_yes("YeS") == true);
66 assert(is_yes("") == false);
67 assert(is_yes("FAlse") == false);
77 SharedBuffer<u8> fromdata(4);
83 std::ostringstream os(std::ios_base::binary);
84 compress(fromdata, os, 0);
86 std::string str_out = os.str();
88 dstream<<"str_out.size()="<<str_out.size()<<std::endl;
89 dstream<<"TestCompress: 1,5,5,1 -> ";
90 for(u32 i=0; i<str_out.size(); i++)
92 dstream<<(u32)str_out[i]<<",";
96 assert(str_out.size() == 10);
98 assert(str_out[0] == 0);
99 assert(str_out[1] == 0);
100 assert(str_out[2] == 0);
101 assert(str_out[3] == 4);
102 assert(str_out[4] == 0);
103 assert(str_out[5] == 1);
104 assert(str_out[6] == 1);
105 assert(str_out[7] == 5);
106 assert(str_out[8] == 0);
107 assert(str_out[9] == 1);
109 std::istringstream is(str_out, std::ios_base::binary);
110 std::ostringstream os2(std::ios_base::binary);
112 decompress(is, os2, 0);
113 std::string str_out2 = os2.str();
115 dstream<<"decompress: ";
116 for(u32 i=0; i<str_out2.size(); i++)
118 dstream<<(u32)str_out2[i]<<",";
122 assert(str_out2.size() == fromdata.getSize());
124 for(u32 i=0; i<str_out2.size(); i++)
126 assert(str_out2[i] == fromdata[i]);
133 SharedBuffer<u8> fromdata(4);
139 std::ostringstream os(std::ios_base::binary);
140 compress(fromdata, os, SER_FMT_VER_HIGHEST);
142 std::string str_out = os.str();
144 dstream<<"str_out.size()="<<str_out.size()<<std::endl;
145 dstream<<"TestCompress: 1,5,5,1 -> ";
146 for(u32 i=0; i<str_out.size(); i++)
148 dstream<<(u32)str_out[i]<<",";
152 /*assert(str_out.size() == 10);
154 assert(str_out[0] == 0);
155 assert(str_out[1] == 0);
156 assert(str_out[2] == 0);
157 assert(str_out[3] == 4);
158 assert(str_out[4] == 0);
159 assert(str_out[5] == 1);
160 assert(str_out[6] == 1);
161 assert(str_out[7] == 5);
162 assert(str_out[8] == 0);
163 assert(str_out[9] == 1);*/
165 std::istringstream is(str_out, std::ios_base::binary);
166 std::ostringstream os2(std::ios_base::binary);
168 decompress(is, os2, SER_FMT_VER_HIGHEST);
169 std::string str_out2 = os2.str();
171 dstream<<"decompress: ";
172 for(u32 i=0; i<str_out2.size(); i++)
174 dstream<<(u32)str_out2[i]<<",";
178 assert(str_out2.size() == fromdata.getSize());
180 for(u32 i=0; i<str_out2.size(); i++)
182 assert(str_out2[i] == fromdata[i]);
196 assert(n.d == CONTENT_AIR);
197 assert(n.getLight(LIGHTBANK_DAY) == 0);
198 assert(n.getLight(LIGHTBANK_NIGHT) == 0);
202 assert(n.light_propagates() == true);
204 assert(n.light_propagates() == false);
208 struct TestVoxelManipulator
216 VoxelArea a(v3s16(-1,-1,-1), v3s16(1,1,1));
217 assert(a.index(0,0,0) == 1*3*3 + 1*3 + 1);
218 assert(a.index(-1,-1,-1) == 0);
220 VoxelArea c(v3s16(-2,-2,-2), v3s16(2,2,2));
221 // An area that is 1 bigger in x+ and z-
222 VoxelArea d(v3s16(-2,-2,-3), v3s16(3,2,2));
224 core::list<VoxelArea> aa;
228 core::array<VoxelArea> results;
229 results.push_back(VoxelArea(v3s16(-2,-2,-3),v3s16(3,2,-3)));
230 results.push_back(VoxelArea(v3s16(3,-2,-2),v3s16(3,2,2)));
232 assert(aa.size() == results.size());
234 dstream<<"Result of diff:"<<std::endl;
235 for(core::list<VoxelArea>::Iterator
236 i = aa.begin(); i != aa.end(); i++)
241 s32 j = results.linear_search(*i);
255 dstream<<"*** Setting (-1,0,-1)=2 ***"<<std::endl;
257 v.setNodeNoRef(v3s16(-1,0,-1), MapNode(2));
261 assert(v.getNode(v3s16(-1,0,-1)).d == 2);
263 dstream<<"*** Reading from inexistent (0,0,-1) ***"<<std::endl;
265 EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,0,-1)));
269 dstream<<"*** Adding area ***"<<std::endl;
275 assert(v.getNode(v3s16(-1,0,-1)).d == 2);
276 EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,1,1)));
284 const char *content =
296 v3s16 size(12, 4, 2);
297 VoxelArea area(v3s16(0,0,0), size-v3s16(1,1,1));
299 const char *p = content;
300 for(s16 z=0; z<size.Z; z++)
301 for(s16 y=size.Y-1; y>=0; y--)
302 for(s16 x=0; x<size.X; x++)
305 //n.pressure = size.Y - y;
314 v.setNode(v3s16(x,y,z), n);
318 v.print(dstream, VOXELPRINT_WATERPRESSURE);
320 core::map<v3s16, u8> active_nodes;
321 v.updateAreaWaterPressure(area, active_nodes);
323 v.print(dstream, VOXELPRINT_WATERPRESSURE);
325 //s16 highest_y = -32768;
327 NOTE: These are commented out because this behaviour is changed
330 //assert(v.getWaterPressure(v3s16(7, 1, 1), highest_y, 0) == -1);
331 //assert(highest_y == 3);
332 /*assert(v.getWaterPressure(v3s16(7, 1, 1), highest_y, 0) == 3);
333 //assert(highest_y == 3);*/
335 active_nodes.clear();
336 active_nodes[v3s16(9,1,0)] = 1;
337 //v.flowWater(active_nodes, 0, true, 1000);
338 v.flowWater(active_nodes, 0, false, 1000);
340 dstream<<"Final result of flowWater:"<<std::endl;
341 v.print(dstream, VOXELPRINT_WATERPRESSURE);
349 class TC : public NodeContainer
355 core::list<v3s16> validity_exceptions;
359 position_valid = true;
362 virtual bool isValidPosition(v3s16 p)
364 //return position_valid ^ (p==position_valid_exception);
365 bool exception = false;
366 for(core::list<v3s16>::Iterator i=validity_exceptions.begin();
367 i != validity_exceptions.end(); i++)
375 return exception ? !position_valid : position_valid;
378 virtual MapNode getNode(v3s16 p)
380 if(isValidPosition(p) == false)
381 throw InvalidPositionException();
385 virtual void setNode(v3s16 p, MapNode & n)
387 if(isValidPosition(p) == false)
388 throw InvalidPositionException();
391 virtual u16 nodeContainerId() const
401 MapBlock b(&parent, v3s16(1,1,1));
402 v3s16 relpos(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
404 assert(b.getPosRelative() == relpos);
406 assert(b.getBox().MinEdge.X == MAP_BLOCKSIZE);
407 assert(b.getBox().MaxEdge.X == MAP_BLOCKSIZE*2-1);
408 assert(b.getBox().MinEdge.Y == MAP_BLOCKSIZE);
409 assert(b.getBox().MaxEdge.Y == MAP_BLOCKSIZE*2-1);
410 assert(b.getBox().MinEdge.Z == MAP_BLOCKSIZE);
411 assert(b.getBox().MaxEdge.Z == MAP_BLOCKSIZE*2-1);
413 assert(b.isValidPosition(v3s16(0,0,0)) == true);
414 assert(b.isValidPosition(v3s16(-1,0,0)) == false);
415 assert(b.isValidPosition(v3s16(-1,-142,-2341)) == false);
416 assert(b.isValidPosition(v3s16(-124,142,2341)) == false);
417 assert(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
418 assert(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE,MAP_BLOCKSIZE-1)) == false);
421 TODO: this method should probably be removed
422 if the block size isn't going to be set variable
424 /*assert(b.getSizeNodes() == v3s16(MAP_BLOCKSIZE,
425 MAP_BLOCKSIZE, MAP_BLOCKSIZE));*/
427 // Changed flag should be initially set
428 assert(b.getChangedFlag() == true);
429 b.resetChangedFlag();
430 assert(b.getChangedFlag() == false);
432 // All nodes should have been set to
433 // .d=CONTENT_AIR and .getLight() = 0
434 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
435 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
436 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
438 assert(b.getNode(v3s16(x,y,z)).d == CONTENT_AIR);
439 assert(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_DAY) == 0);
440 assert(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_NIGHT) == 0);
444 Parent fetch functions
446 parent.position_valid = false;
451 // Positions in the block should still be valid
452 assert(b.isValidPositionParent(v3s16(0,0,0)) == true);
453 assert(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
454 n = b.getNodeParent(v3s16(0,MAP_BLOCKSIZE-1,0));
455 assert(n.d == CONTENT_AIR);
457 // ...but outside the block they should be invalid
458 assert(b.isValidPositionParent(v3s16(-121,2341,0)) == false);
459 assert(b.isValidPositionParent(v3s16(-1,0,0)) == false);
460 assert(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == false);
463 bool exception_thrown = false;
465 // This should throw an exception
466 MapNode n = b.getNodeParent(v3s16(0,0,-1));
468 catch(InvalidPositionException &e)
470 exception_thrown = true;
472 assert(exception_thrown);
475 parent.position_valid = true;
476 // Now the positions outside should be valid
477 assert(b.isValidPositionParent(v3s16(-121,2341,0)) == true);
478 assert(b.isValidPositionParent(v3s16(-1,0,0)) == true);
479 assert(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == true);
480 n = b.getNodeParent(v3s16(0,0,MAP_BLOCKSIZE));
489 assert(b.getNode(p).d == 4);
490 //TODO: Update to new system
491 /*assert(b.getNodeTile(p) == 4);
492 assert(b.getNodeTile(v3s16(-1,-1,0)) == 5);*/
497 // Set lighting of all nodes to 0
498 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
499 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
500 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
501 MapNode n = b.getNode(v3s16(x,y,z));
502 n.setLight(LIGHTBANK_DAY, 0);
503 n.setLight(LIGHTBANK_NIGHT, 0);
504 b.setNode(v3s16(x,y,z), n);
510 Check how the block handles being a lonely sky block
512 parent.position_valid = true;
513 b.setIsUnderground(false);
514 parent.node.d = CONTENT_AIR;
515 parent.node.setLight(LIGHTBANK_DAY, LIGHT_SUN);
516 parent.node.setLight(LIGHTBANK_NIGHT, 0);
517 core::map<v3s16, bool> light_sources;
518 // The bottom block is invalid, because we have a shadowing node
519 assert(b.propagateSunlight(light_sources) == false);
520 assert(b.getNode(v3s16(1,4,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
521 assert(b.getNode(v3s16(1,3,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
522 assert(b.getNode(v3s16(1,2,0)).getLight(LIGHTBANK_DAY) == 0);
523 assert(b.getNode(v3s16(1,1,0)).getLight(LIGHTBANK_DAY) == 0);
524 assert(b.getNode(v3s16(1,0,0)).getLight(LIGHTBANK_DAY) == 0);
525 assert(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
526 assert(b.getFaceLight(1000, p, v3s16(0,1,0)) == LIGHT_SUN);
527 assert(b.getFaceLight(1000, p, v3s16(0,-1,0)) == 0);
528 assert(b.getFaceLight(0, p, v3s16(0,-1,0)) == 0);
529 // According to MapBlock::getFaceLight,
530 // The face on the z+ side should have double-diminished light
531 //assert(b.getFaceLight(p, v3s16(0,0,1)) == diminish_light(diminish_light(LIGHT_MAX)));
532 // The face on the z+ side should have diminished light
533 assert(b.getFaceLight(1000, p, v3s16(0,0,1)) == diminish_light(LIGHT_MAX));
536 Check how the block handles being in between blocks with some non-sunlight
537 while being underground
540 // Make neighbours to exist and set some non-sunlight to them
541 parent.position_valid = true;
542 b.setIsUnderground(true);
543 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
544 core::map<v3s16, bool> light_sources;
545 // The block below should be valid because there shouldn't be
546 // sunlight in there either
547 assert(b.propagateSunlight(light_sources) == true);
548 // Should not touch nodes that are not affected (that is, all of them)
549 //assert(b.getNode(v3s16(1,2,3)).getLight() == LIGHT_SUN);
550 // Should set light of non-sunlighted blocks to 0.
551 assert(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == 0);
554 Set up a situation where:
555 - There is only air in this block
556 - There is a valid non-sunlighted block at the bottom, and
557 - Invalid blocks elsewhere.
558 - the block is not underground.
560 This should result in bottom block invalidity
563 b.setIsUnderground(false);
565 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
566 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
567 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
570 n.setLight(LIGHTBANK_DAY, 0);
571 b.setNode(v3s16(x,y,z), n);
575 // Make neighbours invalid
576 parent.position_valid = false;
577 // Add exceptions to the top of the bottom block
578 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
579 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
581 parent.validity_exceptions.push_back(v3s16(MAP_BLOCKSIZE+x, MAP_BLOCKSIZE-1, MAP_BLOCKSIZE+z));
583 // Lighting value for the valid nodes
584 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
585 core::map<v3s16, bool> light_sources;
586 // Bottom block is not valid
587 assert(b.propagateSunlight(light_sources) == false);
594 class TC : public NodeContainer
603 position_valid = true;
606 virtual bool isValidPosition(v3s16 p)
608 return position_valid;
611 virtual MapNode getNode(v3s16 p)
613 if(position_valid == false)
614 throw InvalidPositionException();
618 virtual void setNode(v3s16 p, MapNode & n)
620 if(position_valid == false)
621 throw InvalidPositionException();
624 virtual u16 nodeContainerId() const
633 parent.position_valid = false;
635 // Create one with no heightmaps
636 ServerMapSector sector(&parent, v2s16(1,1), 0);
637 //ConstantGenerator *dummyheightmap = new ConstantGenerator();
638 //sector->setHeightmap(dummyheightmap);
640 EXCEPTION_CHECK(InvalidPositionException, sector.getBlockNoCreate(0));
641 EXCEPTION_CHECK(InvalidPositionException, sector.getBlockNoCreate(1));
643 MapBlock * bref = sector.createBlankBlock(-2);
645 EXCEPTION_CHECK(InvalidPositionException, sector.getBlockNoCreate(0));
646 assert(sector.getBlockNoCreate(-2) == bref);
648 //TODO: Check for AlreadyExistsException
650 /*bool exception_thrown = false;
654 catch(InvalidPositionException &e){
655 exception_thrown = true;
657 assert(exception_thrown);*/
664 void TestSingleFixed()
667 OneChildHeightmap hm1(BS1);
669 // Test that it is filled with < GROUNDHEIGHT_VALID_MINVALUE
670 for(s16 y=0; y<=BS1; y++){
671 for(s16 x=0; x<=BS1; x++){
673 assert(hm1.m_child.getGroundHeight(p)
674 < GROUNDHEIGHT_VALID_MINVALUE);
678 hm1.m_child.setGroundHeight(v2s16(1,0), 2.0);
679 //hm1.m_child.print();
680 assert(fabs(hm1.getGroundHeight(v2s16(1,0))-2.0)<0.001);
681 hm1.setGroundHeight(v2s16(0,1), 3.0);
682 assert(fabs(hm1.m_child.getGroundHeight(v2s16(0,1))-3.0)<0.001);
685 for(s16 y=0; y<=BS1; y++){
686 for(s16 x=0; x<=BS1; x++){
688 hm1.m_child.setGroundHeight(p, -1.0);
692 f32 corners[] = {0.0, 0.0, 1.0, 1.0};
693 hm1.m_child.generateContinued(0.0, 0.0, corners);
696 assert(fabs(hm1.m_child.getGroundHeight(v2s16(1,0))-0.2)<0.05);
697 assert(fabs(hm1.m_child.getGroundHeight(v2s16(4,3))-0.7)<0.05);
698 assert(fabs(hm1.m_child.getGroundHeight(v2s16(4,4))-1.0)<0.05);
703 //g_heightmap_debugprint = true;
705 UnlimitedHeightmap hm1(BS1,
706 new ConstantGenerator(0.0),
707 new ConstantGenerator(0.0),
708 new ConstantGenerator(5.0));
709 // Go through it so it generates itself
710 for(s16 y=0; y<=BS1; y++){
711 for(s16 x=0; x<=BS1; x++){
713 hm1.getGroundHeight(p);
717 dstream<<"UnlimitedHeightmap hm1:"<<std::endl;
720 dstream<<"testing UnlimitedHeightmap set/get"<<std::endl;
723 // Get first heightmap and try setGroundHeight
724 FixedHeightmap * href = hm1.getHeightmap(v2s16(0,0));
725 href->setGroundHeight(p1, v1);
726 // Read from UnlimitedHeightmap
727 assert(fabs(hm1.getGroundHeight(p1)-v1)<0.001);
732 dstream<<"Running random code (get a human to check this)"<<std::endl;
733 dstream<<"rand() values: ";
734 for(u16 i=0; i<5; i++)
735 dstream<<(u16)rand()<<" ";
739 UnlimitedHeightmap hm1(BS1,
740 new ConstantGenerator(10.0),
741 new ConstantGenerator(0.3),
742 new ConstantGenerator(0.0));
744 // Force hm1 to generate a some heightmap
745 hm1.getGroundHeight(v2s16(0,0));
746 hm1.getGroundHeight(v2s16(0,BS1));
747 /*hm1.getGroundHeight(v2s16(BS1,-1));
748 hm1.getGroundHeight(v2s16(BS1-1,-1));*/
751 // Get the (0,0) and (1,0) heightmaps
752 /*FixedHeightmap * hr00 = hm1.getHeightmap(v2s16(0,0));
753 FixedHeightmap * hr01 = hm1.getHeightmap(v2s16(1,0));
754 f32 corners[] = {1.0, 1.0, 1.0, 1.0};
755 hr00->generateContinued(0.0, 0.0, corners);
763 //srand(7); // Get constant random
764 srand(time(0)); // Get better random
776 const int port = 30003;
780 const char sendbuffer[] = "hello world!";
781 socket.Send(Address(127,0,0,1,port), sendbuffer, sizeof(sendbuffer));
786 memset(rcvbuffer, 0, sizeof(rcvbuffer));
790 int bytes_read = socket.Receive(sender, rcvbuffer, sizeof(rcvbuffer));
794 //FIXME: This fails on some systems
795 assert(strncmp(sendbuffer, rcvbuffer, sizeof(sendbuffer))==0);
796 assert(sender.getAddress() == Address(127,0,0,1, 0).getAddress());
800 struct TestConnection
805 Test helper functions
808 // Some constants for testing
809 u32 proto_id = 0x12345678;
812 SharedBuffer<u8> data1(1);
814 Address a(127,0,0,1, 10);
817 con::BufferedPacket p1 = con::makePacket(a, data1,
818 proto_id, peer_id, channel);
820 We should now have a packet with this data:
823 [4] u16 sender_peer_id
828 assert(readU32(&p1.data[0]) == proto_id);
829 assert(readU16(&p1.data[4]) == peer_id);
830 assert(readU8(&p1.data[6]) == channel);
831 assert(readU8(&p1.data[7]) == data1[0]);
833 //dstream<<"initial data1[0]="<<((u32)data1[0]&0xff)<<std::endl;
835 SharedBuffer<u8> p2 = con::makeReliablePacket(data1, seqnum);
837 /*dstream<<"p2.getSize()="<<p2.getSize()<<", data1.getSize()="
838 <<data1.getSize()<<std::endl;
839 dstream<<"readU8(&p2[3])="<<readU8(&p2[3])
840 <<" p2[3]="<<((u32)p2[3]&0xff)<<std::endl;
841 dstream<<"data1[0]="<<((u32)data1[0]&0xff)<<std::endl;*/
843 assert(p2.getSize() == 3 + data1.getSize());
844 assert(readU8(&p2[0]) == TYPE_RELIABLE);
845 assert(readU16(&p2[1]) == seqnum);
846 assert(readU8(&p2[3]) == data1[0]);
849 struct Handler : public con::PeerHandler
851 Handler(const char *a_name)
857 void peerAdded(con::Peer *peer)
859 dstream<<"Handler("<<name<<")::peerAdded(): "
860 "id="<<peer->id<<std::endl;
864 void deletingPeer(con::Peer *peer, bool timeout)
866 dstream<<"Handler("<<name<<")::deletingPeer(): "
868 <<", timeout="<<timeout<<std::endl;
880 DSTACK("TestConnection::Run");
885 Test some real connections
887 u32 proto_id = 0xad26846a;
889 Handler hand_server("server");
890 Handler hand_client("client");
892 dstream<<"** Creating server Connection"<<std::endl;
893 con::Connection server(proto_id, 512, 5.0, &hand_server);
896 dstream<<"** Creating client Connection"<<std::endl;
897 con::Connection client(proto_id, 512, 5.0, &hand_client);
899 assert(hand_server.count == 0);
900 assert(hand_client.count == 0);
904 Address server_address(127,0,0,1, 30001);
905 dstream<<"** running client.Connect()"<<std::endl;
906 client.Connect(server_address);
910 // Client should have added server now
911 assert(hand_client.count == 1);
912 assert(hand_client.last_id == 1);
913 // But server should not have added client
914 assert(hand_server.count == 0);
920 dstream<<"** running server.Receive()"<<std::endl;
921 u32 size = server.Receive(peer_id, data, 100);
922 dstream<<"** Server received: peer_id="<<peer_id
926 catch(con::NoIncomingDataException &e)
928 // No actual data received, but the client has
929 // probably been connected
932 // Client should be the same
933 assert(hand_client.count == 1);
934 assert(hand_client.last_id == 1);
935 // Server should have the client
936 assert(hand_server.count == 1);
937 assert(hand_server.last_id == 2);
941 while(client.Connected() == false)
947 dstream<<"** running client.Receive()"<<std::endl;
948 u32 size = client.Receive(peer_id, data, 100);
949 dstream<<"** Client received: peer_id="<<peer_id
953 catch(con::NoIncomingDataException &e)
965 dstream<<"** running server.Receive()"<<std::endl;
966 u32 size = server.Receive(peer_id, data, 100);
967 dstream<<"** Server received: peer_id="<<peer_id
971 catch(con::NoIncomingDataException &e)
976 /*u8 data[] = "Hello World!";
977 u32 datasize = sizeof(data);*/
978 SharedBuffer<u8> data = SharedBufferFromString("Hello World!");
980 dstream<<"** running client.Send()"<<std::endl;
981 client.Send(PEER_ID_SERVER, 0, data, true);
987 dstream<<"** running server.Receive()"<<std::endl;
988 u32 size = server.Receive(peer_id, recvdata, 100);
989 dstream<<"** Server received: peer_id="<<peer_id
993 assert(memcmp(*data, recvdata, data.getSize()) == 0);
996 u16 peer_id_client = 2;
1000 Send consequent packets in different order
1002 //u8 data1[] = "hello1";
1003 //u8 data2[] = "hello2";
1004 SharedBuffer<u8> data1 = SharedBufferFromString("hello1");
1005 SharedBuffer<u8> data2 = SharedBufferFromString("Hello2");
1007 Address client_address =
1008 server.GetPeer(peer_id_client)->address;
1010 dstream<<"*** Sending packets in wrong order (2,1,2)"
1014 con::Channel *ch = &server.GetPeer(peer_id_client)->channels[chn];
1015 u16 sn = ch->next_outgoing_seqnum;
1016 ch->next_outgoing_seqnum = sn+1;
1017 server.Send(peer_id_client, chn, data2, true);
1018 ch->next_outgoing_seqnum = sn;
1019 server.Send(peer_id_client, chn, data1, true);
1020 ch->next_outgoing_seqnum = sn+1;
1021 server.Send(peer_id_client, chn, data2, true);
1025 dstream<<"*** Receiving the packets"<<std::endl;
1031 dstream<<"** running client.Receive()"<<std::endl;
1033 size = client.Receive(peer_id, recvdata, 20);
1034 dstream<<"** Client received: peer_id="<<peer_id
1036 <<", data="<<recvdata
1038 assert(size == data1.getSize());
1039 assert(memcmp(*data1, recvdata, data1.getSize()) == 0);
1040 assert(peer_id == PEER_ID_SERVER);
1042 dstream<<"** running client.Receive()"<<std::endl;
1044 size = client.Receive(peer_id, recvdata, 20);
1045 dstream<<"** Client received: peer_id="<<peer_id
1047 <<", data="<<recvdata
1049 assert(size == data2.getSize());
1050 assert(memcmp(*data2, recvdata, data2.getSize()) == 0);
1051 assert(peer_id == PEER_ID_SERVER);
1053 bool got_exception = false;
1056 dstream<<"** running client.Receive()"<<std::endl;
1058 size = client.Receive(peer_id, recvdata, 20);
1059 dstream<<"** Client received: peer_id="<<peer_id
1061 <<", data="<<recvdata
1064 catch(con::NoIncomingDataException &e)
1066 dstream<<"** No incoming data for client"<<std::endl;
1067 got_exception = true;
1069 assert(got_exception);
1073 SharedBuffer<u8> data1(1100);
1074 for(u16 i=0; i<1100; i++){
1078 dstream<<"Sending data (size="<<1100<<"):";
1079 for(int i=0; i<1100 && i<20; i++){
1080 if(i%2==0) DEBUGPRINT(" ");
1081 DEBUGPRINT("%.2X", ((int)((const char*)*data1)[i])&0xff);
1087 server.Send(peer_id_client, 0, data1, true);
1092 dstream<<"** running client.Receive()"<<std::endl;
1094 u16 size = client.Receive(peer_id, recvdata, 2000);
1095 dstream<<"** Client received: peer_id="<<peer_id
1099 dstream<<"Received data (size="<<size<<"):";
1100 for(int i=0; i<size && i<20; i++){
1101 if(i%2==0) DEBUGPRINT(" ");
1102 DEBUGPRINT("%.2X", ((int)((const char*)recvdata)[i])&0xff);
1108 assert(memcmp(*data1, recvdata, data1.getSize()) == 0);
1109 assert(peer_id == PEER_ID_SERVER);
1112 // Check peer handlers
1113 assert(hand_client.count == 1);
1114 assert(hand_client.last_id == 1);
1115 assert(hand_server.count == 1);
1116 assert(hand_server.last_id == 2);
1125 dstream<<"Running " #X <<std::endl;\
1131 DSTACK(__FUNCTION_NAME);
1132 dstream<<"run_tests() started"<<std::endl;
1133 TEST(TestUtilities);
1136 TEST(TestVoxelManipulator);
1138 TEST(TestMapSector);
1139 TEST(TestHeightmap);
1140 if(INTERNET_SIMULATOR == false){
1142 dout_con<<"=== BEGIN RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
1143 TEST(TestConnection);
1144 dout_con<<"=== END RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
1146 dstream<<"run_tests() passed"<<std::endl;