test_vector3i.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*************************************************************************/
  2. /* test_vector3i.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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] Axis methods") {
  36. Vector3i vector = Vector3i(1, 2, 3);
  37. CHECK_MESSAGE(
  38. vector.max_axis_index() == Vector3i::Axis::AXIS_Z,
  39. "Vector3i max_axis_index should work as expected.");
  40. CHECK_MESSAGE(
  41. vector.min_axis_index() == Vector3i::Axis::AXIS_X,
  42. "Vector3i min_axis_index should work as expected.");
  43. CHECK_MESSAGE(
  44. vector.get_axis(vector.max_axis_index()) == 3,
  45. "Vector3i get_axis should work as expected.");
  46. CHECK_MESSAGE(
  47. vector[vector.min_axis_index()] == 1,
  48. "Vector3i array operator should work as expected.");
  49. vector.set_axis(Vector3i::Axis::AXIS_Y, 4);
  50. CHECK_MESSAGE(
  51. vector.get_axis(Vector3i::Axis::AXIS_Y) == 4,
  52. "Vector3i set_axis should work as expected.");
  53. vector[Vector3i::Axis::AXIS_Y] = 5;
  54. CHECK_MESSAGE(
  55. vector[Vector3i::Axis::AXIS_Y] == 5,
  56. "Vector3i array operator setter should work as expected.");
  57. }
  58. TEST_CASE("[Vector3i] Clamp method") {
  59. const Vector3i vector = Vector3i(10, 10, 10);
  60. CHECK_MESSAGE(
  61. Vector3i(-5, 5, 15).clamp(Vector3i(), vector) == Vector3i(0, 5, 10),
  62. "Vector3i clamp should work as expected.");
  63. CHECK_MESSAGE(
  64. vector.clamp(Vector3i(0, 10, 15), Vector3i(5, 10, 20)) == Vector3i(5, 10, 15),
  65. "Vector3i clamp should work as expected.");
  66. }
  67. TEST_CASE("[Vector3i] Length methods") {
  68. const Vector3i vector1 = Vector3i(10, 10, 10);
  69. const Vector3i vector2 = Vector3i(20, 30, 40);
  70. CHECK_MESSAGE(
  71. vector1.length_squared() == 300,
  72. "Vector3i length_squared should work as expected and return exact result.");
  73. CHECK_MESSAGE(
  74. Math::is_equal_approx(vector1.length(), 10 * Math_SQRT3),
  75. "Vector3i length should work as expected.");
  76. CHECK_MESSAGE(
  77. vector2.length_squared() == 2900,
  78. "Vector3i length_squared should work as expected and return exact result.");
  79. CHECK_MESSAGE(
  80. Math::is_equal_approx(vector2.length(), 53.8516480713450403125),
  81. "Vector3i length should work as expected.");
  82. }
  83. TEST_CASE("[Vector3i] Operators") {
  84. const Vector3i vector1 = Vector3i(4, 5, 9);
  85. const Vector3i vector2 = Vector3i(1, 2, 3);
  86. CHECK_MESSAGE(
  87. (vector1 + vector2) == Vector3i(5, 7, 12),
  88. "Vector3i addition with integers should give exact results.");
  89. CHECK_MESSAGE(
  90. (vector1 - vector2) == Vector3i(3, 3, 6),
  91. "Vector3i subtraction with integers should give exact results.");
  92. CHECK_MESSAGE(
  93. (vector1 * vector2) == Vector3i(4, 10, 27),
  94. "Vector3i multiplication with integers should give exact results.");
  95. CHECK_MESSAGE(
  96. (vector1 / vector2) == Vector3i(4, 2, 3),
  97. "Vector3i division with integers should give exact results.");
  98. CHECK_MESSAGE(
  99. (vector1 * 2) == Vector3i(8, 10, 18),
  100. "Vector3i multiplication with integers should give exact results.");
  101. CHECK_MESSAGE(
  102. (vector1 / 2) == Vector3i(2, 2, 4),
  103. "Vector3i division with integers should give exact results.");
  104. CHECK_MESSAGE(
  105. ((Vector3)vector1) == Vector3(4, 5, 9),
  106. "Vector3i cast to Vector3 should work as expected.");
  107. CHECK_MESSAGE(
  108. ((Vector3)vector2) == Vector3(1, 2, 3),
  109. "Vector3i cast to Vector3 should work as expected.");
  110. CHECK_MESSAGE(
  111. Vector3i(Vector3(1.1, 2.9, 3.9)) == Vector3i(1, 2, 3),
  112. "Vector3i constructed from Vector3 should work as expected.");
  113. }
  114. TEST_CASE("[Vector3i] Abs and sign methods") {
  115. const Vector3i vector1 = Vector3i(1, 3, 5);
  116. const Vector3i vector2 = Vector3i(1, -3, -5);
  117. CHECK_MESSAGE(
  118. vector1.abs() == vector1,
  119. "Vector3i abs should work as expected.");
  120. CHECK_MESSAGE(
  121. vector2.abs() == vector1,
  122. "Vector3i abs should work as expected.");
  123. CHECK_MESSAGE(
  124. vector1.sign() == Vector3i(1, 1, 1),
  125. "Vector3i sign should work as expected.");
  126. CHECK_MESSAGE(
  127. vector2.sign() == Vector3i(1, -1, -1),
  128. "Vector3i sign should work as expected.");
  129. }
  130. } // namespace TestVector3i
  131. #endif // TEST_VECTOR3I_H