]> git.lizzy.rs Git - minetest.git/blobdiff - src/test.cpp
Set the BS constant to be floating-point; this removes the need for floating point...
[minetest.git] / src / test.cpp
index 829aec8c15eac5098bbf8b7eaffc28f531b2690e..37412d179e5f0a111723d0b11136eb2dbf11f365 100644 (file)
@@ -23,21 +23,17 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "map.h"
 #include "player.h"
 #include "main.h"
-#include "heightmap.h"
 #include "socket.h"
 #include "connection.h"
 #include "utility.h"
 #include "serialization.h"
 #include "voxel.h"
 #include <sstream>
-
-#ifdef _WIN32
-       #include <windows.h>
-       #define sleep_ms(x) Sleep(x)
-#else
-       #include <unistd.h>
-       #define sleep_ms(x) usleep(x*1000)
-#endif
+#include "porting.h"
+#include "content_mapnode.h"
+#include "mapsector.h"
+#include "settings.h"
+#include "log.h"
 
 /*
        Asserts that the exception occurs
@@ -54,9 +50,9 @@ struct TestUtilities
 {
        void Run()
        {
-               /*dstream<<"wrapDegrees(100.0) = "<<wrapDegrees(100.0)<<std::endl;
-               dstream<<"wrapDegrees(720.5) = "<<wrapDegrees(720.5)<<std::endl;
-               dstream<<"wrapDegrees(-0.5) = "<<wrapDegrees(-0.5)<<std::endl;*/
+               /*infostream<<"wrapDegrees(100.0) = "<<wrapDegrees(100.0)<<std::endl;
+               infostream<<"wrapDegrees(720.5) = "<<wrapDegrees(720.5)<<std::endl;
+               infostream<<"wrapDegrees(-0.5) = "<<wrapDegrees(-0.5)<<std::endl;*/
                assert(fabs(wrapDegrees(100.0) - 100.0) < 0.001);
                assert(fabs(wrapDegrees(720.5) - 0.5) < 0.001);
                assert(fabs(wrapDegrees(-0.5) - (-0.5)) < 0.001);
@@ -67,11 +63,45 @@ struct TestUtilities
                assert(is_yes("FAlse") == false);
        }
 };
