test_vector4.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. #pragma once
  31. #include "core/math/vector4.h"
  32. #include "tests/test_macros.h"
  33. namespace TestVector4 {
  34. TEST_CASE("[Vector4] Constructor methods") {
  35. constexpr Vector4 vector_empty = Vector4();
  36. constexpr Vector4 vector_zero = Vector4(0.0, 0.0, 0.0, 0.0);
  37. static_assert(
  38. vector_empty == vector_zero,
  39. "Vector4 Constructor with no inputs should return a zero Vector4.");
  40. }
  41. TEST_CASE("[Vector4] Axis methods") {
  42. Vector4 vector = Vector4(1.2, 3.4, 5.6, -0.9);
  43. CHECK_MESSAGE(
  44. vector.max_axis_index() == Vector4::Axis::AXIS_Z,
  45. "Vector4 max_axis_index should work as expected.");
  46. CHECK_MESSAGE(
  47. vector.min_axis_index() == Vector4::Axis::AXIS_W,
  48. "Vector4 min_axis_index should work as expected.");
  49. CHECK_MESSAGE(
  50. vector[vector.max_axis_index()] == (real_t)5.6,
  51. "Vector4 array operator should work as expected.");
  52. CHECK_MESSAGE(
  53. vector[vector.min_axis_index()] == (real_t)-0.9,
  54. "Vector4 array operator should work as expected.");
  55. vector[Vector4::Axis::AXIS_Y] = 3.7;
  56. CHECK_MESSAGE(
  57. vector[Vector4::Axis::AXIS_Y] == (real_t)3.7,
  58. "Vector4 array operator setter should work as expected.");
  59. }
  60. TEST_CASE("[Vector4] Interpolation methods") {
  61. constexpr Vector4 vector1 = Vector4(1, 2, 3, 4);
  62. constexpr Vector4 vector2 = Vector4(4, 5, 6, 7);
  63. CHECK_MESSAGE(
  64. vector1.lerp(vector2, 0.5) == Vector4(2.5, 3.5, 4.5, 5.5),
  65. "Vector4 lerp should work as expected.");
  66. CHECK_MESSAGE(
  67. vector1.lerp(vector2, 1.0 / 3.0).is_equal_approx(Vector4(2, 3, 4, 5)),
  68. "Vector4 lerp should work as expected.");
  69. CHECK_MESSAGE(
  70. vector1.cubic_interpolate(vector2, Vector4(), Vector4(7, 7, 7, 7), 0.5) == Vector4(2.375, 3.5, 4.625, 5.75),
  71. "Vector4 cubic_interpolate should work as expected.");
  72. CHECK_MESSAGE(
  73. 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)),
  74. "Vector4 cubic_interpolate should work as expected.");
  75. }
  76. TEST_CASE("[Vector4] Length methods") {
  77. constexpr Vector4 vector1 = Vector4(10, 10, 10, 10);
  78. constexpr Vector4 vector2 = Vector4(20, 30, 40, 50);
  79. CHECK_MESSAGE(
  80. vector1.length_squared() == 400,
  81. "Vector4 length_squared should work as expected and return exact result.");
  82. CHECK_MESSAGE(
  83. vector1.length() == doctest::Approx(20),
  84. "Vector4 length should work as expected.");
  85. CHECK_MESSAGE(
  86. vector2.length_squared() == 5400,
  87. "Vector4 length_squared should work as expected and return exact result.");
  88. CHECK_MESSAGE(
  89. vector2.length() == doctest::Approx((real_t)73.484692283495),
  90. "Vector4 length should work as expected.");
  91. CHECK_MESSAGE(
  92. vector1.distance_to(vector2) == doctest::Approx((real_t)54.772255750517),
  93. "Vector4 distance_to should work as expected.");
  94. CHECK_MESSAGE(
  95. vector1.distance_squared_to(vector2) == doctest::Approx(3000),
  96. "Vector4 distance_squared_to should work as expected.");
  97. }
  98. TEST_CASE("[Vector4] Limiting methods") {
  99. constexpr Vector4 vector = Vector4(10, 10, 10, 10);
  100. CHECK_MESSAGE(
  101. Vector4(-5, 5, 15, -15).clamp(Vector4(), vector) == Vector4(0, 5, 10, 0),
  102. "Vector4 clamp should work as expected.");
  103. CHECK_MESSAGE(
  104. vector.clamp(Vector4(0, 10, 15, 18), Vector4(5, 10, 20, 25)) == Vector4(5, 10, 15, 18),
  105. "Vector4 clamp should work as expected.");
  106. }
  107. TEST_CASE("[Vector4] Normalization methods") {
  108. CHECK_MESSAGE(
  109. Vector4(1, 0, 0, 0).is_normalized() == true,
  110. "Vector4 is_normalized should return true for a normalized vector.");
  111. CHECK_MESSAGE(
  112. Vector4(1, 1, 1, 1).is_normalized() == false,
  113. "Vector4 is_normalized should return false for a non-normalized vector.");
  114. CHECK_MESSAGE(
  115. Vector4(1, 0, 0, 0).normalized() == Vector4(1, 0, 0, 0),
  116. "Vector4 normalized should return the same vector for a normalized vector.");
  117. CHECK_MESSAGE(
  118. Vector4(1, 1, 0, 0).normalized().is_equal_approx(Vector4(Math::SQRT12, Math::SQRT12, 0, 0)),
  119. "Vector4 normalized should work as expected.");
  120. CHECK_MESSAGE(
  121. Vector4(1, 1, 1, 1).normalized().is_equal_approx(Vector4(0.5, 0.5, 0.5, 0.5)),
  122. "Vector4 normalized should work as expected.");
  123. }
  124. TEST_CASE("[Vector4] Operators") {
  125. constexpr Vector4 decimal1 = Vector4(2.3, 4.9, 7.8, 3.2);
  126. constexpr Vector4 decimal2 = Vector4(1.2, 3.4, 5.6, 1.7);
  127. constexpr Vector4 power1 = Vector4(0.75, 1.5, 0.625, 0.125);
  128. constexpr Vector4 power2 = Vector4(0.5, 0.125, 0.25, 0.75);
  129. constexpr Vector4 int1 = Vector4(4, 5, 9, 2);
  130. constexpr Vector4 int2 = Vector4(1, 2, 3, 1);
  131. static_assert(
  132. -decimal1 == Vector4(-2.3, -4.9, -7.8, -3.2),
  133. "Vector4 change of sign should work as expected.");
  134. CHECK_MESSAGE(
  135. (decimal1 + decimal2).is_equal_approx(Vector4(3.5, 8.3, 13.4, 4.9)),
  136. "Vector4 addition should behave as expected.");
  137. static_assert(
  138. (power1 + power2) == Vector4(1.25, 1.625, 0.875, 0.875),
  139. "Vector4 addition with powers of two should give exact results.");
  140. static_assert(
  141. (int1 + int2) == Vector4(5, 7, 12, 3),
  142. "Vector4 addition with integers should give exact results.");
  143. CHECK_MESSAGE(
  144. (decimal1 - decimal2).is_equal_approx(Vector4(1.1, 1.5, 2.2, 1.5)),
  145. "Vector4 subtraction should behave as expected.");
  146. static_assert(
  147. (power1 - power2) == Vector4(0.25, 1.375, 0.375, -0.625),
  148. "Vector4 subtraction with powers of two should give exact results.");
  149. static_assert(
  150. (int1 - int2) == Vector4(3, 3, 6, 1),
  151. "Vector4 subtraction with integers should give exact results.");
  152. CHECK_MESSAGE(
  153. (decimal1 * decimal2).is_equal_approx(Vector4(2.76, 16.66, 43.68, 5.44)),
  154. "Vector4 multiplication should behave as expected.");
  155. static_assert(
  156. (power1 * power2) == Vector4(0.375, 0.1875, 0.15625, 0.09375),
  157. "Vector4 multiplication with powers of two should give exact results.");
  158. static_assert(
  159. (int1 * int2) == Vector4(4, 10, 27, 2),
  160. "Vector4 multiplication with integers should give exact results.");
  161. CHECK_MESSAGE(
  162. (decimal1 / decimal2).is_equal_approx(Vector4(1.91666666666666666, 1.44117647058823529, 1.39285714285714286, 1.88235294118)),
  163. "Vector4 division should behave as expected.");
  164. static_assert(
  165. (power1 / power2) == Vector4(1.5, 12.0, 2.5, 1.0 / 6.0),
  166. "Vector4 division with powers of two should give exact results.");
  167. static_assert(
  168. (int1 / int2) == Vector4(4, 2.5, 3, 2),
  169. "Vector4 division with integers should give exact results.");
  170. CHECK_MESSAGE(
  171. (decimal1 * 2).is_equal_approx(Vector4(4.6, 9.8, 15.6, 6.4)),
  172. "Vector4 multiplication should behave as expected.");
  173. static_assert(
  174. (power1 * 2) == Vector4(1.5, 3, 1.25, 0.25),
  175. "Vector4 multiplication with powers of two should give exact results.");
  176. static_assert(
  177. (int1 * 2) == Vector4(8, 10, 18, 4),
  178. "Vector4 multiplication with integers should give exact results.");
  179. CHECK_MESSAGE(
  180. (decimal1 / 2).is_equal_approx(Vector4(1.15, 2.45, 3.9, 1.6)),
  181. "Vector4 division should behave as expected.");
  182. static_assert(
  183. (power1 / 2) == Vector4(0.375, 0.75, 0.3125, 0.0625),
  184. "Vector4 division with powers of two should give exact results.");
  185. static_assert(
  186. (int1 / 2) == Vector4(2, 2.5, 4.5, 1),
  187. "Vector4 division with integers should give exact results.");
  188. CHECK_MESSAGE(
  189. ((String)decimal1) == "(2.3, 4.9, 7.8, 3.2)",
  190. "Vector4 cast to String should work as expected.");
  191. CHECK_MESSAGE(
  192. ((String)decimal2) == "(1.2, 3.4, 5.6, 1.7)",
  193. "Vector4 cast to String should work as expected.");
  194. CHECK_MESSAGE(
  195. ((String)Vector4(9.7, 9.8, 9.9, -1.8)) == "(9.7, 9.8, 9.9, -1.8)",
  196. "Vector4 cast to String should work as expected.");
  197. #ifdef REAL_T_IS_DOUBLE
  198. CHECK_MESSAGE(
  199. ((String)Vector4(Math::E, Math::SQRT2, Math::SQRT3, Math::SQRT3)) == "(2.71828182845905, 1.4142135623731, 1.73205080756888, 1.73205080756888)",
  200. "Vector4 cast to String should print the correct amount of digits for real_t = double.");
  201. #else
  202. CHECK_MESSAGE(
  203. ((String)Vector4(Math::E, Math::SQRT2, Math::SQRT3, Math::SQRT3)) == "(2.718282, 1.414214, 1.732051, 1.732051)",
  204. "Vector4 cast to String should print the correct amount of digits for real_t = float.");
  205. #endif // REAL_T_IS_DOUBLE
  206. }
  207. TEST_CASE("[Vector4] Other methods") {
  208. constexpr Vector4 vector = Vector4(1.2, 3.4, 5.6, 1.6);
  209. CHECK_MESSAGE(
  210. vector.direction_to(Vector4()).is_equal_approx(-vector.normalized()),
  211. "Vector4 direction_to should work as expected.");
  212. CHECK_MESSAGE(
  213. Vector4(1, 1, 1, 1).direction_to(Vector4(2, 2, 2, 2)).is_equal_approx(Vector4(0.5, 0.5, 0.5, 0.5)),
  214. "Vector4 direction_to should work as expected.");
  215. CHECK_MESSAGE(
  216. vector.inverse().is_equal_approx(Vector4(1 / 1.2, 1 / 3.4, 1 / 5.6, 1 / 1.6)),
  217. "Vector4 inverse should work as expected.");
  218. CHECK_MESSAGE(
  219. vector.posmod(2).is_equal_approx(Vector4(1.2, 1.4, 1.6, 1.6)),
  220. "Vector4 posmod should work as expected.");
  221. CHECK_MESSAGE(
  222. (-vector).posmod(2).is_equal_approx(Vector4(0.8, 0.6, 0.4, 0.4)),
  223. "Vector4 posmod should work as expected.");
  224. CHECK_MESSAGE(
  225. vector.posmodv(Vector4(1, 2, 3, 4)).is_equal_approx(Vector4(0.2, 1.4, 2.6, 1.6)),
  226. "Vector4 posmodv should work as expected.");
  227. CHECK_MESSAGE(
  228. (-vector).posmodv(Vector4(2, 3, 4, 5)).is_equal_approx(Vector4(0.8, 2.6, 2.4, 3.4)),
  229. "Vector4 posmodv should work as expected.");
  230. CHECK_MESSAGE(
  231. vector.snapped(Vector4(1, 1, 1, 1)) == Vector4(1, 3, 6, 2),
  232. "Vector4 snapped to integers should be the same as rounding.");
  233. CHECK_MESSAGE(
  234. vector.snapped(Vector4(0.25, 0.25, 0.25, 0.25)) == Vector4(1.25, 3.5, 5.5, 1.5),
  235. "Vector4 snapped to 0.25 should give exact results.");
  236. CHECK_MESSAGE(
  237. Vector4(1.2, 2.5, 2.0, 1.6).is_equal_approx(vector.min(Vector4(3.0, 2.5, 2.0, 3.4))),
  238. "Vector4 min should return expected value.");
  239. CHECK_MESSAGE(
  240. Vector4(5.3, 3.4, 5.6, 4.2).is_equal_approx(vector.max(Vector4(5.3, 2.0, 3.0, 4.2))),
  241. "Vector4 max should return expected value.");
  242. }
  243. TEST_CASE("[Vector4] Rounding methods") {
  244. constexpr Vector4 vector1 = Vector4(1.2, 3.4, 5.6, 1.6);
  245. constexpr Vector4 vector2 = Vector4(1.2, -3.4, -5.6, -1.6);
  246. CHECK_MESSAGE(
  247. vector1.abs() == vector1,
  248. "Vector4 abs should work as expected.");
  249. CHECK_MESSAGE(
  250. vector2.abs() == vector1,
  251. "Vector4 abs should work as expected.");
  252. CHECK_MESSAGE(
  253. vector1.ceil() == Vector4(2, 4, 6, 2),
  254. "Vector4 ceil should work as expected.");
  255. CHECK_MESSAGE(
  256. vector2.ceil() == Vector4(2, -3, -5, -1),
  257. "Vector4 ceil should work as expected.");
  258. CHECK_MESSAGE(
  259. vector1.floor() == Vector4(1, 3, 5, 1),
  260. "Vector4 floor should work as expected.");
  261. CHECK_MESSAGE(
  262. vector2.floor() == Vector4(1, -4, -6, -2),
  263. "Vector4 floor should work as expected.");
  264. CHECK_MESSAGE(
  265. vector1.round() == Vector4(1, 3, 6, 2),
  266. "Vector4 round should work as expected.");
  267. CHECK_MESSAGE(
  268. vector2.round() == Vector4(1, -3, -6, -2),
  269. "Vector4 round should work as expected.");
  270. CHECK_MESSAGE(
  271. vector1.sign() == Vector4(1, 1, 1, 1),
  272. "Vector4 sign should work as expected.");
  273. CHECK_MESSAGE(
  274. vector2.sign() == Vector4(1, -1, -1, -1),
  275. "Vector4 sign should work as expected.");
  276. }
  277. TEST_CASE("[Vector4] Linear algebra methods") {
  278. constexpr Vector4 vector_x = Vector4(1, 0, 0, 0);
  279. constexpr Vector4 vector_y = Vector4(0, 1, 0, 0);
  280. constexpr Vector4 vector1 = Vector4(1.7, 2.3, 1, 9.1);
  281. constexpr Vector4 vector2 = Vector4(-8.2, -16, 3, 2.4);
  282. CHECK_MESSAGE(
  283. vector_x.dot(vector_y) == 0.0,
  284. "Vector4 dot product of perpendicular vectors should be zero.");
  285. CHECK_MESSAGE(
  286. vector_x.dot(vector_x) == 1.0,
  287. "Vector4 dot product of identical unit vectors should be one.");
  288. CHECK_MESSAGE(
  289. (vector_x * 10).dot(vector_x * 10) == 100.0,
  290. "Vector4 dot product of same direction vectors should behave as expected.");
  291. CHECK_MESSAGE(
  292. (vector1 * 2).dot(vector2 * 4) == doctest::Approx((real_t)-25.9 * 8),
  293. "Vector4 dot product should work as expected.");
  294. }
  295. TEST_CASE("[Vector4] Finite number checks") {
  296. constexpr double infinite[] = { Math::NaN, Math::INF, -Math::INF };
  297. CHECK_MESSAGE(
  298. Vector4(0, 1, 2, 3).is_finite(),
  299. "Vector4(0, 1, 2, 3) should be finite");
  300. for (double x : infinite) {
  301. CHECK_FALSE_MESSAGE(
  302. Vector4(x, 1, 2, 3).is_finite(),
  303. "Vector4 with one component infinite should not be finite.");
  304. CHECK_FALSE_MESSAGE(
  305. Vector4(0, x, 2, 3).is_finite(),
  306. "Vector4 with one component infinite should not be finite.");
  307. CHECK_FALSE_MESSAGE(
  308. Vector4(0, 1, x, 3).is_finite(),
  309. "Vector4 with one component infinite should not be finite.");
  310. CHECK_FALSE_MESSAGE(
  311. Vector4(0, 1, 2, x).is_finite(),
  312. "Vector4 with one component infinite should not be finite.");
  313. }
  314. for (double x : infinite) {
  315. for (double y : infinite) {
  316. CHECK_FALSE_MESSAGE(
  317. Vector4(x, y, 2, 3).is_finite(),
  318. "Vector4 with two components infinite should not be finite.");
  319. CHECK_FALSE_MESSAGE(
  320. Vector4(x, 1, y, 3).is_finite(),
  321. "Vector4 with two components infinite should not be finite.");
  322. CHECK_FALSE_MESSAGE(
  323. Vector4(x, 1, 2, y).is_finite(),
  324. "Vector4 with two components infinite should not be finite.");
  325. CHECK_FALSE_MESSAGE(
  326. Vector4(0, x, y, 3).is_finite(),
  327. "Vector4 with two components infinite should not be finite.");
  328. CHECK_FALSE_MESSAGE(
  329. Vector4(0, x, 2, y).is_finite(),
  330. "Vector4 with two components infinite should not be finite.");
  331. CHECK_FALSE_MESSAGE(
  332. Vector4(0, 1, x, y).is_finite(),
  333. "Vector4 with two components infinite should not be finite.");
  334. }
  335. }
  336. for (double x : infinite) {
  337. for (double y : infinite) {
  338. for (double z : infinite) {
  339. CHECK_FALSE_MESSAGE(
  340. Vector4(0, x, y, z).is_finite(),
  341. "Vector4 with three components infinite should not be finite.");
  342. CHECK_FALSE_MESSAGE(
  343. Vector4(x, 1, y, z).is_finite(),
  344. "Vector4 with three components infinite should not be finite.");
  345. CHECK_FALSE_MESSAGE(
  346. Vector4(x, y, 2, z).is_finite(),
  347. "Vector4 with three components infinite should not be finite.");
  348. CHECK_FALSE_MESSAGE(
  349. Vector4(x, y, z, 3).is_finite(),
  350. "Vector4 with three components infinite should not be finite.");
  351. }
  352. }
  353. }
  354. for (double x : infinite) {
  355. for (double y : infinite) {
  356. for (double z : infinite) {
  357. for (double w : infinite) {
  358. CHECK_FALSE_MESSAGE(
  359. Vector4(x, y, z, w).is_finite(),
  360. "Vector4 with four components infinite should not be finite.");
  361. }
  362. }
  363. }
  364. }
  365. }
  366. } // namespace TestVector4