test_vector2i.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /**************************************************************************/
  2. /* test_vector2i.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_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] Constructor methods") {
  37. const Vector2i vector_empty = Vector2i();
  38. const Vector2i vector_zero = Vector2i(0, 0);
  39. CHECK_MESSAGE(
  40. vector_empty == vector_zero,
  41. "Vector2i Constructor with no inputs should return a zero Vector2i.");
  42. }
  43. TEST_CASE("[Vector2i] Axis methods") {
  44. Vector2i vector = Vector2i(2, 3);
  45. CHECK_MESSAGE(
  46. vector.max_axis_index() == Vector2i::Axis::AXIS_Y,
  47. "Vector2i max_axis_index should work as expected.");
  48. CHECK_MESSAGE(
  49. vector.min_axis_index() == Vector2i::Axis::AXIS_X,
  50. "Vector2i min_axis_index should work as expected.");
  51. CHECK_MESSAGE(
  52. vector[vector.min_axis_index()] == 2,
  53. "Vector2i array operator should work as expected.");
  54. vector[Vector2i::Axis::AXIS_Y] = 5;
  55. CHECK_MESSAGE(
  56. vector[Vector2i::Axis::AXIS_Y] == 5,
  57. "Vector2i array operator setter should work as expected.");
  58. }
  59. TEST_CASE("[Vector2i] Clamp method") {
  60. const Vector2i vector = Vector2i(10, 10);
  61. CHECK_MESSAGE(
  62. Vector2i(-5, 15).clamp(Vector2i(), vector) == Vector2i(0, 10),
  63. "Vector2i clamp should work as expected.");
  64. CHECK_MESSAGE(
  65. vector.clamp(Vector2i(0, 15), Vector2i(5, 20)) == Vector2i(5, 15),
  66. "Vector2i clamp should work as expected.");
  67. }
  68. TEST_CASE("[Vector2i] Length methods") {
  69. const Vector2i vector1 = Vector2i(10, 10);
  70. const Vector2i vector2 = Vector2i(20, 30);
  71. CHECK_MESSAGE(
  72. vector1.length_squared() == 200,
  73. "Vector2i length_squared should work as expected and return exact result.");
  74. CHECK_MESSAGE(
  75. vector1.length() == doctest::Approx(10 * Math_SQRT2),
  76. "Vector2i length should work as expected.");
  77. CHECK_MESSAGE(
  78. vector2.length_squared() == 1300,
  79. "Vector2i length_squared should work as expected and return exact result.");
  80. CHECK_MESSAGE(
  81. vector2.length() == doctest::Approx(36.05551275463989293119),
  82. "Vector2i length should work as expected.");
  83. CHECK_MESSAGE(
  84. vector1.distance_squared_to(vector2) == 500,
  85. "Vector2i distance_squared_to should work as expected and return exact result.");
  86. CHECK_MESSAGE(
  87. vector1.distance_to(vector2) == doctest::Approx(22.36067977499789696409),
  88. "Vector2i distance_to should work as expected.");
  89. }
  90. TEST_CASE("[Vector2i] Operators") {
  91. const Vector2i vector1 = Vector2i(5, 9);
  92. const Vector2i vector2 = Vector2i(2, 3);
  93. CHECK_MESSAGE(
  94. (vector1 + vector2) == Vector2i(7, 12),
  95. "Vector2i addition with integers should give exact results.");
  96. CHECK_MESSAGE(
  97. (vector1 - vector2) == Vector2i(3, 6),
  98. "Vector2i subtraction with integers should give exact results.");
  99. CHECK_MESSAGE(
  100. (vector1 * vector2) == Vector2i(10, 27),
  101. "Vector2i multiplication with integers should give exact results.");
  102. CHECK_MESSAGE(
  103. (vector1 / vector2) == Vector2i(2, 3),
  104. "Vector2i division with integers should give exact results.");
  105. CHECK_MESSAGE(
  106. (vector1 * 2) == Vector2i(10, 18),
  107. "Vector2i multiplication with integers should give exact results.");
  108. CHECK_MESSAGE(
  109. (vector1 / 2) == Vector2i(2, 4),
  110. "Vector2i division with integers should give exact results.");
  111. CHECK_MESSAGE(
  112. ((Vector2)vector1) == Vector2(5, 9),
  113. "Vector2i cast to Vector2 should work as expected.");
  114. CHECK_MESSAGE(
  115. ((Vector2)vector2) == Vector2(2, 3),
  116. "Vector2i cast to Vector2 should work as expected.");
  117. CHECK_MESSAGE(
  118. Vector2i(Vector2(1.1, 2.9)) == Vector2i(1, 2),
  119. "Vector2i constructed from Vector2 should work as expected.");
  120. }
  121. TEST_CASE("[Vector2i] Other methods") {
  122. const Vector2i vector = Vector2i(1, 3);
  123. CHECK_MESSAGE(
  124. vector.aspect() == doctest::Approx((real_t)1.0 / (real_t)3.0),
  125. "Vector2i aspect should work as expected.");
  126. CHECK_MESSAGE(
  127. vector.min(Vector2i(3, 2)) == Vector2i(1, 2),
  128. "Vector2i min should return expected value.");
  129. CHECK_MESSAGE(
  130. vector.max(Vector2i(5, 2)) == Vector2i(5, 3),
  131. "Vector2i max should return expected value.");
  132. CHECK_MESSAGE(
  133. vector.snapped(Vector2i(4, 2)) == Vector2i(0, 4),
  134. "Vector2i snapped should work as expected.");
  135. }
  136. TEST_CASE("[Vector2i] Abs and sign methods") {
  137. const Vector2i vector1 = Vector2i(1, 3);
  138. const Vector2i vector2 = Vector2i(1, -3);
  139. CHECK_MESSAGE(
  140. vector1.abs() == vector1,
  141. "Vector2i abs should work as expected.");
  142. CHECK_MESSAGE(
  143. vector2.abs() == vector1,
  144. "Vector2i abs should work as expected.");
  145. CHECK_MESSAGE(
  146. vector1.sign() == Vector2i(1, 1),
  147. "Vector2i sign should work as expected.");
  148. CHECK_MESSAGE(
  149. vector2.sign() == Vector2i(1, -1),
  150. "Vector2i sign should work as expected.");
  151. }
  152. } // namespace TestVector2i
  153. #endif // TEST_VECTOR2I_H