/* Minetest-c55 Copyright (C) 2010 celeron55, Perttu Ahola This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include "test.h" #include "common_irrlicht.h" #include "debug.h" #include "map.h" #include "player.h" #include "main.h" #include "socket.h" #include "connection.h" #include "utility.h" #include "serialization.h" #include "voxel.h" #include #include "porting.h" #include "content_mapnode.h" #include "mapsector.h" #include "settings.h" #include "log.h" /* Asserts that the exception occurs */ #define EXCEPTION_CHECK(EType, code)\ {\ bool exception_thrown = false;\ try{ code; }\ catch(EType &e) { exception_thrown = true; }\ assert(exception_thrown);\ } struct TestUtilities { void Run() { /*infostream<<"wrapDegrees(100.0) = "< fromdata(4); fromdata[0]=1; fromdata[1]=5; fromdata[2]=5; fromdata[3]=1; std::ostringstream os(std::ios_base::binary); compress(fromdata, os, 0); std::string str_out = os.str(); infostream<<"str_out.size()="< "; for(u32 i=0; i fromdata(4); fromdata[0]=1; fromdata[1]=5; fromdata[2]=5; fromdata[3]=1; std::ostringstream os(std::ios_base::binary); compress(fromdata, os, SER_FMT_VER_HIGHEST); std::string str_out = os.str(); infostream<<"str_out.size()="< "; for(u32 i=0; i aa; d.diff(c, aa); // Correct results core::array results; results.push_back(VoxelArea(v3s16(-2,-2,-3),v3s16(3,2,-3))); results.push_back(VoxelArea(v3s16(3,-2,-2),v3s16(3,2,2))); assert(aa.size() == results.size()); infostream<<"Result of diff:"<::Iterator i = aa.begin(); i != aa.end(); i++) { i->print(infostream); infostream<=0; y--) for(s16 x=0; x active_nodes; v.updateAreaWaterPressure(area, active_nodes); v.print(infostream, VOXELPRINT_WATERPRESSURE); //s16 highest_y = -32768; /* NOTE: These are commented out because this behaviour is changed all the time */ //assert(v.getWaterPressure(v3s16(7, 1, 1), highest_y, 0) == -1); //assert(highest_y == 3); /*assert(v.getWaterPressure(v3s16(7, 1, 1), highest_y, 0) == 3); //assert(highest_y == 3);*/ active_nodes.clear(); active_nodes[v3s16(9,1,0)] = 1; //v.flowWater(active_nodes, 0, true, 1000); v.flowWater(active_nodes, 0, false, 1000); infostream<<"Final result of flowWater:"< validity_exceptions; TC() { position_valid = true; } virtual bool isValidPosition(v3s16 p) { //return position_valid ^ (p==position_valid_exception); bool exception = false; for(core::list::Iterator i=validity_exceptions.begin(); i != validity_exceptions.end(); i++) { if(p == *i) { exception = true; break; } } return exception ? !position_valid : position_valid; } virtual MapNode getNode(v3s16 p) { if(isValidPosition(p) == false) throw InvalidPositionException(); return node; } virtual void setNode(v3s16 p, MapNode & n) { if(isValidPosition(p) == false) throw InvalidPositionException(); }; virtual u16 nodeContainerId() const { return 666; } }; void Run() { TC parent; MapBlock b(&parent, v3s16(1,1,1)); v3s16 relpos(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE); assert(b.getPosRelative() == relpos); assert(b.getBox().MinEdge.X == MAP_BLOCKSIZE); assert(b.getBox().MaxEdge.X == MAP_BLOCKSIZE*2-1); assert(b.getBox().MinEdge.Y == MAP_BLOCKSIZE); assert(b.getBox().MaxEdge.Y == MAP_BLOCKSIZE*2-1); assert(b.getBox().MinEdge.Z == MAP_BLOCKSIZE); assert(b.getBox().MaxEdge.Z == MAP_BLOCKSIZE*2-1); assert(b.isValidPosition(v3s16(0,0,0)) == true); assert(b.isValidPosition(v3s16(-1,0,0)) == false); assert(b.isValidPosition(v3s16(-1,-142,-2341)) == false); assert(b.isValidPosition(v3s16(-124,142,2341)) == false); assert(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true); assert(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE,MAP_BLOCKSIZE-1)) == false); /* TODO: this method should probably be removed if the block size isn't going to be set variable */ /*assert(b.getSizeNodes() == v3s16(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE));*/ // Changed flag should be initially set assert(b.getChangedFlag() == true); b.resetChangedFlag(); assert(b.getChangedFlag() == false); // All nodes should have been set to // .d=CONTENT_IGNORE and .getLight() = 0 for(u16 z=0; z light_sources; // The bottom block is invalid, because we have a shadowing node assert(b.propagateSunlight(light_sources) == false); assert(b.getNode(v3s16(1,4,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN); assert(b.getNode(v3s16(1,3,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN); assert(b.getNode(v3s16(1,2,0)).getLight(LIGHTBANK_DAY) == 0); assert(b.getNode(v3s16(1,1,0)).getLight(LIGHTBANK_DAY) == 0); assert(b.getNode(v3s16(1,0,0)).getLight(LIGHTBANK_DAY) == 0); assert(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == LIGHT_SUN); assert(b.getFaceLight2(1000, p, v3s16(0,1,0)) == LIGHT_SUN); assert(b.getFaceLight2(1000, p, v3s16(0,-1,0)) == 0); assert(b.getFaceLight2(0, p, v3s16(0,-1,0)) == 0); // According to MapBlock::getFaceLight, // The face on the z+ side should have double-diminished light //assert(b.getFaceLight(p, v3s16(0,0,1)) == diminish_light(diminish_light(LIGHT_MAX))); // The face on the z+ side should have diminished light assert(b.getFaceLight2(1000, p, v3s16(0,0,1)) == diminish_light(LIGHT_MAX)); } /* Check how the block handles being in between blocks with some non-sunlight while being underground */ { // Make neighbours to exist and set some non-sunlight to them parent.position_valid = true; b.setIsUnderground(true); parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2); core::map light_sources; // The block below should be valid because there shouldn't be // sunlight in there either assert(b.propagateSunlight(light_sources, true) == true); // Should not touch nodes that are not affected (that is, all of them) //assert(b.getNode(v3s16(1,2,3)).getLight() == LIGHT_SUN); // Should set light of non-sunlighted blocks to 0. assert(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == 0); } /* Set up a situation where: - There is only air in this block - There is a valid non-sunlighted block at the bottom, and - Invalid blocks elsewhere. - the block is not underground. This should result in bottom block invalidity */ { b.setIsUnderground(false); // Clear block for(u16 z=0; z light_sources; // Bottom block is not valid assert(b.propagateSunlight(light_sources) == false); } } }; struct TestMapSector { class TC : public NodeContainer { public: MapNode node; bool position_valid; TC() { position_valid = true; } virtual bool isValidPosition(v3s16 p) { return position_valid; } virtual MapNode getNode(v3s16 p) { if(position_valid == false) throw InvalidPositionException(); return node; } virtual void setNode(v3s16 p, MapNode & n) { if(position_valid == false) throw InvalidPositionException(); }; virtual u16 nodeContainerId() const { return 666; } }; void Run() { TC parent; parent.position_valid = false; // Create one with no heightmaps ServerMapSector sector(&parent, v2s16(1,1)); assert(sector.getBlockNoCreateNoEx(0) == 0); assert(sector.getBlockNoCreateNoEx(1) == 0); MapBlock * bref = sector.createBlankBlock(-2); assert(sector.getBlockNoCreateNoEx(0) == 0); assert(sector.getBlockNoCreateNoEx(-2) == bref); //TODO: Check for AlreadyExistsException /*bool exception_thrown = false; try{ sector.getBlock(0); } catch(InvalidPositionException &e){ exception_thrown = true; } assert(exception_thrown);*/ } }; #endif struct TestSocket { void Run() { const int port = 30003; UDPSocket socket; socket.Bind(port); const char sendbuffer[] = "hello world!"; socket.Send(Address(127,0,0,1,port), sendbuffer, sizeof(sendbuffer)); sleep_ms(50); char rcvbuffer[256]; memset(rcvbuffer, 0, sizeof(rcvbuffer)); Address sender; for(;;) { int bytes_read = socket.Receive(sender, rcvbuffer, sizeof(rcvbuffer)); if(bytes_read < 0) break; } //FIXME: This fails on some systems assert(strncmp(sendbuffer, rcvbuffer, sizeof(sendbuffer))==0); assert(sender.getAddress() == Address(127,0,0,1, 0).getAddress()); } }; struct TestConnection { void TestHelpers() { /* Test helper functions */ // Some constants for testing u32 proto_id = 0x12345678; u16 peer_id = 123; u8 channel = 2; SharedBuffer data1(1); data1[0] = 100; Address a(127,0,0,1, 10); u16 seqnum = 34352; con::BufferedPacket p1 = con::makePacket(a, data1, proto_id, peer_id, channel); /* We should now have a packet with this data: Header: [0] u32 protocol_id [4] u16 sender_peer_id [6] u8 channel Data: [7] u8 data1[0] */ assert(readU32(&p1.data[0]) == proto_id); assert(readU16(&p1.data[4]) == peer_id); assert(readU8(&p1.data[6]) == channel); assert(readU8(&p1.data[7]) == data1[0]); //infostream<<"initial data1[0]="<<((u32)data1[0]&0xff)< p2 = con::makeReliablePacket(data1, seqnum); /*infostream<<"p2.getSize()="< data; infostream<<"** running client.Receive()"< data = SharedBufferFromString("Hello World!"); infostream<<"** running client.Send()"< recvdata; infostream<<"** running server.Receive()"< data1 = SharedBufferFromString("hello1"); SharedBuffer data2 = SharedBufferFromString("Hello2"); Address client_address = server.GetPeerAddress(peer_id_client); infostream<<"*** Sending packets in wrong order (2,1,2)" <channels[chn]; u16 sn = ch->next_outgoing_seqnum; ch->next_outgoing_seqnum = sn+1; server.Send(peer_id_client, chn, data2, true); ch->next_outgoing_seqnum = sn; server.Send(peer_id_client, chn, data1, true); ch->next_outgoing_seqnum = sn+1; server.Send(peer_id_client, chn, data2, true); sleep_ms(50); infostream<<"*** Receiving the packets"< recvdata; u32 size; infostream<<"** running client.Receive()"<20) infostream<<"..."; infostream< recvdata; infostream<<"** running client.Receive()"< 5000) break; try{ size = client.Receive(peer_id, recvdata); received = true; }catch(con::NoIncomingDataException &e){ } sleep_ms(10); } assert(received); infostream<<"** Client received: peer_id="<20) infostream<<"..."; infostream<