VectorTests.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #include "tests/VectorTests.h"
  2. #include "math/Vector.h"
  3. #include "test/Test.h"
  4. const float eps = 0.0001f;
  5. using V3 = Core::Vector3;
  6. using I3 = Core::IntVector3;
  7. static void testInitAndRead() {
  8. V3 v1;
  9. Core::Vector2 v2(1.0f, 2.0f);
  10. V3 v3(3.0f, 4.0f, 5.0f);
  11. Core::Vector4 v4(6.0f, 7.0f, 8.0f, 9.0f);
  12. CORE_TEST_FLOAT(0.0f, v1[0], 0.0f);
  13. CORE_TEST_FLOAT(0.0f, v1[0], 0.0f);
  14. CORE_TEST_FLOAT(0.0f, v1[0], 0.0f);
  15. CORE_TEST_FLOAT(1.0f, v2[0], 0.0f);
  16. CORE_TEST_FLOAT(2.0f, v2[1], 0.0f);
  17. CORE_TEST_FLOAT(3.0f, v3[0], 0.0f);
  18. CORE_TEST_FLOAT(4.0f, v3[1], 0.0f);
  19. CORE_TEST_FLOAT(5.0f, v3[2], 0.0f);
  20. CORE_TEST_FLOAT(6.0f, v4[0], 0.0f);
  21. CORE_TEST_FLOAT(7.0f, v4[1], 0.0f);
  22. CORE_TEST_FLOAT(8.0f, v4[2], 0.0f);
  23. CORE_TEST_FLOAT(9.0f, v4[3], 0.0f);
  24. }
  25. static void testSetAngles() {
  26. float root = Core::Math::squareRoot(2.0f) * 0.5f;
  27. CORE_TEST_VECTOR(V3(1.0f, 0.0f, 0.0f), V3().setAngles(0.0f, 0.0f));
  28. CORE_TEST_VECTOR(V3(root, 0.0f, -root), V3().setAngles(45.0f, 0.0f));
  29. CORE_TEST_VECTOR(V3(0.0f, 0.0f, -1.0f), V3().setAngles(90.0f, 0.0f));
  30. CORE_TEST_VECTOR(V3(-root, 0.0f, -root), V3().setAngles(135.0f, 0.0f));
  31. CORE_TEST_VECTOR(V3(-1.0f, 0.0f, 0.0f), V3().setAngles(180.0f, 0.0f));
  32. CORE_TEST_VECTOR(V3(-root, 0.0f, root), V3().setAngles(225.0f, 0.0f));
  33. CORE_TEST_VECTOR(V3(0.0f, 0.0f, 1.0f), V3().setAngles(270.0f, 0.0f));
  34. CORE_TEST_VECTOR(V3(root, 0.0f, root), V3().setAngles(315.0f, 0.0f));
  35. CORE_TEST_VECTOR(V3(0.0f, 1.0f, 0.0f), V3().setAngles(0.0f, 90.0f));
  36. CORE_TEST_VECTOR(V3(0.0f, 1.0f, 0.0f), V3().setAngles(90.0f, 90.0f));
  37. CORE_TEST_VECTOR(V3(0.0f, 1.0f, 0.0f), V3().setAngles(180.0f, 90.0f));
  38. CORE_TEST_VECTOR(V3(0.0f, 1.0f, 0.0f), V3().setAngles(270.0f, 90.0f));
  39. CORE_TEST_VECTOR(V3(0.0f, -1.0f, 0.0f), V3().setAngles(0.0f, -90.0f));
  40. CORE_TEST_VECTOR(V3(0.0f, -1.0f, 0.0f), V3().setAngles(90.0f, -90.0f));
  41. CORE_TEST_VECTOR(V3(0.0f, -1.0f, 0.0f), V3().setAngles(180.0f, -90.0f));
  42. CORE_TEST_VECTOR(V3(0.0f, -1.0f, 0.0f), V3().setAngles(270.0f, -90.0f));
  43. CORE_TEST_VECTOR(V3(root, root, 0.0f), V3().setAngles(0.0f, 45.0f));
  44. CORE_TEST_VECTOR(V3(0.0f, root, -root), V3().setAngles(90.0f, 45.0f));
  45. CORE_TEST_VECTOR(V3(-root, root, 0.0f), V3().setAngles(180.0f, 45.0f));
  46. CORE_TEST_VECTOR(V3(0.0f, root, root), V3().setAngles(270.0f, 45.0f));
  47. CORE_TEST_VECTOR(V3(root, -root, 0.0f), V3().setAngles(0.0f, -45.0f));
  48. CORE_TEST_VECTOR(V3(0.0f, -root, -root), V3().setAngles(90.0f, -45.0f));
  49. CORE_TEST_VECTOR(V3(-root, -root, 0.0f), V3().setAngles(180.0f, -45.0f));
  50. CORE_TEST_VECTOR(V3(0.0f, -root, root), V3().setAngles(270.0f, -45.0f));
  51. CORE_TEST_VECTOR(V3(0.5f, root, -0.5f), V3().setAngles(45.0f, 45.0f));
  52. }
  53. static void testCross() {
  54. CORE_TEST_VECTOR(V3(0.0f, 0.0f, 1.0f),
  55. V3(1.0f, 0.0f, 0.0f).cross(V3(0.0f, 1.0f, 0.0f)));
  56. CORE_TEST_VECTOR(V3(0.0f, -1.0f, 0.0f),
  57. V3(1.0f, 0.0f, 0.0f).cross(V3(0.0f, 0.0f, 1.0f)));
  58. CORE_TEST_VECTOR(V3(0.0f, 0.0f, -1.0f),
  59. V3(0.0f, 1.0f, 0.0f).cross(V3(1.0f, 0.0f, 0.0f)));
  60. CORE_TEST_VECTOR(V3(1.0f, 0.0f, 0.0f),
  61. V3(0.0f, 1.0f, 0.0f).cross(V3(0.0f, 0.0f, 1.0f)));
  62. CORE_TEST_VECTOR(V3(0.0f, 1.0f, 0.0f),
  63. V3(0.0f, 0.0f, 1.0f).cross(V3(1.0f, 0.0f, 0.0f)));
  64. CORE_TEST_VECTOR(V3(-1.0f, 0.0f, 0.0f),
  65. V3(0.0f, 0.0f, 1.0f).cross(V3(0.0f, 1.0f, 0.0f)));
  66. }
  67. static void testSetAdd() {
  68. V3 v;
  69. v += V3(1.0f, 2.0f, 3.0f);
  70. CORE_TEST_VECTOR(V3(1.0f, 2.0f, 3.0f), v);
  71. v += V3(2.0f, 3.0f, 4.0f);
  72. CORE_TEST_VECTOR(V3(3.0f, 5.0f, 7.0f), v);
  73. }
  74. static void testAdd() {
  75. CORE_TEST_VECTOR(V3(1.0f, 2.0f, 3.0f), V3() + V3(1.0f, 2.0f, 3.0f));
  76. CORE_TEST_VECTOR(V3(3.0f, 5.0f, 7.0f),
  77. V3(1.0f, 2.0f, 3.0f) + V3(2.0f, 3.0f, 4.0f));
  78. }
  79. static void testSetSub() {
  80. V3 v;
  81. v -= V3(1.0f, 2.0f, 3.0f);
  82. CORE_TEST_VECTOR(V3(-1.0f, -2.0f, -3.0f), v);
  83. v -= V3(2.0f, 3.0f, 4.0f);
  84. CORE_TEST_VECTOR(V3(-3.0f, -5.0f, -7.0f), v);
  85. }
  86. static void testSub() {
  87. CORE_TEST_VECTOR(V3(1.0f, 2.0f, 3.0f), V3() - V3(-1.0f, -2.0f, -3.0f));
  88. CORE_TEST_VECTOR(V3(-1.0f, -1.0f, -1.0f),
  89. V3(1.0f, 2.0f, 3.0f) - V3(2.0f, 3.0f, 4.0f));
  90. }
  91. static void testInvert() {
  92. CORE_TEST_VECTOR(V3(-1.0f, 2.0f, 3.0f), -V3(1.0f, -2.0f, -3.0f));
  93. }
  94. static void testSetMul() {
  95. V3 v(1.0f, 2.0f, 3.0f);
  96. v *= 3.0f;
  97. CORE_TEST_VECTOR(V3(3.0f, 6.0f, 9.0f), v);
  98. v *= -2.0f;
  99. CORE_TEST_VECTOR(V3(-6.0f, -12.0f, -18.0f), v);
  100. }
  101. static void testMul() {
  102. CORE_TEST_VECTOR(V3(-3.0f, -6.0f, -9.0f), 3.0f * V3(-1.0f, -2.0f, -3.0f));
  103. CORE_TEST_VECTOR(V3(3.0f, 6.0f, 9.0f), V3(1.0f, 2.0f, 3.0f) * 3.0);
  104. }
  105. static void testSetMulVector() {
  106. V3 v(1.0f, 2.0f, 3.0f);
  107. v *= V3(2.0f, 1.0f, 3.0f);
  108. CORE_TEST_VECTOR(V3(2.0f, 2.0f, 9.0f), v);
  109. v *= V3(-3.0f, 4.0f, -2.0f);
  110. CORE_TEST_VECTOR(V3(-6.0f, 8.0f, -18.0f), v);
  111. }
  112. static void testMulVector() {
  113. CORE_TEST_VECTOR(V3(-2.0f, -2.0f, -9.0f),
  114. V3(2.0f, 1.0f, 3.0f) * V3(-1.0f, -2.0f, -3.0f));
  115. CORE_TEST_VECTOR(V3(2.0f, 2.0f, 9.0f),
  116. V3(1.0f, 2.0f, 3.0f) * V3(2.0f, 1.0f, 3.0f));
  117. }
  118. static void testSetDiv() {
  119. V3 v(12.0f, 24.0f, 9.0f);
  120. v /= 3.0f;
  121. CORE_TEST_VECTOR(V3(4.0f, 8.0f, 3.0f), v);
  122. v /= -2.0f;
  123. CORE_TEST_VECTOR(V3(-2.0f, -4.0f, -1.5f), v);
  124. }
  125. static void testDiv() {
  126. CORE_TEST_VECTOR(V3(-1.0f, -2.0f, -3.0f), V3(-3.0f, -6.0f, -9.0f) / 3.0f);
  127. }
  128. static void testDot() {
  129. CORE_TEST_FLOAT(9.0f, V3(-4.0f, 2.0f, -3.0f).dot(V3(-1.0f, -2.0f, -3.0f)),
  130. eps);
  131. CORE_TEST_FLOAT(-22.0f, V3(2.0f, 2.0f, -4.0f).dot(V3(1.0f, -2.0f, 5.0f)),
  132. eps);
  133. }
  134. static void testSquareLength() {
  135. CORE_TEST_FLOAT(29.0f, V3(-4.0f, 2.0f, -3.0f).squareLength(), eps);
  136. CORE_TEST_FLOAT(24.0f, V3(2.0f, 2.0f, -4.0f).squareLength(), eps);
  137. }
  138. static void testLength() {
  139. CORE_TEST_FLOAT(3.0f, V3(-2.0f, 2.0f, -1.0f).length(), eps);
  140. CORE_TEST_FLOAT(7.0f, V3(6.0f, 2.0f, -3.0f).length(), eps);
  141. }
  142. static void testNormalize() {
  143. V3 v1(-2.0f, 2.0f, -1.0f);
  144. V3 v2 = v1 * (1.0f / 3.0f);
  145. v1.normalize();
  146. CORE_TEST_VECTOR(v2, v1);
  147. V3 v3(6.0f, 2.0f, -3.0f);
  148. V3 v4 = v3 * (1.0f / 7.0f);
  149. v3.normalize();
  150. CORE_TEST_VECTOR(v4, v3);
  151. }
  152. static void testCast() {
  153. CORE_TEST_VECTOR(V3(-2.5f, 2.6f, 9.0f).toInt(), I3(-2, 2, 9));
  154. CORE_TEST_VECTOR(I3(-2.5f, 2.6f, 9.0f).toFloat(), V3(-2.0f, 2.0f, 9.0f));
  155. }
  156. static void testToString() {
  157. Core::String32<200> s;
  158. CORE_TEST_ERROR(s.append(Core::Vector<1, float>()));
  159. CORE_TEST_ERROR(s.append(Core::Vector2(2.0f, 3.0f)));
  160. CORE_TEST_ERROR(s.append(V3(4.0f, 5.0f, 6.0f)));
  161. Core::String32<200> s2;
  162. CORE_TEST_ERROR(s2.append("[0.00][2.00, 3.00][4.00, 5.00, 6.00]"));
  163. CORE_TEST_EQUAL(s2, s);
  164. }
  165. void Core::VectorTests::test() {
  166. testInitAndRead();
  167. testSetAngles();
  168. testCross();
  169. testSetAdd();
  170. testAdd();
  171. testSetSub();
  172. testSub();
  173. testInvert();
  174. testSetMul();
  175. testMul();
  176. testSetMulVector();
  177. testMulVector();
  178. testSetDiv();
  179. testDiv();
  180. testDot();
  181. testSquareLength();
  182. testLength();
  183. testNormalize();
  184. testCast();
  185. testToString();
  186. }