test_vector3i.h 6.8 KB

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