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);
75 SharedBuffer<u8> fromdata(4);
81 std::ostringstream os(std::ios_base::binary);
82 compress(fromdata, os, 0);
84 std::string str_out = os.str();
86 dstream<<"str_out.size()="<<str_out.size()<<std::endl;
87 dstream<<"TestCompress: 1,5,5,1 -> ";
88 for(u32 i=0; i<str_out.size(); i++)
90 dstream<<(u32)str_out[i]<<",";
94 assert(str_out.size() == 10);
96 assert(str_out[0] == 0);
97 assert(str_out[1] == 0);
98 assert(str_out[2] == 0);
99 assert(str_out[3] == 4);
100 assert(str_out[4] == 0);
101 assert(str_out[5] == 1);
102 assert(str_out[6] == 1);
103 assert(str_out[7] == 5);
104 assert(str_out[8] == 0);
105 assert(str_out[9] == 1);
107 std::istringstream is(str_out, std::ios_base::binary);
108 std::ostringstream os2(std::ios_base::binary);
110 decompress(is, os2, 0);
111 std::string str_out2 = os2.str();
113 dstream<<"decompress: ";
114 for(u32 i=0; i<str_out2.size(); i++)
116 dstream<<(u32)str_out2[i]<<",";
120 assert(str_out2.size() == fromdata.getSize());
122 for(u32 i=0; i<str_out2.size(); i++)
124 assert(str_out2[i] == fromdata[i]);
136 assert(n.d == MATERIAL_AIR);
137 assert(n.getLight() == 0);
141 assert(n.light_propagates() == true);
143 assert(n.light_propagates() == false);
147 struct TestVoxelManipulator
155 VoxelArea a(v3s16(-1,-1,-1), v3s16(1,1,1));
156 assert(a.index(0,0,0) == 1*3*3 + 1*3 + 1);
157 assert(a.index(-1,-1,-1) == 0);
159 VoxelArea c(v3s16(-2,-2,-2), v3s16(2,2,2));
160 // An area that is 1 bigger in x+ and z-
161 VoxelArea d(v3s16(-2,-2,-3), v3s16(3,2,2));
163 core::list<VoxelArea> aa;
167 core::array<VoxelArea> results;
168 results.push_back(VoxelArea(v3s16(-2,-2,-3),v3s16(3,2,-3)));
169 results.push_back(VoxelArea(v3s16(3,-2,-2),v3s16(3,2,2)));
171 assert(aa.size() == results.size());
173 dstream<<"Result of diff:"<<std::endl;
174 for(core::list<VoxelArea>::Iterator
175 i = aa.begin(); i != aa.end(); i++)
180 s32 j = results.linear_search(*i);
194 dstream<<"*** Setting (-1,0,-1)=2 ***"<<std::endl;
196 v.setNodeNoRef(v3s16(-1,0,-1), MapNode(2));
200 assert(v.getNode(v3s16(-1,0,-1)).d == 2);
202 dstream<<"*** Reading from inexistent (0,0,-1) ***"<<std::endl;
204 EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,0,-1)));
208 dstream<<"*** Adding area ***"<<std::endl;
214 assert(v.getNode(v3s16(-1,0,-1)).d == 2);
215 EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,1,1)));
223 const char *content =
235 v3s16 size(12, 4, 2);
236 VoxelArea area(v3s16(0,0,0), size-v3s16(1,1,1));
238 const char *p = content;
239 for(s16 z=0; z<size.Z; z++)
240 for(s16 y=size.Y-1; y>=0; y--)
241 for(s16 x=0; x<size.X; x++)
244 //n.pressure = size.Y - y;
246 n.d = MATERIAL_STONE;
248 n.d = MATERIAL_WATER;
253 v.setNode(v3s16(x,y,z), n);
257 v.print(dstream, VOXELPRINT_WATERPRESSURE);
259 core::map<v3s16, u8> active_nodes;
260 v.updateAreaWaterPressure(area, active_nodes);
262 v.print(dstream, VOXELPRINT_WATERPRESSURE);
264 s16 highest_y = -32768;
265 assert(v.getWaterPressure(v3s16(7, 1, 1), highest_y, 0) == -1);
266 assert(highest_y == 3);
268 active_nodes.clear();
269 active_nodes[v3s16(9,1,0)] = 1;
270 //v.flowWater(active_nodes, 0, false);
271 v.flowWater(active_nodes, 0, true);
273 dstream<<"Final result of flowWater:"<<std::endl;
274 v.print(dstream, VOXELPRINT_WATERPRESSURE);
282 class TC : public NodeContainer
288 core::list<v3s16> validity_exceptions;
292 position_valid = true;
295 virtual bool isValidPosition(v3s16 p)
297 //return position_valid ^ (p==position_valid_exception);
298 bool exception = false;
299 for(core::list<v3s16>::Iterator i=validity_exceptions.begin();
300 i != validity_exceptions.end(); i++)
308 return exception ? !position_valid : position_valid;
311 virtual MapNode getNode(v3s16 p)
313 if(isValidPosition(p) == false)
314 throw InvalidPositionException();
318 virtual void setNode(v3s16 p, MapNode & n)
320 if(isValidPosition(p) == false)
321 throw InvalidPositionException();
324 virtual u16 nodeContainerId() const
334 MapBlock b(&parent, v3s16(1,1,1));
335 v3s16 relpos(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
337 assert(b.getPosRelative() == relpos);
339 assert(b.getBox().MinEdge.X == MAP_BLOCKSIZE);
340 assert(b.getBox().MaxEdge.X == MAP_BLOCKSIZE*2-1);
341 assert(b.getBox().MinEdge.Y == MAP_BLOCKSIZE);
342 assert(b.getBox().MaxEdge.Y == MAP_BLOCKSIZE*2-1);
343 assert(b.getBox().MinEdge.Z == MAP_BLOCKSIZE);
344 assert(b.getBox().MaxEdge.Z == MAP_BLOCKSIZE*2-1);
346 assert(b.isValidPosition(v3s16(0,0,0)) == true);
347 assert(b.isValidPosition(v3s16(-1,0,0)) == false);
348 assert(b.isValidPosition(v3s16(-1,-142,-2341)) == false);
349 assert(b.isValidPosition(v3s16(-124,142,2341)) == false);
350 assert(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
351 assert(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE,MAP_BLOCKSIZE-1)) == false);
354 TODO: this method should probably be removed
355 if the block size isn't going to be set variable
357 /*assert(b.getSizeNodes() == v3s16(MAP_BLOCKSIZE,
358 MAP_BLOCKSIZE, MAP_BLOCKSIZE));*/
360 // Changed flag should be initially set
361 assert(b.getChangedFlag() == true);
362 b.resetChangedFlag();
363 assert(b.getChangedFlag() == false);
365 // All nodes should have been set to
366 // .d=MATERIAL_AIR and .getLight() = 0
367 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
368 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
369 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
370 assert(b.getNode(v3s16(x,y,z)).d == MATERIAL_AIR);
371 assert(b.getNode(v3s16(x,y,z)).getLight() == 0);
375 Parent fetch functions
377 parent.position_valid = false;
382 // Positions in the block should still be valid
383 assert(b.isValidPositionParent(v3s16(0,0,0)) == true);
384 assert(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
385 n = b.getNodeParent(v3s16(0,MAP_BLOCKSIZE-1,0));
386 assert(n.d == MATERIAL_AIR);
388 // ...but outside the block they should be invalid
389 assert(b.isValidPositionParent(v3s16(-121,2341,0)) == false);
390 assert(b.isValidPositionParent(v3s16(-1,0,0)) == false);
391 assert(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == false);
394 bool exception_thrown = false;
396 // This should throw an exception
397 MapNode n = b.getNodeParent(v3s16(0,0,-1));
399 catch(InvalidPositionException &e)
401 exception_thrown = true;
403 assert(exception_thrown);
406 parent.position_valid = true;
407 // Now the positions outside should be valid
408 assert(b.isValidPositionParent(v3s16(-121,2341,0)) == true);
409 assert(b.isValidPositionParent(v3s16(-1,0,0)) == true);
410 assert(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == true);
411 n = b.getNodeParent(v3s16(0,0,MAP_BLOCKSIZE));
420 assert(b.getNode(p).d == 4);
421 assert(b.getNodeMaterial(p) == 4);
422 assert(b.getNodeMaterial(v3s16(-1,-1,0)) == 5);
427 // Set lighting of all nodes to 0
428 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
429 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
430 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
431 MapNode n = b.getNode(v3s16(x,y,z));
433 b.setNode(v3s16(x,y,z), n);
439 Check how the block handles being a lonely sky block
441 parent.position_valid = true;
442 b.setIsUnderground(false);
443 parent.node.d = MATERIAL_AIR;
444 parent.node.setLight(LIGHT_SUN);
445 core::map<v3s16, bool> light_sources;
446 // The bottom block is invalid, because we have a shadowing node
447 assert(b.propagateSunlight(light_sources) == false);
448 assert(b.getNode(v3s16(1,4,0)).getLight() == LIGHT_SUN);
449 assert(b.getNode(v3s16(1,3,0)).getLight() == LIGHT_SUN);
450 assert(b.getNode(v3s16(1,2,0)).getLight() == 0);
451 assert(b.getNode(v3s16(1,1,0)).getLight() == 0);
452 assert(b.getNode(v3s16(1,0,0)).getLight() == 0);
453 assert(b.getNode(v3s16(1,2,3)).getLight() == LIGHT_SUN);
454 assert(b.getFaceLight(p, v3s16(0,1,0)) == LIGHT_SUN);
455 assert(b.getFaceLight(p, v3s16(0,-1,0)) == 0);
456 // According to MapBlock::getFaceLight,
457 // The face on the z+ side should have double-diminished light
458 assert(b.getFaceLight(p, v3s16(0,0,1)) == diminish_light(diminish_light(LIGHT_MAX)));
461 Check how the block handles being in between blocks with some non-sunlight
462 while being underground
465 // Make neighbours to exist and set some non-sunlight to them
466 parent.position_valid = true;
467 b.setIsUnderground(true);
468 parent.node.setLight(LIGHT_MAX/2);
469 core::map<v3s16, bool> light_sources;
470 // The block below should be valid because there shouldn't be
471 // sunlight in there either
472 assert(b.propagateSunlight(light_sources) == true);
473 // Should not touch nodes that are not affected (that is, all of them)
474 //assert(b.getNode(v3s16(1,2,3)).getLight() == LIGHT_SUN);
475 // Should set light of non-sunlighted blocks to 0.
476 assert(b.getNode(v3s16(1,2,3)).getLight() == 0);
479 Set up a situation where:
480 - There is only air in this block
481 - There is a valid non-sunlighted block at the bottom, and
482 - Invalid blocks elsewhere.
483 - the block is not underground.
485 This should result in bottom block invalidity
488 b.setIsUnderground(false);
490 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
491 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
492 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
496 b.setNode(v3s16(x,y,z), n);
500 // Make neighbours invalid
501 parent.position_valid = false;
502 // Add exceptions to the top of the bottom block
503 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
504 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
506 parent.validity_exceptions.push_back(v3s16(MAP_BLOCKSIZE+x, MAP_BLOCKSIZE-1, MAP_BLOCKSIZE+z));
508 // Lighting value for the valid nodes
509 parent.node.setLight(LIGHT_MAX/2);
510 core::map<v3s16, bool> light_sources;
511 // Bottom block is not valid
512 assert(b.propagateSunlight(light_sources) == false);
519 class TC : public NodeContainer
528 position_valid = true;
531 virtual bool isValidPosition(v3s16 p)
533 return position_valid;
536 virtual MapNode getNode(v3s16 p)
538 if(position_valid == false)
539 throw InvalidPositionException();
543 virtual void setNode(v3s16 p, MapNode & n)
545 if(position_valid == false)
546 throw InvalidPositionException();
549 virtual u16 nodeContainerId() const
558 parent.position_valid = false;
560 // Create one with no heightmaps
561 ServerMapSector sector(&parent, v2s16(1,1), 0);
562 //ConstantGenerator *dummyheightmap = new ConstantGenerator();
563 //sector->setHeightmap(dummyheightmap);
565 EXCEPTION_CHECK(InvalidPositionException, sector.getBlockNoCreate(0));
566 EXCEPTION_CHECK(InvalidPositionException, sector.getBlockNoCreate(1));
568 MapBlock * bref = sector.createBlankBlock(-2);
570 EXCEPTION_CHECK(InvalidPositionException, sector.getBlockNoCreate(0));
571 assert(sector.getBlockNoCreate(-2) == bref);
573 //TODO: Check for AlreadyExistsException
575 /*bool exception_thrown = false;
579 catch(InvalidPositionException &e){
580 exception_thrown = true;
582 assert(exception_thrown);*/
589 void TestSingleFixed()
592 OneChildHeightmap hm1(BS1);
594 // Test that it is filled with < GROUNDHEIGHT_VALID_MINVALUE
595 for(s16 y=0; y<=BS1; y++){
596 for(s16 x=0; x<=BS1; x++){
598 assert(hm1.m_child.getGroundHeight(p)
599 < GROUNDHEIGHT_VALID_MINVALUE);
603 hm1.m_child.setGroundHeight(v2s16(1,0), 2.0);
604 //hm1.m_child.print();
605 assert(fabs(hm1.getGroundHeight(v2s16(1,0))-2.0)<0.001);
606 hm1.setGroundHeight(v2s16(0,1), 3.0);
607 assert(fabs(hm1.m_child.getGroundHeight(v2s16(0,1))-3.0)<0.001);
610 for(s16 y=0; y<=BS1; y++){
611 for(s16 x=0; x<=BS1; x++){
613 hm1.m_child.setGroundHeight(p, -1.0);
617 f32 corners[] = {0.0, 0.0, 1.0, 1.0};
618 hm1.m_child.generateContinued(0.0, 0.0, corners);
621 assert(fabs(hm1.m_child.getGroundHeight(v2s16(1,0))-0.2)<0.05);
622 assert(fabs(hm1.m_child.getGroundHeight(v2s16(4,3))-0.7)<0.05);
623 assert(fabs(hm1.m_child.getGroundHeight(v2s16(4,4))-1.0)<0.05);
628 //g_heightmap_debugprint = true;
630 UnlimitedHeightmap hm1(BS1,
631 new ConstantGenerator(0.0),
632 new ConstantGenerator(0.0),
633 new ConstantGenerator(5.0));
634 // Go through it so it generates itself
635 for(s16 y=0; y<=BS1; y++){
636 for(s16 x=0; x<=BS1; x++){
638 hm1.getGroundHeight(p);
642 dstream<<"UnlimitedHeightmap hm1:"<<std::endl;
645 dstream<<"testing UnlimitedHeightmap set/get"<<std::endl;
648 // Get first heightmap and try setGroundHeight
649 FixedHeightmap * href = hm1.getHeightmap(v2s16(0,0));
650 href->setGroundHeight(p1, v1);
651 // Read from UnlimitedHeightmap
652 assert(fabs(hm1.getGroundHeight(p1)-v1)<0.001);
657 dstream<<"Running random code (get a human to check this)"<<std::endl;
658 dstream<<"rand() values: ";
659 for(u16 i=0; i<5; i++)
660 dstream<<(u16)rand()<<" ";
664 UnlimitedHeightmap hm1(BS1,
665 new ConstantGenerator(10.0),
666 new ConstantGenerator(0.3),
667 new ConstantGenerator(0.0));
669 // Force hm1 to generate a some heightmap
670 hm1.getGroundHeight(v2s16(0,0));
671 hm1.getGroundHeight(v2s16(0,BS1));
672 /*hm1.getGroundHeight(v2s16(BS1,-1));
673 hm1.getGroundHeight(v2s16(BS1-1,-1));*/
676 // Get the (0,0) and (1,0) heightmaps
677 /*FixedHeightmap * hr00 = hm1.getHeightmap(v2s16(0,0));
678 FixedHeightmap * hr01 = hm1.getHeightmap(v2s16(1,0));
679 f32 corners[] = {1.0, 1.0, 1.0, 1.0};
680 hr00->generateContinued(0.0, 0.0, corners);
688 //srand(7); // Get constant random
689 srand(time(0)); // Get better random
701 const int port = 30003;
705 const char sendbuffer[] = "hello world!";
706 socket.Send(Address(127,0,0,1,port), sendbuffer, sizeof(sendbuffer));
711 memset(rcvbuffer, 0, sizeof(rcvbuffer));
715 int bytes_read = socket.Receive(sender, rcvbuffer, sizeof(rcvbuffer));
719 //FIXME: This fails on some systems
720 assert(strncmp(sendbuffer, rcvbuffer, sizeof(sendbuffer))==0);
721 assert(sender.getAddress() == Address(127,0,0,1, 0).getAddress());
725 struct TestConnection
730 Test helper functions
733 // Some constants for testing
734 u32 proto_id = 0x12345678;
737 SharedBuffer<u8> data1(1);
739 Address a(127,0,0,1, 10);
742 con::BufferedPacket p1 = con::makePacket(a, data1,
743 proto_id, peer_id, channel);
745 We should now have a packet with this data:
748 [4] u16 sender_peer_id
753 assert(readU32(&p1.data[0]) == proto_id);
754 assert(readU16(&p1.data[4]) == peer_id);
755 assert(readU8(&p1.data[6]) == channel);
756 assert(readU8(&p1.data[7]) == data1[0]);
758 //dstream<<"initial data1[0]="<<((u32)data1[0]&0xff)<<std::endl;
760 SharedBuffer<u8> p2 = con::makeReliablePacket(data1, seqnum);
762 /*dstream<<"p2.getSize()="<<p2.getSize()<<", data1.getSize()="
763 <<data1.getSize()<<std::endl;
764 dstream<<"readU8(&p2[3])="<<readU8(&p2[3])
765 <<" p2[3]="<<((u32)p2[3]&0xff)<<std::endl;
766 dstream<<"data1[0]="<<((u32)data1[0]&0xff)<<std::endl;*/
768 assert(p2.getSize() == 3 + data1.getSize());
769 assert(readU8(&p2[0]) == TYPE_RELIABLE);
770 assert(readU16(&p2[1]) == seqnum);
771 assert(readU8(&p2[3]) == data1[0]);
774 struct Handler : public con::PeerHandler
776 Handler(const char *a_name)
782 void peerAdded(con::Peer *peer)
784 dstream<<"Handler("<<name<<")::peerAdded(): "
785 "id="<<peer->id<<std::endl;
789 void deletingPeer(con::Peer *peer, bool timeout)
791 dstream<<"Handler("<<name<<")::deletingPeer(): "
793 <<", timeout="<<timeout<<std::endl;
805 DSTACK("TestConnection::Run");
810 Test some real connections
812 u32 proto_id = 0xad26846a;
814 Handler hand_server("server");
815 Handler hand_client("client");
817 dstream<<"** Creating server Connection"<<std::endl;
818 con::Connection server(proto_id, 512, 5.0, &hand_server);
821 dstream<<"** Creating client Connection"<<std::endl;
822 con::Connection client(proto_id, 512, 5.0, &hand_client);
824 assert(hand_server.count == 0);
825 assert(hand_client.count == 0);
829 Address server_address(127,0,0,1, 30001);
830 dstream<<"** running client.Connect()"<<std::endl;
831 client.Connect(server_address);
835 // Client should have added server now
836 assert(hand_client.count == 1);
837 assert(hand_client.last_id == 1);
838 // But server should not have added client
839 assert(hand_server.count == 0);
845 dstream<<"** running server.Receive()"<<std::endl;
846 u32 size = server.Receive(peer_id, data, 100);
847 dstream<<"** Server received: peer_id="<<peer_id
851 catch(con::NoIncomingDataException &e)
853 // No actual data received, but the client has
854 // probably been connected
857 // Client should be the same
858 assert(hand_client.count == 1);
859 assert(hand_client.last_id == 1);
860 // Server should have the client
861 assert(hand_server.count == 1);
862 assert(hand_server.last_id == 2);
866 while(client.Connected() == false)
872 dstream<<"** running client.Receive()"<<std::endl;
873 u32 size = client.Receive(peer_id, data, 100);
874 dstream<<"** Client received: peer_id="<<peer_id
878 catch(con::NoIncomingDataException &e)
890 dstream<<"** running server.Receive()"<<std::endl;
891 u32 size = server.Receive(peer_id, data, 100);
892 dstream<<"** Server received: peer_id="<<peer_id
896 catch(con::NoIncomingDataException &e)
901 /*u8 data[] = "Hello World!";
902 u32 datasize = sizeof(data);*/
903 SharedBuffer<u8> data = SharedBufferFromString("Hello World!");
905 dstream<<"** running client.Send()"<<std::endl;
906 client.Send(PEER_ID_SERVER, 0, data, true);
912 dstream<<"** running server.Receive()"<<std::endl;
913 u32 size = server.Receive(peer_id, recvdata, 100);
914 dstream<<"** Server received: peer_id="<<peer_id
918 assert(memcmp(*data, recvdata, data.getSize()) == 0);
921 u16 peer_id_client = 2;
925 Send consequent packets in different order
927 //u8 data1[] = "hello1";
928 //u8 data2[] = "hello2";
929 SharedBuffer<u8> data1 = SharedBufferFromString("hello1");
930 SharedBuffer<u8> data2 = SharedBufferFromString("Hello2");
932 Address client_address =
933 server.GetPeer(peer_id_client)->address;
935 dstream<<"*** Sending packets in wrong order (2,1,2)"
939 con::Channel *ch = &server.GetPeer(peer_id_client)->channels[chn];
940 u16 sn = ch->next_outgoing_seqnum;
941 ch->next_outgoing_seqnum = sn+1;
942 server.Send(peer_id_client, chn, data2, true);
943 ch->next_outgoing_seqnum = sn;
944 server.Send(peer_id_client, chn, data1, true);
945 ch->next_outgoing_seqnum = sn+1;
946 server.Send(peer_id_client, chn, data2, true);
950 dstream<<"*** Receiving the packets"<<std::endl;
956 dstream<<"** running client.Receive()"<<std::endl;
958 size = client.Receive(peer_id, recvdata, 20);
959 dstream<<"** Client received: peer_id="<<peer_id
961 <<", data="<<recvdata
963 assert(size == data1.getSize());
964 assert(memcmp(*data1, recvdata, data1.getSize()) == 0);
965 assert(peer_id == PEER_ID_SERVER);
967 dstream<<"** running client.Receive()"<<std::endl;
969 size = client.Receive(peer_id, recvdata, 20);
970 dstream<<"** Client received: peer_id="<<peer_id
972 <<", data="<<recvdata
974 assert(size == data2.getSize());
975 assert(memcmp(*data2, recvdata, data2.getSize()) == 0);
976 assert(peer_id == PEER_ID_SERVER);
978 bool got_exception = false;
981 dstream<<"** running client.Receive()"<<std::endl;
983 size = client.Receive(peer_id, recvdata, 20);
984 dstream<<"** Client received: peer_id="<<peer_id
986 <<", data="<<recvdata
989 catch(con::NoIncomingDataException &e)
991 dstream<<"** No incoming data for client"<<std::endl;
992 got_exception = true;
994 assert(got_exception);
998 SharedBuffer<u8> data1(1100);
999 for(u16 i=0; i<1100; i++){
1003 dstream<<"Sending data (size="<<1100<<"):";
1004 for(int i=0; i<1100 && i<20; i++){
1005 if(i%2==0) printf(" ");
1006 printf("%.2X", ((int)((const char*)*data1)[i])&0xff);
1012 server.Send(peer_id_client, 0, data1, true);
1017 dstream<<"** running client.Receive()"<<std::endl;
1019 u16 size = client.Receive(peer_id, recvdata, 2000);
1020 dstream<<"** Client received: peer_id="<<peer_id
1024 dstream<<"Received data (size="<<size<<"):";
1025 for(int i=0; i<size && i<20; i++){
1026 if(i%2==0) printf(" ");
1027 printf("%.2X", ((int)((const char*)recvdata)[i])&0xff);
1033 assert(memcmp(*data1, recvdata, data1.getSize()) == 0);
1034 assert(peer_id == PEER_ID_SERVER);
1037 // Check peer handlers
1038 assert(hand_client.count == 1);
1039 assert(hand_client.last_id == 1);
1040 assert(hand_server.count == 1);
1041 assert(hand_server.last_id == 2);
1050 dstream<<"Running " #X <<std::endl;\
1056 DSTACK(__FUNCTION_NAME);
1057 dstream<<"run_tests() started"<<std::endl;
1058 TEST(TestUtilities);
1061 TEST(TestVoxelManipulator);
1063 TEST(TestMapSector);
1064 TEST(TestHeightmap);
1065 if(INTERNET_SIMULATOR == false){
1067 dout_con<<"=== BEGIN RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
1068 TEST(TestConnection);
1069 dout_con<<"=== END RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
1071 dstream<<"run_tests() passed"<<std::endl;