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 Asserts that the exception occurs
38 #define EXCEPTION_CHECK(EType, code)\
40 bool exception_thrown = false;\
42 catch(EType &e) { exception_thrown = true; }\
43 assert(exception_thrown);\
50 /*dstream<<"wrapDegrees(100.0) = "<<wrapDegrees(100.0)<<std::endl;
51 dstream<<"wrapDegrees(720.5) = "<<wrapDegrees(720.5)<<std::endl;
52 dstream<<"wrapDegrees(-0.5) = "<<wrapDegrees(-0.5)<<std::endl;*/
53 assert(fabs(wrapDegrees(100.0) - 100.0) < 0.001);
54 assert(fabs(wrapDegrees(720.5) - 0.5) < 0.001);
55 assert(fabs(wrapDegrees(-0.5) - (-0.5)) < 0.001);
56 assert(fabs(wrapDegrees(-365.5) - (-5.5)) < 0.001);
57 assert(lowercase("Foo bAR") == "foo bar");
58 assert(is_yes("YeS") == true);
59 assert(is_yes("") == false);
60 assert(is_yes("FAlse") == false);
70 SharedBuffer<u8> fromdata(4);
76 std::ostringstream os(std::ios_base::binary);
77 compress(fromdata, os, 0);
79 std::string str_out = os.str();
81 dstream<<"str_out.size()="<<str_out.size()<<std::endl;
82 dstream<<"TestCompress: 1,5,5,1 -> ";
83 for(u32 i=0; i<str_out.size(); i++)
85 dstream<<(u32)str_out[i]<<",";
89 assert(str_out.size() == 10);
91 assert(str_out[0] == 0);
92 assert(str_out[1] == 0);
93 assert(str_out[2] == 0);
94 assert(str_out[3] == 4);
95 assert(str_out[4] == 0);
96 assert(str_out[5] == 1);
97 assert(str_out[6] == 1);
98 assert(str_out[7] == 5);
99 assert(str_out[8] == 0);
100 assert(str_out[9] == 1);
102 std::istringstream is(str_out, std::ios_base::binary);
103 std::ostringstream os2(std::ios_base::binary);
105 decompress(is, os2, 0);
106 std::string str_out2 = os2.str();
108 dstream<<"decompress: ";
109 for(u32 i=0; i<str_out2.size(); i++)
111 dstream<<(u32)str_out2[i]<<",";
115 assert(str_out2.size() == fromdata.getSize());
117 for(u32 i=0; i<str_out2.size(); i++)
119 assert(str_out2[i] == fromdata[i]);
126 SharedBuffer<u8> fromdata(4);
132 std::ostringstream os(std::ios_base::binary);
133 compress(fromdata, os, SER_FMT_VER_HIGHEST);
135 std::string str_out = os.str();
137 dstream<<"str_out.size()="<<str_out.size()<<std::endl;
138 dstream<<"TestCompress: 1,5,5,1 -> ";
139 for(u32 i=0; i<str_out.size(); i++)
141 dstream<<(u32)str_out[i]<<",";
145 /*assert(str_out.size() == 10);
147 assert(str_out[0] == 0);
148 assert(str_out[1] == 0);
149 assert(str_out[2] == 0);
150 assert(str_out[3] == 4);
151 assert(str_out[4] == 0);
152 assert(str_out[5] == 1);
153 assert(str_out[6] == 1);
154 assert(str_out[7] == 5);
155 assert(str_out[8] == 0);
156 assert(str_out[9] == 1);*/
158 std::istringstream is(str_out, std::ios_base::binary);
159 std::ostringstream os2(std::ios_base::binary);
161 decompress(is, os2, SER_FMT_VER_HIGHEST);
162 std::string str_out2 = os2.str();
164 dstream<<"decompress: ";
165 for(u32 i=0; i<str_out2.size(); i++)
167 dstream<<(u32)str_out2[i]<<",";
171 assert(str_out2.size() == fromdata.getSize());
173 for(u32 i=0; i<str_out2.size(); i++)
175 assert(str_out2[i] == fromdata[i]);
189 assert(n.d == CONTENT_AIR);
190 assert(n.getLight(LIGHTBANK_DAY) == 0);
191 assert(n.getLight(LIGHTBANK_NIGHT) == 0);
195 assert(n.light_propagates() == true);
197 assert(n.light_propagates() == false);
201 struct TestVoxelManipulator
209 VoxelArea a(v3s16(-1,-1,-1), v3s16(1,1,1));
210 assert(a.index(0,0,0) == 1*3*3 + 1*3 + 1);
211 assert(a.index(-1,-1,-1) == 0);
213 VoxelArea c(v3s16(-2,-2,-2), v3s16(2,2,2));
214 // An area that is 1 bigger in x+ and z-
215 VoxelArea d(v3s16(-2,-2,-3), v3s16(3,2,2));
217 core::list<VoxelArea> aa;
221 core::array<VoxelArea> results;
222 results.push_back(VoxelArea(v3s16(-2,-2,-3),v3s16(3,2,-3)));
223 results.push_back(VoxelArea(v3s16(3,-2,-2),v3s16(3,2,2)));
225 assert(aa.size() == results.size());
227 dstream<<"Result of diff:"<<std::endl;
228 for(core::list<VoxelArea>::Iterator
229 i = aa.begin(); i != aa.end(); i++)
234 s32 j = results.linear_search(*i);
248 dstream<<"*** Setting (-1,0,-1)=2 ***"<<std::endl;
250 v.setNodeNoRef(v3s16(-1,0,-1), MapNode(2));
254 assert(v.getNode(v3s16(-1,0,-1)).d == 2);
256 dstream<<"*** Reading from inexistent (0,0,-1) ***"<<std::endl;
258 EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,0,-1)));
262 dstream<<"*** Adding area ***"<<std::endl;
268 assert(v.getNode(v3s16(-1,0,-1)).d == 2);
269 EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,1,1)));
277 const char *content =
289 v3s16 size(12, 4, 2);
290 VoxelArea area(v3s16(0,0,0), size-v3s16(1,1,1));
292 const char *p = content;
293 for(s16 z=0; z<size.Z; z++)
294 for(s16 y=size.Y-1; y>=0; y--)
295 for(s16 x=0; x<size.X; x++)
298 //n.pressure = size.Y - y;
307 v.setNode(v3s16(x,y,z), n);
311 v.print(dstream, VOXELPRINT_WATERPRESSURE);
313 core::map<v3s16, u8> active_nodes;
314 v.updateAreaWaterPressure(area, active_nodes);
316 v.print(dstream, VOXELPRINT_WATERPRESSURE);
318 //s16 highest_y = -32768;
320 NOTE: These are commented out because this behaviour is changed
323 //assert(v.getWaterPressure(v3s16(7, 1, 1), highest_y, 0) == -1);
324 //assert(highest_y == 3);
325 /*assert(v.getWaterPressure(v3s16(7, 1, 1), highest_y, 0) == 3);
326 //assert(highest_y == 3);*/
328 active_nodes.clear();
329 active_nodes[v3s16(9,1,0)] = 1;
330 //v.flowWater(active_nodes, 0, true, 1000);
331 v.flowWater(active_nodes, 0, false, 1000);
333 dstream<<"Final result of flowWater:"<<std::endl;
334 v.print(dstream, VOXELPRINT_WATERPRESSURE);
342 class TC : public NodeContainer
348 core::list<v3s16> validity_exceptions;
352 position_valid = true;
355 virtual bool isValidPosition(v3s16 p)
357 //return position_valid ^ (p==position_valid_exception);
358 bool exception = false;
359 for(core::list<v3s16>::Iterator i=validity_exceptions.begin();
360 i != validity_exceptions.end(); i++)
368 return exception ? !position_valid : position_valid;
371 virtual MapNode getNode(v3s16 p)
373 if(isValidPosition(p) == false)
374 throw InvalidPositionException();
378 virtual void setNode(v3s16 p, MapNode & n)
380 if(isValidPosition(p) == false)
381 throw InvalidPositionException();
384 virtual u16 nodeContainerId() const
394 MapBlock b(&parent, v3s16(1,1,1));
395 v3s16 relpos(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
397 assert(b.getPosRelative() == relpos);
399 assert(b.getBox().MinEdge.X == MAP_BLOCKSIZE);
400 assert(b.getBox().MaxEdge.X == MAP_BLOCKSIZE*2-1);
401 assert(b.getBox().MinEdge.Y == MAP_BLOCKSIZE);
402 assert(b.getBox().MaxEdge.Y == MAP_BLOCKSIZE*2-1);
403 assert(b.getBox().MinEdge.Z == MAP_BLOCKSIZE);
404 assert(b.getBox().MaxEdge.Z == MAP_BLOCKSIZE*2-1);
406 assert(b.isValidPosition(v3s16(0,0,0)) == true);
407 assert(b.isValidPosition(v3s16(-1,0,0)) == false);
408 assert(b.isValidPosition(v3s16(-1,-142,-2341)) == false);
409 assert(b.isValidPosition(v3s16(-124,142,2341)) == false);
410 assert(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
411 assert(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE,MAP_BLOCKSIZE-1)) == false);
414 TODO: this method should probably be removed
415 if the block size isn't going to be set variable
417 /*assert(b.getSizeNodes() == v3s16(MAP_BLOCKSIZE,
418 MAP_BLOCKSIZE, MAP_BLOCKSIZE));*/
420 // Changed flag should be initially set
421 assert(b.getChangedFlag() == true);
422 b.resetChangedFlag();
423 assert(b.getChangedFlag() == false);
425 // All nodes should have been set to
426 // .d=CONTENT_AIR and .getLight() = 0
427 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
428 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
429 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
431 assert(b.getNode(v3s16(x,y,z)).d == CONTENT_AIR);
432 assert(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_DAY) == 0);
433 assert(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_NIGHT) == 0);
437 Parent fetch functions
439 parent.position_valid = false;
444 // Positions in the block should still be valid
445 assert(b.isValidPositionParent(v3s16(0,0,0)) == true);
446 assert(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
447 n = b.getNodeParent(v3s16(0,MAP_BLOCKSIZE-1,0));
448 assert(n.d == CONTENT_AIR);
450 // ...but outside the block they should be invalid
451 assert(b.isValidPositionParent(v3s16(-121,2341,0)) == false);
452 assert(b.isValidPositionParent(v3s16(-1,0,0)) == false);
453 assert(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == false);
456 bool exception_thrown = false;
458 // This should throw an exception
459 MapNode n = b.getNodeParent(v3s16(0,0,-1));
461 catch(InvalidPositionException &e)
463 exception_thrown = true;
465 assert(exception_thrown);
468 parent.position_valid = true;
469 // Now the positions outside should be valid
470 assert(b.isValidPositionParent(v3s16(-121,2341,0)) == true);
471 assert(b.isValidPositionParent(v3s16(-1,0,0)) == true);
472 assert(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == true);
473 n = b.getNodeParent(v3s16(0,0,MAP_BLOCKSIZE));
482 assert(b.getNode(p).d == 4);
483 //TODO: Update to new system
484 /*assert(b.getNodeTile(p) == 4);
485 assert(b.getNodeTile(v3s16(-1,-1,0)) == 5);*/
490 // Set lighting of all nodes to 0
491 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
492 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
493 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
494 MapNode n = b.getNode(v3s16(x,y,z));
495 n.setLight(LIGHTBANK_DAY, 0);
496 n.setLight(LIGHTBANK_NIGHT, 0);
497 b.setNode(v3s16(x,y,z), n);
503 Check how the block handles being a lonely sky block
505 parent.position_valid = true;
506 b.setIsUnderground(false);
507 parent.node.d = CONTENT_AIR;
508 parent.node.setLight(LIGHTBANK_DAY, LIGHT_SUN);
509 parent.node.setLight(LIGHTBANK_NIGHT, 0);
510 core::map<v3s16, bool> light_sources;
511 // The bottom block is invalid, because we have a shadowing node
512 assert(b.propagateSunlight(light_sources) == false);
513 assert(b.getNode(v3s16(1,4,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
514 assert(b.getNode(v3s16(1,3,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
515 assert(b.getNode(v3s16(1,2,0)).getLight(LIGHTBANK_DAY) == 0);
516 assert(b.getNode(v3s16(1,1,0)).getLight(LIGHTBANK_DAY) == 0);
517 assert(b.getNode(v3s16(1,0,0)).getLight(LIGHTBANK_DAY) == 0);
518 assert(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
519 assert(b.getFaceLight(1000, p, v3s16(0,1,0)) == LIGHT_SUN);
520 assert(b.getFaceLight(1000, p, v3s16(0,-1,0)) == 0);
521 assert(b.getFaceLight(0, p, v3s16(0,-1,0)) == 0);
522 // According to MapBlock::getFaceLight,
523 // The face on the z+ side should have double-diminished light
524 //assert(b.getFaceLight(p, v3s16(0,0,1)) == diminish_light(diminish_light(LIGHT_MAX)));
525 // The face on the z+ side should have diminished light
526 assert(b.getFaceLight(1000, p, v3s16(0,0,1)) == diminish_light(LIGHT_MAX));
529 Check how the block handles being in between blocks with some non-sunlight
530 while being underground
533 // Make neighbours to exist and set some non-sunlight to them
534 parent.position_valid = true;
535 b.setIsUnderground(true);
536 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
537 core::map<v3s16, bool> light_sources;
538 // The block below should be valid because there shouldn't be
539 // sunlight in there either
540 assert(b.propagateSunlight(light_sources) == true);
541 // Should not touch nodes that are not affected (that is, all of them)
542 //assert(b.getNode(v3s16(1,2,3)).getLight() == LIGHT_SUN);
543 // Should set light of non-sunlighted blocks to 0.
544 assert(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == 0);
547 Set up a situation where:
548 - There is only air in this block
549 - There is a valid non-sunlighted block at the bottom, and
550 - Invalid blocks elsewhere.
551 - the block is not underground.
553 This should result in bottom block invalidity
556 b.setIsUnderground(false);
558 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
559 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
560 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
563 n.setLight(LIGHTBANK_DAY, 0);
564 b.setNode(v3s16(x,y,z), n);
568 // Make neighbours invalid
569 parent.position_valid = false;
570 // Add exceptions to the top of the bottom block
571 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
572 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
574 parent.validity_exceptions.push_back(v3s16(MAP_BLOCKSIZE+x, MAP_BLOCKSIZE-1, MAP_BLOCKSIZE+z));
576 // Lighting value for the valid nodes
577 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
578 core::map<v3s16, bool> light_sources;
579 // Bottom block is not valid
580 assert(b.propagateSunlight(light_sources) == false);
587 class TC : public NodeContainer
596 position_valid = true;
599 virtual bool isValidPosition(v3s16 p)
601 return position_valid;
604 virtual MapNode getNode(v3s16 p)
606 if(position_valid == false)
607 throw InvalidPositionException();
611 virtual void setNode(v3s16 p, MapNode & n)
613 if(position_valid == false)
614 throw InvalidPositionException();
617 virtual u16 nodeContainerId() const
626 parent.position_valid = false;
628 // Create one with no heightmaps
629 ServerMapSector sector(&parent, v2s16(1,1), 0);
630 //ConstantGenerator *dummyheightmap = new ConstantGenerator();
631 //sector->setHeightmap(dummyheightmap);
633 EXCEPTION_CHECK(InvalidPositionException, sector.getBlockNoCreate(0));
634 EXCEPTION_CHECK(InvalidPositionException, sector.getBlockNoCreate(1));
636 MapBlock * bref = sector.createBlankBlock(-2);
638 EXCEPTION_CHECK(InvalidPositionException, sector.getBlockNoCreate(0));
639 assert(sector.getBlockNoCreate(-2) == bref);
641 //TODO: Check for AlreadyExistsException
643 /*bool exception_thrown = false;
647 catch(InvalidPositionException &e){
648 exception_thrown = true;
650 assert(exception_thrown);*/
657 void TestSingleFixed()
660 OneChildHeightmap hm1(BS1);
662 // Test that it is filled with < GROUNDHEIGHT_VALID_MINVALUE
663 for(s16 y=0; y<=BS1; y++){
664 for(s16 x=0; x<=BS1; x++){
666 assert(hm1.m_child.getGroundHeight(p)
667 < GROUNDHEIGHT_VALID_MINVALUE);
671 hm1.m_child.setGroundHeight(v2s16(1,0), 2.0);
672 //hm1.m_child.print();
673 assert(fabs(hm1.getGroundHeight(v2s16(1,0))-2.0)<0.001);
674 hm1.setGroundHeight(v2s16(0,1), 3.0);
675 assert(fabs(hm1.m_child.getGroundHeight(v2s16(0,1))-3.0)<0.001);
678 for(s16 y=0; y<=BS1; y++){
679 for(s16 x=0; x<=BS1; x++){
681 hm1.m_child.setGroundHeight(p, -1.0);
685 f32 corners[] = {0.0, 0.0, 1.0, 1.0};
686 hm1.m_child.generateContinued(0.0, 0.0, corners);
689 assert(fabs(hm1.m_child.getGroundHeight(v2s16(1,0))-0.2)<0.05);
690 assert(fabs(hm1.m_child.getGroundHeight(v2s16(4,3))-0.7)<0.05);
691 assert(fabs(hm1.m_child.getGroundHeight(v2s16(4,4))-1.0)<0.05);
696 //g_heightmap_debugprint = true;
698 UnlimitedHeightmap hm1(BS1,
699 new ConstantGenerator(0.0),
700 new ConstantGenerator(0.0),
701 new ConstantGenerator(5.0));
702 // Go through it so it generates itself
703 for(s16 y=0; y<=BS1; y++){
704 for(s16 x=0; x<=BS1; x++){
706 hm1.getGroundHeight(p);
710 dstream<<"UnlimitedHeightmap hm1:"<<std::endl;
713 dstream<<"testing UnlimitedHeightmap set/get"<<std::endl;
716 // Get first heightmap and try setGroundHeight
717 FixedHeightmap * href = hm1.getHeightmap(v2s16(0,0));
718 href->setGroundHeight(p1, v1);
719 // Read from UnlimitedHeightmap
720 assert(fabs(hm1.getGroundHeight(p1)-v1)<0.001);
725 dstream<<"Running random code (get a human to check this)"<<std::endl;
726 dstream<<"myrand() values: ";
727 for(u16 i=0; i<5; i++)
728 dstream<<(u16)myrand()<<" ";
732 UnlimitedHeightmap hm1(BS1,
733 new ConstantGenerator(10.0),
734 new ConstantGenerator(0.3),
735 new ConstantGenerator(0.0));
737 // Force hm1 to generate a some heightmap
738 hm1.getGroundHeight(v2s16(0,0));
739 hm1.getGroundHeight(v2s16(0,BS1));
740 /*hm1.getGroundHeight(v2s16(BS1,-1));
741 hm1.getGroundHeight(v2s16(BS1-1,-1));*/
744 // Get the (0,0) and (1,0) heightmaps
745 /*FixedHeightmap * hr00 = hm1.getHeightmap(v2s16(0,0));
746 FixedHeightmap * hr01 = hm1.getHeightmap(v2s16(1,0));
747 f32 corners[] = {1.0, 1.0, 1.0, 1.0};
748 hr00->generateContinued(0.0, 0.0, corners);
756 //srand(7); // Get constant random
757 srand(time(0)); // Get better random
769 const int port = 30003;
773 const char sendbuffer[] = "hello world!";
774 socket.Send(Address(127,0,0,1,port), sendbuffer, sizeof(sendbuffer));
779 memset(rcvbuffer, 0, sizeof(rcvbuffer));
783 int bytes_read = socket.Receive(sender, rcvbuffer, sizeof(rcvbuffer));
787 //FIXME: This fails on some systems
788 assert(strncmp(sendbuffer, rcvbuffer, sizeof(sendbuffer))==0);
789 assert(sender.getAddress() == Address(127,0,0,1, 0).getAddress());
793 struct TestConnection
798 Test helper functions
801 // Some constants for testing
802 u32 proto_id = 0x12345678;
805 SharedBuffer<u8> data1(1);
807 Address a(127,0,0,1, 10);
810 con::BufferedPacket p1 = con::makePacket(a, data1,
811 proto_id, peer_id, channel);
813 We should now have a packet with this data:
816 [4] u16 sender_peer_id
821 assert(readU32(&p1.data[0]) == proto_id);
822 assert(readU16(&p1.data[4]) == peer_id);
823 assert(readU8(&p1.data[6]) == channel);
824 assert(readU8(&p1.data[7]) == data1[0]);
826 //dstream<<"initial data1[0]="<<((u32)data1[0]&0xff)<<std::endl;
828 SharedBuffer<u8> p2 = con::makeReliablePacket(data1, seqnum);
830 /*dstream<<"p2.getSize()="<<p2.getSize()<<", data1.getSize()="
831 <<data1.getSize()<<std::endl;
832 dstream<<"readU8(&p2[3])="<<readU8(&p2[3])
833 <<" p2[3]="<<((u32)p2[3]&0xff)<<std::endl;
834 dstream<<"data1[0]="<<((u32)data1[0]&0xff)<<std::endl;*/
836 assert(p2.getSize() == 3 + data1.getSize());
837 assert(readU8(&p2[0]) == TYPE_RELIABLE);
838 assert(readU16(&p2[1]) == seqnum);
839 assert(readU8(&p2[3]) == data1[0]);
842 struct Handler : public con::PeerHandler
844 Handler(const char *a_name)
850 void peerAdded(con::Peer *peer)
852 dstream<<"Handler("<<name<<")::peerAdded(): "
853 "id="<<peer->id<<std::endl;
857 void deletingPeer(con::Peer *peer, bool timeout)
859 dstream<<"Handler("<<name<<")::deletingPeer(): "
861 <<", timeout="<<timeout<<std::endl;
873 DSTACK("TestConnection::Run");
878 Test some real connections
880 u32 proto_id = 0xad26846a;
882 Handler hand_server("server");
883 Handler hand_client("client");
885 dstream<<"** Creating server Connection"<<std::endl;
886 con::Connection server(proto_id, 512, 5.0, &hand_server);
889 dstream<<"** Creating client Connection"<<std::endl;
890 con::Connection client(proto_id, 512, 5.0, &hand_client);
892 assert(hand_server.count == 0);
893 assert(hand_client.count == 0);
897 Address server_address(127,0,0,1, 30001);
898 dstream<<"** running client.Connect()"<<std::endl;
899 client.Connect(server_address);
903 // Client should have added server now
904 assert(hand_client.count == 1);
905 assert(hand_client.last_id == 1);
906 // But server should not have added client
907 assert(hand_server.count == 0);
913 dstream<<"** running server.Receive()"<<std::endl;
914 u32 size = server.Receive(peer_id, data, 100);
915 dstream<<"** Server received: peer_id="<<peer_id
919 catch(con::NoIncomingDataException &e)
921 // No actual data received, but the client has
922 // probably been connected
925 // Client should be the same
926 assert(hand_client.count == 1);
927 assert(hand_client.last_id == 1);
928 // Server should have the client
929 assert(hand_server.count == 1);
930 assert(hand_server.last_id == 2);
934 while(client.Connected() == false)
940 dstream<<"** running client.Receive()"<<std::endl;
941 u32 size = client.Receive(peer_id, data, 100);
942 dstream<<"** Client received: peer_id="<<peer_id
946 catch(con::NoIncomingDataException &e)
958 dstream<<"** running server.Receive()"<<std::endl;
959 u32 size = server.Receive(peer_id, data, 100);
960 dstream<<"** Server received: peer_id="<<peer_id
964 catch(con::NoIncomingDataException &e)
969 /*u8 data[] = "Hello World!";
970 u32 datasize = sizeof(data);*/
971 SharedBuffer<u8> data = SharedBufferFromString("Hello World!");
973 dstream<<"** running client.Send()"<<std::endl;
974 client.Send(PEER_ID_SERVER, 0, data, true);
980 dstream<<"** running server.Receive()"<<std::endl;
981 u32 size = server.Receive(peer_id, recvdata, 100);
982 dstream<<"** Server received: peer_id="<<peer_id
986 assert(memcmp(*data, recvdata, data.getSize()) == 0);
989 u16 peer_id_client = 2;
993 Send consequent packets in different order
995 //u8 data1[] = "hello1";
996 //u8 data2[] = "hello2";
997 SharedBuffer<u8> data1 = SharedBufferFromString("hello1");
998 SharedBuffer<u8> data2 = SharedBufferFromString("Hello2");
1000 Address client_address =
1001 server.GetPeer(peer_id_client)->address;
1003 dstream<<"*** Sending packets in wrong order (2,1,2)"
1007 con::Channel *ch = &server.GetPeer(peer_id_client)->channels[chn];
1008 u16 sn = ch->next_outgoing_seqnum;
1009 ch->next_outgoing_seqnum = sn+1;
1010 server.Send(peer_id_client, chn, data2, true);
1011 ch->next_outgoing_seqnum = sn;
1012 server.Send(peer_id_client, chn, data1, true);
1013 ch->next_outgoing_seqnum = sn+1;
1014 server.Send(peer_id_client, chn, data2, true);
1018 dstream<<"*** Receiving the packets"<<std::endl;
1024 dstream<<"** running client.Receive()"<<std::endl;
1026 size = client.Receive(peer_id, recvdata, 20);
1027 dstream<<"** Client received: peer_id="<<peer_id
1029 <<", data="<<recvdata
1031 assert(size == data1.getSize());
1032 assert(memcmp(*data1, recvdata, data1.getSize()) == 0);
1033 assert(peer_id == PEER_ID_SERVER);
1035 dstream<<"** running client.Receive()"<<std::endl;
1037 size = client.Receive(peer_id, recvdata, 20);
1038 dstream<<"** Client received: peer_id="<<peer_id
1040 <<", data="<<recvdata
1042 assert(size == data2.getSize());
1043 assert(memcmp(*data2, recvdata, data2.getSize()) == 0);
1044 assert(peer_id == PEER_ID_SERVER);
1046 bool got_exception = false;
1049 dstream<<"** running client.Receive()"<<std::endl;
1051 size = client.Receive(peer_id, recvdata, 20);
1052 dstream<<"** Client received: peer_id="<<peer_id
1054 <<", data="<<recvdata
1057 catch(con::NoIncomingDataException &e)
1059 dstream<<"** No incoming data for client"<<std::endl;
1060 got_exception = true;
1062 assert(got_exception);
1066 SharedBuffer<u8> data1(1100);
1067 for(u16 i=0; i<1100; i++){
1071 dstream<<"Sending data (size="<<1100<<"):";
1072 for(int i=0; i<1100 && i<20; i++){
1073 if(i%2==0) DEBUGPRINT(" ");
1074 DEBUGPRINT("%.2X", ((int)((const char*)*data1)[i])&0xff);
1080 server.Send(peer_id_client, 0, data1, true);
1085 dstream<<"** running client.Receive()"<<std::endl;
1087 u16 size = client.Receive(peer_id, recvdata, 2000);
1088 dstream<<"** Client received: peer_id="<<peer_id
1092 dstream<<"Received data (size="<<size<<"):";
1093 for(int i=0; i<size && i<20; i++){
1094 if(i%2==0) DEBUGPRINT(" ");
1095 DEBUGPRINT("%.2X", ((int)((const char*)recvdata)[i])&0xff);
1101 assert(memcmp(*data1, recvdata, data1.getSize()) == 0);
1102 assert(peer_id == PEER_ID_SERVER);
1105 // Check peer handlers
1106 assert(hand_client.count == 1);
1107 assert(hand_client.last_id == 1);
1108 assert(hand_server.count == 1);
1109 assert(hand_server.last_id == 2);
1118 dstream<<"Running " #X <<std::endl;\
1124 DSTACK(__FUNCTION_NAME);
1125 dstream<<"run_tests() started"<<std::endl;
1126 TEST(TestUtilities);
1129 TEST(TestVoxelManipulator);
1131 TEST(TestMapSector);
1132 TEST(TestHeightmap);
1133 if(INTERNET_SIMULATOR == false){
1135 dout_con<<"=== BEGIN RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
1136 TEST(TestConnection);
1137 dout_con<<"=== END RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
1139 dstream<<"run_tests() passed"<<std::endl;