test_vector2i.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*************************************************************************/
  2. /* test_vector2i.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_VECTOR2I_H
  31. #define TEST_VECTOR2I_H
  32. #include "core/math/vector2.h"
  33. #include "core/math/vector2i.h"
  34. #include "tests/test_macros.h"
  35. namespace TestVector2i {
  36. TEST_CASE("[Vector2i] Axis methods") {
  37. Vector2i vector = Vector2i(2, 3);
  38. CHECK_MESSAGE(
  39. vector.max_axis_index() == Vector2i::Axis::AXIS_Y,
  40. "Vector2i max_axis_index should work as expected.");
  41. CHECK_MESSAGE(
  42. vector.min_axis_index() == Vector2i::Axis::AXIS_X,
  43. "Vector2i min_axis_index should work as expected.");
  44. CHECK_MESSAGE(
  45. vector[vector.min_axis_index()] == 2,
  46. "Vector2i array operator should work as expected.");
  47. vector[Vector2i::Axis::AXIS_Y] = 5;
  48. CHECK_MESSAGE(
  49. vector[Vector2i::Axis::AXIS_Y] == 5,
  50. "Vector2i array operator setter should work as expected.");
  51. }
  52. TEST_CASE("[Vector2i] Clamp method") {
  53. const Vector2i vector = Vector2i(10, 10);
  54. CHECK_MESSAGE(
  55. Vector2i(-5, 15).clamp(Vector2i(), vector) == Vector2i(0, 10),
  56. "Vector2i clamp should work as expected.");
  57. CHECK_MESSAGE(
  58. vector.clamp(Vector2i(0, 15), Vector2i(5, 20)) == Vector2i(5, 15),
  59. "Vector2i clamp should work as expected.");
  60. }
  61. TEST_CASE("[Vector2i] Length methods") {
  62. const Vector2i vector1 = Vector2i(10, 10);
  63. const Vector2i vector2 = Vector2i(20, 30);
  64. CHECK_MESSAGE(
  65. vector1.length_squared() == 200,
  66. "Vector2i length_squared should work as expected and return exact result.");
  67. CHECK_MESSAGE(
  68. Math::is_equal_approx(vector1.length(), 10 * Math_SQRT2),
  69. "Vector2i length should work as expected.");
  70. CHECK_MESSAGE(
  71. vector2.length_squared() == 1300,
  72. "Vector2i length_squared should work as expected and return exact result.");
  73. CHECK_MESSAGE(
  74. Math::is_equal_approx(vector2.length(), 36.05551275463989293119),
  75. "Vector2i length should work as expected.");
  76. }
  77. TEST_CASE("[Vector2i] Operators") {
  78. const Vector2i vector1 = Vector2i(5, 9);
  79. const Vector2i vector2 = Vector2i(2, 3);
  80. CHECK_MESSAGE(
  81. (vector1 + vector2) == Vector2i(7, 12),
  82. "Vector2i addition with integers should give exact results.");
  83. CHECK_MESSAGE(
  84. (vector1 - vector2) == Vector2i(3, 6),
  85. "Vector2i subtraction with integers should give exact results.");
  86. CHECK_MESSAGE(
  87. (vector1 * vector2) == Vector2i(10, 27),
  88. "Vector2i multiplication with integers should give exact results.");
  89. CHECK_MESSAGE(
  90. (vector1 / vector2) == Vector2i(2, 3),
  91. "Vector2i division with integers should give exact results.");
  92. CHECK_MESSAGE(
  93. (vector1 * 2) == Vector2i(10, 18),
  94. "Vector2i multiplication with integers should give exact results.");
  95. CHECK_MESSAGE(
  96. (vector1 / 2) == Vector2i(2, 4),
  97. "Vector2i division with integers should give exact results.");
  98. CHECK_MESSAGE(
  99. ((Vector2)vector1) == Vector2(5, 9),
  100. "Vector2i cast to Vector2 should work as expected.");
  101. CHECK_MESSAGE(
  102. ((Vector2)vector2) == Vector2(2, 3),
  103. "Vector2i cast to Vector2 should work as expected.");
  104. CHECK_MESSAGE(
  105. Vector2i(Vector2(1.1, 2.9)) == Vector2i(1, 2),
  106. "Vector2i constructed from Vector2 should work as expected.");
  107. }
  108. TEST_CASE("[Vector2i] Other methods") {
  109. const Vector2i vector = Vector2i(1, 3);
  110. CHECK_MESSAGE(
  111. Math::is_equal_approx(vector.aspect(), (real_t)1.0 / (real_t)3.0),
  112. "Vector2i aspect should work as expected.");
  113. }
  114. TEST_CASE("[Vector2i] Abs and sign methods") {
  115. const Vector2i vector1 = Vector2i(1, 3);
  116. const Vector2i vector2 = Vector2i(1, -3);
  117. CHECK_MESSAGE(
  118. vector1.abs() == vector1,
  119. "Vector2i abs should work as expected.");
  120. CHECK_MESSAGE(
  121. vector2.abs() == vector1,
  122. "Vector2i abs should work as expected.");
  123. CHECK_MESSAGE(
  124. vector1.sign() == Vector2i(1, 1),
  125. "Vector2i sign should work as expected.");
  126. CHECK_MESSAGE(
  127. vector2.sign() == Vector2i(1, -1),
  128. "Vector2i sign should work as expected.");
  129. }
  130. } // namespace TestVector2i
  131. #endif // TEST_VECTOR2I_H