VectorTests.cpp 7.1 KB

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