test_vector2.h 20 KB

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