VectorTests.cpp 7.5 KB

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