test_vector4.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*************************************************************************/
  2. /* test_vector4.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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] Axis methods") {
  37. Vector4 vector = Vector4(1.2, 3.4, 5.6, -0.9);
  38. CHECK_MESSAGE(
  39. vector.max_axis_index() == Vector4::Axis::AXIS_Z,
  40. "Vector4 max_axis_index should work as expected.");
  41. CHECK_MESSAGE(
  42. vector.min_axis_index() == Vector4::Axis::AXIS_W,
  43. "Vector4 min_axis_index should work as expected.");
  44. CHECK_MESSAGE(
  45. vector.get_axis(vector.max_axis_index()) == (real_t)5.6,
  46. "Vector4 get_axis should work as expected.");
  47. CHECK_MESSAGE(
  48. vector[vector.min_axis_index()] == (real_t)-0.9,
  49. "Vector4 array operator should work as expected.");
  50. vector.set_axis(Vector4::Axis::AXIS_Y, 4.7);
  51. CHECK_MESSAGE(
  52. vector.get_axis(Vector4::Axis::AXIS_Y) == (real_t)4.7,
  53. "Vector4 set_axis should work as expected.");
  54. vector[Vector4::Axis::AXIS_Y] = 3.7;
  55. CHECK_MESSAGE(
  56. vector[Vector4::Axis::AXIS_Y] == (real_t)3.7,
  57. "Vector4 array operator setter should work as expected.");
  58. }
  59. TEST_CASE("[Vector4] Interpolation methods") {
  60. const Vector4 vector1 = Vector4(1, 2, 3, 4);
  61. const Vector4 vector2 = Vector4(4, 5, 6, 7);
  62. CHECK_MESSAGE(
  63. vector1.lerp(vector2, 0.5) == Vector4(2.5, 3.5, 4.5, 5.5),
  64. "Vector4 lerp should work as expected.");
  65. CHECK_MESSAGE(
  66. vector1.lerp(vector2, 1.0 / 3.0).is_equal_approx(Vector4(2, 3, 4, 5)),
  67. "Vector4 lerp should work as expected.");
  68. CHECK_MESSAGE(
  69. vector1.cubic_interpolate(vector2, Vector4(), Vector4(7, 7, 7, 7), 0.5) == Vector4(2.375, 3.5, 4.625, 5.75),
  70. "Vector4 cubic_interpolate should work as expected.");
  71. CHECK_MESSAGE(
  72. 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)),
  73. "Vector4 cubic_interpolate should work as expected.");
  74. }
  75. TEST_CASE("[Vector4] Length methods") {
  76. const Vector4 vector1 = Vector4(10, 10, 10, 10);
  77. const Vector4 vector2 = Vector4(20, 30, 40, 50);
  78. CHECK_MESSAGE(
  79. vector1.length_squared() == 400,
  80. "Vector4 length_squared should work as expected and return exact result.");
  81. CHECK_MESSAGE(
  82. Math::is_equal_approx(vector1.length(), 20),
  83. "Vector4 length should work as expected.");
  84. CHECK_MESSAGE(
  85. vector2.length_squared() == 5400,
  86. "Vector4 length_squared should work as expected and return exact result.");
  87. CHECK_MESSAGE(
  88. Math::is_equal_approx(vector2.length(), (real_t)73.484692283495),
  89. "Vector4 length should work as expected.");
  90. CHECK_MESSAGE(
  91. Math::is_equal_approx(vector1.distance_to(vector2), (real_t)54.772255750517),
  92. "Vector4 distance_to should work as expected.");
  93. CHECK_MESSAGE(
  94. Math::is_equal_approx(vector1.distance_squared_to(vector2), 3000),
  95. "Vector4 distance_squared_to should work as expected.");
  96. }
  97. TEST_CASE("[Vector4] Limiting methods") {
  98. const Vector4 vector = Vector4(10, 10, 10, 10);
  99. CHECK_MESSAGE(
  100. Vector4(-5, 5, 15, -15).clamp(Vector4(), vector) == Vector4(0, 5, 10, 0),
  101. "Vector4 clamp should work as expected.");
  102. CHECK_MESSAGE(
  103. vector.clamp(Vector4(0, 10, 15, 18), Vector4(5, 10, 20, 25)) == Vector4(5, 10, 15, 18),
  104. "Vector4 clamp should work as expected.");
  105. }
  106. TEST_CASE("[Vector4] Normalization methods") {
  107. CHECK_MESSAGE(
  108. Vector4(1, 0, 0, 0).is_normalized() == true,
  109. "Vector4 is_normalized should return true for a normalized vector.");
  110. CHECK_MESSAGE(
  111. Vector4(1, 1, 1, 1).is_normalized() == false,
  112. "Vector4 is_normalized should return false for a non-normalized vector.");
  113. CHECK_MESSAGE(
  114. Vector4(1, 0, 0, 0).normalized() == Vector4(1, 0, 0, 0),
  115. "Vector4 normalized should return the same vector for a normalized vector.");
  116. CHECK_MESSAGE(
  117. Vector4(1, 1, 0, 0).normalized().is_equal_approx(Vector4(Math_SQRT12, Math_SQRT12, 0, 0)),
  118. "Vector4 normalized should work as expected.");
  119. CHECK_MESSAGE(
  120. Vector4(1, 1, 1, 1).normalized().is_equal_approx(Vector4(0.5, 0.5, 0.5, 0.5)),
  121. "Vector4 normalized should work as expected.");
  122. }
  123. TEST_CASE("[Vector4] Operators") {
  124. const Vector4 decimal1 = Vector4(2.3, 4.9, 7.8, 3.2);
  125. const Vector4 decimal2 = Vector4(1.2, 3.4, 5.6, 1.7);
  126. const Vector4 power1 = Vector4(0.75, 1.5, 0.625, 0.125);
  127. const Vector4 power2 = Vector4(0.5, 0.125, 0.25, 0.75);
  128. const Vector4 int1 = Vector4(4, 5, 9, 2);
  129. const Vector4 int2 = Vector4(1, 2, 3, 1);
  130. CHECK_MESSAGE(
  131. -decimal1 == Vector4(-2.3, -4.9, -7.8, -3.2),
  132. "Vector4 change of sign should work as expected.");
  133. CHECK_MESSAGE(
  134. (decimal1 + decimal2).is_equal_approx(Vector4(3.5, 8.3, 13.4, 4.9)),
  135. "Vector4 addition should behave as expected.");
  136. CHECK_MESSAGE(
  137. (power1 + power2) == Vector4(1.25, 1.625, 0.875, 0.875),
  138. "Vector4 addition with powers of two should give exact results.");
  139. CHECK_MESSAGE(
  140. (int1 + int2) == Vector4(5, 7, 12, 3),
  141. "Vector4 addition with integers should give exact results.");
  142. CHECK_MESSAGE(
  143. (decimal1 - decimal2).is_equal_approx(Vector4(1.1, 1.5, 2.2, 1.5)),
  144. "Vector4 subtraction should behave as expected.");
  145. CHECK_MESSAGE(
  146. (power1 - power2) == Vector4(0.25, 1.375, 0.375, -0.625),
  147. "Vector4 subtraction with powers of two should give exact results.");
  148. CHECK_MESSAGE(
  149. (int1 - int2) == Vector4(3, 3, 6, 1),
  150. "Vector4 subtraction with integers should give exact results.");
  151. CHECK_MESSAGE(
  152. (decimal1 * decimal2).is_equal_approx(Vector4(2.76, 16.66, 43.68, 5.44)),
  153. "Vector4 multiplication should behave as expected.");
  154. CHECK_MESSAGE(
  155. (power1 * power2) == Vector4(0.375, 0.1875, 0.15625, 0.09375),
  156. "Vector4 multiplication with powers of two should give exact results.");
  157. CHECK_MESSAGE(
  158. (int1 * int2) == Vector4(4, 10, 27, 2),
  159. "Vector4 multiplication with integers should give exact results.");
  160. CHECK_MESSAGE(
  161. (decimal1 / decimal2).is_equal_approx(Vector4(1.91666666666666666, 1.44117647058823529, 1.39285714285714286, 1.88235294118)),
  162. "Vector4 division should behave as expected.");
  163. CHECK_MESSAGE(
  164. (power1 / power2) == Vector4(1.5, 12.0, 2.5, 1.0 / 6.0),
  165. "Vector4 division with powers of two should give exact results.");
  166. CHECK_MESSAGE(
  167. (int1 / int2) == Vector4(4, 2.5, 3, 2),
  168. "Vector4 division with integers should give exact results.");
  169. CHECK_MESSAGE(
  170. (decimal1 * 2).is_equal_approx(Vector4(4.6, 9.8, 15.6, 6.4)),
  171. "Vector4 multiplication should behave as expected.");
  172. CHECK_MESSAGE(
  173. (power1 * 2) == Vector4(1.5, 3, 1.25, 0.25),
  174. "Vector4 multiplication with powers of two should give exact results.");
  175. CHECK_MESSAGE(
  176. (int1 * 2) == Vector4(8, 10, 18, 4),
  177. "Vector4 multiplication with integers should give exact results.");
  178. CHECK_MESSAGE(
  179. (decimal1 / 2).is_equal_approx(Vector4(1.15, 2.45, 3.9, 1.6)),
  180. "Vector4 division should behave as expected.");
  181. CHECK_MESSAGE(
  182. (power1 / 2) == Vector4(0.375, 0.75, 0.3125, 0.0625),
  183. "Vector4 division with powers of two should give exact results.");
  184. CHECK_MESSAGE(
  185. (int1 / 2) == Vector4(2, 2.5, 4.5, 1),
  186. "Vector4 division with integers should give exact results.");
  187. CHECK_MESSAGE(
  188. ((String)decimal1) == "(2.3, 4.9, 7.8, 3.2)",
  189. "Vector4 cast to String should work as expected.");
  190. CHECK_MESSAGE(
  191. ((String)decimal2) == "(1.2, 3.4, 5.6, 1.7)",
  192. "Vector4 cast to String should work as expected.");
  193. CHECK_MESSAGE(
  194. ((String)Vector4(9.7, 9.8, 9.9, -1.8)) == "(9.7, 9.8, 9.9, -1.8)",
  195. "Vector4 cast to String should work as expected.");
  196. #ifdef REAL_T_IS_DOUBLE
  197. CHECK_MESSAGE(
  198. ((String)Vector4(Math_E, Math_SQRT2, Math_SQRT3, Math_SQRT3)) == "(2.71828182845905, 1.4142135623731, 1.73205080756888, 1.73205080756888)",
  199. "Vector4 cast to String should print the correct amount of digits for real_t = double.");
  200. #else
  201. CHECK_MESSAGE(
  202. ((String)Vector4(Math_E, Math_SQRT2, Math_SQRT3, Math_SQRT3)) == "(2.718282, 1.414214, 1.732051, 1.732051)",
  203. "Vector4 cast to String should print the correct amount of digits for real_t = float.");
  204. #endif // REAL_T_IS_DOUBLE
  205. }
  206. TEST_CASE("[Vector4] Other methods") {
  207. const Vector4 vector = Vector4(1.2, 3.4, 5.6, 1.6);
  208. CHECK_MESSAGE(
  209. vector.direction_to(Vector4()).is_equal_approx(-vector.normalized()),
  210. "Vector4 direction_to should work as expected.");
  211. CHECK_MESSAGE(
  212. Vector4(1, 1, 1, 1).direction_to(Vector4(2, 2, 2, 2)).is_equal_approx(Vector4(0.5, 0.5, 0.5, 0.5)),
  213. "Vector4 direction_to should work as expected.");
  214. CHECK_MESSAGE(
  215. vector.inverse().is_equal_approx(Vector4(1 / 1.2, 1 / 3.4, 1 / 5.6, 1 / 1.6)),
  216. "Vector4 inverse should work as expected.");
  217. CHECK_MESSAGE(
  218. vector.posmod(2).is_equal_approx(Vector4(1.2, 1.4, 1.6, 1.6)),
  219. "Vector4 posmod should work as expected.");
  220. CHECK_MESSAGE(
  221. (-vector).posmod(2).is_equal_approx(Vector4(0.8, 0.6, 0.4, 0.4)),
  222. "Vector4 posmod should work as expected.");
  223. CHECK_MESSAGE(
  224. vector.posmodv(Vector4(1, 2, 3, 4)).is_equal_approx(Vector4(0.2, 1.4, 2.6, 1.6)),
  225. "Vector4 posmodv should work as expected.");
  226. CHECK_MESSAGE(
  227. (-vector).posmodv(Vector4(2, 3, 4, 5)).is_equal_approx(Vector4(0.8, 2.6, 2.4, 3.4)),
  228. "Vector4 posmodv should work as expected.");
  229. CHECK_MESSAGE(
  230. vector.snapped(Vector4(1, 1, 1, 1)) == Vector4(1, 3, 6, 2),
  231. "Vector4 snapped to integers should be the same as rounding.");
  232. CHECK_MESSAGE(
  233. vector.snapped(Vector4(0.25, 0.25, 0.25, 0.25)) == Vector4(1.25, 3.5, 5.5, 1.5),
  234. "Vector4 snapped to 0.25 should give exact results.");
  235. }
  236. TEST_CASE("[Vector4] Rounding methods") {
  237. const Vector4 vector1 = Vector4(1.2, 3.4, 5.6, 1.6);
  238. const Vector4 vector2 = Vector4(1.2, -3.4, -5.6, -1.6);
  239. CHECK_MESSAGE(
  240. vector1.abs() == vector1,
  241. "Vector4 abs should work as expected.");
  242. CHECK_MESSAGE(
  243. vector2.abs() == vector1,
  244. "Vector4 abs should work as expected.");
  245. CHECK_MESSAGE(
  246. vector1.ceil() == Vector4(2, 4, 6, 2),
  247. "Vector4 ceil should work as expected.");
  248. CHECK_MESSAGE(
  249. vector2.ceil() == Vector4(2, -3, -5, -1),
  250. "Vector4 ceil should work as expected.");
  251. CHECK_MESSAGE(
  252. vector1.floor() == Vector4(1, 3, 5, 1),
  253. "Vector4 floor should work as expected.");
  254. CHECK_MESSAGE(
  255. vector2.floor() == Vector4(1, -4, -6, -2),
  256. "Vector4 floor should work as expected.");
  257. CHECK_MESSAGE(
  258. vector1.round() == Vector4(1, 3, 6, 2),
  259. "Vector4 round should work as expected.");
  260. CHECK_MESSAGE(
  261. vector2.round() == Vector4(1, -3, -6, -2),
  262. "Vector4 round should work as expected.");
  263. CHECK_MESSAGE(
  264. vector1.sign() == Vector4(1, 1, 1, 1),
  265. "Vector4 sign should work as expected.");
  266. CHECK_MESSAGE(
  267. vector2.sign() == Vector4(1, -1, -1, -1),
  268. "Vector4 sign should work as expected.");
  269. }
  270. TEST_CASE("[Vector4] Linear algebra methods") {
  271. const Vector4 vector_x = Vector4(1, 0, 0, 0);
  272. const Vector4 vector_y = Vector4(0, 1, 0, 0);
  273. const Vector4 vector1 = Vector4(1.7, 2.3, 1, 9.1);
  274. const Vector4 vector2 = Vector4(-8.2, -16, 3, 2.4);
  275. CHECK_MESSAGE(
  276. vector_x.dot(vector_y) == 0.0,
  277. "Vector4 dot product of perpendicular vectors should be zero.");
  278. CHECK_MESSAGE(
  279. vector_x.dot(vector_x) == 1.0,
  280. "Vector4 dot product of identical unit vectors should be one.");
  281. CHECK_MESSAGE(
  282. (vector_x * 10).dot(vector_x * 10) == 100.0,
  283. "Vector4 dot product of same direction vectors should behave as expected.");
  284. CHECK_MESSAGE(
  285. Math::is_equal_approx((vector1 * 2).dot(vector2 * 4), (real_t)-25.9 * 8),
  286. "Vector4 dot product should work as expected.");
  287. }
  288. } // namespace TestVector4
  289. #endif // TEST_VECTOR4_H