test_vector2.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /**************************************************************************/
  2. /* test_vector2.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_VECTOR2_H
  31. #define TEST_VECTOR2_H
  32. #include "core/math/vector2.h"
  33. #include "core/math/vector2i.h"
  34. #include "tests/test_macros.h"
  35. namespace TestVector2 {
  36. TEST_CASE("[Vector2] Constructor methods") {
  37. const Vector2 vector_empty = Vector2();
  38. const Vector2 vector_zero = Vector2(0.0, 0.0);
  39. CHECK_MESSAGE(
  40. vector_empty == vector_zero,
  41. "Vector2 Constructor with no inputs should return a zero Vector2.");
  42. }
  43. TEST_CASE("[Vector2] Angle methods") {
  44. const Vector2 vector_x = Vector2(1, 0);
  45. const Vector2 vector_y = Vector2(0, 1);
  46. CHECK_MESSAGE(
  47. vector_x.angle_to(vector_y) == doctest::Approx((real_t)Math_TAU / 4),
  48. "Vector2 angle_to should work as expected.");
  49. CHECK_MESSAGE(
  50. vector_y.angle_to(vector_x) == doctest::Approx((real_t)-Math_TAU / 4),
  51. "Vector2 angle_to should work as expected.");
  52. CHECK_MESSAGE(
  53. vector_x.angle_to_point(vector_y) == doctest::Approx((real_t)Math_TAU * 3 / 8),
  54. "Vector2 angle_to_point should work as expected.");
  55. CHECK_MESSAGE(
  56. vector_y.angle_to_point(vector_x) == doctest::Approx((real_t)-Math_TAU / 8),
  57. "Vector2 angle_to_point should work as expected.");
  58. }
  59. TEST_CASE("[Vector2] Axis methods") {
  60. Vector2 vector = Vector2(1.2, 3.4);
  61. CHECK_MESSAGE(
  62. vector.max_axis_index() == Vector2::Axis::AXIS_Y,
  63. "Vector2 max_axis_index should work as expected.");
  64. CHECK_MESSAGE(
  65. vector.min_axis_index() == Vector2::Axis::AXIS_X,
  66. "Vector2 min_axis_index should work as expected.");
  67. CHECK_MESSAGE(
  68. vector[vector.min_axis_index()] == (real_t)1.2,
  69. "Vector2 array operator should work as expected.");
  70. vector[Vector2::Axis::AXIS_Y] = 3.7;
  71. CHECK_MESSAGE(
  72. vector[Vector2::Axis::AXIS_Y] == (real_t)3.7,
  73. "Vector2 array operator setter should work as expected.");
  74. }
  75. TEST_CASE("[Vector2] Interpolation methods") {
  76. const Vector2 vector1 = Vector2(1, 2);
  77. const Vector2 vector2 = Vector2(4, 5);
  78. CHECK_MESSAGE(
  79. vector1.lerp(vector2, 0.5) == Vector2(2.5, 3.5),
  80. "Vector2 lerp should work as expected.");
  81. CHECK_MESSAGE(
  82. vector1.lerp(vector2, 1.0 / 3.0).is_equal_approx(Vector2(2, 3)),
  83. "Vector2 lerp should work as expected.");
  84. CHECK_MESSAGE(
  85. vector1.normalized().slerp(vector2.normalized(), 0.5).is_equal_approx(Vector2(0.538953602313995361, 0.84233558177947998)),
  86. "Vector2 slerp should work as expected.");
  87. CHECK_MESSAGE(
  88. vector1.normalized().slerp(vector2.normalized(), 1.0 / 3.0).is_equal_approx(Vector2(0.508990883827209473, 0.860771894454956055)),
  89. "Vector2 slerp should work as expected.");
  90. CHECK_MESSAGE(
  91. Vector2(5, 0).slerp(Vector2(0, 5), 0.5).is_equal_approx(Vector2(5, 5) * Math_SQRT12),
  92. "Vector2 slerp with non-normalized values should work as expected.");
  93. CHECK_MESSAGE(
  94. Vector2(1, 1).slerp(Vector2(2, 2), 0.5).is_equal_approx(Vector2(1.5, 1.5)),
  95. "Vector2 slerp with colinear inputs should behave as expected.");
  96. CHECK_MESSAGE(
  97. Vector2().slerp(Vector2(), 0.5) == Vector2(),
  98. "Vector2 slerp with both inputs as zero vectors should return a zero vector.");
  99. CHECK_MESSAGE(
  100. Vector2().slerp(Vector2(1, 1), 0.5) == Vector2(0.5, 0.5),
  101. "Vector2 slerp with one input as zero should behave like a regular lerp.");
  102. CHECK_MESSAGE(
  103. Vector2(1, 1).slerp(Vector2(), 0.5) == Vector2(0.5, 0.5),
  104. "Vector2 slerp with one input as zero should behave like a regular lerp.");
  105. CHECK_MESSAGE(
  106. Vector2(4, 6).slerp(Vector2(8, 10), 0.5).is_equal_approx(Vector2(5.9076470794008017626, 8.07918879020090480697)),
  107. "Vector2 slerp should work as expected.");
  108. CHECK_MESSAGE(
  109. vector1.slerp(vector2, 0.5).length() == doctest::Approx((real_t)4.31959610746631919),
  110. "Vector2 slerp with different length input should return a vector with an interpolated length.");
  111. CHECK_MESSAGE(
  112. vector1.angle_to(vector1.slerp(vector2, 0.5)) * 2 == doctest::Approx(vector1.angle_to(vector2)),
  113. "Vector2 slerp with different length input should return a vector with an interpolated angle.");
  114. CHECK_MESSAGE(
  115. vector1.cubic_interpolate(vector2, Vector2(), Vector2(7, 7), 0.5) == Vector2(2.375, 3.5),
  116. "Vector2 cubic_interpolate should work as expected.");
  117. CHECK_MESSAGE(
  118. vector1.cubic_interpolate(vector2, Vector2(), Vector2(7, 7), 1.0 / 3.0).is_equal_approx(Vector2(1.851851940155029297, 2.962963104248046875)),
  119. "Vector2 cubic_interpolate should work as expected.");
  120. CHECK_MESSAGE(
  121. Vector2(1, 0).move_toward(Vector2(10, 0), 3) == Vector2(4, 0),
  122. "Vector2 move_toward should work as expected.");
  123. }
  124. TEST_CASE("[Vector2] Length methods") {
  125. const Vector2 vector1 = Vector2(10, 10);
  126. const Vector2 vector2 = Vector2(20, 30);
  127. CHECK_MESSAGE(
  128. vector1.length_squared() == 200,
  129. "Vector2 length_squared should work as expected and return exact result.");
  130. CHECK_MESSAGE(
  131. vector1.length() == doctest::Approx(10 * (real_t)Math_SQRT2),
  132. "Vector2 length should work as expected.");
  133. CHECK_MESSAGE(
  134. vector2.length_squared() == 1300,
  135. "Vector2 length_squared should work as expected and return exact result.");
  136. CHECK_MESSAGE(
  137. vector2.length() == doctest::Approx((real_t)36.05551275463989293119),
  138. "Vector2 length should work as expected.");
  139. CHECK_MESSAGE(
  140. vector1.distance_squared_to(vector2) == 500,
  141. "Vector2 distance_squared_to should work as expected and return exact result.");
  142. CHECK_MESSAGE(
  143. vector1.distance_to(vector2) == doctest::Approx((real_t)22.36067977499789696409),
  144. "Vector2 distance_to should work as expected.");
  145. }
  146. TEST_CASE("[Vector2] Limiting methods") {
  147. const Vector2 vector = Vector2(10, 10);
  148. CHECK_MESSAGE(
  149. vector.limit_length().is_equal_approx(Vector2(Math_SQRT12, Math_SQRT12)),
  150. "Vector2 limit_length should work as expected.");
  151. CHECK_MESSAGE(
  152. vector.limit_length(5).is_equal_approx(5 * Vector2(Math_SQRT12, Math_SQRT12)),
  153. "Vector2 limit_length should work as expected.");
  154. CHECK_MESSAGE(
  155. Vector2(-5, 15).clamp(Vector2(), vector).is_equal_approx(Vector2(0, 10)),
  156. "Vector2 clamp should work as expected.");
  157. CHECK_MESSAGE(
  158. vector.clamp(Vector2(0, 15), Vector2(5, 20)).is_equal_approx(Vector2(5, 15)),
  159. "Vector2 clamp should work as expected.");
  160. }
  161. TEST_CASE("[Vector2] Normalization methods") {
  162. CHECK_MESSAGE(
  163. Vector2(1, 0).is_normalized() == true,
  164. "Vector2 is_normalized should return true for a normalized vector.");
  165. CHECK_MESSAGE(
  166. Vector2(1, 1).is_normalized() == false,
  167. "Vector2 is_normalized should return false for a non-normalized vector.");
  168. CHECK_MESSAGE(
  169. Vector2(1, 0).normalized() == Vector2(1, 0),
  170. "Vector2 normalized should return the same vector for a normalized vector.");
  171. CHECK_MESSAGE(
  172. Vector2(1, 1).normalized().is_equal_approx(Vector2(Math_SQRT12, Math_SQRT12)),
  173. "Vector2 normalized should work as expected.");
  174. Vector2 vector = Vector2(3.2, -5.4);
  175. vector.normalize();
  176. CHECK_MESSAGE(
  177. vector == Vector2(3.2, -5.4).normalized(),
  178. "Vector2 normalize should convert same way as Vector2 normalized.");
  179. CHECK_MESSAGE(
  180. vector.is_equal_approx(Vector2(0.509802390301732898898, -0.860291533634174266891)),
  181. "Vector2 normalize should work as expected.");
  182. }
  183. TEST_CASE("[Vector2] Operators") {
  184. const Vector2 decimal1 = Vector2(2.3, 4.9);
  185. const Vector2 decimal2 = Vector2(1.2, 3.4);
  186. const Vector2 power1 = Vector2(0.75, 1.5);
  187. const Vector2 power2 = Vector2(0.5, 0.125);
  188. const Vector2 int1 = Vector2(4, 5);
  189. const Vector2 int2 = Vector2(1, 2);
  190. CHECK_MESSAGE(
  191. (decimal1 + decimal2).is_equal_approx(Vector2(3.5, 8.3)),
  192. "Vector2 addition should behave as expected.");
  193. CHECK_MESSAGE(
  194. (power1 + power2) == Vector2(1.25, 1.625),
  195. "Vector2 addition with powers of two should give exact results.");
  196. CHECK_MESSAGE(
  197. (int1 + int2) == Vector2(5, 7),
  198. "Vector2 addition with integers should give exact results.");
  199. CHECK_MESSAGE(
  200. (decimal1 - decimal2).is_equal_approx(Vector2(1.1, 1.5)),
  201. "Vector2 subtraction should behave as expected.");
  202. CHECK_MESSAGE(
  203. (power1 - power2) == Vector2(0.25, 1.375),
  204. "Vector2 subtraction with powers of two should give exact results.");
  205. CHECK_MESSAGE(
  206. (int1 - int2) == Vector2(3, 3),
  207. "Vector2 subtraction with integers should give exact results.");
  208. CHECK_MESSAGE(
  209. (decimal1 * decimal2).is_equal_approx(Vector2(2.76, 16.66)),
  210. "Vector2 multiplication should behave as expected.");
  211. CHECK_MESSAGE(
  212. (power1 * power2) == Vector2(0.375, 0.1875),
  213. "Vector2 multiplication with powers of two should give exact results.");
  214. CHECK_MESSAGE(
  215. (int1 * int2) == Vector2(4, 10),
  216. "Vector2 multiplication with integers should give exact results.");
  217. CHECK_MESSAGE(
  218. (decimal1 / decimal2).is_equal_approx(Vector2(1.91666666666666666, 1.44117647058823529)),
  219. "Vector2 division should behave as expected.");
  220. CHECK_MESSAGE(
  221. (power1 / power2) == Vector2(1.5, 12.0),
  222. "Vector2 division with powers of two should give exact results.");
  223. CHECK_MESSAGE(
  224. (int1 / int2) == Vector2(4, 2.5),
  225. "Vector2 division with integers should give exact results.");
  226. CHECK_MESSAGE(
  227. (decimal1 * 2).is_equal_approx(Vector2(4.6, 9.8)),
  228. "Vector2 multiplication should behave as expected.");
  229. CHECK_MESSAGE(
  230. (power1 * 2) == Vector2(1.5, 3),
  231. "Vector2 multiplication with powers of two should give exact results.");
  232. CHECK_MESSAGE(
  233. (int1 * 2) == Vector2(8, 10),
  234. "Vector2 multiplication with integers should give exact results.");
  235. CHECK_MESSAGE(
  236. (decimal1 / 2).is_equal_approx(Vector2(1.15, 2.45)),
  237. "Vector2 division should behave as expected.");
  238. CHECK_MESSAGE(
  239. (power1 / 2) == Vector2(0.375, 0.75),
  240. "Vector2 division with powers of two should give exact results.");
  241. CHECK_MESSAGE(
  242. (int1 / 2) == Vector2(2, 2.5),
  243. "Vector2 division with integers should give exact results.");
  244. CHECK_MESSAGE(
  245. ((Vector2i)decimal1) == Vector2i(2, 4),
  246. "Vector2 cast to Vector2i should work as expected.");
  247. CHECK_MESSAGE(
  248. ((Vector2i)decimal2) == Vector2i(1, 3),
  249. "Vector2 cast to Vector2i should work as expected.");
  250. CHECK_MESSAGE(
  251. Vector2(Vector2i(1, 2)) == Vector2(1, 2),
  252. "Vector2 constructed from Vector2i should work as expected.");
  253. CHECK_MESSAGE(
  254. ((String)decimal1) == "(2.3, 4.9)",
  255. "Vector2 cast to String should work as expected.");
  256. CHECK_MESSAGE(
  257. ((String)decimal2) == "(1.2, 3.4)",
  258. "Vector2 cast to String should work as expected.");
  259. CHECK_MESSAGE(
  260. ((String)Vector2(9.8, 9.9)) == "(9.8, 9.9)",
  261. "Vector2 cast to String should work as expected.");
  262. #ifdef REAL_T_IS_DOUBLE
  263. CHECK_MESSAGE(
  264. ((String)Vector2(Math_PI, Math_TAU)) == "(3.14159265358979, 6.28318530717959)",
  265. "Vector2 cast to String should print the correct amount of digits for real_t = double.");
  266. #else
  267. CHECK_MESSAGE(
  268. ((String)Vector2(Math_PI, Math_TAU)) == "(3.141593, 6.283185)",
  269. "Vector2 cast to String should print the correct amount of digits for real_t = float.");
  270. #endif // REAL_T_IS_DOUBLE
  271. }
  272. TEST_CASE("[Vector2] Other methods") {
  273. const Vector2 vector = Vector2(1.2, 3.4);
  274. CHECK_MESSAGE(
  275. vector.aspect() == doctest::Approx((real_t)1.2 / (real_t)3.4),
  276. "Vector2 aspect should work as expected.");
  277. CHECK_MESSAGE(
  278. vector.direction_to(Vector2()).is_equal_approx(-vector.normalized()),
  279. "Vector2 direction_to should work as expected.");
  280. CHECK_MESSAGE(
  281. Vector2(1, 1).direction_to(Vector2(2, 2)).is_equal_approx(Vector2(Math_SQRT12, Math_SQRT12)),
  282. "Vector2 direction_to should work as expected.");
  283. CHECK_MESSAGE(
  284. vector.posmod(2).is_equal_approx(Vector2(1.2, 1.4)),
  285. "Vector2 posmod should work as expected.");
  286. CHECK_MESSAGE(
  287. (-vector).posmod(2).is_equal_approx(Vector2(0.8, 0.6)),
  288. "Vector2 posmod should work as expected.");
  289. CHECK_MESSAGE(
  290. vector.posmodv(Vector2(1, 2)).is_equal_approx(Vector2(0.2, 1.4)),
  291. "Vector2 posmodv should work as expected.");
  292. CHECK_MESSAGE(
  293. (-vector).posmodv(Vector2(2, 3)).is_equal_approx(Vector2(0.8, 2.6)),
  294. "Vector2 posmodv should work as expected.");
  295. CHECK_MESSAGE(
  296. vector.rotated(Math_TAU).is_equal_approx(Vector2(1.2, 3.4)),
  297. "Vector2 rotated should work as expected.");
  298. CHECK_MESSAGE(
  299. vector.rotated(Math_TAU / 4).is_equal_approx(Vector2(-3.4, 1.2)),
  300. "Vector2 rotated should work as expected.");
  301. CHECK_MESSAGE(
  302. vector.rotated(Math_TAU / 3).is_equal_approx(Vector2(-3.544486372867091398996, -0.660769515458673623883)),
  303. "Vector2 rotated should work as expected.");
  304. CHECK_MESSAGE(
  305. vector.rotated(Math_TAU / 2).is_equal_approx(vector.rotated(Math_TAU / -2)),
  306. "Vector2 rotated should work as expected.");
  307. CHECK_MESSAGE(
  308. vector.snapped(Vector2(1, 1)) == Vector2(1, 3),
  309. "Vector2 snapped to integers should be the same as rounding.");
  310. CHECK_MESSAGE(
  311. Vector2(3.4, 5.6).snapped(Vector2(1, 1)) == Vector2(3, 6),
  312. "Vector2 snapped to integers should be the same as rounding.");
  313. CHECK_MESSAGE(
  314. vector.snapped(Vector2(0.25, 0.25)) == Vector2(1.25, 3.5),
  315. "Vector2 snapped to 0.25 should give exact results.");
  316. CHECK_MESSAGE(
  317. Vector2(1.2, 2.5).is_equal_approx(vector.min(Vector2(3.0, 2.5))),
  318. "Vector2 min should return expected value.");
  319. CHECK_MESSAGE(
  320. Vector2(5.3, 3.4).is_equal_approx(vector.max(Vector2(5.3, 2.0))),
  321. "Vector2 max should return expected value.");
  322. }
  323. TEST_CASE("[Vector2] Plane methods") {
  324. const Vector2 vector = Vector2(1.2, 3.4);
  325. const Vector2 vector_y = Vector2(0, 1);
  326. const Vector2 vector_normal = Vector2(0.95879811270838721622267, 0.2840883296913739899919);
  327. const real_t p_d = 99.1;
  328. CHECK_MESSAGE(
  329. vector.bounce(vector_y) == Vector2(1.2, -3.4),
  330. "Vector2 bounce on a plane with normal of the Y axis should.");
  331. CHECK_MESSAGE(
  332. vector.bounce(vector_normal).is_equal_approx(Vector2(-2.85851197982345523329, 2.197477931904161412358)),
  333. "Vector2 bounce with normal should return expected value.");
  334. CHECK_MESSAGE(
  335. vector.reflect(vector_y) == Vector2(-1.2, 3.4),
  336. "Vector2 reflect on a plane with normal of the Y axis should.");
  337. CHECK_MESSAGE(
  338. vector.reflect(vector_normal).is_equal_approx(Vector2(2.85851197982345523329, -2.197477931904161412358)),
  339. "Vector2 reflect with normal should return expected value.");
  340. CHECK_MESSAGE(
  341. vector.project(vector_y) == Vector2(0, 3.4),
  342. "Vector2 projected on the Y axis should only give the Y component.");
  343. CHECK_MESSAGE(
  344. vector.project(vector_normal).is_equal_approx(Vector2(2.0292559899117276166, 0.60126103404791929382)),
  345. "Vector2 projected on a normal should return expected value.");
  346. CHECK_MESSAGE(
  347. vector_normal.plane_project(p_d, vector).is_equal_approx(Vector2(94.187635516479631, 30.951892004882851)),
  348. "Vector2 plane_project should return expected value.");
  349. CHECK_MESSAGE(
  350. vector.slide(vector_y) == Vector2(1.2, 0),
  351. "Vector2 slide on a plane with normal of the Y axis should set the Y to zero.");
  352. CHECK_MESSAGE(
  353. vector.slide(vector_normal).is_equal_approx(Vector2(-0.8292559899117276166456, 2.798738965952080706179)),
  354. "Vector2 slide with normal should return expected value.");
  355. // There's probably a better way to test these ones?
  356. #ifdef MATH_CHECKS
  357. const Vector2 vector_non_normal = Vector2(5.4, 1.6);
  358. ERR_PRINT_OFF;
  359. CHECK_MESSAGE(
  360. vector.bounce(vector_non_normal).is_equal_approx(Vector2()),
  361. "Vector2 bounce should return empty Vector2 with non-normalized input.");
  362. CHECK_MESSAGE(
  363. vector.reflect(vector_non_normal).is_equal_approx(Vector2()),
  364. "Vector2 reflect should return empty Vector2 with non-normalized input.");
  365. CHECK_MESSAGE(
  366. vector.slide(vector_non_normal).is_equal_approx(Vector2()),
  367. "Vector2 slide should return empty Vector2 with non-normalized input.");
  368. ERR_PRINT_ON;
  369. #endif // MATH_CHECKS
  370. }
  371. TEST_CASE("[Vector2] Rounding methods") {
  372. const Vector2 vector1 = Vector2(1.2, 5.6);
  373. const Vector2 vector2 = Vector2(1.2, -5.6);
  374. CHECK_MESSAGE(
  375. vector1.abs() == vector1,
  376. "Vector2 abs should work as expected.");
  377. CHECK_MESSAGE(
  378. vector2.abs() == vector1,
  379. "Vector2 abs should work as expected.");
  380. CHECK_MESSAGE(
  381. vector1.ceil() == Vector2(2, 6),
  382. "Vector2 ceil should work as expected.");
  383. CHECK_MESSAGE(
  384. vector2.ceil() == Vector2(2, -5),
  385. "Vector2 ceil should work as expected.");
  386. CHECK_MESSAGE(
  387. vector1.floor() == Vector2(1, 5),
  388. "Vector2 floor should work as expected.");
  389. CHECK_MESSAGE(
  390. vector2.floor() == Vector2(1, -6),
  391. "Vector2 floor should work as expected.");
  392. CHECK_MESSAGE(
  393. vector1.round() == Vector2(1, 6),
  394. "Vector2 round should work as expected.");
  395. CHECK_MESSAGE(
  396. vector2.round() == Vector2(1, -6),
  397. "Vector2 round should work as expected.");
  398. CHECK_MESSAGE(
  399. vector1.sign() == Vector2(1, 1),
  400. "Vector2 sign should work as expected.");
  401. CHECK_MESSAGE(
  402. vector2.sign() == Vector2(1, -1),
  403. "Vector2 sign should work as expected.");
  404. }
  405. TEST_CASE("[Vector2] Linear algebra methods") {
  406. const Vector2 vector_x = Vector2(1, 0);
  407. const Vector2 vector_y = Vector2(0, 1);
  408. const Vector2 a = Vector2(3.5, 8.5);
  409. const Vector2 b = Vector2(5.2, 4.6);
  410. CHECK_MESSAGE(
  411. vector_x.cross(vector_y) == 1,
  412. "Vector2 cross product of X and Y should give 1.");
  413. CHECK_MESSAGE(
  414. vector_y.cross(vector_x) == -1,
  415. "Vector2 cross product of Y and X should give negative 1.");
  416. CHECK_MESSAGE(
  417. a.cross(b) == doctest::Approx((real_t)-28.1),
  418. "Vector2 cross should return expected value.");
  419. CHECK_MESSAGE(
  420. Vector2(-a.x, a.y).cross(Vector2(b.x, -b.y)) == doctest::Approx((real_t)-28.1),
  421. "Vector2 cross should return expected value.");
  422. CHECK_MESSAGE(
  423. vector_x.dot(vector_y) == 0.0,
  424. "Vector2 dot product of perpendicular vectors should be zero.");
  425. CHECK_MESSAGE(
  426. vector_x.dot(vector_x) == 1.0,
  427. "Vector2 dot product of identical unit vectors should be one.");
  428. CHECK_MESSAGE(
  429. (vector_x * 10).dot(vector_x * 10) == 100.0,
  430. "Vector2 dot product of same direction vectors should behave as expected.");
  431. CHECK_MESSAGE(
  432. a.dot(b) == doctest::Approx((real_t)57.3),
  433. "Vector2 dot should return expected value.");
  434. CHECK_MESSAGE(
  435. Vector2(-a.x, a.y).dot(Vector2(b.x, -b.y)) == doctest::Approx((real_t)-57.3),
  436. "Vector2 dot should return expected value.");
  437. }
  438. TEST_CASE("[Vector2] Finite number checks") {
  439. const double infinite[] = { NAN, INFINITY, -INFINITY };
  440. CHECK_MESSAGE(
  441. Vector2(0, 1).is_finite(),
  442. "Vector2(0, 1) should be finite");
  443. for (double x : infinite) {
  444. CHECK_FALSE_MESSAGE(
  445. Vector2(x, 1).is_finite(),
  446. "Vector2 with one component infinite should not be finite.");
  447. CHECK_FALSE_MESSAGE(
  448. Vector2(0, x).is_finite(),
  449. "Vector2 with one component infinite should not be finite.");
  450. }
  451. for (double x : infinite) {
  452. for (double y : infinite) {
  453. CHECK_FALSE_MESSAGE(
  454. Vector2(x, y).is_finite(),
  455. "Vector2 with two components infinite should not be finite.");
  456. }
  457. }
  458. }
  459. } // namespace TestVector2
  460. #endif // TEST_VECTOR2_H