test_voxelmanipulator.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 <algorithm>
  18. #include "gamedef.h"
  19. #include "log.h"
  20. #include "voxel.h"
  21. class TestVoxelManipulator : public TestBase {
  22. public:
  23. TestVoxelManipulator() { TestManager::registerTestModule(this); }
  24. const char *getName() { return "TestVoxelManipulator"; }
  25. void runTests(IGameDef *gamedef);
  26. void testVoxelArea();
  27. void testVoxelManipulator(INodeDefManager *nodedef);
  28. };
  29. static TestVoxelManipulator g_test_instance;
  30. void TestVoxelManipulator::runTests(IGameDef *gamedef)
  31. {
  32. TEST(testVoxelArea);
  33. TEST(testVoxelManipulator, gamedef->getNodeDefManager());
  34. }
  35. ////////////////////////////////////////////////////////////////////////////////
  36. void TestVoxelManipulator::testVoxelArea()
  37. {
  38. VoxelArea a(v3s16(-1,-1,-1), v3s16(1,1,1));
  39. UASSERT(a.index(0,0,0) == 1*3*3 + 1*3 + 1);
  40. UASSERT(a.index(-1,-1,-1) == 0);
  41. VoxelArea c(v3s16(-2,-2,-2), v3s16(2,2,2));
  42. // An area that is 1 bigger in x+ and z-
  43. VoxelArea d(v3s16(-2,-2,-3), v3s16(3,2,2));
  44. std::list<VoxelArea> aa;
  45. d.diff(c, aa);
  46. // Correct results
  47. std::vector<VoxelArea> results;
  48. results.emplace_back(v3s16(-2,-2,-3), v3s16(3,2,-3));
  49. results.emplace_back(v3s16(3,-2,-2), v3s16(3,2,2));
  50. UASSERT(aa.size() == results.size());
  51. infostream<<"Result of diff:"<<std::endl;
  52. for (std::list<VoxelArea>::const_iterator
  53. it = aa.begin(); it != aa.end(); ++it) {
  54. it->print(infostream);
  55. infostream << std::endl;
  56. std::vector<VoxelArea>::iterator j;
  57. j = std::find(results.begin(), results.end(), *it);
  58. UASSERT(j != results.end());
  59. results.erase(j);
  60. }
  61. }
  62. void TestVoxelManipulator::testVoxelManipulator(INodeDefManager *nodedef)
  63. {
  64. VoxelManipulator v;
  65. v.print(infostream, nodedef);
  66. infostream << "*** Setting (-1,0,-1)=2 ***" << std::endl;
  67. v.setNodeNoRef(v3s16(-1,0,-1), MapNode(t_CONTENT_GRASS));
  68. v.print(infostream, nodedef);
  69. UASSERT(v.getNode(v3s16(-1,0,-1)).getContent() == t_CONTENT_GRASS);
  70. infostream << "*** Reading from inexistent (0,0,-1) ***" << std::endl;
  71. EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,0,-1)));
  72. v.print(infostream, nodedef);
  73. infostream << "*** Adding area ***" << std::endl;
  74. VoxelArea a(v3s16(-1,-1,-1), v3s16(1,1,1));
  75. v.addArea(a);
  76. v.print(infostream, nodedef);
  77. UASSERT(v.getNode(v3s16(-1,0,-1)).getContent() == t_CONTENT_GRASS);
  78. EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,1,1)));
  79. }