]> git.lizzy.rs Git - minetest.git/blobdiff - src/test.cpp
Random Lua tweaks/fixes
[minetest.git] / src / test.cpp
index c1f04b2ef2b238039ab5d20979972d20ded79ef5..f778f2d9149431122d91838daec2187e13c09999 100644 (file)
@@ -31,6 +31,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include <sstream>
 #include "porting.h"
 #include "content_mapnode.h"
+#include "nodedef.h"
 #include "mapsector.h"
 #include "settings.h"
 #include "log.h"
@@ -216,26 +217,26 @@ struct TestCompress
 
 struct TestMapNode
 {
-       void Run()
+       void Run(INodeDefManager *nodedef)
        {
                MapNode n;
 
                // Default values
                assert(n.getContent() == CONTENT_AIR);
-               assert(n.getLight(LIGHTBANK_DAY) == 0);
-               assert(n.getLight(LIGHTBANK_NIGHT) == 0);
+               assert(n.getLight(LIGHTBANK_DAY, nodedef) == 0);
+               assert(n.getLight(LIGHTBANK_NIGHT, nodedef) == 0);
                
                // Transparency
                n.setContent(CONTENT_AIR);
-               assert(n.light_propagates() == true);
-               n.setContent(CONTENT_STONE);
-               assert(n.light_propagates() == false);
+               assert(nodedef->get(n).light_propagates == true);
+               n.setContent(LEGN(nodedef, "CONTENT_STONE"));
+               assert(nodedef->get(n).light_propagates == false);
        }
 };
 
 struct TestVoxelManipulator
 {
-       void Run()
+       void Run(INodeDefManager *nodedef)
        {
                /*
                        VoxelArea
@@ -278,13 +279,13 @@ struct TestVoxelManipulator
                
                VoxelManipulator v;
 
-               v.print(infostream);
+               v.print(infostream, nodedef);
 
                infostream<<"*** Setting (-1,0,-1)=2 ***"<<std::endl;
                
                v.setNodeNoRef(v3s16(-1,0,-1), MapNode(2));
 
-               v.print(infostream);
+               v.print(infostream, nodedef);
 
                assert(v.getNode(v3s16(-1,0,-1)).getContent() == 2);
 
@@ -292,85 +293,16 @@ struct TestVoxelManipulator
 
                EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,0,-1)));
 
-               v.print(infostream);
+               v.print(infostream, nodedef);
 
                infostream<<"*** Adding area ***"<<std::endl;
 
                v.addArea(a);
                
-               v.print(infostream);
+               v.print(infostream, nodedef);
 
                assert(v.getNode(v3s16(-1,0,-1)).getContent() == 2);
                EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,1,1)));
-
-#if 0
-               /*
-                       Water stuff
-               */
-
-               v.clear();
-
-               const char *content =
-                       "#...######  "
-                       "#...##..##  "
-                       "#........ .."
-                       "############"
-
-                       "#...######  "
-                       "#...##..##  "
-                       "#........#  "
-                       "############"
-               ;
-
-               v3s16 size(12, 4, 2);
-               VoxelArea area(v3s16(0,0,0), size-v3s16(1,1,1));
-               
-               const char *p = content;
-               for(s16 z=0; z<size.Z; z++)
-               for(s16 y=size.Y-1; y>=0; y--)
-               for(s16 x=0; x<size.X; x++)
-               {
-                       MapNode n;
-                       //n.pressure = size.Y - y;
-                       if(*p == '#')
-                               n.setContent(CONTENT_STONE);
-                       else if(*p == '.')
-                               n.setContent(CONTENT_WATER);
-                       else if(*p == ' ')
-                               n.setContent(CONTENT_AIR);
-                       else
-                               assert(0);
-                       v.setNode(v3s16(x,y,z), n);
-                       p++;
-               }
-
-               v.print(infostream, VOXELPRINT_WATERPRESSURE);
-               
-               core::map<v3s16, u8> 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:"<<std::endl;
-               v.print(infostream, VOXELPRINT_WATERPRESSURE);
-#endif
-               
-               //assert(0);
        }
 };
 
@@ -461,9 +393,9 @@ struct TestMapBlock
                                MAP_BLOCKSIZE, MAP_BLOCKSIZE));*/
                
                // Changed flag should be initially set
-               assert(b.getChangedFlag() == true);
-               b.resetChangedFlag();
-               assert(b.getChangedFlag() == false);
+               assert(b.getModified() == MOD_STATE_WRITE_NEEDED);
+               b.resetModified();
+               assert(b.getModified() == MOD_STATE_CLEAN);
 
                // All nodes should have been set to
                // .d=CONTENT_IGNORE and .getLight() = 0
