LCOV - code coverage report
Current view: top level - src/unittest - test_voxelalgorithms.cpp (source / functions) Hit Total Coverage
Test: report Lines: 4 100 4.0 %
Date: 2015-07-11 18:23:49 Functions: 4 8 50.0 %

          Line data    Source code
       1             : /*
       2             : Minetest
       3             : Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
       4             : 
       5             : This program is free software; you can redistribute it and/or modify
       6             : it under the terms of the GNU Lesser General Public License as published by
       7             : the Free Software Foundation; either version 2.1 of the License, or
       8             : (at your option) any later version.
       9             : 
      10             : This program is distributed in the hope that it will be useful,
      11             : but WITHOUT ANY WARRANTY; without even the implied warranty of
      12             : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      13             : GNU Lesser General Public License for more details.
      14             : 
      15             : You should have received a copy of the GNU Lesser General Public License along
      16             : with this program; if not, write to the Free Software Foundation, Inc.,
      17             : 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
      18             : */
      19             : 
      20             : #include "test.h"
      21             : 
      22             : #include "gamedef.h"
      23             : #include "voxelalgorithms.h"
      24             : 
      25           1 : class TestVoxelAlgorithms : public TestBase {
      26             : public:
      27           1 :         TestVoxelAlgorithms() { TestManager::registerTestModule(this); }
      28           0 :         const char *getName() { return "TestVoxelAlgorithms"; }
      29             : 
      30             :         void runTests(IGameDef *gamedef);
      31             : 
      32             :         void testPropogateSunlight(INodeDefManager *ndef);
      33             :         void testClearLightAndCollectSources(INodeDefManager *ndef);
      34             : };
      35             : 
      36           1 : static TestVoxelAlgorithms g_test_instance;
      37             : 
      38           0 : void TestVoxelAlgorithms::runTests(IGameDef *gamedef)
      39             : {
      40           0 :         INodeDefManager *ndef = gamedef->getNodeDefManager();
      41             : 
      42           0 :         TEST(testPropogateSunlight, ndef);
      43           0 :         TEST(testClearLightAndCollectSources, ndef);
      44           0 : }
      45             : 
      46             : ////////////////////////////////////////////////////////////////////////////////
      47             : 
      48           0 : void TestVoxelAlgorithms::testPropogateSunlight(INodeDefManager *ndef)
      49             : {
      50           0 :         VoxelManipulator v;
      51             : 
      52           0 :         for (u16 z = 0; z < 3; z++)
      53           0 :         for (u16 y = 0; y < 3; y++)
      54           0 :         for (u16 x = 0; x < 3; x++) {
      55           0 :                 v3s16 p(x,y,z);
      56           0 :                 v.setNodeNoRef(p, MapNode(CONTENT_AIR));
      57             :         }
      58             : 
      59           0 :         VoxelArea a(v3s16(0,0,0), v3s16(2,2,2));
      60             : 
      61             :         {
      62           0 :                 std::set<v3s16> light_sources;
      63           0 :                 voxalgo::setLight(v, a, 0, ndef);
      64             :                 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
      65           0 :                                 v, a, true, light_sources, ndef);
      66             :                 //v.print(dstream, ndef, VOXELPRINT_LIGHT_DAY);
      67           0 :                 UASSERT(res.bottom_sunlight_valid == true);
      68           0 :                 UASSERT(v.getNode(v3s16(1,1,1)).getLight(LIGHTBANK_DAY, ndef)
      69           0 :                                 == LIGHT_SUN);
      70             :         }
      71             : 
      72           0 :         v.setNodeNoRef(v3s16(0,0,0), MapNode(t_CONTENT_STONE));
      73             : 
      74             :         {
      75           0 :                 std::set<v3s16> light_sources;
      76           0 :                 voxalgo::setLight(v, a, 0, ndef);
      77             :                 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
      78           0 :                                 v, a, true, light_sources, ndef);
      79           0 :                 UASSERT(res.bottom_sunlight_valid == true);
      80           0 :                 UASSERT(v.getNode(v3s16(1,1,1)).getLight(LIGHTBANK_DAY, ndef)
      81           0 :                                 == LIGHT_SUN);
      82             :         }
      83             : 
      84             :         {
      85           0 :                 std::set<v3s16> light_sources;
      86           0 :                 voxalgo::setLight(v, a, 0, ndef);
      87             :                 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
      88           0 :                                 v, a, false, light_sources, ndef);
      89           0 :                 UASSERT(res.bottom_sunlight_valid == true);
      90           0 :                 UASSERT(v.getNode(v3s16(2,0,2)).getLight(LIGHTBANK_DAY, ndef)
      91           0 :                                 == 0);
      92             :         }
      93             : 
      94           0 :         v.setNodeNoRef(v3s16(1,3,2), MapNode(t_CONTENT_STONE));
      95             : 
      96             :         {
      97           0 :                 std::set<v3s16> light_sources;
      98           0 :                 voxalgo::setLight(v, a, 0, ndef);
      99             :                 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
     100           0 :                                 v, a, true, light_sources, ndef);
     101           0 :                 UASSERT(res.bottom_sunlight_valid == true);
     102           0 :                 UASSERT(v.getNode(v3s16(1,1,2)).getLight(LIGHTBANK_DAY, ndef)
     103           0 :                                 == 0);
     104             :         }
     105             : 
     106             :         {
     107           0 :                 std::set<v3s16> light_sources;
     108           0 :                 voxalgo::setLight(v, a, 0, ndef);
     109             :                 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
     110           0 :                                 v, a, false, light_sources, ndef);
     111           0 :                 UASSERT(res.bottom_sunlight_valid == true);
     112           0 :                 UASSERT(v.getNode(v3s16(1,0,2)).getLight(LIGHTBANK_DAY, ndef)
     113           0 :                                 == 0);
     114             :         }
     115             : 
     116             :         {
     117           0 :                 MapNode n(CONTENT_AIR);
     118           0 :                 n.setLight(LIGHTBANK_DAY, 10, ndef);
     119           0 :                 v.setNodeNoRef(v3s16(1,-1,2), n);
     120             :         }
     121             : 
     122             :         {
     123           0 :                 std::set<v3s16> light_sources;
     124           0 :                 voxalgo::setLight(v, a, 0, ndef);
     125             :                 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
     126           0 :                                 v, a, true, light_sources, ndef);
     127           0 :                 UASSERT(res.bottom_sunlight_valid == true);
     128             :         }
     129             : 
     130             :         {
     131           0 :                 std::set<v3s16> light_sources;
     132           0 :                 voxalgo::setLight(v, a, 0, ndef);
     133             :                 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
     134           0 :                                 v, a, false, light_sources, ndef);
     135           0 :                 UASSERT(res.bottom_sunlight_valid == true);
     136             :         }
     137             : 
     138             :         {
     139           0 :                 MapNode n(CONTENT_AIR);
     140           0 :                 n.setLight(LIGHTBANK_DAY, LIGHT_SUN, ndef);
     141           0 :                 v.setNodeNoRef(v3s16(1,-1,2), n);
     142             :         }
     143             : 
     144             :         {
     145           0 :                 std::set<v3s16> light_sources;
     146           0 :                 voxalgo::setLight(v, a, 0, ndef);
     147             :                 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
     148           0 :                                 v, a, true, light_sources, ndef);
     149           0 :                 UASSERT(res.bottom_sunlight_valid == false);
     150             :         }
     151             : 
     152             :         {
     153           0 :                 std::set<v3s16> light_sources;
     154           0 :                 voxalgo::setLight(v, a, 0, ndef);
     155             :                 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
     156           0 :                                 v, a, false, light_sources, ndef);
     157           0 :                 UASSERT(res.bottom_sunlight_valid == false);
     158             :         }
     159             : 
     160           0 :         v.setNodeNoRef(v3s16(1,3,2), MapNode(CONTENT_IGNORE));
     161             : 
     162             :         {
     163           0 :                 std::set<v3s16> light_sources;
     164           0 :                 voxalgo::setLight(v, a, 0, ndef);
     165             :                 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
     166           0 :                                 v, a, true, light_sources, ndef);
     167           0 :                 UASSERT(res.bottom_sunlight_valid == true);
     168             :         }
     169           0 : }
     170             : 
     171           0 : void TestVoxelAlgorithms::testClearLightAndCollectSources(INodeDefManager *ndef)
     172             : {
     173           0 :         VoxelManipulator v;
     174             : 
     175           0 :         for (u16 z = 0; z < 3; z++)
     176           0 :         for (u16 y = 0; y < 3; y++)
     177           0 :         for (u16 x = 0; x < 3; x++) {
     178           0 :                 v3s16 p(x,y,z);
     179           0 :                 v.setNode(p, MapNode(CONTENT_AIR));
     180             :         }
     181             : 
     182           0 :         VoxelArea a(v3s16(0,0,0), v3s16(2,2,2));
     183           0 :         v.setNodeNoRef(v3s16(0,0,0), MapNode(t_CONTENT_STONE));
     184           0 :         v.setNodeNoRef(v3s16(1,1,1), MapNode(t_CONTENT_TORCH));
     185             : 
     186             :         {
     187           0 :                 MapNode n(CONTENT_AIR);
     188           0 :                 n.setLight(LIGHTBANK_DAY, 1, ndef);
     189           0 :                 v.setNode(v3s16(1,1,2), n);
     190             :         }
     191             : 
     192             :         {
     193           0 :                 std::set<v3s16> light_sources;
     194           0 :                 std::map<v3s16, u8> unlight_from;
     195           0 :                 voxalgo::clearLightAndCollectSources(v, a, LIGHTBANK_DAY,
     196           0 :                                 ndef, light_sources, unlight_from);
     197             :                 //v.print(dstream, ndef, VOXELPRINT_LIGHT_DAY);
     198           0 :                 UASSERT(v.getNode(v3s16(0,1,1)).getLight(LIGHTBANK_DAY, ndef) == 0);
     199           0 :                 UASSERT(light_sources.find(v3s16(1,1,1)) != light_sources.end());
     200           0 :                 UASSERT(light_sources.size() == 1);
     201           0 :                 UASSERT(unlight_from.find(v3s16(1,1,2)) != unlight_from.end());
     202           0 :                 UASSERT(unlight_from.size() == 1);
     203             :         }
     204           3 : }

Generated by: LCOV version 1.11