VectorTests.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 testDot(Test& test) {
  153. test.checkFloat(
  154. 9.0f, Vector3(-4.0f, 2.0f, -3.0f).dot(Vector3(-1.0f, -2.0f, -3.0f)),
  155. eps, "dot 1");
  156. test.checkFloat(-22.0f,
  157. Vector3(2.0f, 2.0f, -4.0f).dot(Vector3(1.0f, -2.0f, 5.0f)),
  158. eps, "dot 2");
  159. }
  160. static void testSquareLength(Test& test) {
  161. test.checkFloat(29.0f, Vector3(-4.0f, 2.0f, -3.0f).squareLength(), eps,
  162. "square length 1");
  163. test.checkFloat(24.0f, Vector3(2.0f, 2.0f, -4.0f).squareLength(), eps,
  164. "square length 2");
  165. }
  166. static void testLength(Test& test) {
  167. test.checkFloat(3.0f, Vector3(-2.0f, 2.0f, -1.0f).length(), eps,
  168. "length 1");
  169. test.checkFloat(7.0f, Vector3(6.0f, 2.0f, -3.0f).length(), eps, "length 2");
  170. }
  171. static void testNormalize(Test& test) {
  172. Vector3 v1(-2.0f, 2.0f, -1.0f);
  173. Vector3 v2 = v1 * (1.0f / 3.0f);
  174. v1.normalize();
  175. compareVectors(test, v2, v1, "normalize 1");
  176. Vector3 v3(6.0f, 2.0f, -3.0f);
  177. Vector3 v4 = v3 * (1.0f / 7.0f);
  178. v3.normalize();
  179. compareVectors(test, v4, v3, "normalize 2");
  180. }
  181. static void testToString(Test& test) {
  182. StringBuffer<50> s;
  183. s.append(Vector<1>())
  184. .append(Vector<2>(2.0f, 3.0f))
  185. .append(Vector<3>(4.0f, 5.0f, 6.0f));
  186. test.checkEqual(StringBuffer<50>("[0.00][2.00, 3.00][4.00, 5.00, 6.00]"), s,
  187. "to string");
  188. }
  189. void VectorTests::test() {
  190. Test test("Vector");
  191. testInitAndRead(test);
  192. testSetAngles(test);
  193. testCross(test);
  194. testSetAdd(test);
  195. testAdd(test);
  196. testSetSub(test);
  197. testSub(test);
  198. testInvert(test);
  199. testSetMul(test);
  200. testMul(test);
  201. testDot(test);
  202. testSquareLength(test);
  203. testLength(test);
  204. testNormalize(test);
  205. testToString(test);
  206. test.finalize();
  207. }