test_vector4i.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*************************************************************************/
  2. /* test_vector4i.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_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] Axis methods") {
  36. Vector4i vector = Vector4i(1, 2, 3, 4);
  37. CHECK_MESSAGE(
  38. vector.max_axis_index() == Vector4i::Axis::AXIS_W,
  39. "Vector4i max_axis_index should work as expected.");
  40. CHECK_MESSAGE(
  41. vector.min_axis_index() == Vector4i::Axis::AXIS_X,
  42. "Vector4i min_axis_index should work as expected.");
  43. CHECK_MESSAGE(
  44. vector.get_axis(vector.max_axis_index()) == 4,
  45. "Vector4i get_axis should work as expected.");
  46. CHECK_MESSAGE(
  47. vector[vector.min_axis_index()] == 1,
  48. "Vector4i array operator should work as expected.");
  49. vector.set_axis(Vector4i::Axis::AXIS_Y, 5);
  50. CHECK_MESSAGE(
  51. vector.get_axis(Vector4i::Axis::AXIS_Y) == 5,
  52. "Vector4i set_axis should work as expected.");
  53. vector[Vector4i::Axis::AXIS_Y] = 5;
  54. CHECK_MESSAGE(
  55. vector[Vector4i::Axis::AXIS_Y] == 5,
  56. "Vector4i array operator setter should work as expected.");
  57. }
  58. TEST_CASE("[Vector4i] Clamp method") {
  59. const Vector4i vector = Vector4i(10, 10, 10, 10);
  60. CHECK_MESSAGE(
  61. Vector4i(-5, 5, 15, INT_MAX).clamp(Vector4i(), vector) == Vector4i(0, 5, 10, 10),
  62. "Vector4i clamp should work as expected.");
  63. CHECK_MESSAGE(
  64. vector.clamp(Vector4i(0, 10, 15, -10), Vector4i(5, 10, 20, -5)) == Vector4i(5, 10, 15, -5),
  65. "Vector4i clamp should work as expected.");
  66. }
  67. TEST_CASE("[Vector4i] Length methods") {
  68. const Vector4i vector1 = Vector4i(10, 10, 10, 10);
  69. const Vector4i vector2 = Vector4i(20, 30, 40, 50);
  70. CHECK_MESSAGE(
  71. vector1.length_squared() == 400,
  72. "Vector4i length_squared should work as expected and return exact result.");
  73. CHECK_MESSAGE(
  74. Math::is_equal_approx(vector1.length(), 20),
  75. "Vector4i length should work as expected.");
  76. CHECK_MESSAGE(
  77. vector2.length_squared() == 5400,
  78. "Vector4i length_squared should work as expected and return exact result.");
  79. CHECK_MESSAGE(
  80. Math::is_equal_approx(vector2.length(), 73.4846922835),
  81. "Vector4i length should work as expected.");
  82. }
  83. TEST_CASE("[Vector4i] Operators") {
  84. const Vector4i vector1 = Vector4i(4, 5, 9, 2);
  85. const Vector4i vector2 = Vector4i(1, 2, 3, 4);
  86. CHECK_MESSAGE(
  87. -vector1 == Vector4i(-4, -5, -9, -2),
  88. "Vector4i change of sign should work as expected.");
  89. CHECK_MESSAGE(
  90. (vector1 + vector2) == Vector4i(5, 7, 12, 6),
  91. "Vector4i addition with integers should give exact results.");
  92. CHECK_MESSAGE(
  93. (vector1 - vector2) == Vector4i(3, 3, 6, -2),
  94. "Vector4i subtraction with integers should give exact results.");
  95. CHECK_MESSAGE(
  96. (vector1 * vector2) == Vector4i(4, 10, 27, 8),
  97. "Vector4i multiplication with integers should give exact results.");
  98. CHECK_MESSAGE(
  99. (vector1 / vector2) == Vector4i(4, 2, 3, 0),
  100. "Vector4i division with integers should give exact results.");
  101. CHECK_MESSAGE(
  102. (vector1 * 2) == Vector4i(8, 10, 18, 4),
  103. "Vector4i multiplication with integers should give exact results.");
  104. CHECK_MESSAGE(
  105. (vector1 / 2) == Vector4i(2, 2, 4, 1),
  106. "Vector4i division with integers should give exact results.");
  107. CHECK_MESSAGE(
  108. ((Vector4)vector1) == Vector4(4, 5, 9, 2),
  109. "Vector4i cast to Vector4 should work as expected.");
  110. CHECK_MESSAGE(
  111. ((Vector4)vector2) == Vector4(1, 2, 3, 4),
  112. "Vector4i cast to Vector4 should work as expected.");
  113. CHECK_MESSAGE(
  114. Vector4i(Vector4(1.1, 2.9, 3.9, 100.5)) == Vector4i(1, 2, 3, 100),
  115. "Vector4i constructed from Vector4 should work as expected.");
  116. }
  117. TEST_CASE("[Vector4i] Abs and sign methods") {
  118. const Vector4i vector1 = Vector4i(1, 3, 5, 7);
  119. const Vector4i vector2 = Vector4i(1, -3, -5, 7);
  120. CHECK_MESSAGE(
  121. vector1.abs() == vector1,
  122. "Vector4i abs should work as expected.");
  123. CHECK_MESSAGE(
  124. vector2.abs() == vector1,
  125. "Vector4i abs should work as expected.");
  126. CHECK_MESSAGE(
  127. vector1.sign() == Vector4i(1, 1, 1, 1),
  128. "Vector4i sign should work as expected.");
  129. CHECK_MESSAGE(
  130. vector2.sign() == Vector4i(1, -1, -1, 1),
  131. "Vector4i sign should work as expected.");
  132. }
  133. } // namespace TestVector4i
  134. #endif // TEST_VECTOR4I_H