test_vector2i.h 6.7 KB

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