]> git.lizzy.rs Git - minetest.git/blob - src/unittest/test_settings.cpp
Increase `ftos` precision (#13141)
[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 "defaultsettings.h"
25 #include "noise.h"
26
27 class TestSettings : public TestBase {
28 public:
29         TestSettings() { TestManager::registerTestModule(this); }
30         const char *getName() { return "TestSettings"; }
31
32         void runTests(IGameDef *gamedef);
33
34         void testAllSettings();
35         void testDefaults();
36         void testFlagDesc();
37
38         static const char *config_text_before;
39         static const std::string config_text_after;
40 };
41
42 static TestSettings g_test_instance;
43
44 void TestSettings::runTests(IGameDef *gamedef)
45 {
46         TEST(testAllSettings);
47         TEST(testDefaults);
48         TEST(testFlagDesc);
49 }
50
51 ////////////////////////////////////////////////////////////////////////////////
52
53 const char *TestSettings::config_text_before =
54         "leet = 1337\n"
55         "leetleet = 13371337\n"
56         "leetleet_neg = -13371337\n"
57         "floaty_thing = 1.1\n"
58         "stringy_thing = asd /( ¤%&(/\" BLÖÄRP\n"
59         "coord = (1, 2, 4.5)\n"
60         "      # this is just a comment\n"
61         "this is an invalid line\n"
62         "asdf = {\n"
63         "       a   = 5\n"
64         "       bb  = 2.5\n"
65         "       ccc = \"\"\"\n"
66         "testy\n"
67         "   testa   \n"
68         "\"\"\"\n"
69         "\n"
70         "}\n"
71         "blarg = \"\"\" \n"
72         "some multiline text\n"
73         "     with leading whitespace!\n"
74         "\"\"\"\n"
75         "np_terrain = 5, 40, (250, 250, 250), 12341, 5, 0.700012505, 2.40012503\n"
76         "zoop = true\n"
77         "[dummy_eof_end_tag]\n";
78
79 const std::string TestSettings::config_text_after =
80         "leet = 1337\n"
81         "leetleet = 13371337\n"
82         "leetleet_neg = -13371337\n"
83         "floaty_thing = 1.1\n"
84         "stringy_thing = asd /( ¤%&(/\" BLÖÄRP\n"
85         "coord = (1, 2, 4.5)\n"
86         "      # this is just a comment\n"
87         "this is an invalid line\n"
88         "asdf = {\n"
89         "       a   = 5\n"
90         "       bb  = 2.5\n"
91         "       ccc = \"\"\"\n"
92         "testy\n"
93         "   testa   \n"
94         "\"\"\"\n"
95         "\n"
96         "}\n"
97         "blarg = \"\"\" \n"
98         "some multiline text\n"
99         "     with leading whitespace!\n"
100         "\"\"\"\n"
101         "np_terrain = {\n"
102         "       flags = defaults\n"
103         "       lacunarity = 2.40012503\n"
104         "       octaves = 6\n"
105         "       offset = 3.5\n"
106         "       persistence = 0.700012505\n"
107         "       scale = 40\n"
108         "       seed = 12341\n"
109         "       spread = (250,250,250)\n"
110         "}\n"
111         "zoop = true\n"
112         "coord2 = (1,2,3.3)\n"
113         "floaty_thing_2 = 1.25\n"
114         "groupy_thing = {\n"
115         "       animals = cute\n"
116         "       num_apples = 4\n"
117         "       num_oranges = 53\n"
118         "}\n"
119         "[dummy_eof_end_tag]";
120
121 void compare_settings(const std::string &name, Settings *a, Settings *b)
122 {
123         auto keys = a->getNames();
124         Settings *group1, *group2;
125         std::string value1, value2;
126         for (auto &key : keys) {
127                 if (a->getGroupNoEx(key, group1)) {
128                         UASSERT(b->getGroupNoEx(key, group2));
129
130                         compare_settings(name + "->" + key, group1, group2);
131                         continue;
132                 }
133
134                 UASSERT(b->getNoEx(key, value1));
135                 // For identification
136                 value1 = name + "->" + key + "=" + value1;
137                 value2 = name + "->" + key + "=" + a->get(key);
138                 UASSERTCMP(std::string, ==, value2, value1);
139         }
140 }
141
142 void TestSettings::testAllSettings()
143 {
144         try {
145         Settings s("[dummy_eof_end_tag]");
146
147         // Test reading of settings
148         std::istringstream is(config_text_before);
149         s.parseConfigLines(is);
150
151         UASSERT(s.getS32("leet") == 1337);
152         UASSERT(s.getS16("leetleet") == 32767);
153         UASSERT(s.getS16("leetleet_neg") == -32768);
154
155         // Not sure if 1.1 is an exact value as a float, but doesn't matter
156         UASSERT(fabs(s.getFloat("floaty_thing") - 1.1) < 0.001);
157         UASSERT(s.get("stringy_thing") == "asd /( ¤%&(/\" BLÖÄRP");
158         UASSERT(fabs(s.getV3F("coord").X - 1.0) < 0.001);
159         UASSERT(fabs(s.getV3F("coord").Y - 2.0) < 0.001);
160         UASSERT(fabs(s.getV3F("coord").Z - 4.5) < 0.001);
161
162         // Test the setting of settings too
163         s.setFloat("floaty_thing_2", 1.25);
164         s.setV3F("coord2", v3f(1, 2, 3.3));
165         UASSERT(s.get("floaty_thing_2").substr(0,4) == "1.25");
166         UASSERT(fabs(s.getFloat("floaty_thing_2") - 1.25) < 0.001);
167         UASSERT(fabs(s.getV3F("coord2").X - 1.0) < 0.001);
168         UASSERT(fabs(s.getV3F("coord2").Y - 2.0) < 0.001);
169         UASSERT(fabs(s.getV3F("coord2").Z - 3.3) < 0.001);
170
171         // Test settings groups
172         Settings *group = s.getGroup("asdf");
173         UASSERT(group != NULL);
174         UASSERT(s.getGroupNoEx("zoop", group) == false);
175         UASSERT(group->getS16("a") == 5);
176         UASSERT(fabs(group->getFloat("bb") - 2.5) < 0.001);
177
178         Settings group3;
179         group3.set("cat", "meow");
180         group3.set("dog", "woof");
181
182         Settings group2;
183         group2.setS16("num_apples", 4);
184         group2.setS16("num_oranges", 53);
185         group2.setGroup("animals", group3);
186         group2.set("animals", "cute"); //destroys group 3
187         s.setGroup("groupy_thing", group2);
188
189         // Test set failure conditions
190         UASSERT(s.set("Zoop = Poop\nsome_other_setting", "false") == false);
191         UASSERT(s.set("sneaky", "\"\"\"\njabberwocky = false") == false);
192         UASSERT(s.set("hehe", "asdfasdf\n\"\"\"\nsomething = false") == false);
193
194         // Test multiline settings
195         UASSERT(group->get("ccc") == "testy\n   testa   ");
196
197         UASSERT(s.get("blarg") ==
198                 "some multiline text\n"
199                 "     with leading whitespace!");
200
201         // Test NoiseParams
202         UASSERT(s.getEntry("np_terrain").is_group == false);
203
204         NoiseParams np;
205         UASSERT(s.getNoiseParams("np_terrain", np) == true);
206         UASSERT(std::fabs(np.offset - 5) < 0.001f);
207         UASSERT(std::fabs(np.scale - 40) < 0.001f);
208         UASSERT(std::fabs(np.spread.X - 250) < 0.001f);
209         UASSERT(std::fabs(np.spread.Y - 250) < 0.001f);
210         UASSERT(std::fabs(np.spread.Z - 250) < 0.001f);
211         UASSERT(np.seed == 12341);
212         UASSERT(np.octaves == 5);
213         UASSERT(std::fabs(np.persist - 0.7) < 0.001f);
214
215         np.offset  = 3.5;
216         np.octaves = 6;
217         s.setNoiseParams("np_terrain", np);
218
219         UASSERT(s.getEntry("np_terrain").is_group == true);
220
221         // Test writing
222         std::ostringstream os(std::ios_base::binary);
223         is.clear();
224         is.seekg(0);
225
226         UASSERT(s.updateConfigObject(is, os, 0) == true);
227
228         {
229                 // Confirm settings
230                 Settings s2("[dummy_eof_end_tag]");
231                 std::istringstream is(config_text_after, std::ios_base::binary);
232                 UASSERT(s2.parseConfigLines(is) == true);
233
234                 compare_settings("(main)", &s, &s2);
235         }
236
237         } catch (SettingNotFoundException &e) {
238                 UASSERT(!"Setting not found!");
239         }
240 }
241
242 void TestSettings::testDefaults()
243 {
244         Settings *game = Settings::createLayer(SL_GAME);
245         Settings *def = Settings::getLayer(SL_DEFAULTS);
246
247         def->set("name", "FooBar");
248         UASSERT(def->get("name") == "FooBar");
249         UASSERT(game->get("name") == "FooBar");
250
251         game->set("name", "Baz");
252         UASSERT(game->get("name") == "Baz");
253
254         delete game;
255
256         // Restore default settings
257         delete Settings::getLayer(SL_DEFAULTS);
258         set_default_settings();
259 }
260
261 void TestSettings::testFlagDesc()
262 {
263         Settings &s = *Settings::createLayer(SL_GAME);
264         FlagDesc flagdesc[] = {
265                 { "biomes",  0x01 },
266                 { "trees",   0x02 },
267                 { "jungles", 0x04 },
268                 { "oranges", 0x08 },
269                 { "tables",  0x10 },
270                 { nullptr,      0 }
271         };
272
273         // Enabled: biomes, jungles, oranges (default)
274         s.setDefault("test_desc", flagdesc, readFlagString(
275                 "biomes,notrees,jungles,oranges", flagdesc, nullptr));
276         UASSERT(s.getFlagStr("test_desc", flagdesc, nullptr) == (0x01 | 0x04 | 0x08));
277
278         // Enabled: jungles, oranges, tables
279         s.set("test_desc", "nobiomes,tables");
280         UASSERT(s.getFlagStr("test_desc", flagdesc, nullptr) == (0x04 | 0x08 | 0x10));
281
282         // Enabled: (nothing)
283         s.set("test_desc", "nobiomes,nojungles,nooranges,notables");
284         UASSERT(s.getFlagStr("test_desc", flagdesc, nullptr) == 0x00);
285
286         // Numeric flag tests (override)
287         // Enabled: trees, tables
288         s.setDefault("test_flags", flagdesc, 0x02 | 0x10);
289         UASSERT(s.getFlagStr("test_flags", flagdesc, nullptr) == (0x02 | 0x10));
290
291         // Enabled: tables
292         s.set("test_flags", "16");
293         UASSERT(s.getFlagStr("test_flags", flagdesc, nullptr) == 0x10);
294
295         delete &s;
296 }