test_vector4.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /**************************************************************************/
  2. /* test_vector4.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef TEST_VECTOR4_H
  31. #define TEST_VECTOR4_H
  32. #include "core/math/vector4.h"
  33. #include "tests/test_macros.h"
  34. #define Math_SQRT3 1.7320508075688772935274463415059
  35. namespace TestVector4 {
  36. TEST_CASE("[Vector4] Constructor methods") {
  37. const Vector4 vector_empty = Vector4();
  38. const Vector4 vector_zero = Vector4(0.0, 0.0, 0.0, 0.0);
  39. CHECK_MESSAGE(
  40. vector_empty == vector_zero,
  41. "Vector4 Constructor with no inputs should return a zero Vector4.");
  42. }
  43. TEST_CASE("[Vector4] Axis methods") {
  44. Vector4 vector = Vector4(1.2, 3.4, 5.6, -0.9);
  45. CHECK_MESSAGE(
  46. vector.max_axis_index() == Vector4::Axis::AXIS_Z,
  47. "Vector4 max_axis_index should work as expected.");
  48. CHECK_MESSAGE(
  49. vector.min_axis_index() == Vector4::Axis::AXIS_W,
  50. "Vector4 min_axis_index should work as expected.");
  51. CHECK_MESSAGE(
  52. vector[vector.max_axis_index()] == (real_t)5.6,
  53. "Vector4 array operator should work as expected.");
  54. CHECK_MESSAGE(
  55. vector[vector.min_axis_index()] == (real_t)-0.9,
  56. "Vector4 array operator should work as expected.");
  57. vector[Vector4::Axis::AXIS_Y] = 3.7;
  58. CHECK_MESSAGE(
  59. vector[Vector4::Axis::AXIS_Y] == (real_t)3.7,
  60. "Vector4 array operator setter should work as expected.");
  61. }
  62. TEST_CASE("[Vector4] Interpolation methods") {
  63. const Vector4 vector1 = Vector4(1, 2, 3, 4);
  64. const Vector4 vector2 = Vector4(4, 5, 6, 7);
  65. CHECK_MESSAGE(
  66. vector1.lerp(vector2, 0.5) == Vector4(2.5, 3.5, 4.5, 5.5),
  67. "Vector4 lerp should work as expected.");
  68. CHECK_MESSAGE(
  69. vector1.lerp(vector2, 1.0 / 3.0).is_equal_approx(Vector4(2, 3, 4, 5)),
  70. "Vector4 lerp should work as expected.");
  71. CHECK_MESSAGE(
  72. vector1.cubic_interpolate(vector2, Vector4(), Vector4(7, 7, 7, 7), 0.5) == Vector4(2.375, 3.5, 4.625, 5.75),
  73. "Vector4 cubic_interpolate should work as expected.");
  74. CHECK_MESSAGE(
  75. vector1.cubic_interpolate(vector2, Vector4(), Vector4(7, 7, 7, 7), 1.0 / 3.0).is_equal_approx(Vector4(1.851851940155029297, 2.962963104248046875, 4.074074268341064453, 5.185185185185)),
  76. "Vector4 cubic_interpolate should work as expected.");
  77. }
  78. TEST_CASE("[Vector4] Length methods") {
  79. const Vector4 vector1 = Vector4(10, 10, 10, 10);
  80. const Vector4 vector2 = Vector4(20, 30, 40, 50);
  81. CHECK_MESSAGE(
  82. vector1.length_squared() == 400,
  83. "Vector4 length_squared should work as expected and return exact result.");
  84. CHECK_MESSAGE(
  85. vector1.length() == doctest::Approx(20),
  86. "Vector4 length should work as expected.");
  87. CHECK_MESSAGE(
  88. vector2.length_squared() == 5400,
  89. "Vector4 length_squared should work as expected and return exact result.");
  90. CHECK_MESSAGE(
  91. vector2.length() == doctest::Approx((real_t)73.484692283495),
  92. "Vector4 length should work as expected.");
  93. CHECK_MESSAGE(
  94. vector1.distance_to(vector2) == doctest::Approx((real_t)54.772255750517),
  95. "Vector4 distance_to should work as expected.");
  96. CHECK_MESSAGE(
  97. vector1.distance_squared_to(vector2) == doctest::Approx(3000),
  98. "Vector4 distance_squared_to should work as expected.");
  99. }
  100. TEST_CASE("[Vector4] Limiting methods") {
  101. const Vector4 vector = Vector4(10, 10, 10, 10);
  102. CHECK_MESSAGE(
  103. Vector4(-5, 5, 15, -15).clamp(Vector4(), vector) == Vector4(0, 5, 10, 0),
  104. "Vector4 clamp should work as expected.");
  105. CHECK_MESSAGE(
  106. vector.clamp(Vector4(0, 10, 15, 18), Vector4(5, 10, 20, 25)) == Vector4(5, 10, 15, 18),
  107. "Vector4 clamp should work as expected.");
  108. }
  109. TEST_CASE("[Vector4] Normalization methods") {
  110. CHECK_MESSAGE(
  111. Vector4(1, 0, 0, 0).is_normalized() == true,
  112. "Vector4 is_normalized should return true for a normalized vector.");
  113. CHECK_MESSAGE(
  114. Vector4(1, 1, 1, 1).is_normalized() == false,
  115. "Vector4 is_normalized should return false for a non-normalized vector.");
  116. CHECK_MESSAGE(
  117. Vector4(1, 0, 0, 0).normalized() == Vector4(1, 0, 0, 0),
  118. "Vector4 normalized should return the same vector for a normalized vector.");
  119. CHECK_MESSAGE(
  120. Vector4(1, 1, 0, 0).normalized().is_equal_approx(Vector4(Math_SQRT12, Math_SQRT12, 0, 0)),
  121. "Vector4 normalized should work as expected.");
  122. CHECK_MESSAGE(
  123. Vector4(1, 1, 1, 1).normalized().is_equal_approx(Vector4(0.5, 0.5, 0.5, 0.5)),
  124. "Vector4 normalized should work as expected.");
  125. }
  126. TEST_CASE("[Vector4] Operators") {
  127. const Vector4 decimal1 = Vector4(2.3, 4.9, 7.8, 3.2);
  128. const Vector4 decimal2 = Vector4(1.2, 3.4, 5.6, 1.7);
  129. const Vector4 power1 = Vector4(0.75, 1.5, 0.625, 0.125);
  130. const Vector4 power2 = Vector4(0.5, 0.125, 0.25, 0.75);
  131. const Vector4 int1 = Vector4(4, 5, 9, 2);
  132. const Vector4 int2 = Vector4(1, 2, 3, 1);
  133. CHECK_MESSAGE(
  134. -decimal1 == Vector4(-2.3, -4.9, -7.8, -3.2),
  135. "Vector4 change of sign should work as expected.");
  136. CHECK_MESSAGE(
  137. (decimal1 + decimal2).is_equal_approx(Vector4(3.5, 8.3, 13.4, 4.9)),
  138. "Vector4 addition should behave as expected.");
  139. CHECK_MESSAGE(
  140. (power1 + power2) == Vector4(1.25, 1.625, 0.875, 0.875),
  141. "Vector4 addition with powers of two should give exact results.");
  142. CHECK_MESSAGE(
  143. (int1 + int2) == Vector4(5, 7, 12, 3),
  144. "Vector4 addition with integers should give exact results.");
  145. CHECK_MESSAGE(
  146. (decimal1 - decimal2).is_equal_approx(Vector4(1.1, 1.5, 2.2, 1.5)),
  147. "Vector4 subtraction should behave as expected.");
  148. CHECK_MESSAGE(
  149. (power1 - power2) == Vector4(0.25, 1.375, 0.375, -0.625),
  150. "Vector4 subtraction with powers of two should give exact results.");
  151. CHECK_MESSAGE(
  152. (int1 - int2) == Vector4(3, 3, 6, 1),
  153. "Vector4 subtraction with integers should give exact results.");
  154. CHECK_MESSAGE(
  155. (decimal1 * decimal2).is_equal_approx(Vector4(2.76, 16.66, 43.68, 5.44)),
  156. "Vector4 multiplication should behave as expected.");
  157. CHECK_MESSAGE(
  158. (power1 * power2) == Vector4(0.375, 0.1875, 0.15625, 0.09375),
  159. "Vector4 multiplication with powers of two should give exact results.");
  160. CHECK_MESSAGE(
  161. (int1 * int2) == Vector4(4, 10, 27, 2),
  162. "Vector4 multiplication with integers should give exact results.");
  163. CHECK_MESSAGE(
  164. (decimal1 / decimal2).is_equal_approx(Vector4(1.91666666666666666, 1.44117647058823529, 1.39285714285714286, 1.88235294118)),
  165. "Vector4 division should behave as expected.");
  166. CHECK_MESSAGE(
  167. (power1 / power2) == Vector4(1.5, 12.0, 2.5, 1.0 / 6.0),
  168. "Vector4 division with powers of two should give exact results.");
  169. CHECK_MESSAGE(
  170. (int1 / int2) == Vector4(4, 2.5, 3, 2),
  171. "Vector4 division with integers should give exact results.");
  172. CHECK_MESSAGE(
  173. (decimal1 * 2).is_equal_approx(Vector4(4.6, 9.8, 15.6, 6.4)),
  174. "Vector4 multiplication should behave as expected.");
  175. CHECK_MESSAGE(
  176. (power1 * 2) == Vector4(1.5, 3, 1.25, 0.25),
  177. "Vector4 multiplication with powers of two should give exact results.");
  178. CHECK_MESSAGE(
  179. (int1 * 2) == Vector4(8, 10, 18, 4),
  180. "Vector4 multiplication with integers should give exact results.");
  181. CHECK_MESSAGE(
  182. (decimal1 / 2).is_equal_approx(Vector4(1.15, 2.45, 3.9, 1.6)),
  183. "Vector4 division should behave as expected.");
  184. CHECK_MESSAGE(
  185. (power1 / 2) == Vector4(0.375, 0.75, 0.3125, 0.0625),
  186. "Vector4 division with powers of two should give exact results.");
  187. CHECK_MESSAGE(
  188. (int1 / 2) == Vector4(2, 2.5, 4.5, 1),
  189. "Vector4 division with integers should give exact results.");
  190. CHECK_MESSAGE(
  191. ((String)decimal1) == "(2.3, 4.9, 7.8, 3.2)",
  192. "Vector4 cast to String should work as expected.");
  193. CHECK_MESSAGE(
  194. ((String)decimal2) == "(1.2, 3.4, 5.6, 1.7)",
  195. "Vector4 cast to String should work as expected.");
  196. CHECK_MESSAGE(
  197. ((String)Vector4(9.7, 9.8, 9.9, -1.8)) == "(9.7, 9.8, 9.9, -1.8)",
  198. "Vector4 cast to String should work as expected.");
  199. #ifdef REAL_T_IS_DOUBLE
  200. CHECK_MESSAGE(
  201. ((String)Vector4(Math_E, Math_SQRT2, Math_SQRT3, Math_SQRT3)) == "(2.71828182845905, 1.4142135623731, 1.73205080756888, 1.73205080756888)",
  202. "Vector4 cast to String should print the correct amount of digits for real_t = double.");
  203. #else
  204. CHECK_MESSAGE(
  205. ((String)Vector4(Math_E, Math_SQRT2, Math_SQRT3, Math_SQRT3)) == "(2.718282, 1.414214, 1.732051, 1.732051)",
  206. "Vector4 cast to String should print the correct amount of digits for real_t = float.");
  207. #endif // REAL_T_IS_DOUBLE
  208. }
  209. TEST_CASE("[Vector4] Other methods") {
  210. const Vector4 vector = Vector4(1.2, 3.4, 5.6, 1.6);
  211. CHECK_MESSAGE(
  212. vector.direction_to(Vector4()).is_equal_approx(-vector.normalized()),
  213. "Vector4 direction_to should work as expected.");
  214. CHECK_MESSAGE(
  215. Vector4(1, 1, 1, 1).direction_to(Vector4(2, 2, 2, 2)).is_equal_approx(Vector4(0.5, 0.5, 0.5, 0.5)),
  216. "Vector4 direction_to should work as expected.");
  217. CHECK_MESSAGE(
  218. vector.inverse().is_equal_approx(Vector4(1 / 1.2, 1 / 3.4, 1 / 5.6, 1 / 1.6)),
  219. "Vector4 inverse should work as expected.");
  220. CHECK_MESSAGE(
  221. vector.posmod(2).is_equal_approx(Vector4(1.2, 1.4, 1.6, 1.6)),
  222. "Vector4 posmod should work as expected.");
  223. CHECK_MESSAGE(
  224. (-vector).posmod(2).is_equal_approx(Vector4(0.8, 0.6, 0.4, 0.4)),
  225. "Vector4 posmod should work as expected.");
  226. CHECK_MESSAGE(
  227. vector.posmodv(Vector4(1, 2, 3, 4)).is_equal_approx(Vector4(0.2, 1.4, 2.6, 1.6)),
  228. "Vector4 posmodv should work as expected.");
  229. CHECK_MESSAGE(
  230. (-vector).posmodv(Vector4(2, 3, 4, 5)).is_equal_approx(Vector4(0.8, 2.6, 2.4, 3.4)),
  231. "Vector4 posmodv should work as expected.");
  232. CHECK_MESSAGE(
  233. vector.snapped(Vector4(1, 1, 1, 1)) == Vector4(1, 3, 6, 2),
  234. "Vector4 snapped to integers should be the same as rounding.");
  235. CHECK_MESSAGE(
  236. vector.snapped(Vector4(0.25, 0.25, 0.25, 0.25)) == Vector4(1.25, 3.5, 5.5, 1.5),
  237. "Vector4 snapped to 0.25 should give exact results.");
  238. CHECK_MESSAGE(
  239. Vector4(1.2, 2.5, 2.0, 1.6).is_equal_approx(vector.min(Vector4(3.0, 2.5, 2.0, 3.4))),
  240. "Vector4 min should return expected value.");
  241. CHECK_MESSAGE(
  242. Vector4(5.3, 3.4, 5.6, 4.2).is_equal_approx(vector.max(Vector4(5.3, 2.0, 3.0, 4.2))),
  243. "Vector4 max should return expected value.");
  244. }
  245. TEST_CASE("[Vector4] Rounding methods") {
  246. const Vector4 vector1 = Vector4(1.2, 3.4, 5.6, 1.6);
  247. const Vector4 vector2 = Vector4(1.2, -3.4, -5.6, -1.6);
  248. CHECK_MESSAGE(
  249. vector1.abs() == vector1,
  250. "Vector4 abs should work as expected.");
  251. CHECK_MESSAGE(
  252. vector2.abs() == vector1,
  253. "Vector4 abs should work as expected.");
  254. CHECK_MESSAGE(
  255. vector1.ceil() == Vector4(2, 4, 6, 2),
  256. "Vector4 ceil should work as expected.");
  257. CHECK_MESSAGE(
  258. vector2.ceil() == Vector4(2, -3, -5, -1),
  259. "Vector4 ceil should work as expected.");
  260. CHECK_MESSAGE(
  261. vector1.floor() == Vector4(1, 3, 5, 1),
  262. "Vector4 floor should work as expected.");
  263. CHECK_MESSAGE(
  264. vector2.floor() == Vector4(1, -4, -6, -2),
  265. "Vector4 floor should work as expected.");
  266. CHECK_MESSAGE(
  267. vector1.round() == Vector4(1, 3, 6, 2),
  268. "Vector4 round should work as expected.");
  269. CHECK_MESSAGE(
  270. vector2.round() == Vector4(1, -3, -6, -2),
  271. "Vector4 round should work as expected.");
  272. CHECK_MESSAGE(
  273. vector1.sign() == Vector4(1, 1, 1, 1),
  274. "Vector4 sign should work as expected.");
  275. CHECK_MESSAGE(
  276. vector2.sign() == Vector4(1, -1, -1, -1),
  277. "Vector4 sign should work as expected.");
  278. }
  279. TEST_CASE("[Vector4] Linear algebra methods") {
  280. const Vector4 vector_x = Vector4(1, 0, 0, 0);
  281. const Vector4 vector_y = Vector4(0, 1, 0, 0);
  282. const Vector4 vector1 = Vector4(1.7, 2.3, 1, 9.1);
  283. const Vector4 vector2 = Vector4(-8.2, -16, 3, 2.4);
  284. CHECK_MESSAGE(
  285. vector_x.dot(vector_y) == 0.0,
  286. "Vector4 dot product of perpendicular vectors should be zero.");
  287. CHECK_MESSAGE(
  288. vector_x.dot(vector_x) == 1.0,
  289. "Vector4 dot product of identical unit vectors should be one.");
  290. CHECK_MESSAGE(
  291. (vector_x * 10).dot(vector_x * 10) == 100.0,
  292. "Vector4 dot product of same direction vectors should behave as expected.");
  293. CHECK_MESSAGE(
  294. (vector1 * 2).dot(vector2 * 4) == doctest::Approx((real_t)-25.9 * 8),
  295. "Vector4 dot product should work as expected.");
  296. }
  297. TEST_CASE("[Vector4] Finite number checks") {
  298. const double infinite[] = { NAN, INFINITY, -INFINITY };
  299. CHECK_MESSAGE(
  300. Vector4(0, 1, 2, 3).is_finite(),
  301. "Vector4(0, 1, 2, 3) should be finite");
  302. for (double x : infinite) {
  303. CHECK_FALSE_MESSAGE(
  304. Vector4(x, 1, 2, 3).is_finite(),
  305. "Vector4 with one component infinite should not be finite.");
  306. CHECK_FALSE_MESSAGE(
  307. Vector4(0, x, 2, 3).is_finite(),
  308. "Vector4 with one component infinite should not be finite.");
  309. CHECK_FALSE_MESSAGE(
  310. Vector4(0, 1, x, 3).is_finite(),
  311. "Vector4 with one component infinite should not be finite.");
  312. CHECK_FALSE_MESSAGE(
  313. Vector4(0, 1, 2, x).is_finite(),
  314. "Vector4 with one component infinite should not be finite.");
  315. }
  316. for (double x : infinite) {
  317. for (double y : infinite) {
  318. CHECK_FALSE_MESSAGE(
  319. Vector4(x, y, 2, 3).is_finite(),
  320. "Vector4 with two components infinite should not be finite.");
  321. CHECK_FALSE_MESSAGE(
  322. Vector4(x, 1, y, 3).is_finite(),
  323. "Vector4 with two components infinite should not be finite.");
  324. CHECK_FALSE_MESSAGE(
  325. Vector4(x, 1, 2, y).is_finite(),
  326. "Vector4 with two components infinite should not be finite.");
  327. CHECK_FALSE_MESSAGE(
  328. Vector4(0, x, y, 3).is_finite(),
  329. "Vector4 with two components infinite should not be finite.");
  330. CHECK_FALSE_MESSAGE(
  331. Vector4(0, x, 2, y).is_finite(),
  332. "Vector4 with two components infinite should not be finite.");
  333. CHECK_FALSE_MESSAGE(
  334. Vector4(0, 1, x, y).is_finite(),
  335. "Vector4 with two components infinite should not be finite.");
  336. }
  337. }
  338. for (double x : infinite) {
  339. for (double y : infinite) {
  340. for (double z : infinite) {
  341. CHECK_FALSE_MESSAGE(
  342. Vector4(0, x, y, z).is_finite(),
  343. "Vector4 with three components infinite should not be finite.");
  344. CHECK_FALSE_MESSAGE(
  345. Vector4(x, 1, y, z).is_finite(),
  346. "Vector4 with three components infinite should not be finite.");
  347. CHECK_FALSE_MESSAGE(
  348. Vector4(x, y, 2, z).is_finite(),
  349. "Vector4 with three components infinite should not be finite.");
  350. CHECK_FALSE_MESSAGE(
  351. Vector4(x, y, z, 3).is_finite(),
  352. "Vector4 with three components infinite should not be finite.");
  353. }
  354. }
  355. }
  356. for (double x : infinite) {
  357. for (double y : infinite) {
  358. for (double z : infinite) {
  359. for (double w : infinite) {
  360. CHECK_FALSE_MESSAGE(
  361. Vector4(x, y, z, w).is_finite(),
  362. "Vector4 with four components infinite should not be finite.");
  363. }
  364. }
  365. }
  366. }
  367. }
  368. } // namespace TestVector4
  369. #endif // TEST_VECTOR4_H