3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
24 #include "defaultsettings.h"
27 class TestSettings : public TestBase {
29 TestSettings() { TestManager::registerTestModule(this); }
30 const char *getName() { return "TestSettings"; }
32 void runTests(IGameDef *gamedef);
34 void testAllSettings();
38 static const char *config_text_before;
39 static const std::string config_text_after;
42 static TestSettings g_test_instance;
44 void TestSettings::runTests(IGameDef *gamedef)
46 TEST(testAllSettings);
51 ////////////////////////////////////////////////////////////////////////////////
53 const char *TestSettings::config_text_before =
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"
72 "some multiline text\n"
73 " with leading whitespace!\n"
75 "np_terrain = 5, 40, (250, 250, 250), 12341, 5, 0.700012505, 2.40012503\n"
77 "[dummy_eof_end_tag]\n";
79 const std::string TestSettings::config_text_after =
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"
98 "some multiline text\n"
99 " with leading whitespace!\n"
102 " flags = defaults\n"
103 " lacunarity = 2.40012503\n"
106 " persistence = 0.700012505\n"
109 " spread = (250,250,250)\n"
112 "coord2 = (1,2,3.3)\n"
113 "floaty_thing_2 = 1.25\n"
117 " num_oranges = 53\n"
119 "[dummy_eof_end_tag]";
121 void compare_settings(const std::string &name, Settings *a, Settings *b)
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));
130 compare_settings(name + "->" + key, group1, group2);
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);
142 void TestSettings::testAllSettings()
145 Settings s("[dummy_eof_end_tag]");
147 // Test reading of settings
148 std::istringstream is(config_text_before);
149 s.parseConfigLines(is);
151 UASSERT(s.getS32("leet") == 1337);
152 UASSERT(s.getS16("leetleet") == 32767);
153 UASSERT(s.getS16("leetleet_neg") == -32768);
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);
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);
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);
179 group3.set("cat", "meow");
180 group3.set("dog", "woof");
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);
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);
194 // Test multiline settings
195 UASSERT(group->get("ccc") == "testy\n testa ");
197 UASSERT(s.get("blarg") ==
198 "some multiline text\n"
199 " with leading whitespace!");
202 UASSERT(s.getEntry("np_terrain").is_group == false);
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);
217 s.setNoiseParams("np_terrain", np);
219 UASSERT(s.getEntry("np_terrain").is_group == true);
222 std::ostringstream os(std::ios_base::binary);
226 UASSERT(s.updateConfigObject(is, os, 0) == true);
230 Settings s2("[dummy_eof_end_tag]");
231 std::istringstream is(config_text_after, std::ios_base::binary);
232 UASSERT(s2.parseConfigLines(is) == true);
234 compare_settings("(main)", &s, &s2);
237 } catch (SettingNotFoundException &e) {
238 UASSERT(!"Setting not found!");
242 void TestSettings::testDefaults()
244 Settings *game = Settings::createLayer(SL_GAME);
245 Settings *def = Settings::getLayer(SL_DEFAULTS);
247 def->set("name", "FooBar");
248 UASSERT(def->get("name") == "FooBar");
249 UASSERT(game->get("name") == "FooBar");
251 game->set("name", "Baz");
252 UASSERT(game->get("name") == "Baz");
256 // Restore default settings
257 delete Settings::getLayer(SL_DEFAULTS);
258 set_default_settings();
261 void TestSettings::testFlagDesc()
263 Settings &s = *Settings::createLayer(SL_GAME);
264 FlagDesc flagdesc[] = {
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));
278 // Enabled: jungles, oranges, tables
279 s.set("test_desc", "nobiomes,tables");
280 UASSERT(s.getFlagStr("test_desc", flagdesc, nullptr) == (0x04 | 0x08 | 0x10));
282 // Enabled: (nothing)
283 s.set("test_desc", "nobiomes,nojungles,nooranges,notables");
284 UASSERT(s.getFlagStr("test_desc", flagdesc, nullptr) == 0x00);
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));
292 s.set("test_flags", "16");
293 UASSERT(s.getFlagStr("test_flags", flagdesc, nullptr) == 0x10);