test_vector3i.h 6.8 KB

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