test_voxelarea.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. Minetest
  3. Copyright (C) 2018 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
  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 "voxel.h"
  18. class TestVoxelArea : public TestBase
  19. {
  20. public:
  21. TestVoxelArea() { TestManager::registerTestModule(this); }
  22. const char *getName() { return "TestVoxelArea"; }
  23. void runTests(IGameDef *gamedef);
  24. void test_addarea();
  25. void test_pad();
  26. void test_extent();
  27. void test_volume();
  28. void test_contains_voxelarea();
  29. void test_contains_point();
  30. void test_contains_i();
  31. void test_equal();
  32. void test_plus();
  33. void test_minor();
  34. void test_index_xyz_all_pos();
  35. void test_index_xyz_x_neg();
  36. void test_index_xyz_y_neg();
  37. void test_index_xyz_z_neg();
  38. void test_index_xyz_xy_neg();
  39. void test_index_xyz_xz_neg();
  40. void test_index_xyz_yz_neg();
  41. void test_index_xyz_all_neg();
  42. void test_index_v3s16_all_pos();
  43. void test_index_v3s16_x_neg();
  44. void test_index_v3s16_y_neg();
  45. void test_index_v3s16_z_neg();
  46. void test_index_v3s16_xy_neg();
  47. void test_index_v3s16_xz_neg();
  48. void test_index_v3s16_yz_neg();
  49. void test_index_v3s16_all_neg();
  50. void test_add_x();
  51. void test_add_y();
  52. void test_add_z();
  53. void test_add_p();
  54. };
  55. static TestVoxelArea g_test_instance;
  56. void TestVoxelArea::runTests(IGameDef *gamedef)
  57. {
  58. TEST(test_addarea);
  59. TEST(test_pad);
  60. TEST(test_extent);
  61. TEST(test_volume);
  62. TEST(test_contains_voxelarea);
  63. TEST(test_contains_point);
  64. TEST(test_contains_i);
  65. TEST(test_equal);
  66. TEST(test_plus);
  67. TEST(test_minor);
  68. TEST(test_index_xyz_all_pos);
  69. TEST(test_index_xyz_x_neg);
  70. TEST(test_index_xyz_y_neg);
  71. TEST(test_index_xyz_z_neg);
  72. TEST(test_index_xyz_xy_neg);
  73. TEST(test_index_xyz_xz_neg);
  74. TEST(test_index_xyz_yz_neg);
  75. TEST(test_index_xyz_all_neg);
  76. TEST(test_index_v3s16_all_pos);
  77. TEST(test_index_v3s16_x_neg);
  78. TEST(test_index_v3s16_y_neg);
  79. TEST(test_index_v3s16_z_neg);
  80. TEST(test_index_v3s16_xy_neg);
  81. TEST(test_index_v3s16_xz_neg);
  82. TEST(test_index_v3s16_yz_neg);
  83. TEST(test_index_v3s16_all_neg);
  84. TEST(test_add_x);
  85. TEST(test_add_y);
  86. TEST(test_add_z);
  87. TEST(test_add_p);
  88. }
  89. void TestVoxelArea::test_addarea()
  90. {
  91. VoxelArea v1(v3s16(-1447, 8854, -875), v3s16(-147, -9547, 669));
  92. VoxelArea v2(v3s16(-887, 4445, -5478), v3s16(447, -8779, 4778));
  93. v1.addArea(v2);
  94. UASSERT(v1.MinEdge == v3s16(-1447, 4445, -5478));
  95. UASSERT(v1.MaxEdge == v3s16(447, -8779, 4778));
  96. }
  97. void TestVoxelArea::test_pad()
  98. {
  99. VoxelArea v1(v3s16(-1447, 8854, -875), v3s16(-147, -9547, 669));
  100. v1.pad(v3s16(100, 200, 300));
  101. UASSERT(v1.MinEdge == v3s16(-1547, 8654, -1175));
  102. UASSERT(v1.MaxEdge == v3s16(-47, -9347, 969));
  103. }
  104. void TestVoxelArea::test_extent()
  105. {
  106. VoxelArea v1(v3s16(-1337, -547, -789), v3s16(-147, 447, 669));
  107. UASSERT(v1.getExtent() == v3s16(1191, 995, 1459));
  108. VoxelArea v2(v3s16(32493, -32507, 32752), v3s16(32508, -32492, 32767));
  109. UASSERT(v2.getExtent() == v3s16(16, 16, 16));
  110. }
  111. void TestVoxelArea::test_volume()
  112. {
  113. VoxelArea v1(v3s16(-1337, -547, -789), v3s16(-147, 447, 669));
  114. UASSERTEQ(s32, v1.getVolume(), 1728980655);
  115. VoxelArea v2(v3s16(32493, -32507, 32752), v3s16(32508, -32492, 32767));
  116. UASSERTEQ(s32, v2.getVolume(), 4096);
  117. }
  118. void TestVoxelArea::test_contains_voxelarea()
  119. {
  120. VoxelArea v1(v3s16(-1337, -9547, -789), v3s16(-147, 750, 669));
  121. UASSERTEQ(bool, v1.contains(VoxelArea(v3s16(-200, 10, 10), v3s16(-150, 10, 10))),
  122. true);
  123. UASSERTEQ(bool, v1.contains(VoxelArea(v3s16(-2550, 10, 10), v3s16(10, 10, 10))),
  124. false);
  125. UASSERTEQ(bool, v1.contains(VoxelArea(v3s16(-10, 10, 10), v3s16(3500, 10, 10))),
  126. false);
  127. UASSERTEQ(bool,
  128. v1.contains(VoxelArea(
  129. v3s16(-800, -400, 669), v3s16(-500, 200, 669))),
  130. true);
  131. UASSERTEQ(bool,
  132. v1.contains(VoxelArea(
  133. v3s16(-800, -400, 670), v3s16(-500, 200, 670))),
  134. false);
  135. }
  136. void TestVoxelArea::test_contains_point()
  137. {
  138. VoxelArea v1(v3s16(-1337, -9547, -789), v3s16(-147, 750, 669));
  139. UASSERTEQ(bool, v1.contains(v3s16(-200, 10, 10)), true);
  140. UASSERTEQ(bool, v1.contains(v3s16(-10000, 10, 10)), false);
  141. UASSERTEQ(bool, v1.contains(v3s16(-100, 10000, 10)), false);
  142. UASSERTEQ(bool, v1.contains(v3s16(-100, 100, 10000)), false);
  143. UASSERTEQ(bool, v1.contains(v3s16(-100, 100, -10000)), false);
  144. UASSERTEQ(bool, v1.contains(v3s16(10000, 100, 10)), false);
  145. }
  146. void TestVoxelArea::test_contains_i()
  147. {
  148. VoxelArea v1(v3s16(-1337, -9547, -789), v3s16(-147, 750, 669));
  149. UASSERTEQ(bool, v1.contains(10), true);
  150. UASSERTEQ(bool, v1.contains(v1.getVolume()), false);
  151. UASSERTEQ(bool, v1.contains(v1.getVolume() - 1), true);
  152. UASSERTEQ(bool, v1.contains(v1.getVolume() + 1), false);
  153. UASSERTEQ(bool, v1.contains(-1), false)
  154. VoxelArea v2(v3s16(10, 10, 10), v3s16(30, 30, 30));
  155. UASSERTEQ(bool, v2.contains(10), true);
  156. UASSERTEQ(bool, v2.contains(0), true);
  157. UASSERTEQ(bool, v2.contains(-1), false);
  158. }
  159. void TestVoxelArea::test_equal()
  160. {
  161. VoxelArea v1(v3s16(-1337, -9547, -789), v3s16(-147, 750, 669));
  162. UASSERTEQ(bool, v1 == VoxelArea(v3s16(-1337, -9547, -789), v3s16(-147, 750, 669)),
  163. true);
  164. UASSERTEQ(bool, v1 == VoxelArea(v3s16(0, 0, 0), v3s16(-147, 750, 669)), false);
  165. UASSERTEQ(bool, v1 == VoxelArea(v3s16(0, 0, 0), v3s16(-147, 750, 669)), false);
  166. UASSERTEQ(bool, v1 == VoxelArea(v3s16(0, 0, 0), v3s16(0, 0, 0)), false);
  167. }
  168. void TestVoxelArea::test_plus()
  169. {
  170. VoxelArea v1(v3s16(-10, -10, -10), v3s16(100, 100, 100));
  171. UASSERT(v1 + v3s16(10, 0, 0) ==
  172. VoxelArea(v3s16(0, -10, -10), v3s16(110, 100, 100)));
  173. UASSERT(v1 + v3s16(10, -10, 0) ==
  174. VoxelArea(v3s16(0, -20, -10), v3s16(110, 90, 100)));
  175. UASSERT(v1 + v3s16(0, 0, 35) ==
  176. VoxelArea(v3s16(-10, -10, 25), v3s16(100, 100, 135)));
  177. }
  178. void TestVoxelArea::test_minor()
  179. {
  180. VoxelArea v1(v3s16(-10, -10, -10), v3s16(100, 100, 100));
  181. UASSERT(v1 - v3s16(10, 0, 0) ==
  182. VoxelArea(v3s16(-20, -10, -10), v3s16(90, 100, 100)));
  183. UASSERT(v1 - v3s16(10, -10, 0) ==
  184. VoxelArea(v3s16(-20, 0, -10), v3s16(90, 110, 100)));
  185. UASSERT(v1 - v3s16(0, 0, 35) ==
  186. VoxelArea(v3s16(-10, -10, -45), v3s16(100, 100, 65)));
  187. }
  188. void TestVoxelArea::test_index_xyz_all_pos()
  189. {
  190. VoxelArea v1;
  191. UASSERTEQ(s32, v1.index(156, 25, 236), 155);
  192. VoxelArea v2(v3s16(756, 8854, -875), v3s16(-147, -9547, 669));
  193. UASSERTEQ(s32, v2.index(156, 25, 236), 1267138774);
  194. }
  195. void TestVoxelArea::test_index_xyz_x_neg()
  196. {
  197. VoxelArea v1;
  198. UASSERTEQ(s32, v1.index(-147, 25, 366), -148);
  199. VoxelArea v2(v3s16(756, 8854, -875), v3s16(-147, -9547, 669));
  200. UASSERTEQ(s32, v2.index(-147, 25, 366), -870244825);
  201. }
  202. void TestVoxelArea::test_index_xyz_y_neg()
  203. {
  204. VoxelArea v1;
  205. UASSERTEQ(s32, v1.index(247, -269, 100), 246);
  206. VoxelArea v2(v3s16(756, 8854, -875), v3s16(-147, -9547, 669));
  207. UASSERTEQ(s32, v2.index(247, -269, 100), -989760747);
  208. }
  209. void TestVoxelArea::test_index_xyz_z_neg()
  210. {
  211. VoxelArea v1;
  212. UASSERTEQ(s32, v1.index(244, 336, -887), 243);
  213. VoxelArea v2(v3s16(756, 8854, -875), v3s16(-147, -9547, 669));
  214. UASSERTEQ(s32, v2.index(244, 336, -887), -191478876);
  215. }
  216. void TestVoxelArea::test_index_xyz_xy_neg()
  217. {
  218. VoxelArea v1;
  219. UASSERTEQ(s32, v1.index(-365, -47, 6978), -366);
  220. VoxelArea v2(v3s16(756, 8854, -875), v3s16(-147, -9547, 669));
  221. UASSERTEQ(s32, v2.index(-365, -47, 6978), 1493679101);
  222. }
  223. void TestVoxelArea::test_index_xyz_yz_neg()
  224. {
  225. VoxelArea v1;
  226. UASSERTEQ(s32, v1.index(66, -58, -789), 65);
  227. VoxelArea v2(v3s16(756, 8854, -875), v3s16(-147, -9547, 669));
  228. UASSERTEQ(s32, v2.index(66, -58, -789), 1435362734);
  229. }
  230. void TestVoxelArea::test_index_xyz_xz_neg()
  231. {
  232. VoxelArea v1;
  233. UASSERTEQ(s32, v1.index(-36, 589, -992), -37);
  234. VoxelArea v2(v3s16(756, 8854, -875), v3s16(-147, -9547, 669));
  235. UASSERTEQ(s32, v2.index(-36, 589, -992), -1934371362);
  236. }
  237. void TestVoxelArea::test_index_xyz_all_neg()
  238. {
  239. VoxelArea v1;
  240. UASSERTEQ(s32, v1.index(-88, -99, -1474), -89);
  241. VoxelArea v2(v3s16(756, 8854, -875), v3s16(-147, -9547, 669));
  242. UASSERTEQ(s32, v2.index(-88, -99, -1474), -1343473846);
  243. }
  244. void TestVoxelArea::test_index_v3s16_all_pos()
  245. {
  246. VoxelArea v1;
  247. UASSERTEQ(s32, v1.index(v3s16(156, 25, 236)), 155);
  248. VoxelArea v2(v3s16(756, 8854, -875), v3s16(-147, -9547, 669));
  249. UASSERTEQ(s32, v2.index(v3s16(156, 25, 236)), 1267138774);
  250. }
  251. void TestVoxelArea::test_index_v3s16_x_neg()
  252. {
  253. VoxelArea v1;
  254. UASSERTEQ(s32, v1.index(v3s16(-147, 25, 366)), -148);
  255. VoxelArea v2(v3s16(756, 8854, -875), v3s16(-147, -9547, 669));
  256. UASSERTEQ(s32, v2.index(v3s16(-147, 25, 366)), -870244825);
  257. }
  258. void TestVoxelArea::test_index_v3s16_y_neg()
  259. {
  260. VoxelArea v1;
  261. UASSERTEQ(s32, v1.index(v3s16(247, -269, 100)), 246);
  262. VoxelArea v2(v3s16(756, 8854, -875), v3s16(-147, -9547, 669));
  263. UASSERTEQ(s32, v2.index(v3s16(247, -269, 100)), -989760747);
  264. }
  265. void TestVoxelArea::test_index_v3s16_z_neg()
  266. {
  267. VoxelArea v1;
  268. UASSERTEQ(s32, v1.index(v3s16(244, 336, -887)), 243);
  269. VoxelArea v2(v3s16(756, 8854, -875), v3s16(-147, -9547, 669));
  270. UASSERTEQ(s32, v2.index(v3s16(244, 336, -887)), -191478876);
  271. }
  272. void TestVoxelArea::test_index_v3s16_xy_neg()
  273. {
  274. VoxelArea v1;
  275. UASSERTEQ(s32, v1.index(v3s16(-365, -47, 6978)), -366);
  276. VoxelArea v2(v3s16(756, 8854, -875), v3s16(-147, -9547, 669));
  277. UASSERTEQ(s32, v2.index(v3s16(-365, -47, 6978)), 1493679101);
  278. }
  279. void TestVoxelArea::test_index_v3s16_yz_neg()
  280. {
  281. VoxelArea v1;
  282. UASSERTEQ(s32, v1.index(v3s16(66, -58, -789)), 65);
  283. VoxelArea v2(v3s16(756, 8854, -875), v3s16(-147, -9547, 669));
  284. UASSERTEQ(s32, v2.index(v3s16(66, -58, -789)), 1435362734);
  285. }
  286. void TestVoxelArea::test_index_v3s16_xz_neg()
  287. {
  288. VoxelArea v1;
  289. UASSERTEQ(s32, v1.index(v3s16(-36, 589, -992)), -37);
  290. VoxelArea v2(v3s16(756, 8854, -875), v3s16(-147, -9547, 669));
  291. UASSERTEQ(s32, v2.index(v3s16(-36, 589, -992)), -1934371362);
  292. }
  293. void TestVoxelArea::test_index_v3s16_all_neg()
  294. {
  295. VoxelArea v1;
  296. UASSERTEQ(s32, v1.index(v3s16(-88, -99, -1474)), -89);
  297. VoxelArea v2(v3s16(756, 8854, -875), v3s16(-147, -9547, 669));
  298. UASSERTEQ(s32, v2.index(v3s16(-88, -99, -1474)), -1343473846);
  299. }
  300. void TestVoxelArea::test_add_x()
  301. {
  302. v3s16 extent;
  303. u32 i = 4;
  304. VoxelArea::add_x(extent, i, 8);
  305. UASSERTEQ(u32, i, 12)
  306. }
  307. void TestVoxelArea::test_add_y()
  308. {
  309. v3s16 extent(740, 16, 87);
  310. u32 i = 8;
  311. VoxelArea::add_y(extent, i, 88);
  312. UASSERTEQ(u32, i, 65128)
  313. }
  314. void TestVoxelArea::test_add_z()
  315. {
  316. v3s16 extent(114, 80, 256);
  317. u32 i = 4;
  318. VoxelArea::add_z(extent, i, 8);
  319. UASSERTEQ(u32, i, 72964)
  320. }
  321. void TestVoxelArea::test_add_p()
  322. {
  323. v3s16 extent(33, 14, 742);
  324. v3s16 a(15, 12, 369);
  325. u32 i = 4;
  326. VoxelArea::add_p(extent, i, a);
  327. UASSERTEQ(u32, i, 170893)
  328. }