]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/test.cpp
Update Lua API documentation to include minetest.get_modnames()
[dragonfireclient.git] / src / test.cpp
index 6588f113c4d622975685cf7d29f43f48c85948cd..9b1346274a4c4613c4af401b50c94a7360ddb22a 100644 (file)
@@ -3,31 +3,31 @@ Minetest-c55
 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
 
 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
+it under the terms of the GNU Lesser General Public License as published by
+the Free Software Foundation; either version 2.1 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.
+GNU Lesser General Public License for more details.
 
-You should have received a copy of the GNU General Public License along
+You should have received a copy of the GNU Lesser 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 "irrlichttypes_extrabloated.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 "collision.h"
 #include <sstream>
 #include "porting.h"
 #include "content_mapnode.h"
@@ -35,6 +35,11 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "mapsector.h"
 #include "settings.h"
 #include "log.h"
+#include "util/string.h"
+#include "voxelalgorithms.h"
+#include "inventory.h"
+#include "util/numeric.h"
+#include "util/serialize.h"
 
 /*
        Asserts that the exception occurs
@@ -53,6 +58,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 
 #define CONTENT_STONE 0
 #define CONTENT_GRASS 0x800
+#define CONTENT_TORCH 100
 
 void define_some_nodes(IWritableItemDefManager *idef, IWritableNodeDefManager *ndef)
 {
@@ -76,7 +82,7 @@ void define_some_nodes(IWritableItemDefManager *idef, IWritableNodeDefManager *n
        f = ContentFeatures();
        f.name = itemdef.name;
        for(int i = 0; i < 6; i++)
-               f.tname_tiles[i] = "default_stone.png";
+               f.tiledef[i].name = "default_stone.png";
        f.is_ground_content = true;
        idef->registerItem(itemdef);
        ndef->set(i, f);
@@ -96,13 +102,29 @@ void define_some_nodes(IWritableItemDefManager *idef, IWritableNodeDefManager *n
                "{default_dirt.png&default_grass_side.png";
        f = ContentFeatures();
        f.name = itemdef.name;
-       f.tname_tiles[0] = "default_grass.png";
-       f.tname_tiles[1] = "default_dirt.png";
+       f.tiledef[0].name = "default_grass.png";
+       f.tiledef[1].name = "default_dirt.png";
        for(int i = 2; i < 6; i++)
-               f.tname_tiles[i] = "default_dirt.png^default_grass_side.png";
+               f.tiledef[i].name = "default_dirt.png^default_grass_side.png";
        f.is_ground_content = true;
        idef->registerItem(itemdef);
        ndef->set(i, f);
+
+       /*
+               Torch (minimal definition for lighting tests)
+       */
+       i = CONTENT_TORCH;
+       itemdef = ItemDefinition();
+       itemdef.type = ITEM_NODE;
+       itemdef.name = "default:torch";
+       f = ContentFeatures();
+       f.name = itemdef.name;
+       f.param_type = CPT_LIGHT;
+       f.light_propagates = true;
+       f.sunlight_propagates = true;
+       f.light_source = LIGHT_MAX-1;
+       idef->registerItem(itemdef);
+       ndef->set(i, f);
 }
 
 struct TestUtilities
@@ -120,6 +142,11 @@ struct TestUtilities
                assert(is_yes("YeS") == true);
                assert(is_yes("") == false);
                assert(is_yes("FAlse") == false);
+               const char *ends[] = {"abc", "c", "bc", NULL};
+               assert(removeStringEnd("abc", ends) == "");
+               assert(removeStringEnd("bc", ends) == "b");
+               assert(removeStringEnd("12c", ends) == "12");
+               assert(removeStringEnd("foo", ends) == "");
        }
 };
 
@@ -477,6 +504,247 @@ struct TestVoxelManipulator
        }
 };
 