+
+struct TestSettings
+{
+       void Run()
+       {
+               Settings s;
+               // Test reading of settings
+               s.parseConfigLine("leet = 1337");
+               s.parseConfigLine("leetleet = 13371337");
+               s.parseConfigLine("leetleet_neg = -13371337");
+               s.parseConfigLine("floaty_thing = 1.1");
+               s.parseConfigLine("stringy_thing = asd /( ¤%&(/\" BLÖÄRP");
+               s.parseConfigLine("coord = (1, 2, 4.5)");
+               assert(s.getS32("leet") == 1337);
+               assert(s.getS16("leetleet") == 32767);
+               assert(s.getS16("leetleet_neg") == -32768);
+               // Not sure if 1.1 is an exact value as a float, but doesn't matter
+               assert(fabs(s.getFloat("floaty_thing") - 1.1) < 0.001);
+               assert(s.get("stringy_thing") == "asd /( ¤%&(/\" BLÖÄRP");
+               assert(fabs(s.getV3F("coord").X - 1.0) < 0.001);
+               assert(fabs(s.getV3F("coord").Y - 2.0) < 0.001);
+               assert(fabs(s.getV3F("coord").Z - 4.5) < 0.001);
+               // Test the setting of settings too
+               s.setFloat("floaty_thing_2", 1.2);
+               s.setV3F("coord2", v3f(1, 2, 3.3));
+               assert(s.get("floaty_thing_2").substr(0,3) == "1.2");
+               assert(fabs(s.getFloat("floaty_thing_2") - 1.2) < 0.001);
+               assert(fabs(s.getV3F("coord2").X - 1.0) < 0.001);
+               assert(fabs(s.getV3F("coord2").Y - 2.0) < 0.001);
+               assert(fabs(s.getV3F("coord2").Z - 3.3) < 0.001);
+       }
+};
                
 struct TestCompress
 {
        void Run()
        {
+               { // ver 0
+
                SharedBuffer<u8> fromdata(4);
                fromdata[0]=1;
                fromdata[1]=5;
@@ -83,13 +113,13 @@ struct TestCompress
 
                std::string str_out = os.str();
                
-               dstream<<"str_out.size()="<<str_out.size()<<std::endl;
-               dstream<<"TestCompress: 1,5,5,1 -> ";
+               infostream<<"str_out.size()="<<str_out.size()<<std::endl;
+               infostream<<"TestCompress: 1,5,5,1 -> ";
                for(u32 i=0; i<str_out.size(); i++)
                {
-                       dstream<<(u32)str_out[i]<<",";
+                       infostream<<(u32)str_out[i]<<",";
                }
-               dstream<<std::endl;
+               infostream<<std::endl;
 
                assert(str_out.size() == 10);
 
@@ -110,12 +140,12 @@ struct TestCompress
                decompress(is, os2, 0);
                std::string str_out2 = os2.str();
 
-               dstream<<"decompress: ";
+               infostream<<"decompress: ";
                for(u32 i=0; i<str_out2.size(); i++)
                {
-                       dstream<<(u32)str_out2[i]<<",";
+                       infostream<<(u32)str_out2[i]<<",";
                }
-               dstream<<std::endl;
+               infostream<<std::endl;
 
                assert(str_out2.size() == fromdata.getSize());
 
@@ -123,6 +153,64 @@ struct TestCompress
                {
                        assert(str_out2[i] == fromdata[i]);
                }
+
+               }
+
+               { // ver HIGHEST
+
+               SharedBuffer<u8> 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()="<<str_out.size()<<std::endl;
+               infostream<<"TestCompress: 1,5,5,1 -> ";
+               for(u32 i=0; i<str_out.size(); i++)
+               {
+                       infostream<<(u32)str_out[i]<<",";
+               }
+               infostream<<std::endl;
+
+               /*assert(str_out.size() == 10);
+
+               assert(str_out[0] == 0);
+               assert(str_out[1] == 0);
+               assert(str_out[2] == 0);
+               assert(str_out[3] == 4);
+               assert(str_out[4] == 0);
+               assert(str_out[5] == 1);
+               assert(str_out[6] == 1);
+               assert(str_out[7] == 5);
+               assert(str_out[8] == 0);
+               assert(str_out[9] == 1);*/
+
+               std::istringstream is(str_out, std::ios_base::binary);
+               std::ostringstream os2(std::ios_base::binary);
+
+               decompress(is, os2, SER_FMT_VER_HIGHEST);
+               std::string str_out2 = os2.str();
+
+               infostream<<"decompress: ";
+               for(u32 i=0; i<str_out2.size(); i++)
+               {
+                       infostream<<(u32)str_out2[i]<<",";
+               }
+               infostream<<std::endl;
+
+               assert(str_out2.size() == fromdata.getSize());
+
+               for(u32 i=0; i<str_out2.size(); i++)
+               {
+                       assert(str_out2[i] == fromdata[i]);
+               }
+
+               }
        }
 };
 
@@ -133,13 +221,14 @@ struct TestMapNode
                MapNode n;
 
                // Default values
-               assert(n.d == MATERIAL_AIR);
-               assert(n.getLight() == 0);
+               assert(n.getContent() == CONTENT_AIR);
+               assert(n.getLight(LIGHTBANK_DAY) == 0);
+               assert(n.getLight(LIGHTBANK_NIGHT) == 0);
                
                // Transparency
-               n.d = MATERIAL_AIR;
+               n.setContent(CONTENT_AIR);
                assert(n.light_propagates() == true);
-               n.d = 0;
+               n.setContent(CONTENT_STONE);
                assert(n.light_propagates() == false);
        }
 };
@@ -170,12 +259,12 @@ struct TestVoxelManipulator
 
                assert(aa.size() == results.size());
                
-               dstream<<"Result of diff:"<<std::endl;
+               infostream<<"Result of diff:"<<std::endl;
                for(core::list<VoxelArea>::Iterator
                                i = aa.begin(); i != aa.end(); i++)
                {
-                       i->print(dstream);
-                       dstream<<std::endl;
+                       i->print(infostream);
+                       infostream<<std::endl;
                        
                        s32 j = results.linear_search(*i);
                        assert(j != -1);
@@ -189,31 +278,32 @@ struct TestVoxelManipulator
                
                VoxelManipulator v;
 
-               v.print(dstream);
+               v.print(infostream);
 
-               dstream<<"*** Setting (-1,0,-1)=2 ***"<<std::endl;
+               infostream<<"*** Setting (-1,0,-1)=2 ***"<<std::endl;
                
                v.setNodeNoRef(v3s16(-1,0,-1), MapNode(2));
 
-               v.print(dstream);
+               v.print(infostream);
 
-               assert(v.getNode(v3s16(-1,0,-1)).d == 2);
+               assert(v.getNode(v3s16(-1,0,-1)).getContent() == 2);
 
-               dstream<<"*** Reading from inexistent (0,0,-1) ***"<<std::endl;
+               infostream<<"*** Reading from inexistent (0,0,-1) ***"<<std::endl;
 
                EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,0,-1)));
 
