VectorTests.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #include <cmath>
  2. #include "tests/VectorTests.h"
  3. #include "tests/Test.h"
  4. #include "utils/StringBuffer.h"
  5. #include "math/Vector.h"
  6. const float eps = 0.0001f;
  7. template<int N>
  8. static void compareVectors(Test& test, const Vector<N>& wanted, const Vector<N>& actual, const char* text) {
  9. for(int i = 0; i < N; i++) {
  10. test.checkFloat(wanted[i], actual[i], eps, StringBuffer<50>(text).append(" (").append(i).append(")"));
  11. }
  12. }
  13. static void testInitAndRead(Test& test) {
  14. Vector3 v1;
  15. Vector2 v2(1.0f, 2.0f);
  16. Vector3 v3(3.0f, 4.0f, 5.0f);
  17. Vector4 v4(6.0f, 7.0f, 8.0f, 9.0f);
  18. test.checkEqual(0.0f, v1[0], "init 1");
  19. test.checkEqual(0.0f, v1[0], "init 2");
  20. test.checkEqual(0.0f, v1[0], "init 3");
  21. test.checkEqual(1.0f, v2[0], "init 4");
  22. test.checkEqual(2.0f, v2[1], "init 5");
  23. test.checkEqual(3.0f, v3[0], "init 6");
  24. test.checkEqual(4.0f, v3[1], "init 7");
  25. test.checkEqual(5.0f, v3[2], "init 8");
  26. test.checkEqual(6.0f, v4[0], "init 9");
  27. test.checkEqual(7.0f, v4[1], "init 10");
  28. test.checkEqual(8.0f, v4[2], "init 11");
  29. test.checkEqual(9.0f, v4[3], "init 12");
  30. }
  31. static void testSetAngles(Test& test) {
  32. float root = sqrtf(2.0f) * 0.5f;
  33. compareVectors(test, Vector3(1.0f, 0.0f, 0.0f), Vector3().setAngles(0.0f, 0.0f), "angle 0 | 0");
  34. compareVectors(test, Vector3(root, 0.0f, -root), Vector3().setAngles(45.0f, 0.0f), "angle 45 | 0");
  35. compareVectors(test, Vector3(0.0f, 0.0f, -1.0f), Vector3().setAngles(90.0f, 0.0f), "angle 90 | 0");
  36. compareVectors(test, Vector3(-root, 0.0f, -root), Vector3().setAngles(135.0f, 0.0f), "angle 135 | 0");
  37. compareVectors(test, Vector3(-1.0f, 0.0f, 0.0f), Vector3().setAngles(180.0f, 0.0f), "angle 180 | 0");
  38. compareVectors(test, Vector3(-root, 0.0f, root), Vector3().setAngles(225.0f, 0.0f), "angle 225 | 0");
  39. compareVectors(test, Vector3(0.0f, 0.0f, 1.0f), Vector3().setAngles(270.0f, 0.0f), "angle 270 | 0");
  40. compareVectors(test, Vector3(root, 0.0f, root), Vector3().setAngles(315.0f, 0.0f), "angle 315 | 0");
  41. compareVectors(test, Vector3(0.0f, 1.0f, 0.0f), Vector3().setAngles(0.0f, 90.0f), "angle 0 | 90");
  42. compareVectors(test, Vector3(0.0f, 1.0f, 0.0f), Vector3().setAngles(90.0f, 90.0f), "angle 90 | 90");
  43. compareVectors(test, Vector3(0.0f, 1.0f, 0.0f), Vector3().setAngles(180.0f, 90.0f), "angle 180 | 90");
  44. compareVectors(test, Vector3(0.0f, 1.0f, 0.0f), Vector3().setAngles(270.0f, 90.0f), "angle 270 | 90");
  45. compareVectors(test, Vector3(0.0f, -1.0f, 0.0f), Vector3().setAngles(0.0f, -90.0f), "angle 0 | -90");
  46. compareVectors(test, Vector3(0.0f, -1.0f, 0.0f), Vector3().setAngles(90.0f, -90.0f), "angle 90 | -90");
  47. compareVectors(test, Vector3(0.0f, -1.0f, 0.0f), Vector3().setAngles(180.0f, -90.0f), "angle 180 | -90");
  48. compareVectors(test, Vector3(0.0f, -1.0f, 0.0f), Vector3().setAngles(270.0f, -90.0f), "angle 270 | -90");
  49. compareVectors(test, Vector3(root, root, 0.0f), Vector3().setAngles(0.0f, 45.0f), "angle 0 | 45");
  50. compareVectors(test, Vector3(0.0f, root, -root), Vector3().setAngles(90.0f, 45.0f), "angle 90 | 45");
  51. compareVectors(test, Vector3(-root, root, 0.0f), Vector3().setAngles(180.0f, 45.0f), "angle 180 | 45");
  52. compareVectors(test, Vector3(0.0f, root, root), Vector3().setAngles(270.0f, 45.0f), "angle 270 | 45");
  53. compareVectors(test, Vector3(root, -root, 0.0f), Vector3().setAngles(0.0f, -45.0f), "angle 0 | -45");
  54. compareVectors(test, Vector3(0.0f, -root, -root), Vector3().setAngles(90.0f, -45.0f), "angle 90 | -45");
  55. compareVectors(test, Vector3(-root, -root, 0.0f), Vector3().setAngles(180.0f, -45.0f), "angle 180 | -45");
  56. compareVectors(test, Vector3(0.0f, -root, root), Vector3().setAngles(270.0f, -45.0f), "angle 270 | -45");
  57. compareVectors(test, Vector3(0.5f, root, -0.5f), Vector3().setAngles(45.0f, 45.0f), "angle 45 | 45");
  58. }
  59. static void testCross(Test& test) {
  60. compareVectors(test, Vector3(0.0f, 0.0f, 1.0f),
  61. Vector3(1.0f, 0.0f, 0.0f).cross(Vector3(0.0f, 1.0f, 0.0f)), "cross 1");
  62. compareVectors(test, Vector3(0.0f, -1.0f, 0.0f),
  63. Vector3(1.0f, 0.0f, 0.0f).cross(Vector3(0.0f, 0.0f, 1.0f)), "cross 2");
  64. compareVectors(test, Vector3(0.0f, 0.0f, -1.0f),
  65. Vector3(0.0f, 1.0f, 0.0f).cross(Vector3(1.0f, 0.0f, 0.0f)), "cross 3");
  66. compareVectors(test, Vector3(1.0f, 0.0f, 0.0f),
  67. Vector3(0.0f, 1.0f, 0.0f).cross(Vector3(0.0f, 0.0f, 1.0f)), "cross 4");
  68. compareVectors(test, Vector3(0.0f, 1.0f, 0.0f),
  69. Vector3(0.0f, 0.0f, 1.0f).cross(Vector3(1.0f, 0.0f, 0.0f)), "cross 5");
  70. compareVectors(test, Vector3(-1.0f, 0.0f, 0.0f),
  71. Vector3(0.0f, 0.0f, 1.0f).cross(Vector3(0.0f, 1.0f, 0.0f)), "cross 6");
  72. }
  73. static void testSetAdd(Test& test) {
  74. Vector3 v;
  75. v += Vector3(1.0f, 2.0f, 3.0f);
  76. compareVectors(test, Vector3(1.0f, 2.0f, 3.0f), v, "set add 1");
  77. v += Vector3(2.0f, 3.0f, 4.0f);
  78. compareVectors(test, Vector3(3.0f, 5.0f, 7.0f), v, "set add 2");
  79. }
  80. static void testAdd(Test& test) {
  81. compareVectors(test, Vector3(1.0f, 2.0f, 3.0f), Vector3() + Vector3(1.0f, 2.0f, 3.0f), "add 1");
  82. compareVectors(test, Vector3(3.0f, 5.0f, 7.0f), Vector3(1.0f, 2.0f, 3.0f) + Vector3(2.0f, 3.0f, 4.0f), "add 2");
  83. }
  84. static void testSetSub(Test& test) {
  85. Vector3 v;
  86. v -= Vector3(1.0f, 2.0f, 3.0f);
  87. compareVectors(test, Vector3(-1.0f, -2.0f, -3.0f), v, "set sub 1");
  88. v -= Vector3(2.0f, 3.0f, 4.0f);
  89. compareVectors(test, Vector3(-3.0f, -5.0f, -7.0f), v, "set sub 2");
  90. }
  91. static void testSub(Test& test) {
  92. compareVectors(test, Vector3(1.0f, 2.0f, 3.0f), Vector3() - Vector3(-1.0f, -2.0f, -3.0f), "sub 1");
  93. compareVectors(test, Vector3(-1.0f, -1.0f, -1.0f), Vector3(1.0f, 2.0f, 3.0f) - Vector3(2.0f, 3.0f, 4.0f), "sub 2");
  94. }
  95. static void testInvert(Test& test) {
  96. compareVectors(test, Vector3(-1.0f, 2.0f, 3.0f), -Vector3(1.0f, -2.0f, -3.0f), "invert");
  97. }
  98. static void testSetMul(Test& test) {
  99. Vector3 v(1.0f, 2.0f, 3.0f);
  100. v *= 3.0f;
  101. compareVectors(test, Vector3(3.0f, 6.0f, 9.0f), v, "set mul 1");
  102. v *= -2.0f;
  103. compareVectors(test, Vector3(-6.0f, -12.0f, -18.0f), v, "set mul 2");
  104. }
  105. static void testMul(Test& test) {
  106. compareVectors(test, Vector3(-3.0f, -6.0f, -9.0f), 3.0f * Vector3(-1.0f, -2.0f, -3.0f), "mul 1");
  107. compareVectors(test, Vector3(3.0f, 6.0f, 9.0f), Vector3(1.0f, 2.0f, 3.0f) * 3.0, "mul 2");
  108. }
  109. static void testDot(Test& test) {
  110. test.checkFloat(9.0f, Vector3(-4.0f, 2.0f, -3.0f).dot(Vector3(-1.0f, -2.0f, -3.0f)), eps, "dot 1");
  111. test.checkFloat(-22.0f, Vector3(2.0f, 2.0f, -4.0f).dot(Vector3(1.0f, -2.0f, 5.0f)), eps, "dot 2");
  112. }
  113. static void testSquareLength(Test& test) {
  114. test.checkFloat(29.0f, Vector3(-4.0f, 2.0f, -3.0f).squareLength(), eps, "square length 1");
  115. test.checkFloat(24.0f, Vector3(2.0f, 2.0f, -4.0f).squareLength(), eps, "square length 2");
  116. }
  117. static void testLength(Test& test) {
  118. test.checkFloat(3.0f, Vector3(-2.0f, 2.0f, -1.0f).length(), eps, "length 1");
  119. test.checkFloat(7.0f, Vector3(6.0f, 2.0f, -3.0f).length(), eps, "length 2");
  120. }
  121. static void testNormalize(Test& test) {
  122. Vector3 v1(-2.0f, 2.0f, -1.0f);
  123. Vector3 v2 = v1 * (1.0f / 3.0f);
  124. v1.normalize();
  125. compareVectors(test, v2, v1, "normalize 1");
  126. Vector3 v3(6.0f, 2.0f, -3.0f);
  127. Vector3 v4 = v3 * (1.0f / 7.0f);
  128. v3.normalize();
  129. compareVectors(test, v4, v3, "normalize 2");
  130. }
  131. static void testToString(Test& test) {
  132. StringBuffer<50> s;
  133. s.append(Vector<1>()).append(Vector<2>(2.0f, 3.0f)).append(Vector<3>(4.0f, 5.0f, 6.0f));
  134. test.checkEqual(StringBuffer<50>("[0.00][2.00, 3.00][4.00, 5.00, 6.00]"), s, "to string");
  135. }
  136. void VectorTests::test() {
  137. Test test("Vector");
  138. testInitAndRead(test);
  139. testSetAngles(test);
  140. testCross(test);
  141. testSetAdd(test);
  142. testAdd(test);
  143. testSetSub(test);
  144. testSub(test);
  145. testInvert(test);
  146. testSetMul(test);
  147. testMul(test);
  148. testDot(test);
  149. testSquareLength(test);
  150. testLength(test);
  151. testNormalize(test);
  152. testToString(test);
  153. test.finalize();
  154. }