test_schematic.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2014 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "test.h"
  17. #include "mapgen/mg_schematic.h"
  18. #include "gamedef.h"
  19. #include "nodedef.h"
  20. class TestSchematic : public TestBase {
  21. public:
  22. TestSchematic() { TestManager::registerTestModule(this); }
  23. const char *getName() { return "TestSchematic"; }
  24. void runTests(IGameDef *gamedef);
  25. void testMtsSerializeDeserialize(const NodeDefManager *ndef);
  26. void testLuaTableSerialize(const NodeDefManager *ndef);
  27. void testFileSerializeDeserialize(const NodeDefManager *ndef);
  28. static const content_t test_schem1_data[7 * 6 * 4];
  29. static const content_t test_schem2_data[3 * 3 * 3];
  30. static const u8 test_schem2_prob[3 * 3 * 3];
  31. static const char *expected_lua_output;
  32. };
  33. static TestSchematic g_test_instance;
  34. void TestSchematic::runTests(IGameDef *gamedef)
  35. {
  36. NodeDefManager *ndef =
  37. (NodeDefManager *)gamedef->getNodeDefManager();
  38. ndef->setNodeRegistrationStatus(true);
  39. TEST(testMtsSerializeDeserialize, ndef);
  40. TEST(testLuaTableSerialize, ndef);
  41. TEST(testFileSerializeDeserialize, ndef);
  42. ndef->resetNodeResolveState();
  43. }
  44. ////////////////////////////////////////////////////////////////////////////////
  45. void TestSchematic::testMtsSerializeDeserialize(const NodeDefManager *ndef)
  46. {
  47. static const v3s16 size(7, 6, 4);
  48. static const u32 volume = size.X * size.Y * size.Z;
  49. std::stringstream ss(std::ios_base::binary |
  50. std::ios_base::in | std::ios_base::out);
  51. Schematic schem;
  52. {
  53. std::vector<std::string> &names = schem.m_nodenames;
  54. names.emplace_back("foo");
  55. names.emplace_back("bar");
  56. names.emplace_back("baz");
  57. names.emplace_back("qux");
  58. }
  59. schem.flags = 0;
  60. schem.size = size;
  61. schem.schemdata = new MapNode[volume];
  62. schem.slice_probs = new u8[size.Y];
  63. for (size_t i = 0; i != volume; i++)
  64. schem.schemdata[i] = MapNode(test_schem1_data[i], MTSCHEM_PROB_ALWAYS, 0);
  65. for (s16 y = 0; y != size.Y; y++)
  66. schem.slice_probs[y] = MTSCHEM_PROB_ALWAYS;
  67. UASSERT(schem.serializeToMts(&ss));
  68. ss.seekg(0);
  69. Schematic schem2;
  70. UASSERT(schem2.deserializeFromMts(&ss));
  71. {
  72. std::vector<std::string> &names = schem2.m_nodenames;
  73. UASSERTEQ(size_t, names.size(), 4);
  74. UASSERTEQ(std::string, names[0], "foo");
  75. UASSERTEQ(std::string, names[1], "bar");
  76. UASSERTEQ(std::string, names[2], "baz");
  77. UASSERTEQ(std::string, names[3], "qux");
  78. }
  79. UASSERT(schem2.size == size);
  80. for (size_t i = 0; i != volume; i++)
  81. UASSERT(schem2.schemdata[i] == schem.schemdata[i]);
  82. for (s16 y = 0; y != size.Y; y++)
  83. UASSERTEQ(u8, schem2.slice_probs[y], schem.slice_probs[y]);
  84. }
  85. void TestSchematic::testLuaTableSerialize(const NodeDefManager *ndef)
  86. {
  87. static const v3s16 size(3, 3, 3);
  88. static const u32 volume = size.X * size.Y * size.Z;
  89. Schematic schem;
  90. schem.flags = 0;
  91. schem.size = size;
  92. schem.schemdata = new MapNode[volume];
  93. schem.slice_probs = new u8[size.Y];
  94. for (size_t i = 0; i != volume; i++)
  95. schem.schemdata[i] = MapNode(test_schem2_data[i], test_schem2_prob[i], 0);
  96. for (s16 y = 0; y != size.Y; y++)
  97. schem.slice_probs[y] = MTSCHEM_PROB_ALWAYS;
  98. std::vector<std::string> &names = schem.m_nodenames;
  99. names.emplace_back("air");
  100. names.emplace_back("default:lava_source");
  101. names.emplace_back("default:glass");
  102. std::ostringstream ss(std::ios_base::binary);
  103. UASSERT(schem.serializeToLua(&ss, false, 0));
  104. UASSERTEQ(std::string, ss.str(), expected_lua_output);
  105. }
  106. void TestSchematic::testFileSerializeDeserialize(const NodeDefManager *ndef)
  107. {
  108. static const v3s16 size(3, 3, 3);
  109. static const u32 volume = size.X * size.Y * size.Z;
  110. static const content_t content_map[] = {
  111. CONTENT_AIR,
  112. t_CONTENT_STONE,
  113. t_CONTENT_LAVA,
  114. };
  115. static const content_t content_map2[] = {
  116. CONTENT_AIR,
  117. t_CONTENT_STONE,
  118. t_CONTENT_WATER,
  119. };
  120. StringMap replace_names;
  121. replace_names["default:lava"] = "default:water";
  122. Schematic schem1, schem2;
  123. //// Construct the schematic to save
  124. schem1.flags = 0;
  125. schem1.size = size;
  126. schem1.schemdata = new MapNode[volume];
  127. schem1.slice_probs = new u8[size.Y];
  128. schem1.slice_probs[0] = 80;
  129. schem1.slice_probs[1] = 160;
  130. schem1.slice_probs[2] = 240;
  131. // Node resolving happened manually.
  132. schem1.m_resolve_done = true;
  133. for (size_t i = 0; i != volume; i++) {
  134. content_t c = content_map[test_schem2_data[i]];
  135. schem1.schemdata[i] = MapNode(c, test_schem2_prob[i], 0);
  136. }
  137. std::string temp_file = getTestTempFile();
  138. UASSERT(schem1.saveSchematicToFile(temp_file, ndef));
  139. UASSERT(schem2.loadSchematicFromFile(temp_file, ndef, &replace_names));
  140. UASSERT(schem2.size == size);
  141. UASSERT(schem2.slice_probs[0] == 80);
  142. UASSERT(schem2.slice_probs[1] == 160);
  143. UASSERT(schem2.slice_probs[2] == 240);
  144. for (size_t i = 0; i != volume; i++) {
  145. content_t c = content_map2[test_schem2_data[i]];
  146. UASSERT(schem2.schemdata[i] == MapNode(c, test_schem2_prob[i], 0));
  147. }
  148. }
  149. // Should form a cross-shaped-thing...?
  150. const content_t TestSchematic::test_schem1_data[7 * 6 * 4] = {
  151. 3, 3, 1, 1, 1, 3, 3, // Y=0, Z=0
  152. 3, 0, 1, 2, 1, 0, 3, // Y=1, Z=0
  153. 3, 0, 1, 2, 1, 0, 3, // Y=2, Z=0
  154. 3, 1, 1, 2, 1, 1, 3, // Y=3, Z=0
  155. 3, 2, 2, 2, 2, 2, 3, // Y=4, Z=0
  156. 3, 1, 1, 2, 1, 1, 3, // Y=5, Z=0
  157. 0, 0, 1, 1, 1, 0, 0, // Y=0, Z=1
  158. 0, 0, 1, 2, 1, 0, 0, // Y=1, Z=1
  159. 0, 0, 1, 2, 1, 0, 0, // Y=2, Z=1
  160. 1, 1, 1, 2, 1, 1, 1, // Y=3, Z=1
  161. 1, 2, 2, 2, 2, 2, 1, // Y=4, Z=1
  162. 1, 1, 1, 2, 1, 1, 1, // Y=5, Z=1
  163. 0, 0, 1, 1, 1, 0, 0, // Y=0, Z=2
  164. 0, 0, 1, 2, 1, 0, 0, // Y=1, Z=2
  165. 0, 0, 1, 2, 1, 0, 0, // Y=2, Z=2
  166. 1, 1, 1, 2, 1, 1, 1, // Y=3, Z=2
  167. 1, 2, 2, 2, 2, 2, 1, // Y=4, Z=2
  168. 1, 1, 1, 2, 1, 1, 1, // Y=5, Z=2
  169. 3, 3, 1, 1, 1, 3, 3, // Y=0, Z=3
  170. 3, 0, 1, 2, 1, 0, 3, // Y=1, Z=3
  171. 3, 0, 1, 2, 1, 0, 3, // Y=2, Z=3
  172. 3, 1, 1, 2, 1, 1, 3, // Y=3, Z=3
  173. 3, 2, 2, 2, 2, 2, 3, // Y=4, Z=3
  174. 3, 1, 1, 2, 1, 1, 3, // Y=5, Z=3
  175. };
  176. const content_t TestSchematic::test_schem2_data[3 * 3 * 3] = {
  177. 0, 0, 0,
  178. 0, 2, 0,
  179. 0, 0, 0,
  180. 0, 2, 0,
  181. 2, 1, 2,
  182. 0, 2, 0,
  183. 0, 0, 0,
  184. 0, 2, 0,
  185. 0, 0, 0,
  186. };
  187. const u8 TestSchematic::test_schem2_prob[3 * 3 * 3] = {
  188. 0x00, 0x00, 0x00,
  189. 0x00, 0xFF, 0x00,
  190. 0x00, 0x00, 0x00,
  191. 0x00, 0xFF, 0x00,
  192. 0xFF, 0xFF, 0xFF,
  193. 0x00, 0xFF, 0x00,
  194. 0x00, 0x00, 0x00,
  195. 0x00, 0xFF, 0x00,
  196. 0x00, 0x00, 0x00,
  197. };
  198. const char *TestSchematic::expected_lua_output =
  199. "schematic = {\n"
  200. "\tsize = {x=3, y=3, z=3},\n"
  201. "\tyslice_prob = {\n"
  202. "\t\t{ypos=0, prob=254},\n"
  203. "\t\t{ypos=1, prob=254},\n"
  204. "\t\t{ypos=2, prob=254},\n"
  205. "\t},\n"
  206. "\tdata = {\n"
  207. "\t\t{name=\"air\", prob=0, param2=0},\n"
  208. "\t\t{name=\"air\", prob=0, param2=0},\n"
  209. "\t\t{name=\"air\", prob=0, param2=0},\n"
  210. "\t\t{name=\"air\", prob=0, param2=0},\n"
  211. "\t\t{name=\"default:glass\", prob=254, param2=0, force_place=true},\n"
  212. "\t\t{name=\"air\", prob=0, param2=0},\n"
  213. "\t\t{name=\"air\", prob=0, param2=0},\n"
  214. "\t\t{name=\"air\", prob=0, param2=0},\n"
  215. "\t\t{name=\"air\", prob=0, param2=0},\n"
  216. "\t\t{name=\"air\", prob=0, param2=0},\n"
  217. "\t\t{name=\"default:glass\", prob=254, param2=0, force_place=true},\n"
  218. "\t\t{name=\"air\", prob=0, param2=0},\n"
  219. "\t\t{name=\"default:glass\", prob=254, param2=0, force_place=true},\n"
  220. "\t\t{name=\"default:lava_source\", prob=254, param2=0, force_place=true},\n"
  221. "\t\t{name=\"default:glass\", prob=254, param2=0, force_place=true},\n"
  222. "\t\t{name=\"air\", prob=0, param2=0},\n"
  223. "\t\t{name=\"default:glass\", prob=254, param2=0, force_place=true},\n"
  224. "\t\t{name=\"air\", prob=0, param2=0},\n"
  225. "\t\t{name=\"air\", prob=0, param2=0},\n"
  226. "\t\t{name=\"air\", prob=0, param2=0},\n"
  227. "\t\t{name=\"air\", prob=0, param2=0},\n"
  228. "\t\t{name=\"air\", prob=0, param2=0},\n"
  229. "\t\t{name=\"default:glass\", prob=254, param2=0, force_place=true},\n"
  230. "\t\t{name=\"air\", prob=0, param2=0},\n"
  231. "\t\t{name=\"air\", prob=0, param2=0},\n"
  232. "\t\t{name=\"air\", prob=0, param2=0},\n"
  233. "\t\t{name=\"air\", prob=0, param2=0},\n"
  234. "\t},\n"
  235. "}\n";