+struct TestVoxelAlgorithms
+{
+       void Run(INodeDefManager *ndef)
+       {
+               /*
+                       voxalgo::propagateSunlight
+               */
+               {
+                       VoxelManipulator v;
+                       for(u16 z=0; z<3; z++)
+                       for(u16 y=0; y<3; y++)
+                       for(u16 x=0; x<3; x++)
+                       {
+                               v3s16 p(x,y,z);
+                               v.setNodeNoRef(p, MapNode(CONTENT_AIR));
+                       }
+                       VoxelArea a(v3s16(0,0,0), v3s16(2,2,2));
+                       {
+                               core::map<v3s16, bool> light_sources;
+                               voxalgo::setLight(v, a, 0, ndef);
+                               voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
+                                               v, a, true, light_sources, ndef);
+                               //v.print(dstream, ndef, VOXELPRINT_LIGHT_DAY);
+                               assert(res.bottom_sunlight_valid == true);
+                               assert(v.getNode(v3s16(1,1,1)).getLight(LIGHTBANK_DAY, ndef)
+                                               == LIGHT_SUN);
+                       }
+                       v.setNodeNoRef(v3s16(0,0,0), MapNode(CONTENT_STONE));
+                       {
+                               core::map<v3s16, bool> light_sources;
+                               voxalgo::setLight(v, a, 0, ndef);
+                               voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
+                                               v, a, true, light_sources, ndef);
+                               assert(res.bottom_sunlight_valid == true);
+                               assert(v.getNode(v3s16(1,1,1)).getLight(LIGHTBANK_DAY, ndef)
+                                               == LIGHT_SUN);
+                       }
+                       {
+                               core::map<v3s16, bool> light_sources;
+                               voxalgo::setLight(v, a, 0, ndef);
+                               voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
+                                               v, a, false, light_sources, ndef);
+                               assert(res.bottom_sunlight_valid == true);
+                               assert(v.getNode(v3s16(2,0,2)).getLight(LIGHTBANK_DAY, ndef)
+                                               == 0);
+                       }
+                       v.setNodeNoRef(v3s16(1,3,2), MapNode(CONTENT_STONE));
+                       {
+                               core::map<v3s16, bool> light_sources;
+                               voxalgo::setLight(v, a, 0, ndef);
+                               voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
+                                               v, a, true, light_sources, ndef);
+                               assert(res.bottom_sunlight_valid == true);
+                               assert(v.getNode(v3s16(1,1,2)).getLight(LIGHTBANK_DAY, ndef)
+                                               == 0);
+                       }
+                       {
+                               core::map<v3s16, bool> light_sources;
+                               voxalgo::setLight(v, a, 0, ndef);
+                               voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
+                                               v, a, false, light_sources, ndef);
+                               assert(res.bottom_sunlight_valid == true);
+                               assert(v.getNode(v3s16(1,0,2)).getLight(LIGHTBANK_DAY, ndef)
+                                               == 0);
+                       }
+                       {
+                               MapNode n(CONTENT_AIR);
+                               n.setLight(LIGHTBANK_DAY, 10, ndef);
+                               v.setNodeNoRef(v3s16(1,-1,2), n);
+                       }
+                       {
+                               core::map<v3s16, bool> light_sources;
+                               voxalgo::setLight(v, a, 0, ndef);
+                               voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
+                                               v, a, true, light_sources, ndef);
+                               assert(res.bottom_sunlight_valid == true);
+                       }
+                       {
+                               core::map<v3s16, bool> light_sources;
+                               voxalgo::setLight(v, a, 0, ndef);
+                               voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
+                                               v, a, false, light_sources, ndef);
+                               assert(res.bottom_sunlight_valid == true);
+                       }
+                       {
+                               MapNode n(CONTENT_AIR);
+                               n.setLight(LIGHTBANK_DAY, LIGHT_SUN, ndef);
+                               v.setNodeNoRef(v3s16(1,-1,2), n);
+                       }
+                       {
+                               core::map<v3s16, bool> light_sources;
+                               voxalgo::setLight(v, a, 0, ndef);
+                               voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
+                                               v, a, true, light_sources, ndef);
+                               assert(res.bottom_sunlight_valid == false);
+                       }
+                       {
+                               core::map<v3s16, bool> light_sources;
+                               voxalgo::setLight(v, a, 0, ndef);
+                               voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
+                                               v, a, false, light_sources, ndef);
+                               assert(res.bottom_sunlight_valid == false);
+                       }
+                       v.setNodeNoRef(v3s16(1,3,2), MapNode(CONTENT_IGNORE));
+                       {
+                               core::map<v3s16, bool> light_sources;
+                               voxalgo::setLight(v, a, 0, ndef);
+                               voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
+                                               v, a, true, light_sources, ndef);
+                               assert(res.bottom_sunlight_valid == true);
+                       }
+               }
+               /*
+                       voxalgo::clearLightAndCollectSources
+               */
+               {
+                       VoxelManipulator v;
+                       for(u16 z=0; z<3; z++)
+                       for(u16 y=0; y<3; y++)
+                       for(u16 x=0; x<3; x++)
+                       {
+                               v3s16 p(x,y,z);
+                               v.setNode(p, MapNode(CONTENT_AIR));
+                       }
+                       VoxelArea a(v3s16(0,0,0), v3s16(2,2,2));
+                       v.setNodeNoRef(v3s16(0,0,0), MapNode(CONTENT_STONE));
+                       v.setNodeNoRef(v3s16(1,1,1), MapNode(CONTENT_TORCH));
+                       {
+                               MapNode n(CONTENT_AIR);
+                               n.setLight(LIGHTBANK_DAY, 1, ndef);
+                               v.setNode(v3s16(1,1,2), n);
+                       }
+                       {
+                               core::map<v3s16, bool> light_sources;
+                               core::map<v3s16, u8> unlight_from;
+                               voxalgo::clearLightAndCollectSources(v, a, LIGHTBANK_DAY,
+                                               ndef, light_sources, unlight_from);
+                               //v.print(dstream, ndef, VOXELPRINT_LIGHT_DAY);
+                               assert(v.getNode(v3s16(0,1,1)).getLight(LIGHTBANK_DAY, ndef)
+                                               == 0);
+                               assert(light_sources.find(v3s16(1,1,1)) != NULL);
+                               assert(light_sources.size() == 1);
+                               assert(unlight_from.find(v3s16(1,1,2)) != NULL);
+                               assert(unlight_from.size() == 1);
+                       }
+               }
+       }
+};
+
+struct TestInventory
+{
+       void Run(IItemDefManager *idef)
+       {
+               std::string serialized_inventory =
+               "List 0 32\n"
+               "Empty\n"
+               "Empty\n"
+               "Empty\n"
+               "Empty\n"
+               "Empty\n"
+               "Empty\n"
+               "Empty\n"
+               "Empty\n"
+               "Empty\n"
+               "Item default:cobble 61\n"
+               "Empty\n"
+               "Empty\n"
+               "Empty\n"
+               "Empty\n"
+               "Empty\n"
+               "Empty\n"
+               "Item default:dirt 71\n"
+               "Empty\n"
+               "Empty\n"
+               "Empty\n"
+               "Empty\n"
+               "Empty\n"
+               "Empty\n"
+               "Empty\n"
+               "Item default:dirt 99\n"
+               "Item default:cobble 38\n"
+               "Empty\n"
+               "Empty\n"
+               "Empty\n"
+               "Empty\n"
+               "Empty\n"
+               "Empty\n"
+               "EndInventoryList\n"
+               "EndInventory\n";
+               
+               std::string serialized_inventory_2 =
+               "List main 32\n"
+               "Empty\n"
+               "Empty\n"
+               "Empty\n"
+               "Empty\n"
+               "Empty\n"
+               "Empty\n"
+               "Empty\n"
+               "Empty\n"
+               "Empty\n"
+               "Item default:cobble 61\n"
+               "Empty\n"
+               "Empty\n"
+               "Empty\n"
+               "Empty\n"
+               "Empty\n"
+               "Empty\n"
+               "Item default:dirt 71\n"
+               "Empty\n"
+               "Empty\n"
+               "Empty\n"
+               "Empty\n"
+               "Empty\n"
+               "Empty\n"
+               "Empty\n"
+               "Item default:dirt 99\n"
+               "Item default:cobble 38\n"
+               "Empty\n"
+               "Empty\n"
+               "Empty\n"
+               "Empty\n"
+               "Empty\n"
+               "Empty\n"
+               "EndInventoryList\n"
+               "EndInventory\n";
+               
+               Inventory inv(idef);
+               std::istringstream is(serialized_inventory, std::ios::binary);
+               inv.deSerialize(is);
+               assert(inv.getList("0"));
+               assert(!inv.getList("main"));
+               inv.getList("0")->setName("main");
+               assert(!inv.getList("0"));
+               assert(inv.getList("main"));
+               std::ostringstream inv_os(std::ios::binary);
+               inv.serialize(inv_os);
+               assert(inv_os.str() == serialized_inventory_2);
+       }
+};
+
 /*
        NOTE: These tests became non-working then NodeContainer was removed.
              These should be redone, utilizing some kind of a virtual
@@ -808,6 +1076,153 @@ struct TestMapSector
 };
 #endif
 
+struct TestCollision
+{
+       void Run()
+       {
+               /*
+                       axisAlignedCollision
+               */
+
+               for(s16 bx = -3; bx <= 3; bx++)
+               for(s16 by = -3; by <= 3; by++)
+               for(s16 bz = -3; bz <= 3; bz++)
+               {
+                       // X-
+                       {
+                               aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
+                               aabb3f m(bx-2, by, bz, bx-1, by+1, bz+1);
+                               v3f v(1, 0, 0);
+                               f32 dtime = 0;
+                               assert(axisAlignedCollision(s, m, v, 0, dtime) == 0);
+                               assert(fabs(dtime - 1.000) < 0.001);
+                       }
+                       {
+                               aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
+                               aabb3f m(bx-2, by, bz, bx-1, by+1, bz+1);
+                               v3f v(-1, 0, 0);
+                               f32 dtime = 0;
+                               assert(axisAlignedCollision(s, m, v, 0, dtime) == -1);
+                       }
+                       {
+                               aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
+                               aabb3f m(bx-2, by+1.5, bz, bx-1, by+2.5, bz-1);
+                               v3f v(1, 0, 0);
+                               f32 dtime;
+                               assert(axisAlignedCollision(s, m, v, 0, dtime) == -1);
+                       }
+                       {
+                               aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
+                               aabb3f m(bx-2, by-1.5, bz, bx-1.5, by+0.5, bz+1);
+                               v3f v(0.5, 0.1, 0);
+                               f32 dtime;
+                               assert(axisAlignedCollision(s, m, v, 0, dtime) == 0);
+                               assert(fabs(dtime - 3.000) < 0.001);
+                       }
+                       {
+                               aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
+                               aabb3f m(bx-2, by-1.5, bz, bx-1.5, by+0.5, bz+1);
+                               v3f v(0.5, 0.1, 0);
+                               f32 dtime;
+                               assert(axisAlignedCollision(s, m, v, 0, dtime) == 0);
+                               assert(fabs(dtime - 3.000) < 0.001);
+                       }
+
+                       // X+
+                       {
+                               aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
+                               aabb3f m(bx+2, by, bz, bx+3, by+1, bz+1);
+                               v3f v(-1, 0, 0);
+                               f32 dtime;
+                               assert(axisAlignedCollision(s, m, v, 0, dtime) == 0);
+                               assert(fabs(dtime - 1.000) < 0.001);
+                       }
+                       {
+                               aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
+                               aabb3f m(bx+2, by, bz, bx+3, by+1, bz+1);
+                               v3f v(1, 0, 0);
+                               f32 dtime;
+                               assert(axisAlignedCollision(s, m, v, 0, dtime) == -1);
+                       }
+                       {
+                               aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
+                               aabb3f m(bx+2, by, bz+1.5, bx+3, by+1, bz+3.5);
+                               v3f v(-1, 0, 0);
+                               f32 dtime;
+                               assert(axisAlignedCollision(s, m, v, 0, dtime) == -1);
+                       }
+                       {
+                               aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
+                               aabb3f m(bx+2, by-1.5, bz, bx+2.5, by-0.5, bz+1);
+                               v3f v(-0.5, 0.2, 0);
+                               f32 dtime;
+                               assert(axisAlignedCollision(s, m, v, 0, dtime) == 1);  // Y, not X!
+                               assert(fabs(dtime - 2.500) < 0.001);
+                       }
+                       {
+                               aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
+                               aabb3f m(bx+2, by-1.5, bz, bx+2.5, by-0.5, bz+1);
+                               v3f v(-0.5, 0.3, 0);
+                               f32 dtime;
+                               assert(axisAlignedCollision(s, m, v, 0, dtime) == 0);
+                               assert(fabs(dtime - 2.000) < 0.001);
+                       }
+
+                       // TODO: Y-, Y+, Z-, Z+
+
+                       // misc
+                       {
+                               aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
+                               aabb3f m(bx+2.3, by+2.29, bz+2.29, bx+4.2, by+4.2, bz+4.2);
+                               v3f v(-1./3, -1./3, -1./3);
+                               f32 dtime;
+                               assert(axisAlignedCollision(s, m, v, 0, dtime) == 0);
+                               assert(fabs(dtime - 0.9) < 0.001);
+                       }
+                       {
+                               aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
+                               aabb3f m(bx+2.29, by+2.3, bz+2.29, bx+4.2, by+4.2, bz+4.2);
+                               v3f v(-1./3, -1./3, -1./3);
+                               f32 dtime;
+                               assert(axisAlignedCollision(s, m, v, 0, dtime) == 1);
+                               assert(fabs(dtime - 0.9) < 0.001);
+                       }
+                       {
+                               aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
+                               aabb3f m(bx+2.29, by+2.29, bz+2.3, bx+4.2, by+4.2, bz+4.2);
+                               v3f v(-1./3, -1./3, -1./3);
+                               f32 dtime;
+                               assert(axisAlignedCollision(s, m, v, 0, dtime) == 2);
+                               assert(fabs(dtime - 0.9) < 0.001);
+                       }
+                       {
+                               aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
+                               aabb3f m(bx-4.2, by-4.2, bz-4.2, bx-2.3, by-2.29, bz-2.29);
+                               v3f v(1./7, 1./7, 1./7);
+                               f32 dtime;
+                               assert(axisAlignedCollision(s, m, v, 0, dtime) == 0);
+                               assert(fabs(dtime - 16.1) < 0.001);
+                       }
+                       {
+                               aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
+                               aabb3f m(bx-4.2, by-4.2, bz-4.2, bx-2.29, by-2.3, bz-2.29);
+                               v3f v(1./7, 1./7, 1./7);
+                               f32 dtime;
+                               assert(axisAlignedCollision(s, m, v, 0, dtime) == 1);
+                               assert(fabs(dtime - 16.1) < 0.001);
+                       }
+                       {
+                               aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
+                               aabb3f m(bx-4.2, by-4.2, bz-4.2, bx-2.29, by-2.29, bz-2.3);
+                               v3f v(1./7, 1./7, 1./7);
+                               f32 dtime;
+                               assert(axisAlignedCollision(s, m, v, 0, dtime) == 2);
+                               assert(fabs(dtime - 16.1) < 0.001);
+                       }
+               }
+       }
+};
+
 struct TestSocket
 {
        void Run()
@@ -1273,8 +1688,11 @@ void run_tests()
        TEST(TestSerialization);
        TESTPARAMS(TestMapNode, ndef);
        TESTPARAMS(TestVoxelManipulator, ndef);
+       TESTPARAMS(TestVoxelAlgorithms, ndef);
+       TESTPARAMS(TestInventory, idef);
        //TEST(TestMapBlock);
        //TEST(TestMapSector);
+       TEST(TestCollision);
        if(INTERNET_SIMULATOR == false){
                TEST(TestSocket);
                dout_con<<"=== BEGIN RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;