test.cpp 19 KB

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