VectorTests.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. #include "tests/VectorTests.h"
  2. #include "math/Vector.h"
  3. #include "tests/Test.h"
  4. #include "utils/StringBuffer.h"
  5. const float eps = 0.0001f;
  6. template<int N, typename T>
  7. static void compareVectors(Test& test, const Vector<N, T>& wanted,
  8. const Vector<N, T>& actual, const char* text) {
  9. for(int i = 0; i < N; i++) {
  10. test.checkFloat(
  11. static_cast<float>(wanted[i]), static_cast<float>(actual[i]), eps,
  12. StringBuffer<50>(text).append(" (").append(i).append(")"));
  13. }
  14. }
  15. static void testInitAndRead(Test& test) {
  16. Vector3 v1;
  17. Vector2 v2(1.0f, 2.0f);
  18. Vector3 v3(3.0f, 4.0f, 5.0f);
  19. Vector4 v4(6.0f, 7.0f, 8.0f, 9.0f);
  20. test.checkEqual(0.0f, v1[0], "init 1");
  21. test.checkEqual(0.0f, v1[0], "init 2");
  22. test.checkEqual(0.0f, v1[0], "init 3");
  23. test.checkEqual(1.0f, v2[0], "init 4");
  24. test.checkEqual(2.0f, v2[1], "init 5");
  25. test.checkEqual(3.0f, v3[0], "init 6");
  26. test.checkEqual(4.0f, v3[1], "init 7");
  27. test.checkEqual(5.0f, v3[2], "init 8");
  28. test.checkEqual(6.0f, v4[0], "init 9");
  29. test.checkEqual(7.0f, v4[1], "init 10");
  30. test.checkEqual(8.0f, v4[2], "init 11");
  31. test.checkEqual(9.0f, v4[3], "init 12");
  32. }
  33. static void testSetAngles(Test& test) {
  34. float root = sqrtf(2.0f) * 0.5f;
  35. compareVectors(test, Vector3(1.0f, 0.0f, 0.0f),
  36. Vector3().setAngles(0.0f, 0.0f), "angle 0 | 0");
  37. compareVectors(test, Vector3(root, 0.0f, -root),
  38. Vector3().setAngles(45.0f, 0.0f), "angle 45 | 0");
  39. compareVectors(test, Vector3(0.0f, 0.0f, -1.0f),
  40. Vector3().setAngles(90.0f, 0.0f), "angle 90 | 0");
  41. compareVectors(test, Vector3(-root, 0.0f, -root),
  42. Vector3().setAngles(135.0f, 0.0f), "angle 135 | 0");
  43. compareVectors(test, Vector3(-1.0f, 0.0f, 0.0f),
  44. Vector3().setAngles(180.0f, 0.0f), "angle 180 | 0");
  45. compareVectors(test, Vector3(-root, 0.0f, root),
  46. Vector3().setAngles(225.0f, 0.0f), "angle 225 | 0");
  47. compareVectors(test, Vector3(0.0f, 0.0f, 1.0f),
  48. Vector3().setAngles(270.0f, 0.0f), "angle 270 | 0");
  49. compareVectors(test, Vector3(root, 0.0f, root),
  50. Vector3().setAngles(315.0f, 0.0f), "angle 315 | 0");
  51. compareVectors(test, Vector3(0.0f, 1.0f, 0.0f),
  52. Vector3().setAngles(0.0f, 90.0f), "angle 0 | 90");
  53. compareVectors(test, Vector3(0.0f, 1.0f, 0.0f),
  54. Vector3().setAngles(90.0f, 90.0f), "angle 90 | 90");
  55. compareVectors(test, Vector3(0.0f, 1.0f, 0.0f),
  56. Vector3().setAngles(180.0f, 90.0f), "angle 180 | 90");
  57. compareVectors(test, Vector3(0.0f, 1.0f, 0.0f),
  58. Vector3().setAngles(270.0f, 90.0f), "angle 270 | 90");
  59. compareVectors(test, Vector3(0.0f, -1.0f, 0.0f),
  60. Vector3().setAngles(0.0f, -90.0f), "angle 0 | -90");
  61. compareVectors(test, Vector3(0.0f, -1.0f, 0.0f),
  62. Vector3().setAngles(90.0f, -90.0f), "angle 90 | -90");
  63. compareVectors(test, Vector3(0.0f, -1.0f, 0.0f),
  64. Vector3().setAngles(180.0f, -90.0f), "angle 180 | -90");
  65. compareVectors(test, Vector3(0.0f, -1.0f, 0.0f),
  66. Vector3().setAngles(270.0f, -90.0f), "angle 270 | -90");
  67. compareVectors(test, Vector3(root, root, 0.0f),
  68. Vector3().setAngles(0.0f, 45.0f), "angle 0 | 45");
  69. compareVectors(test, Vector3(0.0f, root, -root),
  70. Vector3().setAngles(90.0f, 45.0f), "angle 90 | 45");
  71. compareVectors(test, Vector3(-root, root, 0.0f),
  72. Vector3().setAngles(180.0f, 45.0f), "angle 180 | 45");
  73. compareVectors(test, Vector3(0.0f, root, root),
  74. Vector3().setAngles(270.0f, 45.0f), "angle 270 | 45");
  75. compareVectors(test, Vector3(root, -root, 0.0f),
  76. Vector3().setAngles(0.0f, -45.0f), "angle 0 | -45");
  77. compareVectors(test, Vector3(0.0f, -root, -root),
  78. Vector3().setAngles(90.0f, -45.0f), "angle 90 | -45");
  79. compareVectors(test, Vector3(-root, -root, 0.0f),
  80. Vector3().setAngles(180.0f, -45.0f), "angle 180 | -45");
  81. compareVectors(test, Vector3(0.0f, -root, root),
  82. Vector3().setAngles(270.0f, -45.0f), "angle 270 | -45");
  83. compareVectors(test, Vector3(0.5f, root, -0.5f),
  84. Vector3().setAngles(45.0f, 45.0f), "angle 45 | 45");
  85. }
  86. static void testCross(Test& test) {
  87. compareVectors(test, Vector3(0.0f, 0.0f, 1.0f),
  88. Vector3(1.0f, 0.0f, 0.0f).cross(Vector3(0.0f, 1.0f, 0.0f)),
  89. "cross 1");
  90. compareVectors(test, Vector3(0.0f, -1.0f, 0.0f),
  91. Vector3(1.0f, 0.0f, 0.0f).cross(Vector3(0.0f, 0.0f, 1.0f)),
  92. "cross 2");
  93. compareVectors(test, Vector3(0.0f, 0.0f, -1.0f),
  94. Vector3(0.0f, 1.0f, 0.0f).cross(Vector3(1.0f, 0.0f, 0.0f)),
  95. "cross 3");
  96. compareVectors(test, Vector3(1.0f, 0.0f, 0.0f),
  97. Vector3(0.0f, 1.0f, 0.0f).cross(Vector3(0.0f, 0.0f, 1.0f)),
  98. "cross 4");
  99. compareVectors(test, Vector3(0.0f, 1.0f, 0.0f),
  100. Vector3(0.0f, 0.0f, 1.0f).cross(Vector3(1.0f, 0.0f, 0.0f)),
  101. "cross 5");
  102. compareVectors(test, Vector3(-1.0f, 0.0f, 0.0f),
  103. Vector3(0.0f, 0.0f, 1.0f).cross(Vector3(0.0f, 1.0f, 0.0f)),
  104. "cross 6");
  105. }
  106. static void testSetAdd(Test& test) {
  107. Vector3 v;
  108. v += Vector3(1.0f, 2.0f, 3.0f);
  109. compareVectors(test, Vector3(1.0f, 2.0f, 3.0f), v, "set add 1");
  110. v += Vector3(2.0f, 3.0f, 4.0f);
  111. compareVectors(test, Vector3(3.0f, 5.0f, 7.0f), v, "set add 2");
  112. }
  113. static void testAdd(Test& test) {
  114. compareVectors(test, Vector3(1.0f, 2.0f, 3.0f),
  115. Vector3() + Vector3(1.0f, 2.0f, 3.0f), "add 1");
  116. compareVectors(test, Vector3(3.0f, 5.0f, 7.0f),
  117. Vector3(1.0f, 2.0f, 3.0f) + Vector3(2.0f, 3.0f, 4.0f),
  118. "add 2");
  119. }
  120. static void testSetSub(Test& test) {
  121. Vector3 v;
  122. v -= Vector3(1.0f, 2.0f, 3.0f);
  123. compareVectors(test, Vector3(-1.0f, -2.0f, -3.0f), v, "set sub 1");
  124. v -= Vector3(2.0f, 3.0f, 4.0f);
  125. compareVectors(test, Vector3(-3.0f, -5.0f, -7.0f), v, "set sub 2");
  126. }
  127. static void testSub(Test& test) {
  128. compareVectors(test, Vector3(1.0f, 2.0f, 3.0f),
  129. Vector3() - Vector3(-1.0f, -2.0f, -3.0f), "sub 1");
  130. compareVectors(test, Vector3(-1.0f, -1.0f, -1.0f),
  131. Vector3(1.0f, 2.0f, 3.0f) - Vector3(2.0f, 3.0f, 4.0f),
  132. "sub 2");
  133. }
  134. static void testInvert(Test& test) {
  135. compareVectors(test, Vector3(-1.0f, 2.0f, 3.0f),
  136. -Vector3(1.0f, -2.0f, -3.0f), "invert");
  137. }
  138. static void testSetMul(Test& test) {
  139. Vector3 v(1.0f, 2.0f, 3.0f);
  140. v *= 3.0f;
  141. compareVectors(test, Vector3(3.0f, 6.0f, 9.0f), v, "set mul 1");
  142. v *= -2.0f;
  143. compareVectors(test, Vector3(-6.0f, -12.0f, -18.0f), v, "set mul 2");
  144. }
  145. static void testMul(Test& test) {
  146. compareVectors(test, Vector3(-3.0f, -6.0f, -9.0f),
  147. 3.0f * Vector3(-1.0f, -2.0f, -3.0f), "mul 1");
  148. compareVectors(test, Vector3(3.0f, 6.0f, 9.0f),
  149. Vector3(1.0f, 2.0f, 3.0f) * 3.0, "mul 2");
  150. }
  151. static void testSetMulVector(Test& test) {
  152. Vector3 v(1.0f, 2.0f, 3.0f);
  153. v *= Vector3(2.0f, 1.0f, 3.0f);
  154. compareVectors(test, Vector3(2.0f, 2.0f, 9.0f), v, "set mul vector 1");
  155. v *= Vector3(-3.0f, 4.0f, -2.0f);
  156. compareVectors(test, Vector3(-6.0f, 8.0f, -18.0f), v, "set mul vector 2");
  157. }
  158. static void testMulVector(Test& test) {
  159. compareVectors(test, Vector3(-2.0f, -2.0f, -9.0f),
  160. Vector3(2.0f, 1.0f, 3.0f) * Vector3(-1.0f, -2.0f, -3.0f),
  161. "mul vector 1");
  162. compareVectors(test, Vector3(2.0f, 2.0f, 9.0f),
  163. Vector3(1.0f, 2.0f, 3.0f) * Vector3(2.0f, 1.0f, 3.0f),
  164. "mul vector 2");
  165. }
  166. static void testSetDiv(Test& test) {
  167. Vector3 v(12.0f, 24.0f, 9.0f);
  168. v /= 3.0f;
  169. compareVectors(test, Vector3(4.0f, 8.0f, 3.0f), v, "set div 1");
  170. v /= -2.0f;
  171. compareVectors(test, Vector3(-2.0f, -4.0f, -1.5f), v, "set div 2");
  172. }
  173. static void testDiv(Test& test) {
  174. compareVectors(test, Vector3(-1.0f, -2.0f, -3.0f),
  175. Vector3(-3.0f, -6.0f, -9.0f) / 3.0f, "div");
  176. }
  177. static void testDot(Test& test) {
  178. test.checkFloat(
  179. 9.0f, Vector3(-4.0f, 2.0f, -3.0f).dot(Vector3(-1.0f, -2.0f, -3.0f)),
  180. eps, "dot 1");
  181. test.checkFloat(-22.0f,
  182. Vector3(2.0f, 2.0f, -4.0f).dot(Vector3(1.0f, -2.0f, 5.0f)),
  183. eps, "dot 2");
  184. }
  185. static void testSquareLength(Test& test) {
  186. test.checkFloat(29.0f, Vector3(-4.0f, 2.0f, -3.0f).squareLength(), eps,
  187. "square length 1");
  188. test.checkFloat(24.0f, Vector3(2.0f, 2.0f, -4.0f).squareLength(), eps,
  189. "square length 2");
  190. }
  191. static void testLength(Test& test) {
  192. test.checkFloat(3.0f, Vector3(-2.0f, 2.0f, -1.0f).length(), eps,
  193. "length 1");
  194. test.checkFloat(7.0f, Vector3(6.0f, 2.0f, -3.0f).length(), eps, "length 2");
  195. }
  196. static void testNormalize(Test& test) {
  197. Vector3 v1(-2.0f, 2.0f, -1.0f);
  198. Vector3 v2 = v1 * (1.0f / 3.0f);
  199. v1.normalize();
  200. compareVectors(test, v2, v1, "normalize 1");
  201. Vector3 v3(6.0f, 2.0f, -3.0f);
  202. Vector3 v4 = v3 * (1.0f / 7.0f);
  203. v3.normalize();
  204. compareVectors(test, v4, v3, "normalize 2");
  205. }
  206. static void testCast(Test& test) {
  207. compareVectors(test, Vector3(-2.5f, 2.6f, 9.0f).toInt(),
  208. IntVector3(-2, 2, 9), "float to int");
  209. compareVectors(test, IntVector3(-2.5f, 2.6f, 9.0f).toFloat(),
  210. Vector3(-2.0f, 2.0f, 9.0f), "float to int");
  211. }
  212. static void testToString(Test& test) {
  213. StringBuffer<50> s;
  214. s.append(Vector<1, float>())
  215. .append(Vector2(2.0f, 3.0f))
  216. .append(Vector3(4.0f, 5.0f, 6.0f));
  217. test.checkEqual(StringBuffer<50>("[0.00][2.00, 3.00][4.00, 5.00, 6.00]"), s,
  218. "to string");
  219. }
  220. void VectorTests::test() {
  221. Test test("Vector");
  222. testInitAndRead(test);
  223. testSetAngles(test);
  224. testCross(test);
  225. testSetAdd(test);
  226. testAdd(test);
  227. testSetSub(test);
  228. testSub(test);
  229. testInvert(test);
  230. testSetMul(test);
  231. testMul(test);
  232. testSetMulVector(test);
  233. testMulVector(test);
  234. testSetDiv(test);
  235. testDiv(test);
  236. testDot(test);
  237. testSquareLength(test);
  238. testLength(test);
  239. testNormalize(test);
  240. testCast(test);
  241. testToString(test);
  242. test.finalize();
  243. }