]> git.lizzy.rs Git - minetest.git/blob - src/unittest/test_settings.cpp
Settings: Add get_flags API for mapgen flags (mg_flags, mgv6_spflags, ...) (#9284)
[minetest.git] / src / unittest / test_settings.cpp
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 <cmath>
23 #include "settings.h"
24 #include "noise.h"
25
26 class TestSettings : public TestBase {
27 public:
28         TestSettings() { TestManager::registerTestModule(this); }
29         const char *getName() { return "TestSettings"; }
30
31         void runTests(IGameDef *gamedef);
32
33         void testAllSettings();
34         void testFlagDesc();
35
36         static const char *config_text_before;
37         static const std::string config_text_after;
38 };
39
40 static TestSettings g_test_instance;
41
42 void TestSettings::runTests(IGameDef *gamedef)
43 {
44         TEST(testAllSettings);
45         TEST(testFlagDesc);
46 }
47
48 ////////////////////////////////////////////////////////////////////////////////
49
50 const char *TestSettings::config_text_before =
51         "leet = 1337\n"
52         "leetleet = 13371337\n"
53         "leetleet_neg = -13371337\n"
54         "floaty_thing = 1.1\n"
55         "stringy_thing = asd /( ¤%&(/\" BLÖÄRP\n"
56         "coord = (1, 2, 4.5)\n"
57         "      # this is just a comment\n"
58         "this is an invalid line\n"
59         "asdf = {\n"
60         "       a   = 5\n"
61         "       bb  = 2.5\n"
62         "       ccc = \"\"\"\n"
63         "testy\n"
64         "   testa   \n"
65         "\"\"\"\n"
66         "\n"
67         "}\n"
68         "blarg = \"\"\" \n"
69         "some multiline text\n"
70         "     with leading whitespace!\n"
71         "\"\"\"\n"
72         "np_terrain = 5, 40, (250, 250, 250), 12341, 5, 0.7, 2.4\n"
73         "zoop = true";
74
75 const std::string TestSettings::config_text_after =
76         "leet = 1337\n"
77         "leetleet = 13371337\n"
78         "leetleet_neg = -13371337\n"
79         "floaty_thing = 1.1\n"
80         "stringy_thing = asd /( ¤%&(/\" BLÖÄRP\n"
81         "coord = (1, 2, 4.5)\n"
82         "      # this is just a comment\n"
83         "this is an invalid line\n"
84         "asdf = {\n"
85         "       a   = 5\n"
86         "       bb  = 2.5\n"
87         "       ccc = \"\"\"\n"
88         "testy\n"
89         "   testa   \n"
90         "\"\"\"\n"
91         "\n"
92         "}\n"
93         "blarg = \"\"\" \n"
94         "some multiline text\n"
95         "     with leading whitespace!\n"
96         "\"\"\"\n"
97         "np_terrain = {\n"
98         "       flags = defaults\n"
99         "       lacunarity = 2.4\n"
100         "       octaves = 6\n"
101         "       offset = 3.5\n"
102         "       persistence = 0.7\n"
103         "       scale = 40\n"
104         "       seed = 12341\n"
105         "       spread = (250,250,250)\n"
106         "}\n"
107         "zoop = true\n"
108         "coord2 = (1,2,3.3)\n"
109         "floaty_thing_2 = 1.2\n"
110         "groupy_thing = {\n"
111         "       animals = cute\n"
112         "       num_apples = 4\n"
113         "       num_oranges = 53\n"
114         "}\n";
115
116 void TestSettings::testAllSettings()
117 {
118         try {
119         Settings s;
120
121         // Test reading of settings
122         std::istringstream is(config_text_before);
123         s.parseConfigLines(is);
124
125         UASSERT(s.getS32("leet") == 1337);
126         UASSERT(s.getS16("leetleet") == 32767);
127         UASSERT(s.getS16("leetleet_neg") == -32768);
128
129         // Not sure if 1.1 is an exact value as a float, but doesn't matter
130         UASSERT(fabs(s.getFloat("floaty_thing") - 1.1) < 0.001);
131         UASSERT(s.get("stringy_thing") == "asd /( ¤%&(/\" BLÖÄRP");
132         UASSERT(fabs(s.getV3F("coord").X - 1.0) < 0.001);
133         UASSERT(fabs(s.getV3F("coord").Y - 2.0) < 0.001);
134         UASSERT(fabs(s.getV3F("coord").Z - 4.5) < 0.001);
135
136         // Test the setting of settings too
137         s.setFloat("floaty_thing_2", 1.2);
138         s.setV3F("coord2", v3f(1, 2, 3.3));
139         UASSERT(s.get("floaty_thing_2").substr(0,3) == "1.2");
140         UASSERT(fabs(s.getFloat("floaty_thing_2") - 1.2) < 0.001);
141         UASSERT(fabs(s.getV3F("coord2").X - 1.0) < 0.001);
142         UASSERT(fabs(s.getV3F("coord2").Y - 2.0) < 0.001);
143         UASSERT(fabs(s.getV3F("coord2").Z - 3.3) < 0.001);
144
145         // Test settings groups
146         Settings *group = s.getGroup("asdf");
147         UASSERT(group != NULL);
148         UASSERT(s.getGroupNoEx("zoop", group) == false);
149         UASSERT(group->getS16("a") == 5);
150         UASSERT(fabs(group->getFloat("bb") - 2.5) < 0.001);
151
152         Settings *group3 = new Settings;
153         group3->set("cat", "meow");
154         group3->set("dog", "woof");
155
156         Settings *group2 = new Settings;
157         group2->setS16("num_apples", 4);
158         group2->setS16("num_oranges", 53);
159         group2->setGroup("animals", group3);
160         group2->set("animals", "cute"); //destroys group 3
161         s.setGroup("groupy_thing", group2);
162
163         // Test set failure conditions
164         UASSERT(s.set("Zoop = Poop\nsome_other_setting", "false") == false);
165         UASSERT(s.set("sneaky", "\"\"\"\njabberwocky = false") == false);
166         UASSERT(s.set("hehe", "asdfasdf\n\"\"\"\nsomething = false") == false);
167
168         // Test multiline settings
169         UASSERT(group->get("ccc") == "testy\n   testa   ");
170
171         UASSERT(s.get("blarg") ==
172                 "some multiline text\n"
173                 "     with leading whitespace!");
174
175         // Test NoiseParams
176         UASSERT(s.getEntry("np_terrain").is_group == false);
177
178         NoiseParams np;
179         UASSERT(s.getNoiseParams("np_terrain", np) == true);
180         UASSERT(std::fabs(np.offset - 5) < 0.001f);
181         UASSERT(std::fabs(np.scale - 40) < 0.001f);
182         UASSERT(std::fabs(np.spread.X - 250) < 0.001f);
183         UASSERT(std::fabs(np.spread.Y - 250) < 0.001f);
184         UASSERT(std::fabs(np.spread.Z - 250) < 0.001f);
185         UASSERT(np.seed == 12341);
186         UASSERT(np.octaves == 5);
187         UASSERT(std::fabs(np.persist - 0.7) < 0.001f);
188
189         np.offset  = 3.5;
190         np.octaves = 6;
191         s.setNoiseParams("np_terrain", np);
192
193         UASSERT(s.getEntry("np_terrain").is_group == true);
194
195         // Test writing
196         std::ostringstream os(std::ios_base::binary);
197         is.clear();
198         is.seekg(0);
199
200         UASSERT(s.updateConfigObject(is, os, "", 0) == true);
201         //printf(">>>> expected config:\n%s\n", TEST_CONFIG_TEXT_AFTER);
202         //printf(">>>> actual config:\n%s\n", os.str().c_str());
203 #if __cplusplus < 201103L
204         // This test only works in older C++ versions than C++11 because we use unordered_map
205         UASSERT(os.str() == config_text_after);
206 #endif
207         } catch (SettingNotFoundException &e) {
208                 UASSERT(!"Setting not found!");
209         }
210 }
211
212 void TestSettings::testFlagDesc()
213 {
214         Settings s;
215         FlagDesc flagdesc[] = {
216                 { "biomes",  0x01 },
217                 { "trees",   0x02 },
218                 { "jungles", 0x04 },
219                 { "oranges", 0x08 },
220                 { "tables",  0x10 },
221                 { nullptr,      0 }
222         };
223
224         // Enabled: biomes, jungles, oranges (default)
225         s.setDefault("test_desc", flagdesc, readFlagString(
226                 "biomes,notrees,jungles,oranges", flagdesc, nullptr));
227         UASSERT(s.getFlagStr("test_desc", flagdesc, nullptr) == (0x01 | 0x04 | 0x08));
228
229         // Enabled: jungles, oranges, tables
230         s.set("test_desc", "nobiomes,tables");
231         UASSERT(s.getFlagStr("test_desc", flagdesc, nullptr) == (0x04 | 0x08 | 0x10));
232
233         // Enabled: (nothing)
234         s.set("test_desc", "nobiomes,nojungles,nooranges,notables");
235         UASSERT(s.getFlagStr("test_desc", flagdesc, nullptr) == 0x00);
236
237         // Numeric flag tests (override)
238         // Enabled: trees, tables
239         s.setDefault("test_flags", flagdesc, 0x02 | 0x10);
240         UASSERT(s.getFlagStr("test_flags", flagdesc, nullptr) == (0x02 | 0x10));
241
242         // Enabled: tables
243         s.set("test_flags", "16");
244         UASSERT(s.getFlagStr("test_flags", flagdesc, nullptr) == 0x10);
245 }