VectorTests.cpp 9.3 KB

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