test_vector4i.h 7.0 KB

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