-               v.print(dstream);
+               v.print(infostream);
 
-               dstream<<"*** Adding area ***"<<std::endl;
+               infostream<<"*** Adding area ***"<<std::endl;
 
                v.addArea(a);
                
-               v.print(dstream);
+               v.print(infostream);
 
-               assert(v.getNode(v3s16(-1,0,-1)).d == 2);
+               assert(v.getNode(v3s16(-1,0,-1)).getContent() == 2);
                EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,1,1)));
 
+#if 0
                /*
                        Water stuff
                */
@@ -243,42 +333,53 @@ struct TestVoxelManipulator
                        MapNode n;
                        //n.pressure = size.Y - y;
                        if(*p == '#')
-                               n.d = MATERIAL_STONE;
+                               n.setContent(CONTENT_STONE);
                        else if(*p == '.')
-                               n.d = MATERIAL_WATER;
+                               n.setContent(CONTENT_WATER);
                        else if(*p == ' ')
-                               n.d = MATERIAL_AIR;
+                               n.setContent(CONTENT_AIR);
                        else
                                assert(0);
                        v.setNode(v3s16(x,y,z), n);
                        p++;
                }
 
-               v.print(dstream, VOXELPRINT_WATERPRESSURE);
+               v.print(infostream, VOXELPRINT_WATERPRESSURE);
                
                core::map<v3s16, u8> active_nodes;
                v.updateAreaWaterPressure(area, active_nodes);
 
-               v.print(dstream, VOXELPRINT_WATERPRESSURE);
+               v.print(infostream, VOXELPRINT_WATERPRESSURE);
                
-               s16 highest_y = -32768;
-               assert(v.getWaterPressure(v3s16(7, 1, 1), highest_y, 0) == -1);
-               assert(highest_y == 3);
+               //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, false);
-               v.flowWater(active_nodes, 0, true, 1000);
+               //v.flowWater(active_nodes, 0, true, 1000);
+               v.flowWater(active_nodes, 0, false, 1000);
                
-               dstream<<"Final result of flowWater:"<<std::endl;
-               v.print(dstream, VOXELPRINT_WATERPRESSURE);
+               infostream<<"Final result of flowWater:"<<std::endl;
+               v.print(infostream, VOXELPRINT_WATERPRESSURE);
+#endif
                
                //assert(0);
        }
 };
 
+/*
+       NOTE: These tests became non-working then NodeContainer was removed.
+             These should be redone, utilizing some kind of a virtual
+                 interface for Map (IMap would be fine).
+*/
+#if 0
 struct TestMapBlock
 {
        class TC : public NodeContainer
@@ -365,19 +466,32 @@ struct TestMapBlock
                assert(b.getChangedFlag() == false);
 
                // All nodes should have been set to
-               // .d=MATERIAL_AIR and .getLight() = 0
+               // .d=CONTENT_IGNORE and .getLight() = 0
                for(u16 z=0; z<MAP_BLOCKSIZE; z++)
-                       for(u16 y=0; y<MAP_BLOCKSIZE; y++)
-                               for(u16 x=0; x<MAP_BLOCKSIZE; x++){
-                                       assert(b.getNode(v3s16(x,y,z)).d == MATERIAL_AIR);
-                                       assert(b.getNode(v3s16(x,y,z)).getLight() == 0);
-                               }
+               for(u16 y=0; y<MAP_BLOCKSIZE; y++)
+               for(u16 x=0; x<MAP_BLOCKSIZE; x++)
+               {
+                       //assert(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_AIR);
+                       assert(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_IGNORE);
+                       assert(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_DAY) == 0);
+                       assert(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_NIGHT) == 0);
+               }
                
+               {
+                       MapNode n(CONTENT_AIR);
+                       for(u16 z=0; z<MAP_BLOCKSIZE; z++)
+                       for(u16 y=0; y<MAP_BLOCKSIZE; y++)
+                       for(u16 x=0; x<MAP_BLOCKSIZE; x++)
+                       {
+                               b.setNode(v3s16(x,y,z), n);
+                       }
+               }
+                       
                /*
                        Parent fetch functions
                */
                parent.position_valid = false;
-               parent.node.d = 5;
+               parent.node.setContent(5);
 
                MapNode n;
                
