test.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  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 "nodedef.h"
  18. #include "itemdef.h"
  19. #include "gamedef.h"
  20. #include "mods.h"
  21. #include "util/numeric.h"
  22. content_t t_CONTENT_STONE;
  23. content_t t_CONTENT_GRASS;
  24. content_t t_CONTENT_TORCH;
  25. content_t t_CONTENT_WATER;
  26. content_t t_CONTENT_LAVA;
  27. content_t t_CONTENT_BRICK;
  28. ////////////////////////////////////////////////////////////////////////////////
  29. ////
  30. //// TestGameDef
  31. ////
  32. class TestGameDef : public IGameDef {
  33. public:
  34. TestGameDef();
  35. ~TestGameDef();
  36. IItemDefManager *getItemDefManager() { return m_itemdef; }
  37. INodeDefManager *getNodeDefManager() { return m_nodedef; }
  38. ICraftDefManager *getCraftDefManager() { return m_craftdef; }
  39. ITextureSource *getTextureSource() { return m_texturesrc; }
  40. IShaderSource *getShaderSource() { return m_shadersrc; }
  41. ISoundManager *getSoundManager() { return m_soundmgr; }
  42. MtEventManager *getEventManager() { return m_eventmgr; }
  43. scene::ISceneManager *getSceneManager() { return m_scenemgr; }
  44. IRollbackManager *getRollbackManager() { return m_rollbackmgr; }
  45. EmergeManager *getEmergeManager() { return m_emergemgr; }
  46. scene::IAnimatedMesh *getMesh(const std::string &filename) { return NULL; }
  47. bool checkLocalPrivilege(const std::string &priv) { return false; }
  48. u16 allocateUnknownNodeId(const std::string &name) { return 0; }
  49. void defineSomeNodes();
  50. virtual const std::vector<ModSpec> &getMods() const
  51. {
  52. static std::vector<ModSpec> testmodspec;
  53. return testmodspec;
  54. }
  55. virtual const ModSpec* getModSpec(const std::string &modname) const { return NULL; }
  56. virtual std::string getModStoragePath() const { return "."; }
  57. virtual bool registerModStorage(ModMetadata *meta) { return true; }
  58. virtual void unregisterModStorage(const std::string &name) {}
  59. private:
  60. IItemDefManager *m_itemdef = nullptr;
  61. INodeDefManager *m_nodedef = nullptr;
  62. ICraftDefManager *m_craftdef = nullptr;
  63. ITextureSource *m_texturesrc = nullptr;
  64. IShaderSource *m_shadersrc = nullptr;
  65. ISoundManager *m_soundmgr = nullptr;
  66. MtEventManager *m_eventmgr = nullptr;
  67. scene::ISceneManager *m_scenemgr = nullptr;
  68. IRollbackManager *m_rollbackmgr = nullptr;
  69. EmergeManager *m_emergemgr = nullptr;
  70. };
  71. TestGameDef::TestGameDef()
  72. {
  73. m_itemdef = createItemDefManager();
  74. m_nodedef = createNodeDefManager();
  75. defineSomeNodes();
  76. }
  77. TestGameDef::~TestGameDef()
  78. {
  79. delete m_itemdef;
  80. delete m_nodedef;
  81. }
  82. void TestGameDef::defineSomeNodes()
  83. {
  84. IWritableItemDefManager *idef = (IWritableItemDefManager *)m_itemdef;
  85. IWritableNodeDefManager *ndef = (IWritableNodeDefManager *)m_nodedef;
  86. ItemDefinition itemdef;
  87. ContentFeatures f;
  88. //// Stone
  89. itemdef = ItemDefinition();
  90. itemdef.type = ITEM_NODE;
  91. itemdef.name = "default:stone";
  92. itemdef.description = "Stone";
  93. itemdef.groups["cracky"] = 3;
  94. itemdef.inventory_image = "[inventorycube"
  95. "{default_stone.png"
  96. "{default_stone.png"
  97. "{default_stone.png";
  98. f = ContentFeatures();
  99. f.name = itemdef.name;
  100. for (TileDef &tiledef : f.tiledef)
  101. tiledef.name = "default_stone.png";
  102. f.is_ground_content = true;
  103. idef->registerItem(itemdef);
  104. t_CONTENT_STONE = ndef->set(f.name, f);
  105. //// Grass
  106. itemdef = ItemDefinition();
  107. itemdef.type = ITEM_NODE;
  108. itemdef.name = "default:dirt_with_grass";
  109. itemdef.description = "Dirt with grass";
  110. itemdef.groups["crumbly"] = 3;
  111. itemdef.inventory_image = "[inventorycube"
  112. "{default_grass.png"
  113. "{default_dirt.png&default_grass_side.png"
  114. "{default_dirt.png&default_grass_side.png";
  115. f = ContentFeatures();
  116. f.name = itemdef.name;
  117. f.tiledef[0].name = "default_grass.png";
  118. f.tiledef[1].name = "default_dirt.png";
  119. for(int i = 2; i < 6; i++)
  120. f.tiledef[i].name = "default_dirt.png^default_grass_side.png";
  121. f.is_ground_content = true;
  122. idef->registerItem(itemdef);
  123. t_CONTENT_GRASS = ndef->set(f.name, f);
  124. //// Torch (minimal definition for lighting tests)
  125. itemdef = ItemDefinition();
  126. itemdef.type = ITEM_NODE;
  127. itemdef.name = "default:torch";
  128. f = ContentFeatures();
  129. f.name = itemdef.name;
  130. f.param_type = CPT_LIGHT;
  131. f.light_propagates = true;
  132. f.sunlight_propagates = true;
  133. f.light_source = LIGHT_MAX-1;
  134. idef->registerItem(itemdef);
  135. t_CONTENT_TORCH = ndef->set(f.name, f);
  136. //// Water
  137. itemdef = ItemDefinition();
  138. itemdef.type = ITEM_NODE;
  139. itemdef.name = "default:water";
  140. itemdef.description = "Water";
  141. itemdef.inventory_image = "[inventorycube"
  142. "{default_water.png"
  143. "{default_water.png"
  144. "{default_water.png";
  145. f = ContentFeatures();
  146. f.name = itemdef.name;
  147. f.alpha = 128;
  148. f.liquid_type = LIQUID_SOURCE;
  149. f.liquid_viscosity = 4;
  150. f.is_ground_content = true;
  151. f.groups["liquids"] = 3;
  152. for (TileDef &tiledef : f.tiledef)
  153. tiledef.name = "default_water.png";
  154. idef->registerItem(itemdef);
  155. t_CONTENT_WATER = ndef->set(f.name, f);
  156. //// Lava
  157. itemdef = ItemDefinition();
  158. itemdef.type = ITEM_NODE;
  159. itemdef.name = "default:lava";
  160. itemdef.description = "Lava";
  161. itemdef.inventory_image = "[inventorycube"
  162. "{default_lava.png"
  163. "{default_lava.png"
  164. "{default_lava.png";
  165. f = ContentFeatures();
  166. f.name = itemdef.name;
  167. f.alpha = 128;
  168. f.liquid_type = LIQUID_SOURCE;
  169. f.liquid_viscosity = 7;
  170. f.light_source = LIGHT_MAX-1;
  171. f.is_ground_content = true;
  172. f.groups["liquids"] = 3;
  173. for (TileDef &tiledef : f.tiledef)
  174. tiledef.name = "default_lava.png";
  175. idef->registerItem(itemdef);
  176. t_CONTENT_LAVA = ndef->set(f.name, f);
  177. //// Brick
  178. itemdef = ItemDefinition();
  179. itemdef.type = ITEM_NODE;
  180. itemdef.name = "default:brick";
  181. itemdef.description = "Brick";
  182. itemdef.groups["cracky"] = 3;
  183. itemdef.inventory_image = "[inventorycube"
  184. "{default_brick.png"
  185. "{default_brick.png"
  186. "{default_brick.png";
  187. f = ContentFeatures();
  188. f.name = itemdef.name;
  189. for (TileDef &tiledef : f.tiledef)
  190. tiledef.name = "default_brick.png";
  191. f.is_ground_content = true;
  192. idef->registerItem(itemdef);
  193. t_CONTENT_BRICK = ndef->set(f.name, f);
  194. }
  195. ////
  196. //// run_tests
  197. ////
  198. bool run_tests()
  199. {
  200. u64 t1 = porting::getTimeMs();
  201. TestGameDef gamedef;
  202. g_logger.setLevelSilenced(LL_ERROR, true);
  203. u32 num_modules_failed = 0;
  204. u32 num_total_tests_failed = 0;
  205. u32 num_total_tests_run = 0;
  206. std::vector<TestBase *> &testmods = TestManager::getTestModules();
  207. for (size_t i = 0; i != testmods.size(); i++) {
  208. if (!testmods[i]->testModule(&gamedef))
  209. num_modules_failed++;
  210. num_total_tests_failed += testmods[i]->num_tests_failed;
  211. num_total_tests_run += testmods[i]->num_tests_run;
  212. }
  213. u64 tdiff = porting::getTimeMs() - t1;
  214. g_logger.setLevelSilenced(LL_ERROR, false);
  215. const char *overall_status = (num_modules_failed == 0) ? "PASSED" : "FAILED";
  216. rawstream
  217. << "++++++++++++++++++++++++++++++++++++++++"
  218. << "++++++++++++++++++++++++++++++++++++++++" << std::endl
  219. << "Unit Test Results: " << overall_status << std::endl
  220. << " " << num_modules_failed << " / " << testmods.size()
  221. << " failed modules (" << num_total_tests_failed << " / "
  222. << num_total_tests_run << " failed individual tests)." << std::endl
  223. << " Testing took " << tdiff << "ms total." << std::endl
  224. << "++++++++++++++++++++++++++++++++++++++++"
  225. << "++++++++++++++++++++++++++++++++++++++++" << std::endl;
  226. return num_modules_failed;
  227. }
  228. ////
  229. //// TestBase
  230. ////
  231. bool TestBase::testModule(IGameDef *gamedef)
  232. {
  233. rawstream << "======== Testing module " << getName() << std::endl;
  234. u64 t1 = porting::getTimeMs();
  235. runTests(gamedef);
  236. u64 tdiff = porting::getTimeMs() - t1;
  237. rawstream << "======== Module " << getName() << " "
  238. << (num_tests_failed ? "failed" : "passed") << " (" << num_tests_failed
  239. << " failures / " << num_tests_run << " tests) - " << tdiff
  240. << "ms" << std::endl;
  241. if (!m_test_dir.empty())
  242. fs::RecursiveDelete(m_test_dir);
  243. return num_tests_failed == 0;
  244. }
  245. std::string TestBase::getTestTempDirectory()
  246. {
  247. if (!m_test_dir.empty())
  248. return m_test_dir;
  249. char buf[32];
  250. snprintf(buf, sizeof(buf), "%08X", myrand());
  251. m_test_dir = fs::TempPath() + DIR_DELIM "mttest_" + buf;
  252. if (!fs::CreateDir(m_test_dir))
  253. throw TestFailedException();
  254. return m_test_dir;
  255. }
  256. std::string TestBase::getTestTempFile()
  257. {
  258. char buf[32];
  259. snprintf(buf, sizeof(buf), "%08X", myrand());
  260. return getTestTempDirectory() + DIR_DELIM + buf + ".tmp";
  261. }
  262. /*
  263. NOTE: These tests became non-working then NodeContainer was removed.
  264. These should be redone, utilizing some kind of a virtual
  265. interface for Map (IMap would be fine).
  266. */
  267. #if 0
  268. struct TestMapBlock: public TestBase
  269. {
  270. class TC : public NodeContainer
  271. {
  272. public:
  273. MapNode node;
  274. bool position_valid;
  275. core::list<v3s16> validity_exceptions;
  276. TC()
  277. {
  278. position_valid = true;
  279. }
  280. virtual bool isValidPosition(v3s16 p)
  281. {
  282. //return position_valid ^ (p==position_valid_exception);
  283. bool exception = false;
  284. for(core::list<v3s16>::Iterator i=validity_exceptions.begin();
  285. i != validity_exceptions.end(); i++)
  286. {
  287. if(p == *i)
  288. {
  289. exception = true;
  290. break;
  291. }
  292. }
  293. return exception ? !position_valid : position_valid;
  294. }
  295. virtual MapNode getNode(v3s16 p)
  296. {
  297. if(isValidPosition(p) == false)
  298. throw InvalidPositionException();
  299. return node;
  300. }
  301. virtual void setNode(v3s16 p, MapNode & n)
  302. {
  303. if(isValidPosition(p) == false)
  304. throw InvalidPositionException();
  305. };
  306. virtual u16 nodeContainerId() const
  307. {
  308. return 666;
  309. }
  310. };
  311. void Run()
  312. {
  313. TC parent;
  314. MapBlock b(&parent, v3s16(1,1,1));
  315. v3s16 relpos(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
  316. UASSERT(b.getPosRelative() == relpos);
  317. UASSERT(b.getBox().MinEdge.X == MAP_BLOCKSIZE);
  318. UASSERT(b.getBox().MaxEdge.X == MAP_BLOCKSIZE*2-1);
  319. UASSERT(b.getBox().MinEdge.Y == MAP_BLOCKSIZE);
  320. UASSERT(b.getBox().MaxEdge.Y == MAP_BLOCKSIZE*2-1);
  321. UASSERT(b.getBox().MinEdge.Z == MAP_BLOCKSIZE);
  322. UASSERT(b.getBox().MaxEdge.Z == MAP_BLOCKSIZE*2-1);
  323. UASSERT(b.isValidPosition(v3s16(0,0,0)) == true);
  324. UASSERT(b.isValidPosition(v3s16(-1,0,0)) == false);
  325. UASSERT(b.isValidPosition(v3s16(-1,-142,-2341)) == false);
  326. UASSERT(b.isValidPosition(v3s16(-124,142,2341)) == false);
  327. UASSERT(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
  328. UASSERT(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE,MAP_BLOCKSIZE-1)) == false);
  329. /*
  330. TODO: this method should probably be removed
  331. if the block size isn't going to be set variable
  332. */
  333. /*UASSERT(b.getSizeNodes() == v3s16(MAP_BLOCKSIZE,
  334. MAP_BLOCKSIZE, MAP_BLOCKSIZE));*/
  335. // Changed flag should be initially set
  336. UASSERT(b.getModified() == MOD_STATE_WRITE_NEEDED);
  337. b.resetModified();
  338. UASSERT(b.getModified() == MOD_STATE_CLEAN);
  339. // All nodes should have been set to
  340. // .d=CONTENT_IGNORE and .getLight() = 0
  341. for(u16 z=0; z<MAP_BLOCKSIZE; z++)
  342. for(u16 y=0; y<MAP_BLOCKSIZE; y++)
  343. for(u16 x=0; x<MAP_BLOCKSIZE; x++)
  344. {
  345. //UASSERT(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_AIR);
  346. UASSERT(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_IGNORE);
  347. UASSERT(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_DAY) == 0);
  348. UASSERT(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_NIGHT) == 0);
  349. }
  350. {
  351. MapNode n(CONTENT_AIR);
  352. for(u16 z=0; z<MAP_BLOCKSIZE; z++)
  353. for(u16 y=0; y<MAP_BLOCKSIZE; y++)
  354. for(u16 x=0; x<MAP_BLOCKSIZE; x++)
  355. {
  356. b.setNode(v3s16(x,y,z), n);
  357. }
  358. }
  359. /*
  360. Parent fetch functions
  361. */
  362. parent.position_valid = false;
  363. parent.node.setContent(5);
  364. MapNode n;
  365. // Positions in the block should still be valid
  366. UASSERT(b.isValidPositionParent(v3s16(0,0,0)) == true);
  367. UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
  368. n = b.getNodeParent(v3s16(0,MAP_BLOCKSIZE-1,0));
  369. UASSERT(n.getContent() == CONTENT_AIR);
  370. // ...but outside the block they should be invalid
  371. UASSERT(b.isValidPositionParent(v3s16(-121,2341,0)) == false);
  372. UASSERT(b.isValidPositionParent(v3s16(-1,0,0)) == false);
  373. UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == false);
  374. {
  375. bool exception_thrown = false;
  376. try{
  377. // This should throw an exception
  378. MapNode n = b.getNodeParent(v3s16(0,0,-1));
  379. }
  380. catch(InvalidPositionException &e)
  381. {
  382. exception_thrown = true;
  383. }
  384. UASSERT(exception_thrown);
  385. }
  386. parent.position_valid = true;
  387. // Now the positions outside should be valid
  388. UASSERT(b.isValidPositionParent(v3s16(-121,2341,0)) == true);
  389. UASSERT(b.isValidPositionParent(v3s16(-1,0,0)) == true);
  390. UASSERT(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == true);
  391. n = b.getNodeParent(v3s16(0,0,MAP_BLOCKSIZE));
  392. UASSERT(n.getContent() == 5);
  393. /*
  394. Set a node
  395. */
  396. v3s16 p(1,2,0);
  397. n.setContent(4);
  398. b.setNode(p, n);
  399. UASSERT(b.getNode(p).getContent() == 4);
  400. //TODO: Update to new system
  401. /*UASSERT(b.getNodeTile(p) == 4);
  402. UASSERT(b.getNodeTile(v3s16(-1,-1,0)) == 5);*/
  403. /*
  404. propagateSunlight()
  405. */
  406. // Set lighting of all nodes to 0
  407. for(u16 z=0; z<MAP_BLOCKSIZE; z++){
  408. for(u16 y=0; y<MAP_BLOCKSIZE; y++){
  409. for(u16 x=0; x<MAP_BLOCKSIZE; x++){
  410. MapNode n = b.getNode(v3s16(x,y,z));
  411. n.setLight(LIGHTBANK_DAY, 0);
  412. n.setLight(LIGHTBANK_NIGHT, 0);
  413. b.setNode(v3s16(x,y,z), n);
  414. }
  415. }
  416. }
  417. {
  418. /*
  419. Check how the block handles being a lonely sky block
  420. */
  421. parent.position_valid = true;
  422. b.setIsUnderground(false);
  423. parent.node.setContent(CONTENT_AIR);
  424. parent.node.setLight(LIGHTBANK_DAY, LIGHT_SUN);
  425. parent.node.setLight(LIGHTBANK_NIGHT, 0);
  426. core::map<v3s16, bool> light_sources;
  427. // The bottom block is invalid, because we have a shadowing node
  428. UASSERT(b.propagateSunlight(light_sources) == false);
  429. UASSERT(b.getNode(v3s16(1,4,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
  430. UASSERT(b.getNode(v3s16(1,3,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
  431. UASSERT(b.getNode(v3s16(1,2,0)).getLight(LIGHTBANK_DAY) == 0);
  432. UASSERT(b.getNode(v3s16(1,1,0)).getLight(LIGHTBANK_DAY) == 0);
  433. UASSERT(b.getNode(v3s16(1,0,0)).getLight(LIGHTBANK_DAY) == 0);
  434. UASSERT(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
  435. UASSERT(b.getFaceLight2(1000, p, v3s16(0,1,0)) == LIGHT_SUN);
  436. UASSERT(b.getFaceLight2(1000, p, v3s16(0,-1,0)) == 0);
  437. UASSERT(b.getFaceLight2(0, p, v3s16(0,-1,0)) == 0);
  438. // According to MapBlock::getFaceLight,
  439. // The face on the z+ side should have double-diminished light
  440. //UASSERT(b.getFaceLight(p, v3s16(0,0,1)) == diminish_light(diminish_light(LIGHT_MAX)));
  441. // The face on the z+ side should have diminished light
  442. UASSERT(b.getFaceLight2(1000, p, v3s16(0,0,1)) == diminish_light(LIGHT_MAX));
  443. }
  444. /*
  445. Check how the block handles being in between blocks with some non-sunlight
  446. while being underground
  447. */
  448. {
  449. // Make neighbours to exist and set some non-sunlight to them
  450. parent.position_valid = true;
  451. b.setIsUnderground(true);
  452. parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
  453. core::map<v3s16, bool> light_sources;
  454. // The block below should be valid because there shouldn't be
  455. // sunlight in there either
  456. UASSERT(b.propagateSunlight(light_sources, true) == true);
  457. // Should not touch nodes that are not affected (that is, all of them)
  458. //UASSERT(b.getNode(v3s16(1,2,3)).getLight() == LIGHT_SUN);
  459. // Should set light of non-sunlighted blocks to 0.
  460. UASSERT(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == 0);
  461. }
  462. /*
  463. Set up a situation where:
  464. - There is only air in this block
  465. - There is a valid non-sunlighted block at the bottom, and
  466. - Invalid blocks elsewhere.
  467. - the block is not underground.
  468. This should result in bottom block invalidity
  469. */
  470. {
  471. b.setIsUnderground(false);
  472. // Clear block
  473. for(u16 z=0; z<MAP_BLOCKSIZE; z++){
  474. for(u16 y=0; y<MAP_BLOCKSIZE; y++){
  475. for(u16 x=0; x<MAP_BLOCKSIZE; x++){
  476. MapNode n;
  477. n.setContent(CONTENT_AIR);
  478. n.setLight(LIGHTBANK_DAY, 0);
  479. b.setNode(v3s16(x,y,z), n);
  480. }
  481. }
  482. }
  483. // Make neighbours invalid
  484. parent.position_valid = false;
  485. // Add exceptions to the top of the bottom block
  486. for(u16 x=0; x<MAP_BLOCKSIZE; x++)
  487. for(u16 z=0; z<MAP_BLOCKSIZE; z++)
  488. {
  489. parent.validity_exceptions.push_back(v3s16(MAP_BLOCKSIZE+x, MAP_BLOCKSIZE-1, MAP_BLOCKSIZE+z));
  490. }
  491. // Lighting value for the valid nodes
  492. parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
  493. core::map<v3s16, bool> light_sources;
  494. // Bottom block is not valid
  495. UASSERT(b.propagateSunlight(light_sources) == false);
  496. }
  497. }
  498. };
  499. struct TestMapSector: public TestBase
  500. {
  501. class TC : public NodeContainer
  502. {
  503. public:
  504. MapNode node;
  505. bool position_valid;
  506. TC()
  507. {
  508. position_valid = true;
  509. }
  510. virtual bool isValidPosition(v3s16 p)
  511. {
  512. return position_valid;
  513. }
  514. virtual MapNode getNode(v3s16 p)
  515. {
  516. if(position_valid == false)
  517. throw InvalidPositionException();
  518. return node;
  519. }
  520. virtual void setNode(v3s16 p, MapNode & n)
  521. {
  522. if(position_valid == false)
  523. throw InvalidPositionException();
  524. };
  525. virtual u16 nodeContainerId() const
  526. {
  527. return 666;
  528. }
  529. };
  530. void Run()
  531. {
  532. TC parent;
  533. parent.position_valid = false;
  534. // Create one with no heightmaps
  535. ServerMapSector sector(&parent, v2s16(1,1));
  536. UASSERT(sector.getBlockNoCreateNoEx(0) == 0);
  537. UASSERT(sector.getBlockNoCreateNoEx(1) == 0);
  538. MapBlock * bref = sector.createBlankBlock(-2);
  539. UASSERT(sector.getBlockNoCreateNoEx(0) == 0);
  540. UASSERT(sector.getBlockNoCreateNoEx(-2) == bref);
  541. //TODO: Check for AlreadyExistsException
  542. /*bool exception_thrown = false;
  543. try{
  544. sector.getBlock(0);
  545. }
  546. catch(InvalidPositionException &e){
  547. exception_thrown = true;
  548. }
  549. UASSERT(exception_thrown);*/
  550. }
  551. };
  552. #endif