VectorTests.cpp 7.2 KB

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