@@ -385,7 +499,7 @@ struct TestMapBlock
                assert(b.isValidPositionParent(v3s16(0,0,0)) == true);
                assert(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
                n = b.getNodeParent(v3s16(0,MAP_BLOCKSIZE-1,0));
-               assert(n.d == MATERIAL_AIR);
+               assert(n.getContent() == CONTENT_AIR);
 
                // ...but outside the block they should be invalid
                assert(b.isValidPositionParent(v3s16(-121,2341,0)) == false);
@@ -411,17 +525,18 @@ struct TestMapBlock
                assert(b.isValidPositionParent(v3s16(-1,0,0)) == true);
                assert(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == true);
                n = b.getNodeParent(v3s16(0,0,MAP_BLOCKSIZE));
-               assert(n.d == 5);
+               assert(n.getContent() == 5);
 
                /*
                        Set a node
                */
                v3s16 p(1,2,0);
-               n.d = 4;
+               n.setContent(4);
                b.setNode(p, n);
-               assert(b.getNode(p).d == 4);
-               assert(b.getNodeMaterial(p) == 4);
-               assert(b.getNodeMaterial(v3s16(-1,-1,0)) == 5);
+               assert(b.getNode(p).getContent() == 4);
+               //TODO: Update to new system
+               /*assert(b.getNodeTile(p) == 4);
+               assert(b.getNodeTile(v3s16(-1,-1,0)) == 5);*/
                
                /*
                        propagateSunlight()
@@ -431,7 +546,8 @@ struct TestMapBlock
                        for(u16 y=0; y<MAP_BLOCKSIZE; y++){
                                for(u16 x=0; x<MAP_BLOCKSIZE; x++){
                                        MapNode n = b.getNode(v3s16(x,y,z));
-                                       n.setLight(0);
+                                       n.setLight(LIGHTBANK_DAY, 0);
+                                       n.setLight(LIGHTBANK_NIGHT, 0);
                                        b.setNode(v3s16(x,y,z), n);
                                }
                        }
@@ -442,22 +558,26 @@ struct TestMapBlock
                        */
                        parent.position_valid = true;
                        b.setIsUnderground(false);
-                       parent.node.d = MATERIAL_AIR;
-                       parent.node.setLight(LIGHT_SUN);
+                       parent.node.setContent(CONTENT_AIR);
+                       parent.node.setLight(LIGHTBANK_DAY, LIGHT_SUN);
+                       parent.node.setLight(LIGHTBANK_NIGHT, 0);
                        core::map<v3s16, bool> 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() == LIGHT_SUN);
-                       assert(b.getNode(v3s16(1,3,0)).getLight() == LIGHT_SUN);
-                       assert(b.getNode(v3s16(1,2,0)).getLight() == 0);
-                       assert(b.getNode(v3s16(1,1,0)).getLight() == 0);
-                       assert(b.getNode(v3s16(1,0,0)).getLight() == 0);
-                       assert(b.getNode(v3s16(1,2,3)).getLight() == LIGHT_SUN);
-                       assert(b.getFaceLight(p, v3s16(0,1,0)) == LIGHT_SUN);
-                       assert(b.getFaceLight(p, v3s16(0,-1,0)) == 0);
+                       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)));
+                       //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
@@ -467,15 +587,15 @@ struct TestMapBlock
                        // Make neighbours to exist and set some non-sunlight to them
                        parent.position_valid = true;
                        b.setIsUnderground(true);
-                       parent.node.setLight(LIGHT_MAX/2);
+                       parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
                        core::map<v3s16, bool> light_sources;
                        // The block below should be valid because there shouldn't be
                        // sunlight in there either
