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.
26 class TestSettings : public TestBase {
28 TestSettings() { TestManager::registerTestModule(this); }
29 const char *getName() { return "TestSettings"; }
31 void runTests(IGameDef *gamedef);
33 void testAllSettings();
35 static const char *config_text_before;
36 static const std::string config_text_after;
39 static TestSettings g_test_instance;
41 void TestSettings::runTests(IGameDef *gamedef)
43 TEST(testAllSettings);
46 ////////////////////////////////////////////////////////////////////////////////
48 const char *TestSettings::config_text_before =
50 "leetleet = 13371337\n"
51 "leetleet_neg = -13371337\n"
52 "floaty_thing = 1.1\n"
53 "stringy_thing = asd /( ¤%&(/\" BLÖÄRP\n"
54 "coord = (1, 2, 4.5)\n"
55 " # this is just a comment\n"
56 "this is an invalid line\n"
67 "some multiline text\n"
68 " with leading whitespace!\n"
70 "np_terrain = 5, 40, (250, 250, 250), 12341, 5, 0.7, 2.4\n"
73 const std::string TestSettings::config_text_after =
75 "leetleet = 13371337\n"
76 "leetleet_neg = -13371337\n"
77 "floaty_thing = 1.1\n"
78 "stringy_thing = asd /( ¤%&(/\" BLÖÄRP\n"
79 "coord = (1, 2, 4.5)\n"
80 " # this is just a comment\n"
81 "this is an invalid line\n"
92 "some multiline text\n"
93 " with leading whitespace!\n"
100 " persistence = 0.7\n"
103 " spread = (250,250,250)\n"
106 "coord2 = (1,2,3.3)\n"
107 "floaty_thing_2 = 1.2\n"
111 " num_oranges = 53\n"
114 void TestSettings::testAllSettings()
119 // Test reading of settings
120 std::istringstream is(config_text_before);
121 s.parseConfigLines(is);
123 UASSERT(s.getS32("leet") == 1337);
124 UASSERT(s.getS16("leetleet") == 32767);
125 UASSERT(s.getS16("leetleet_neg") == -32768);
127 // Not sure if 1.1 is an exact value as a float, but doesn't matter
128 UASSERT(fabs(s.getFloat("floaty_thing") - 1.1) < 0.001);
129 UASSERT(s.get("stringy_thing") == "asd /( ¤%&(/\" BLÖÄRP");
130 UASSERT(fabs(s.getV3F("coord").X - 1.0) < 0.001);
131 UASSERT(fabs(s.getV3F("coord").Y - 2.0) < 0.001);
132 UASSERT(fabs(s.getV3F("coord").Z - 4.5) < 0.001);
134 // Test the setting of settings too
135 s.setFloat("floaty_thing_2", 1.2);
136 s.setV3F("coord2", v3f(1, 2, 3.3));
137 UASSERT(s.get("floaty_thing_2").substr(0,3) == "1.2");
138 UASSERT(fabs(s.getFloat("floaty_thing_2") - 1.2) < 0.001);
139 UASSERT(fabs(s.getV3F("coord2").X - 1.0) < 0.001);
140 UASSERT(fabs(s.getV3F("coord2").Y - 2.0) < 0.001);
141 UASSERT(fabs(s.getV3F("coord2").Z - 3.3) < 0.001);
143 // Test settings groups
144 Settings *group = s.getGroup("asdf");
145 UASSERT(group != NULL);
146 UASSERT(s.getGroupNoEx("zoop", group) == false);
147 UASSERT(group->getS16("a") == 5);
148 UASSERT(fabs(group->getFloat("bb") - 2.5) < 0.001);
150 Settings *group3 = new Settings;
151 group3->set("cat", "meow");
152 group3->set("dog", "woof");
154 Settings *group2 = new Settings;
155 group2->setS16("num_apples", 4);
156 group2->setS16("num_oranges", 53);
157 group2->setGroup("animals", group3);
158 group2->set("animals", "cute"); //destroys group 3
159 s.setGroup("groupy_thing", group2);
161 // Test set failure conditions
162 UASSERT(s.set("Zoop = Poop\nsome_other_setting", "false") == false);
163 UASSERT(s.set("sneaky", "\"\"\"\njabberwocky = false") == false);
164 UASSERT(s.set("hehe", "asdfasdf\n\"\"\"\nsomething = false") == false);
166 // Test multiline settings
167 UASSERT(group->get("ccc") == "testy\n testa ");
169 UASSERT(s.get("blarg") ==
170 "some multiline text\n"
171 " with leading whitespace!");
174 UASSERT(s.getEntry("np_terrain").is_group == false);
177 UASSERT(s.getNoiseParams("np_terrain", np) == true);
178 UASSERT(std::fabs(np.offset - 5) < 0.001f);
179 UASSERT(std::fabs(np.scale - 40) < 0.001f);
180 UASSERT(std::fabs(np.spread.X - 250) < 0.001f);
181 UASSERT(std::fabs(np.spread.Y - 250) < 0.001f);
182 UASSERT(std::fabs(np.spread.Z - 250) < 0.001f);
183 UASSERT(np.seed == 12341);
184 UASSERT(np.octaves == 5);
185 UASSERT(std::fabs(np.persist - 0.7) < 0.001f);
189 s.setNoiseParams("np_terrain", np);
191 UASSERT(s.getEntry("np_terrain").is_group == true);
194 std::ostringstream os(std::ios_base::binary);
198 UASSERT(s.updateConfigObject(is, os, "", 0) == true);
199 //printf(">>>> expected config:\n%s\n", TEST_CONFIG_TEXT_AFTER);
200 //printf(">>>> actual config:\n%s\n", os.str().c_str());
201 #if __cplusplus < 201103L
202 // This test only works in older C++ versions than C++11 because we use unordered_map
203 UASSERT(os.str() == config_text_after);
205 } catch (SettingNotFoundException &e) {
206 UASSERT(!"Setting not found!");