|
| 1 | + /* |
| 2 | +Minetest |
| 3 | +Copyright (C) 2010-2014 kwolekr, Ryan Kwolek <kwolekr@minetest.net> |
| 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 "mg_schematic.h" |
| 23 | +#include "gamedef.h" |
| 24 | +#include "nodedef.h" |
| 25 | + |
| 26 | +class TestSchematic : public TestBase { |
| 27 | +public: |
| 28 | + TestSchematic() { TestManager::registerTestModule(this); } |
| 29 | + const char *getName() { return "TestSchematic"; } |
| 30 | + |
| 31 | + void runTests(IGameDef *gamedef); |
| 32 | + |
| 33 | + void testMtsSerializeDeserialize(INodeDefManager *ndef); |
| 34 | + void testLuaTableSerialize(INodeDefManager *ndef); |
| 35 | + void testFileSerializeDeserialize(INodeDefManager *ndef); |
| 36 | + |
| 37 | + static const content_t test_schem_data[7 * 6 * 4]; |
| 38 | + static const content_t test_schem_data2[3 * 3 * 3]; |
| 39 | + static const char *expected_lua_output; |
| 40 | +}; |
| 41 | + |
| 42 | +static TestSchematic g_test_instance; |
| 43 | + |
| 44 | +void TestSchematic::runTests(IGameDef *gamedef) |
| 45 | +{ |
| 46 | + IWritableNodeDefManager *ndef = |
| 47 | + (IWritableNodeDefManager *)gamedef->getNodeDefManager(); |
| 48 | + |
| 49 | + ndef->setNodeRegistrationStatus(true); |
| 50 | + |
| 51 | + TEST(testMtsSerializeDeserialize, ndef); |
| 52 | + TEST(testLuaTableSerialize, ndef); |
| 53 | + TEST(testFileSerializeDeserialize, ndef); |
| 54 | + |
| 55 | + ndef->resetNodeResolveState(); |
| 56 | +} |
| 57 | + |
| 58 | +//////////////////////////////////////////////////////////////////////////////// |
| 59 | + |
| 60 | +void TestSchematic::testMtsSerializeDeserialize(INodeDefManager *ndef) |
| 61 | +{ |
| 62 | + static const v3s16 size(7, 6, 4); |
| 63 | + static const u32 volume = size.X * size.Y * size.Z; |
| 64 | + |
| 65 | + std::stringstream ss(std::ios_base::binary | |
| 66 | + std::ios_base::in | std::ios_base::out); |
| 67 | + |
| 68 | + std::vector<std::string> names; |
| 69 | + names.push_back("foo"); |
| 70 | + names.push_back("bar"); |
| 71 | + names.push_back("baz"); |
| 72 | + names.push_back("qux"); |
| 73 | + |
| 74 | + Schematic schem, schem2; |
| 75 | + |
| 76 | + schem.flags = 0; |
| 77 | + schem.size = size; |
| 78 | + schem.schemdata = new MapNode[volume]; |
| 79 | + schem.slice_probs = new u8[size.Y]; |
| 80 | + for (size_t i = 0; i != volume; i++) |
| 81 | + schem.schemdata[i] = MapNode(test_schem_data[i], MTSCHEM_PROB_ALWAYS, 0); |
| 82 | + for (size_t y = 0; y != size.Y; y++) |
| 83 | + schem.slice_probs[y] = MTSCHEM_PROB_ALWAYS; |
| 84 | + |
| 85 | + UASSERT(schem.serializeToMts(&ss, names)); |
| 86 | + |
| 87 | + ss.seekg(0); |
| 88 | + names.clear(); |
| 89 | + |
| 90 | + UASSERT(schem2.deserializeFromMts(&ss, &names)); |
| 91 | + |
| 92 | + UASSERTEQ(size_t, names.size(), 4); |
| 93 | + UASSERTEQ(std::string, names[0], "foo"); |
| 94 | + UASSERTEQ(std::string, names[1], "bar"); |
| 95 | + UASSERTEQ(std::string, names[2], "baz"); |
| 96 | + UASSERTEQ(std::string, names[3], "qux"); |
| 97 | + |
| 98 | + UASSERT(schem2.size == size); |
| 99 | + for (size_t i = 0; i != volume; i++) |
| 100 | + UASSERT(schem2.schemdata[i] == schem.schemdata[i]); |
| 101 | + for (size_t y = 0; y != size.Y; y++) |
| 102 | + UASSERTEQ(u8, schem2.slice_probs[y], schem.slice_probs[y]); |
| 103 | +} |
| 104 | + |
| 105 | + |
| 106 | +void TestSchematic::testLuaTableSerialize(INodeDefManager *ndef) |
| 107 | +{ |
| 108 | + static const v3s16 size(3, 3, 3); |
| 109 | + static const u32 volume = size.X * size.Y * size.Z; |
| 110 | + |
| 111 | + Schematic schem; |
| 112 | + |
| 113 | + schem.flags = 0; |
| 114 | + schem.size = size; |
| 115 | + schem.schemdata = new MapNode[volume]; |
| 116 | + schem.slice_probs = new u8[size.Y]; |
| 117 | + for (size_t i = 0; i != volume; i++) |
| 118 | + schem.schemdata[i] = MapNode(test_schem_data2[i], MTSCHEM_PROB_ALWAYS, 0); |
| 119 | + for (size_t y = 0; y != size.Y; y++) |
| 120 | + schem.slice_probs[y] = MTSCHEM_PROB_ALWAYS; |
| 121 | + |
| 122 | + std::vector<std::string> names; |
| 123 | + names.push_back("air"); |
| 124 | + names.push_back("default:lava_source"); |
| 125 | + names.push_back("default:glass"); |
| 126 | + |
| 127 | + std::ostringstream ss(std::ios_base::binary); |
| 128 | + |
| 129 | + UASSERT(schem.serializeToLua(&ss, names, false, 0)); |
| 130 | + UASSERTEQ(std::string, ss.str(), expected_lua_output); |
| 131 | +} |
| 132 | + |
| 133 | + |
| 134 | +void TestSchematic::testFileSerializeDeserialize(INodeDefManager *ndef) |
| 135 | +{ |
| 136 | + static const v3s16 size(3, 3, 3); |
| 137 | + static const u32 volume = size.X * size.Y * size.Z; |
| 138 | + static const content_t content_map[] = { |
| 139 | + CONTENT_AIR, |
| 140 | + t_CONTENT_STONE, |
| 141 | + t_CONTENT_LAVA, |
| 142 | + }; |
| 143 | + static const content_t content_map2[] = { |
| 144 | + CONTENT_AIR, |
| 145 | + t_CONTENT_STONE, |
| 146 | + t_CONTENT_WATER, |
| 147 | + }; |
| 148 | + StringMap replace_names; |
| 149 | + replace_names["default:lava"] = "default:water"; |
| 150 | + |
| 151 | + Schematic schem1, schem2; |
| 152 | + |
| 153 | + //// Construct the schematic to save |
| 154 | + schem1.flags = 0; |
| 155 | + schem1.size = size; |
| 156 | + schem1.schemdata = new MapNode[volume]; |
| 157 | + schem1.slice_probs = new u8[size.Y]; |
| 158 | + schem1.slice_probs[0] = 80; |
| 159 | + schem1.slice_probs[1] = 160; |
| 160 | + schem1.slice_probs[2] = 240; |
| 161 | + |
| 162 | + for (size_t i = 0; i != volume; i++) { |
| 163 | + content_t c = content_map[test_schem_data2[i]]; |
| 164 | + schem1.schemdata[i] = MapNode(c, MTSCHEM_PROB_ALWAYS, 0); |
| 165 | + } |
| 166 | + |
| 167 | + std::string temp_file = getTestTempFile(); |
| 168 | + UASSERT(schem1.saveSchematicToFile(temp_file, ndef)); |
| 169 | + UASSERT(schem2.loadSchematicFromFile(temp_file, ndef, &replace_names)); |
| 170 | + |
| 171 | + UASSERT(schem2.size == size); |
| 172 | + UASSERT(schem2.slice_probs[0] == 80); |
| 173 | + UASSERT(schem2.slice_probs[1] == 160); |
| 174 | + UASSERT(schem2.slice_probs[2] == 240); |
| 175 | + |
| 176 | + for (size_t i = 0; i != volume; i++) { |
| 177 | + content_t c = content_map2[test_schem_data2[i]]; |
| 178 | + UASSERT(schem2.schemdata[i] == MapNode(c, MTSCHEM_PROB_ALWAYS, 0)); |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | + |
| 183 | +// Should form a cross-shaped-thing...? |
| 184 | +const content_t TestSchematic::test_schem_data[7 * 6 * 4] = { |
| 185 | + 3, 3, 1, 1, 1, 3, 3, // Y=0, Z=0 |
| 186 | + 3, 0, 1, 2, 1, 0, 3, // Y=1, Z=0 |
| 187 | + 3, 0, 1, 2, 1, 0, 3, // Y=2, Z=0 |
| 188 | + 3, 1, 1, 2, 1, 1, 3, // Y=3, Z=0 |
| 189 | + 3, 2, 2, 2, 2, 2, 3, // Y=4, Z=0 |
| 190 | + 3, 1, 1, 2, 1, 1, 3, // Y=5, Z=0 |
| 191 | + |
| 192 | + 0, 0, 1, 1, 1, 0, 0, // Y=0, Z=1 |
| 193 | + 0, 0, 1, 2, 1, 0, 0, // Y=1, Z=1 |
| 194 | + 0, 0, 1, 2, 1, 0, 0, // Y=2, Z=1 |
| 195 | + 1, 1, 1, 2, 1, 1, 1, // Y=3, Z=1 |
| 196 | + 1, 2, 2, 2, 2, 2, 1, // Y=4, Z=1 |
| 197 | + 1, 1, 1, 2, 1, 1, 1, // Y=5, Z=1 |
| 198 | + |
| 199 | + 0, 0, 1, 1, 1, 0, 0, // Y=0, Z=2 |
| 200 | + 0, 0, 1, 2, 1, 0, 0, // Y=1, Z=2 |
| 201 | + 0, 0, 1, 2, 1, 0, 0, // Y=2, Z=2 |
| 202 | + 1, 1, 1, 2, 1, 1, 1, // Y=3, Z=2 |
| 203 | + 1, 2, 2, 2, 2, 2, 1, // Y=4, Z=2 |
| 204 | + 1, 1, 1, 2, 1, 1, 1, // Y=5, Z=2 |
| 205 | + |
| 206 | + 3, 3, 1, 1, 1, 3, 3, // Y=0, Z=3 |
| 207 | + 3, 0, 1, 2, 1, 0, 3, // Y=1, Z=3 |
| 208 | + 3, 0, 1, 2, 1, 0, 3, // Y=2, Z=3 |
| 209 | + 3, 1, 1, 2, 1, 1, 3, // Y=3, Z=3 |
| 210 | + 3, 2, 2, 2, 2, 2, 3, // Y=4, Z=3 |
| 211 | + 3, 1, 1, 2, 1, 1, 3, // Y=5, Z=3 |
| 212 | +}; |
| 213 | + |
| 214 | +const content_t TestSchematic::test_schem_data2[3 * 3 * 3] = { |
| 215 | + 0, 0, 0, |
| 216 | + 0, 2, 0, |
| 217 | + 0, 0, 0, |
| 218 | + |
| 219 | + 0, 2, 0, |
| 220 | + 2, 1, 2, |
| 221 | + 0, 2, 0, |
| 222 | + |
| 223 | + 0, 0, 0, |
| 224 | + 0, 2, 0, |
| 225 | + 0, 0, 0, |
| 226 | +}; |
| 227 | + |
| 228 | +const char *TestSchematic::expected_lua_output = |
| 229 | + "schematic = {\n" |
| 230 | + "\tsize = {x=3, y=3, z=3},\n" |
| 231 | + "\tyslice_prob = {\n" |
| 232 | + "\t\t{ypos=0, prob=255},\n" |
| 233 | + "\t\t{ypos=1, prob=255},\n" |
| 234 | + "\t\t{ypos=2, prob=255},\n" |
| 235 | + "\t},\n" |
| 236 | + "\tdata = {\n" |
| 237 | + "\t\t{name=\"air\", param1=255, param2=0},\n" |
| 238 | + "\t\t{name=\"air\", param1=255, param2=0},\n" |
| 239 | + "\t\t{name=\"air\", param1=255, param2=0},\n" |
| 240 | + "\t\t{name=\"air\", param1=255, param2=0},\n" |
| 241 | + "\t\t{name=\"default:glass\", param1=255, param2=0},\n" |
| 242 | + "\t\t{name=\"air\", param1=255, param2=0},\n" |
| 243 | + "\t\t{name=\"air\", param1=255, param2=0},\n" |
| 244 | + "\t\t{name=\"air\", param1=255, param2=0},\n" |
| 245 | + "\t\t{name=\"air\", param1=255, param2=0},\n" |
| 246 | + "\t\t{name=\"air\", param1=255, param2=0},\n" |
| 247 | + "\t\t{name=\"default:glass\", param1=255, param2=0},\n" |
| 248 | + "\t\t{name=\"air\", param1=255, param2=0},\n" |
| 249 | + "\t\t{name=\"default:glass\", param1=255, param2=0},\n" |
| 250 | + "\t\t{name=\"default:lava_source\", param1=255, param2=0},\n" |
| 251 | + "\t\t{name=\"default:glass\", param1=255, param2=0},\n" |
| 252 | + "\t\t{name=\"air\", param1=255, param2=0},\n" |
| 253 | + "\t\t{name=\"default:glass\", param1=255, param2=0},\n" |
| 254 | + "\t\t{name=\"air\", param1=255, param2=0},\n" |
| 255 | + "\t\t{name=\"air\", param1=255, param2=0},\n" |
| 256 | + "\t\t{name=\"air\", param1=255, param2=0},\n" |
| 257 | + "\t\t{name=\"air\", param1=255, param2=0},\n" |
| 258 | + "\t\t{name=\"air\", param1=255, param2=0},\n" |
| 259 | + "\t\t{name=\"default:glass\", param1=255, param2=0},\n" |
| 260 | + "\t\t{name=\"air\", param1=255, param2=0},\n" |
| 261 | + "\t\t{name=\"air\", param1=255, param2=0},\n" |
| 262 | + "\t\t{name=\"air\", param1=255, param2=0},\n" |
| 263 | + "\t\t{name=\"air\", param1=255, param2=0},\n" |
| 264 | + "\t},\n" |
| 265 | + "}\n"; |
0 commit comments