-                       assert(b.propagateSunlight(light_sources) == true);
+                       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() == 0);
+                       assert(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == 0);
                }
                /*
                        Set up a situation where:
@@ -493,8 +613,8 @@ struct TestMapBlock
                                for(u16 y=0; y<MAP_BLOCKSIZE; y++){
                                        for(u16 x=0; x<MAP_BLOCKSIZE; x++){
                                                MapNode n;
-                                               n.d = MATERIAL_AIR;
-                                               n.setLight(0);
+                                               n.setContent(CONTENT_AIR);
+                                               n.setLight(LIGHTBANK_DAY, 0);
                                                b.setNode(v3s16(x,y,z), n);
                                        }
                                }
@@ -508,7 +628,7 @@ struct TestMapBlock
                                parent.validity_exceptions.push_back(v3s16(MAP_BLOCKSIZE+x, MAP_BLOCKSIZE-1, MAP_BLOCKSIZE+z));
                        }
                        // Lighting value for the valid nodes
-                       parent.node.setLight(LIGHT_MAX/2);
+                       parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
                        core::map<v3s16, bool> light_sources;
                        // Bottom block is not valid
                        assert(b.propagateSunlight(light_sources) == false);
@@ -560,17 +680,15 @@ struct TestMapSector
                parent.position_valid = false;
                
                // Create one with no heightmaps
-               ServerMapSector sector(&parent, v2s16(1,1), 0);
-               //ConstantGenerator *dummyheightmap = new ConstantGenerator();
-               //sector->setHeightmap(dummyheightmap);
+               ServerMapSector sector(&parent, v2s16(1,1));
                
-               EXCEPTION_CHECK(InvalidPositionException, sector.getBlockNoCreate(0));
-               EXCEPTION_CHECK(InvalidPositionException, sector.getBlockNoCreate(1));
+               assert(sector.getBlockNoCreateNoEx(0) == 0);
+               assert(sector.getBlockNoCreateNoEx(1) == 0);
 
                MapBlock * bref = sector.createBlankBlock(-2);
                
-               EXCEPTION_CHECK(InvalidPositionException, sector.getBlockNoCreate(0));
-               assert(sector.getBlockNoCreate(-2) == bref);
+               assert(sector.getBlockNoCreateNoEx(0) == 0);
+               assert(sector.getBlockNoCreateNoEx(-2) == bref);
                
                //TODO: Check for AlreadyExistsException
 
@@ -585,116 +703,7 @@ struct TestMapSector
 
        }
 };
-
-struct TestHeightmap
-{
-       void TestSingleFixed()
-       {
-               const s16 BS1 = 4;
-               OneChildHeightmap hm1(BS1);
-               
-               // Test that it is filled with < GROUNDHEIGHT_VALID_MINVALUE
-               for(s16 y=0; y<=BS1; y++){
-                       for(s16 x=0; x<=BS1; x++){
-                               v2s16 p(x,y);
-                               assert(hm1.m_child.getGroundHeight(p)
-                                       < GROUNDHEIGHT_VALID_MINVALUE);
-                       }
-               }
-
-               hm1.m_child.setGroundHeight(v2s16(1,0), 2.0);
-               //hm1.m_child.print();
-               assert(fabs(hm1.getGroundHeight(v2s16(1,0))-2.0)<0.001);
-               hm1.setGroundHeight(v2s16(0,1), 3.0);
-               assert(fabs(hm1.m_child.getGroundHeight(v2s16(0,1))-3.0)<0.001);
-               
-               // Fill with -1.0
-               for(s16 y=0; y<=BS1; y++){
-                       for(s16 x=0; x<=BS1; x++){
-                               v2s16 p(x,y);
-                               hm1.m_child.setGroundHeight(p, -1.0);
-                       }
-               }
-
-               f32 corners[] = {0.0, 0.0, 1.0, 1.0};
-               hm1.m_child.generateContinued(0.0, 0.0, corners);
-               
-               hm1.m_child.print();
-               assert(fabs(hm1.m_child.getGroundHeight(v2s16(1,0))-0.2)<0.05);
-               assert(fabs(hm1.m_child.getGroundHeight(v2s16(4,3))-0.7)<0.05);
-               assert(fabs(hm1.m_child.getGroundHeight(v2s16(4,4))-1.0)<0.05);
-       }
-
-       void TestUnlimited()
-       {
-               //g_heightmap_debugprint = true;
-               const s16 BS1 = 4;
-               UnlimitedHeightmap hm1(BS1,
-                               new ConstantGenerator(0.0),
-                               new ConstantGenerator(0.0),
-                               new ConstantGenerator(5.0));
-               // Go through it so it generates itself
-               for(s16 y=0; y<=BS1; y++){
-                       for(s16 x=0; x<=BS1; x++){
-                               v2s16 p(x,y);
-                               hm1.getGroundHeight(p);
-                       }
-               }
-               // Print it
-               dstream<<"UnlimitedHeightmap hm1:"<<std::endl;
-               hm1.print();
-               
-               dstream<<"testing UnlimitedHeightmap set/get"<<std::endl;
-               v2s16 p1(0,3);
-               f32 v1(234.01);
-               // Get first heightmap and try setGroundHeight
-               FixedHeightmap * href = hm1.getHeightmap(v2s16(0,0));
-               href->setGroundHeight(p1, v1);
-               // Read from UnlimitedHeightmap
-               assert(fabs(hm1.getGroundHeight(p1)-v1)<0.001);
-       }
-       
-       void Random()
-       {
-               dstream<<"Running random code (get a human to check this)"<<std::endl;
-               dstream<<"rand() values: ";
-               for(u16 i=0; i<5; i++)
-                       dstream<<(u16)rand()<<" ";
-               dstream<<std::endl;
-
-               const s16 BS1 = 8;
-               UnlimitedHeightmap hm1(BS1,
-                               new ConstantGenerator(10.0),
-                               new ConstantGenerator(0.3),
-                               new ConstantGenerator(0.0));
-
-               // Force hm1 to generate a some heightmap
-               hm1.getGroundHeight(v2s16(0,0));
-               hm1.getGroundHeight(v2s16(0,BS1));
-               /*hm1.getGroundHeight(v2s16(BS1,-1));
-               hm1.getGroundHeight(v2s16(BS1-1,-1));*/
-               hm1.print();
-
-               // Get the (0,0) and (1,0) heightmaps
-               /*FixedHeightmap * hr00 = hm1.getHeightmap(v2s16(0,0));
-               FixedHeightmap * hr01 = hm1.getHeightmap(v2s16(1,0));
-               f32 corners[] = {1.0, 1.0, 1.0, 1.0};
-               hr00->generateContinued(0.0, 0.0, corners);
-               hm1.print();*/
-
-               //assert(0);
-       }
-
-       void Run()
-       {
-               //srand(7); // Get constant random
-               srand(time(0)); // Get better random
-
-               TestSingleFixed();
-               TestUnlimited();
-               Random();
-       }
-};
+#endif
 
 struct TestSocket
 {
@@ -757,15 +766,15 @@ struct TestConnection
                assert(readU8(&p1.data[6]) == channel);
                assert(readU8(&p1.data[7]) == data1[0]);
                
-               //dstream<<"initial data1[0]="<<((u32)data1[0]&0xff)<<std::endl;
+               //infostream<<"initial data1[0]="<<((u32)data1[0]&0xff)<<std::endl;
 
                SharedBuffer<u8> p2 = con::makeReliablePacket(data1, seqnum);
 
-               /*dstream<<"p2.getSize()="<<p2.getSize()<<", data1.getSize()="
+               /*infostream<<"p2.getSize()="<<p2.getSize()<<", data1.getSize()="
                                <<data1.getSize()<<std::endl;
-               dstream<<"readU8(&p2[3])="<<readU8(&p2[3])
+               infostream<<"readU8(&p2[3])="<<readU8(&p2[3])
                                <<" p2[3]="<<((u32)p2[3]&0xff)<<std::endl;
-               dstream<<"data1[0]="<<((u32)data1[0]&0xff)<<std::endl;*/
+               infostream<<"data1[0]="<<((u32)data1[0]&0xff)<<std::endl;*/
 
                assert(p2.getSize() == 3 + data1.getSize());
                assert(readU8(&p2[0]) == TYPE_RELIABLE);
@@ -783,14 +792,14 @@ struct TestConnection
                }
                void peerAdded(con::Peer *peer)
                {
-                       dstream<<"Handler("<<name<<")::peerAdded(): "
+                       infostream<<"Handler("<<name<<")::peerAdded(): "
                                        "id="<<peer->id<<std::endl;
                        last_id = peer->id;
                        count++;
                }
                void deletingPeer(con::Peer *peer, bool timeout)
                {
-                       dstream<<"Handler("<<name<<")::deletingPeer(): "
+                       infostream<<"Handler("<<name<<")::deletingPeer(): "
                                        "id="<<peer->id
                                        <<", timeout="<<timeout<<std::endl;
                        last_id = peer->id;
@@ -810,17 +819,20 @@ struct TestConnection
 
                /*
                        Test some real connections
+
+                       NOTE: This mostly tests the legacy interface.
                */
+
                u32 proto_id = 0xad26846a;
 
                Handler hand_server("server");
                Handler hand_client("client");
                
-               dstream<<"** Creating server Connection"<<std::endl;
+               infostream<<"** Creating server Connection"<<std::endl;
                con::Connection server(proto_id, 512, 5.0, &hand_server);
                server.Serve(30001);
                
-               dstream<<"** Creating client Connection"<<std::endl;
+               infostream<<"** Creating client Connection"<<std::endl;
                con::Connection client(proto_id, 512, 5.0, &hand_client);
 
                assert(hand_server.count == 0);
@@ -829,24 +841,43 @@ struct TestConnection
                sleep_ms(50);
                
                Address server_address(127,0,0,1, 30001);
-               dstream<<"** running client.Connect()"<<std::endl;
+               infostream<<"** running client.Connect()"<<std::endl;
                client.Connect(server_address);
 
                sleep_ms(50);
                
+               // Client should not have added client yet
+               assert(hand_client.count == 0);
+               
+               try
+               {
+                       u16 peer_id;
+                       SharedBuffer<u8> data;
+                       infostream<<"** running client.Receive()"<<std::endl;
+                       u32 size = client.Receive(peer_id, data);
+                       infostream<<"** Client received: peer_id="<<peer_id
+                                       <<", size="<<size
+                                       <<std::endl;
+               }
+               catch(con::NoIncomingDataException &e)
+               {
+               }
+
                // Client should have added server now
                assert(hand_client.count == 1);
                assert(hand_client.last_id == 1);
-               // But server should not have added client
+               // Server should not have added client yet
                assert(hand_server.count == 0);
+               
+               sleep_ms(50);
 
                try
                {
                        u16 peer_id;
-                       u8 data[100];
-                       dstream<<"** running server.Receive()"<<std::endl;
-                       u32 size = server.Receive(peer_id, data, 100);
-                       dstream<<"** Server received: peer_id="<<peer_id
+                       SharedBuffer<u8> data;
+                       infostream<<"** running server.Receive()"<<std::endl;
+                       u32 size = server.Receive(peer_id, data);
+                       infostream<<"** Server received: peer_id="<<peer_id
                                        <<", size="<<size
                                        <<std::endl;
                }
@@ -870,10 +901,10 @@ struct TestConnection
                        try
                        {
                                u16 peer_id;
-                               u8 data[100];
-                               dstream<<"** running client.Receive()"<<std::endl;
-                               u32 size = client.Receive(peer_id, data, 100);
-                               dstream<<"** Client received: peer_id="<<peer_id
+                               SharedBuffer<u8> data;
+                               infostream<<"** running client.Receive()"<<std::endl;
+                               u32 size = client.Receive(peer_id, data);
+                               infostream<<"** Client received: peer_id="<<peer_id
                                                <<", size="<<size
                                                <<std::endl;
                        }
@@ -888,57 +919,61 @@ struct TestConnection
                try
                {
                        u16 peer_id;
-                       u8 data[100];
-                       dstream<<"** running server.Receive()"<<std::endl;
-                       u32 size = server.Receive(peer_id, data, 100);
-                       dstream<<"** Server received: peer_id="<<peer_id
+                       SharedBuffer<u8> data;
+                       infostream<<"** running server.Receive()"<<std::endl;
+                       u32 size = server.Receive(peer_id, data);
+                       infostream<<"** Server received: peer_id="<<peer_id
                                        <<", size="<<size
                                        <<std::endl;
                }
                catch(con::NoIncomingDataException &e)
                {
                }
-
+#if 1
+               /*
+                       Simple send-receive test
+               */
                {
                        /*u8 data[] = "Hello World!";
                        u32 datasize = sizeof(data);*/
                        SharedBuffer<u8> data = SharedBufferFromString("Hello World!");
 
-                       dstream<<"** running client.Send()"<<std::endl;
+                       infostream<<"** running client.Send()"<<std::endl;
                        client.Send(PEER_ID_SERVER, 0, data, true);
 
                        sleep_ms(50);
 
                        u16 peer_id;
-                       u8 recvdata[100];
-                       dstream<<"** running server.Receive()"<<std::endl;
-                       u32 size = server.Receive(peer_id, recvdata, 100);
-                       dstream<<"** Server received: peer_id="<<peer_id
+                       SharedBuffer<u8> recvdata;
+                       infostream<<"** running server.Receive()"<<std::endl;
+                       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");
                        SharedBuffer<u8> data2 = SharedBufferFromString("Hello2");
 
                        Address client_address =
-                                       server.GetPeer(peer_id_client)->address;
+                                       server.GetPeerAddress(peer_id_client);
                        
-                       dstream<<"*** Sending packets in wrong order (2,1,2)"
+                       infostream<<"*** Sending packets in wrong order (2,1,2)"
                                        <<std::endl;
                        
                        u8 chn = 0;
-                       con::Channel *ch = &server.GetPeer(peer_id_client)->channels[chn];
+                       con::Channel *ch = &server.getPeer(peer_id_client)->channels[chn];
                        u16 sn = ch->next_outgoing_seqnum;
                        ch->next_outgoing_seqnum = sn+1;
                        server.Send(peer_id_client, chn, data2, true);
@@ -949,90 +984,145 @@ struct TestConnection
 
                        sleep_ms(50);
 
-                       dstream<<"*** Receiving the packets"<<std::endl;
+                       infostream<<"*** Receiving the packets"<<std::endl;
 
                        u16 peer_id;
-                       u8 recvdata[20];
+                       SharedBuffer<u8> recvdata;
                        u32 size;
 
-                       dstream<<"** running client.Receive()"<<std::endl;
+                       infostream<<"** running client.Receive()"<<std::endl;
                        peer_id = 132;
-                       size = client.Receive(peer_id, recvdata, 20);
-                       dstream<<"** Client received: peer_id="<<peer_id
+                       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);
                        
-                       dstream<<"** running client.Receive()"<<std::endl;
+                       infostream<<"** running client.Receive()"<<std::endl;
                        peer_id = 132;
-                       size = client.Receive(peer_id, recvdata, 20);
-                       dstream<<"** Client received: peer_id="<<peer_id
+                       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;
                        try
                        {
-                               dstream<<"** running client.Receive()"<<std::endl;
+                               infostream<<"** running client.Receive()"<<std::endl;
                                peer_id = 132;
-                               size = client.Receive(peer_id, recvdata, 20);
-                               dstream<<"** Client received: peer_id="<<peer_id
+                               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)
                        {
-                               dstream<<"** No incoming data for client"<<std::endl;
+                               infostream<<"** No incoming data for client"<<std::endl;
                                got_exception = true;
                        }
                        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
+               */
                {
-                       //u8 data1[1100];
-                       SharedBuffer<u8> data1(1100);
-                       for(u16 i=0; i<1100; i++){
+                       const int datasize = 30000;
+                       SharedBuffer<u8> data1(datasize);
+                       for(u16 i=0; i<datasize; i++){
                                data1[i] = i/4;
                        }
 
-                       dstream<<"Sending data (size="<<1100<<"):";
-                       for(int i=0; i<1100 && i<20; i++){
-                               if(i%2==0) printf(" ");
-                               printf("%.2X", ((int)((const char*)*data1)[i])&0xff);
+                       infostream<<"Sending data (size="<<datasize<<"):";
+                       for(int i=0; i<datasize && i<20; i++){
+                               if(i%2==0) DEBUGPRINT(" ");
+                               DEBUGPRINT("%.2X", ((int)((const char*)*data1)[i])&0xff);
                        }
-                       if(1100>20)
-                               dstream<<"...";
-                       dstream<<std::endl;
+                       if(datasize>20)
+                               infostream<<"...";
+                       infostream<<std::endl;
                        
                        server.Send(peer_id_client, 0, data1, true);
 
-                       sleep_ms(50);
+                       sleep_ms(3000);
                        
-                       u8 recvdata[2000];
-                       dstream<<"** running client.Receive()"<<std::endl;
+                       SharedBuffer<u8> recvdata;
+                       infostream<<"** running client.Receive()"<<std::endl;
                        u16 peer_id = 132;
-                       u16 size = client.Receive(peer_id, recvdata, 2000);
-                       dstream<<"** Client received: peer_id="<<peer_id
+                       u16 size = 0;
+                       bool received = false;
+                       u32 timems0 = porting::getTimeMs();
+                       for(;;){
+                               if(porting::getTimeMs() - timems0 > 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="<<peer_id
                                        <<", size="<<size
                                        <<std::endl;
 
-                       dstream<<"Received data (size="<<size<<"):";
+                       infostream<<"Received data (size="<<size<<"):";
                        for(int i=0; i<size && i<20; i++){
-                               if(i%2==0) printf(" ");
-                               printf("%.2X", ((int)((const char*)recvdata)[i])&0xff);
+                               if(i%2==0) DEBUGPRINT(" ");
+                               DEBUGPRINT("%.2X", ((int)(recvdata[i]))&0xff);
                        }
                        if(size>20)
-                               dstream<<"...";
-                       dstream<<std::endl;
+                               infostream<<"...";
+                       infostream<<std::endl;
 
-                       assert(memcmp(*data1, recvdata, data1.getSize()) == 0);
+                       assert(memcmp(*data1, *recvdata, data1.getSize()) == 0);
                        assert(peer_id == PEER_ID_SERVER);
                }
                
@@ -1049,27 +1139,27 @@ struct TestConnection
 #define TEST(X)\
 {\
        X x;\
-       dstream<<"Running " #X <<std::endl;\
+       infostream<<"Running " #X <<std::endl;\
        x.Run();\
 }
 
 void run_tests()
 {
        DSTACK(__FUNCTION_NAME);
-       dstream<<"run_tests() started"<<std::endl;
+       infostream<<"run_tests() started"<<std::endl;
        TEST(TestUtilities);
+       TEST(TestSettings);
        TEST(TestCompress);
        TEST(TestMapNode);
        TEST(TestVoxelManipulator);
-       TEST(TestMapBlock);
-       TEST(TestMapSector);
-       TEST(TestHeightmap);
+       //TEST(TestMapBlock);
+       //TEST(TestMapSector);
        if(INTERNET_SIMULATOR == false){
                TEST(TestSocket);
                dout_con<<"=== BEGIN RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
                TEST(TestConnection);
                dout_con<<"=== END RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
        }
-       dstream<<"run_tests() passed"<<std::endl;
+       infostream<<"run_tests() passed"<<std::endl;
 }