LCOV - code coverage report
Current view: top level - src/unittest - test_settings.cpp (source / functions) Hit Total Coverage
Test: report Lines: 4 69 5.8 %
Date: 2015-07-11 18:23:49 Functions: 4 7 57.1 %

          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 "settings.h"
      23             : #include "noise.h"
      24             : 
      25           1 : class TestSettings : public TestBase {
      26             : public:
      27           1 :         TestSettings() { TestManager::registerTestModule(this); }
      28           0 :         const char *getName() { return "TestSettings"; }
      29             : 
      30             :         void runTests(IGameDef *gamedef);
      31             : 
      32             :         void testAllSettings();
      33             : 
      34             :         static const char *config_text_before;
      35             :         static const char *config_text_after;
      36             : };
      37             : 
      38           1 : static TestSettings g_test_instance;
      39             : 
      40           0 : void TestSettings::runTests(IGameDef *gamedef)
      41             : {
      42           0 :         TEST(testAllSettings);
      43           0 : }
      44             : 
      45             : ////////////////////////////////////////////////////////////////////////////////
      46             : 
      47             : const char *TestSettings::config_text_before =
      48             :         "leet = 1337\n"
      49             :         "leetleet = 13371337\n"
      50             :         "leetleet_neg = -13371337\n"
      51             :         "floaty_thing = 1.1\n"
      52             :         "stringy_thing = asd /( ¤%&(/\" BLÖÄRP\n"
      53             :         "coord = (1, 2, 4.5)\n"
      54             :         "      # this is just a comment\n"
      55             :         "this is an invalid line\n"
      56             :         "asdf = {\n"
      57             :         "  a   = 5\n"
      58             :         "  bb  = 2.5\n"
      59             :         "  ccc = \"\"\"\n"
      60             :         "testy\n"
      61             :         "   testa   \n"
      62             :         "\"\"\"\n"
      63             :         "\n"
      64             :         "}\n"
      65             :         "blarg = \"\"\" \n"
      66             :         "some multiline text\n"
      67             :         "     with leading whitespace!\n"
      68             :         "\"\"\"\n"
      69             :         "np_terrain = 5, 40, (250, 250, 250), 12341, 5, 0.7, 2.4\n"
      70             :         "zoop = true";
      71             : 
      72             : const char *TestSettings::config_text_after =
      73             :         "leet = 1337\n"
      74             :         "leetleet = 13371337\n"
      75             :         "leetleet_neg = -13371337\n"
      76             :         "floaty_thing = 1.1\n"
      77             :         "stringy_thing = asd /( ¤%&(/\" BLÖÄRP\n"
      78             :         "coord = (1, 2, 4.5)\n"
      79             :         "      # this is just a comment\n"
      80             :         "this is an invalid line\n"
      81             :         "asdf = {\n"
      82             :         "  a   = 5\n"
      83             :         "  bb  = 2.5\n"
      84             :         "  ccc = \"\"\"\n"
      85             :         "testy\n"
      86             :         "   testa   \n"
      87             :         "\"\"\"\n"
      88             :         "\n"
      89             :         "}\n"
      90             :         "blarg = \"\"\" \n"
      91             :         "some multiline text\n"
      92             :         "     with leading whitespace!\n"
      93             :         "\"\"\"\n"
      94             :         "np_terrain = {\n"
      95             :         "  flags = defaults\n"
      96             :         "  lacunarity = 2.4\n"
      97             :         "  octaves = 6\n"
      98             :         "  offset = 3.5\n"
      99             :         "  persistence = 0.7\n"
     100             :         "  scale = 40\n"
     101             :         "  seed = 12341\n"
     102             :         "  spread = (250,250,250)\n"
     103             :         "}\n"
     104             :         "zoop = true\n"
     105             :         "coord2 = (1,2,3.3)\n"
     106             :         "floaty_thing_2 = 1.2\n"
     107             :         "groupy_thing = {\n"
     108             :         "  animals = cute\n"
     109             :         "  num_apples = 4\n"
     110             :         "  num_oranges = 53\n"
     111             :         "}\n";
     112             : 
     113           0 : void TestSettings::testAllSettings()
     114             : {
     115             :         try {
     116           0 :         Settings s;
     117             : 
     118             :         // Test reading of settings
     119           0 :         std::istringstream is(config_text_before);
     120           0 :         s.parseConfigLines(is);
     121             : 
     122           0 :         UASSERT(s.getS32("leet") == 1337);
     123           0 :         UASSERT(s.getS16("leetleet") == 32767);
     124           0 :         UASSERT(s.getS16("leetleet_neg") == -32768);
     125             : 
     126             :         // Not sure if 1.1 is an exact value as a float, but doesn't matter
     127           0 :         UASSERT(fabs(s.getFloat("floaty_thing") - 1.1) < 0.001);
     128           0 :         UASSERT(s.get("stringy_thing") == "asd /( ¤%&(/\" BLÖÄRP");
     129           0 :         UASSERT(fabs(s.getV3F("coord").X - 1.0) < 0.001);
     130           0 :         UASSERT(fabs(s.getV3F("coord").Y - 2.0) < 0.001);
     131           0 :         UASSERT(fabs(s.getV3F("coord").Z - 4.5) < 0.001);
     132             : 
     133             :         // Test the setting of settings too
     134           0 :         s.setFloat("floaty_thing_2", 1.2);
     135           0 :         s.setV3F("coord2", v3f(1, 2, 3.3));
     136           0 :         UASSERT(s.get("floaty_thing_2").substr(0,3) == "1.2");
     137           0 :         UASSERT(fabs(s.getFloat("floaty_thing_2") - 1.2) < 0.001);
     138           0 :         UASSERT(fabs(s.getV3F("coord2").X - 1.0) < 0.001);
     139           0 :         UASSERT(fabs(s.getV3F("coord2").Y - 2.0) < 0.001);
     140           0 :         UASSERT(fabs(s.getV3F("coord2").Z - 3.3) < 0.001);
     141             : 
     142             :         // Test settings groups
     143           0 :         Settings *group = s.getGroup("asdf");
     144           0 :         UASSERT(group != NULL);
     145           0 :         UASSERT(s.getGroupNoEx("zoop", group) == false);
     146           0 :         UASSERT(group->getS16("a") == 5);
     147           0 :         UASSERT(fabs(group->getFloat("bb") - 2.5) < 0.001);
     148             : 
     149           0 :         Settings *group3 = new Settings;
     150           0 :         group3->set("cat", "meow");
     151           0 :         group3->set("dog", "woof");
     152             : 
     153           0 :         Settings *group2 = new Settings;
     154           0 :         group2->setS16("num_apples", 4);
     155           0 :         group2->setS16("num_oranges", 53);
     156           0 :         group2->setGroup("animals", group3);
     157           0 :         group2->set("animals", "cute"); //destroys group 3
     158           0 :         s.setGroup("groupy_thing", group2);
     159             : 
     160             :         // Test set failure conditions
     161           0 :         UASSERT(s.set("Zoop = Poop\nsome_other_setting", "false") == false);
     162           0 :         UASSERT(s.set("sneaky", "\"\"\"\njabberwocky = false") == false);
     163           0 :         UASSERT(s.set("hehe", "asdfasdf\n\"\"\"\nsomething = false") == false);
     164             : 
     165             :         // Test multiline settings
     166           0 :         UASSERT(group->get("ccc") == "testy\n   testa   ");
     167             : 
     168           0 :         UASSERT(s.get("blarg") ==
     169             :                 "some multiline text\n"
     170           0 :                 "     with leading whitespace!");
     171             : 
     172             :         // Test NoiseParams
     173           0 :         UASSERT(s.getEntry("np_terrain").is_group == false);
     174             : 
     175           0 :         NoiseParams np;
     176           0 :         UASSERT(s.getNoiseParams("np_terrain", np) == true);
     177           0 :         UASSERT(fabs(np.offset - 5) < 0.001);
     178           0 :         UASSERT(fabs(np.scale - 40) < 0.001);
     179           0 :         UASSERT(fabs(np.spread.X - 250) < 0.001);
     180           0 :         UASSERT(fabs(np.spread.Y - 250) < 0.001);
     181           0 :         UASSERT(fabs(np.spread.Z - 250) < 0.001);
     182           0 :         UASSERT(np.seed == 12341);
     183           0 :         UASSERT(np.octaves == 5);
     184           0 :         UASSERT(fabs(np.persist - 0.7) < 0.001);
     185             : 
     186           0 :         np.offset  = 3.5;
     187           0 :         np.octaves = 6;
     188           0 :         s.setNoiseParams("np_terrain", np);
     189             : 
     190           0 :         UASSERT(s.getEntry("np_terrain").is_group == true);
     191             : 
     192             :         // Test writing
     193           0 :         std::ostringstream os(std::ios_base::binary);
     194           0 :         is.clear();
     195           0 :         is.seekg(0);
     196             : 
     197           0 :         UASSERT(s.updateConfigObject(is, os, "", 0) == true);
     198             :         //printf(">>>> expected config:\n%s\n", TEST_CONFIG_TEXT_AFTER);
     199             :         //printf(">>>> actual config:\n%s\n", os.str().c_str());
     200           0 :         UASSERT(os.str() == config_text_after);
     201           0 :         } catch (SettingNotFoundException &e) {
     202           0 :                 UASSERT(!"Setting not found!");
     203             :         }
     204           3 : }

Generated by: LCOV version 1.11