@@ -852,9 +784,9 @@ struct TestConnection
                try
                {
                        u16 peer_id;
-                       u8 data[100];
+                       SharedBuffer<u8> data;
                        infostream<<"** running client.Receive()"<<std::endl;
-                       u32 size = client.Receive(peer_id, data, 100);
+                       u32 size = client.Receive(peer_id, data);
                        infostream<<"** Client received: peer_id="<<peer_id
                                        <<", size="<<size
                                        <<std::endl;
@@ -874,9 +806,9 @@ struct TestConnection
                try
                {
                        u16 peer_id;
-                       u8 data[100];
+                       SharedBuffer<u8> data;
                        infostream<<"** running server.Receive()"<<std::endl;
-                       u32 size = server.Receive(peer_id, data, 100);
+                       u32 size = server.Receive(peer_id, data);
                        infostream<<"** Server received: peer_id="<<peer_id
                                        <<", size="<<size
                                        <<std::endl;
@@ -901,9 +833,9 @@ struct TestConnection
                        try
                        {
                                u16 peer_id;
-                               u8 data[100];
+                               SharedBuffer<u8> data;
                                infostream<<"** running client.Receive()"<<std::endl;
-                               u32 size = client.Receive(peer_id, data, 100);
+                               u32 size = client.Receive(peer_id, data);
                                infostream<<"** Client received: peer_id="<<peer_id
                                                <<", size="<<size
                                                <<std::endl;
@@ -919,9 +851,9 @@ struct TestConnection
                try
                {
                        u16 peer_id;
-                       u8 data[100];
+                       SharedBuffer<u8> data;
                        infostream<<"** running server.Receive()"<<std::endl;
-                       u32 size = server.Receive(peer_id, data, 100);
+                       u32 size = server.Receive(peer_id, data);
                        infostream<<"** Server received: peer_id="<<peer_id
                                        <<", size="<<size
                                        <<std::endl;
@@ -929,7 +861,10 @@ struct TestConnection
                catch(con::NoIncomingDataException &e)
                {
                }
-
+#if 1
+               /*
+                       Simple send-receive test
+               */
                {
                        /*u8 data[] = "Hello World!";
                        u32 datasize = sizeof(data);*/
@@ -941,22 +876,23 @@ struct TestConnection
                        sleep_ms(50);
 
                        u16 peer_id;
-                       u8 recvdata[100];
+                       SharedBuffer<u8> recvdata;
                        infostream<<"** running server.Receive()"<<std::endl;
-                       u32 size = server.Receive(peer_id, recvdata, 100);
+                       u32 size = server.Receive(peer_id, recvdata);
                        infostream<<"** Server received: peer_id="<<peer_id
                                        <<", size="<<size
                                        <<", data="<<*data
                                        <<std::endl;
-                       assert(memcmp(*data, recvdata, data.getSize()) == 0);
+                       assert(memcmp(*data, *recvdata, data.getSize()) == 0);
                }
-               
+#endif
                u16 peer_id_client = 2;
 #if 0
+               /*
+                       Send consequent packets in different order
+                       Not compatible with new Connection, thus commented out.
+               */
                {
-                       /*
-                               Send consequent packets in different order
-                       */
                        //u8 data1[] = "hello1";
                        //u8 data2[] = "hello2";
                        SharedBuffer<u8> data1 = SharedBufferFromString("hello1");
@@ -983,29 +919,29 @@ struct TestConnection
                        infostream<<"*** Receiving the packets"<<std::endl;
 
                        u16 peer_id;
-                       u8 recvdata[20];
+                       SharedBuffer<u8> recvdata;
                        u32 size;
 
                        infostream<<"** running client.Receive()"<<std::endl;
                        peer_id = 132;
-                       size = client.Receive(peer_id, recvdata, 20);
+                       size = client.Receive(peer_id, recvdata);
                        infostream<<"** Client received: peer_id="<<peer_id
                                        <<", size="<<size
-                                       <<", data="<<recvdata
+                                       <<", data="<<*recvdata
                                        <<std::endl;
                        assert(size == data1.getSize());
-                       assert(memcmp(*data1, recvdata, data1.getSize()) == 0);
+                       assert(memcmp(*data1, *recvdata, data1.getSize()) == 0);
                        assert(peer_id == PEER_ID_SERVER);
                        
                        infostream<<"** running client.Receive()"<<std::endl;
                        peer_id = 132;
-                       size = client.Receive(peer_id, recvdata, 20);
+                       size = client.Receive(peer_id, recvdata);
                        infostream<<"** Client received: peer_id="<<peer_id
                                        <<", size="<<size
-                                       <<", data="<<recvdata
+                                       <<", data="<<*recvdata
                                        <<std::endl;
                        assert(size == data2.getSize());
-                       assert(memcmp(*data2, recvdata, data2.getSize()) == 0);
+                       assert(memcmp(*data2, *recvdata, data2.getSize()) == 0);
                        assert(peer_id == PEER_ID_SERVER);
                        
                        bool got_exception = false;
@@ -1013,10 +949,10 @@ struct TestConnection
                        {
                                infostream<<"** running client.Receive()"<<std::endl;
                                peer_id = 132;
-                               size = client.Receive(peer_id, recvdata, 20);
+                               size = client.Receive(peer_id, recvdata);
                                infostream<<"** Client received: peer_id="<<peer_id
                                                <<", size="<<size
-                                               <<", data="<<recvdata
+                                               <<", data="<<*recvdata
                                                <<std::endl;
                        }
                        catch(con::NoIncomingDataException &e)
@@ -1027,6 +963,47 @@ struct TestConnection
                        assert(got_exception);
                }
 #endif
+#if 0
+               /*
+                       Send large amounts of packets (infinite test)
+                       Commented out because of infinity.
+               */
+               {
+                       infostream<<"Sending large amounts of packets (infinite test)"<<std::endl;
+                       int sendcount = 0;
+                       for(;;){
+                               int datasize = myrand_range(0,5)==0?myrand_range(100,10000):myrand_range(0,100);
+                               infostream<<"datasize="<<datasize<<std::endl;
+                               SharedBuffer<u8> data1(datasize);
+                               for(u16 i=0; i<datasize; i++)
+                                       data1[i] = i/4;
+                               
+                               int sendtimes = myrand_range(1,10);
+                               for(int i=0; i<sendtimes; i++){
+                                       server.Send(peer_id_client, 0, data1, true);
+                                       sendcount++;
+                               }
+                               infostream<<"sendcount="<<sendcount<<std::endl;
+                               
+                               //int receivetimes = myrand_range(1,20);
+                               int receivetimes = 20;
+                               for(int i=0; i<receivetimes; i++){
+                                       SharedBuffer<u8> recvdata;
+                                       u16 peer_id = 132;
+                                       u16 size = 0;
+                                       bool received = false;
+                                       try{
+                                               size = client.Receive(peer_id, recvdata);
+                                               received = true;
+                                       }catch(con::NoIncomingDataException &e){
+                                       }
+                               }
+                       }
+               }
+#endif
+               /*
+                       Send a large packet
+               */
                {
                        const int datasize = 30000;
                        SharedBuffer<u8> data1(datasize);
@@ -1047,7 +1024,7 @@ struct TestConnection
 
                        sleep_ms(3000);
                        
-                       u8 recvdata[datasize + 1000];
+                       SharedBuffer<u8> recvdata;
                        infostream<<"** running client.Receive()"<<std::endl;
                        u16 peer_id = 132;
                        u16 size = 0;
@@ -1057,7 +1034,7 @@ struct TestConnection
                                if(porting::getTimeMs() - timems0 > 5000)
                                        break;
                                try{
-                                       size = client.Receive(peer_id, recvdata, datasize + 1000);
+                                       size = client.Receive(peer_id, recvdata);
                                        received = true;
                                }catch(con::NoIncomingDataException &e){
                                }
@@ -1071,13 +1048,13 @@ struct TestConnection
                        infostream<<"Received data (size="<<size<<"):";
                        for(int i=0; i<size && i<20; i++){
                                if(i%2==0) DEBUGPRINT(" ");
-                               DEBUGPRINT("%.2X", ((int)((const char*)recvdata)[i])&0xff);
+                               DEBUGPRINT("%.2X", ((int)(recvdata[i]))&0xff);
                        }
                        if(size>20)
                                infostream<<"...";
                        infostream<<std::endl;
 
-                       assert(memcmp(*data1, recvdata, data1.getSize()) == 0);
+                       assert(memcmp(*data1, *recvdata, data1.getSize()) == 0);
                        assert(peer_id == PEER_ID_SERVER);
                }
                
@@ -1098,15 +1075,27 @@ struct TestConnection
        x.Run();\
 }
 
+#define TESTPARAMS(X, ...)\
+{\
+       X x;\
+       infostream<<"Running " #X <<std::endl;\
+       x.Run(__VA_ARGS__);\
+}
+
 void run_tests()
 {
        DSTACK(__FUNCTION_NAME);
+       
+       // Create node definitions
+       IWritableNodeDefManager *nodedef = createNodeDefManager();
+       content_mapnode_init(nodedef);
+
        infostream<<"run_tests() started"<<std::endl;
        TEST(TestUtilities);
        TEST(TestSettings);
        TEST(TestCompress);
-       TEST(TestMapNode);
-       TEST(TestVoxelManipulator);
+       TESTPARAMS(TestMapNode, nodedef);
+       TESTPARAMS(TestVoxelManipulator, nodedef);
        //TEST(TestMapBlock);
        //TEST(TestMapSector);
        if(INTERNET_SIMULATOR == false){