VectorTests.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. #include <math.h>
  2. #include "../Tests.h"
  3. #include "core/Vector.h"
  4. const float eps = 0.0001f;
  5. #define V2 CoreVector2
  6. #define CV2(a, b, c, d) (&(V2){a, b})
  7. #define FV2(a, b) CV2(a, b, 0, 0)
  8. #define CV20 CV2(0, 0, 0, 0)
  9. #define V3 CoreVector3
  10. #define CV3(a, b, c, d) (&(V3){a, b, c})
  11. #define FV3(a, b, c) CV3(a, b, c, 0)
  12. #define CV30 CV3(0, 0, 0, 0)
  13. #define V4 CoreVector4
  14. #define CV4(a, b, c, d) (&(V4){a, b, c, d})
  15. #define CV40 CV4(0, 0, 0, 0)
  16. #define IV2 CoreIntVector2
  17. #define CIV2(a, b, c, d) (&(IV2){a, b})
  18. #define FIV2(a, b) CIV2(a, b, 0, 0)
  19. #define CIV20 CIV2(0, 0, 0, 0)
  20. #define IV3 CoreIntVector3
  21. #define CIV3(a, b, c, d) (&(IV3){a, b, c})
  22. #define FIV3(a, b, c) CIV3(a, b, c, 0)
  23. #define CIV30 CIV3(0, 0, 0, 0)
  24. #define IV4 CoreIntVector4
  25. #define CIV4(a, b, c, d) (&(IV4){a, b, c, d})
  26. #define CIV40 CIV4(0, 0, 0, 0)
  27. #define TESTS X(V2) X(V3) X(V4) X(IV2) X(IV3) X(IV4)
  28. static void testSetAngles() {
  29. float root = sqrtf(2) * 0.5f;
  30. CORE_TEST_V3(FV3(1, 0, 0), coreAngles(CV30, 0, 0));
  31. CORE_TEST_V3(FV3(root, 0, -root), coreAngles(CV30, 45, 0));
  32. CORE_TEST_V3(FV3(0, 0, -1), coreAngles(CV30, 90, 0));
  33. CORE_TEST_V3(FV3(-root, 0, -root), coreAngles(CV30, 135, 0));
  34. CORE_TEST_V3(FV3(-1, 0, 0), coreAngles(CV30, 180, 0));
  35. CORE_TEST_V3(FV3(-root, 0, root), coreAngles(CV30, 225, 0));
  36. CORE_TEST_V3(FV3(0, 0, 1), coreAngles(CV30, 270, 0));
  37. CORE_TEST_V3(FV3(root, 0, root), coreAngles(CV30, 315, 0));
  38. CORE_TEST_V3(FV3(0, 1, 0), coreAngles(CV30, 0, 90));
  39. CORE_TEST_V3(FV3(0, 1, 0), coreAngles(CV30, 90, 90));
  40. CORE_TEST_V3(FV3(0, 1, 0), coreAngles(CV30, 180, 90));
  41. CORE_TEST_V3(FV3(0, 1, 0), coreAngles(CV30, 270, 90));
  42. CORE_TEST_V3(FV3(0, -1, 0), coreAngles(CV30, 0, -90));
  43. CORE_TEST_V3(FV3(0, -1, 0), coreAngles(CV30, 90, -90));
  44. CORE_TEST_V3(FV3(0, -1, 0), coreAngles(CV30, 180, -90));
  45. CORE_TEST_V3(FV3(0, -1, 0), coreAngles(CV30, 270, -90));
  46. CORE_TEST_V3(FV3(root, root, 0), coreAngles(CV30, 0, 45));
  47. CORE_TEST_V3(FV3(0, root, -root), coreAngles(CV30, 90, 45));
  48. CORE_TEST_V3(FV3(-root, root, 0), coreAngles(CV30, 180, 45));
  49. CORE_TEST_V3(FV3(0, root, root), coreAngles(CV30, 270, 45));
  50. CORE_TEST_V3(FV3(root, -root, 0), coreAngles(CV30, 0, -45));
  51. CORE_TEST_V3(FV3(0, -root, -root), coreAngles(CV30, 90, -45));
  52. CORE_TEST_V3(FV3(-root, -root, 0), coreAngles(CV30, 180, -45));
  53. CORE_TEST_V3(FV3(0, -root, root), coreAngles(CV30, 270, -45));
  54. CORE_TEST_V3(FV3(0.5f, root, -0.5f), coreAngles(CV30, 45, 45));
  55. }
  56. static void testCross() {
  57. CORE_TEST_V3(FV3(0, 0, 1), coreCross(CV30, FV3(1, 0, 0), FV3(0, 1, 0)));
  58. CORE_TEST_V3(FV3(0, -1, 0), coreCross(CV30, FV3(1, 0, 0), FV3(0, 0, 1)));
  59. CORE_TEST_V3(FV3(0, 0, -1), coreCross(CV30, FV3(0, 1, 0), FV3(1, 0, 0)));
  60. CORE_TEST_V3(FV3(1, 0, 0), coreCross(CV30, FV3(0, 1, 0), FV3(0, 0, 1)));
  61. CORE_TEST_V3(FV3(0, 1, 0), coreCross(CV30, FV3(0, 0, 1), FV3(1, 0, 0)));
  62. CORE_TEST_V3(FV3(-1, 0, 0), coreCross(CV30, FV3(0, 0, 1), FV3(0, 1, 0)));
  63. }
  64. static void testSetAdd() {
  65. #define X(T) \
  66. { \
  67. T v = {0}; \
  68. coreAddSet##T(&v, C##T(1, 2, 3, 4)); \
  69. CORE_TEST_##T(C##T(1, 2, 3, 4), &v); \
  70. coreAddSet##T(&v, C##T(2, 3, 4, 5)); \
  71. CORE_TEST_##T(C##T(3, 5, 7, 9), &v); \
  72. }
  73. TESTS
  74. #undef X
  75. }
  76. static void testAdd() {
  77. CORE_TEST_V3(FV3(1, 2, 3), coreAddV3(CV30, CV30, FV3(1, 2, 3)));
  78. CORE_TEST_V3(FV3(3, 5, 7), coreAddV3(CV30, FV3(1, 2, 3), FV3(2, 3, 4)));
  79. }
  80. static void testSetSub() {
  81. #define X(T) \
  82. { \
  83. T v = {0}; \
  84. coreSubSet##T(&v, C##T(1, 2, 3, 4)); \
  85. CORE_TEST_##T(C##T(-1, -2, -3, -4), &v); \
  86. coreSubSet##T(&v, C##T(2, 3, 4, 5)); \
  87. CORE_TEST_##T(C##T(-3, -5, -7, -9), &v); \
  88. }
  89. TESTS
  90. #undef X
  91. }
  92. static void testSub() {
  93. CORE_TEST_V3(FV3(1, 2, 3), coreSubV3(CV30, CV30, FV3(-1, -2, -3)));
  94. CORE_TEST_V3(FV3(-1, -1, -1), coreSubV3(CV30, FV3(1, 2, 3), FV3(2, 3, 4)));
  95. }
  96. static void testSetMul() {
  97. #define X(T) \
  98. { \
  99. T v = *C##T(1, 2, 3, 4); \
  100. coreMulSet##T##F(&v, 3); \
  101. CORE_TEST_##T(C##T(3, 6, 9, 12), &v); \
  102. coreMulSet##T##F(&v, -2); \
  103. CORE_TEST_##T(C##T(-6, -12, -18, -24), &v); \
  104. }
  105. TESTS
  106. #undef X
  107. }
  108. static void testMul() {
  109. CORE_TEST_V3(FV3(3, 6, 9), coreMulV3F(CV30, FV3(1, 2, 3), 3));
  110. }
  111. static void testSetMulVector() {
  112. #define X(T) \
  113. { \
  114. T v = *C##T(1, 2, 3, 4); \
  115. coreMulSet##T(&v, C##T(2, 1, 3, 4)); \
  116. CORE_TEST_##T(C##T(2, 2, 9, 16), &v); \
  117. coreMulSet##T(&v, C##T(-3, 4, -2, -2)); \
  118. CORE_TEST_##T(C##T(-6, 8, -18, -32), &v); \
  119. }
  120. TESTS
  121. #undef X
  122. }
  123. static void testMulVector() {
  124. CORE_TEST_V3(FV3(-2, -2, -9),
  125. coreMulV3(CV30, FV3(2, 1, 3), FV3(-1, -2, -3)));
  126. CORE_TEST_V3(FV3(2, 2, 9), coreMulV3(CV30, FV3(1, 2, 3), FV3(2, 1, 3)));
  127. }
  128. static void testSetDiv() {
  129. #define X(T) \
  130. { \
  131. T v = *C##T(18, 36, 9, 27); \
  132. coreDivSet##T##F(&v, 3); \
  133. CORE_TEST_##T(C##T(6, 12, 3, 9), &v); \
  134. coreDivSet##T##F(&v, -3); \
  135. CORE_TEST_##T(C##T(-2, -4, -1, -3), &v); \
  136. }
  137. TESTS
  138. #undef X
  139. }
  140. static void testDiv() {
  141. CORE_TEST_V3(FV3(-1, -2, -3), coreDivV3F(CV30, FV3(-3, -6, -9), 3));
  142. }
  143. static void testSetDivVector() {
  144. #define X(T) \
  145. { \
  146. T v = *C##T(12, 4, 6, 8); \
  147. coreDivSet##T(&v, C##T(2, 1, 3, 4)); \
  148. CORE_TEST_##T(C##T(6, 4, 2, 2), &v); \
  149. coreDivSet##T(&v, C##T(-3, 4, -2, -1)); \
  150. CORE_TEST_##T(C##T(-2, 1, -1, -2), &v); \
  151. }
  152. TESTS
  153. #undef X
  154. }
  155. static void testDivVector() {
  156. CORE_TEST_V3(FV3(-2, -0.5f, -1),
  157. coreDivV3(CV30, FV3(2, 1, 3), FV3(-1, -2, -3)));
  158. CORE_TEST_V3(FV3(0.5f, 2, 1), coreDivV3(CV30, FV3(1, 2, 3), FV3(2, 1, 3)));
  159. }
  160. static void testSetInvert() {
  161. CORE_TEST_V2(FV2(-1, 2), coreInvertSetV2(FV2(1, -2)));
  162. CORE_TEST_V3(FV3(-1, 2, 3), coreInvertSetV3(FV3(1, -2, -3)));
  163. CORE_TEST_V4(CV4(-1, 2, 3, 4), coreInvertSetV4(CV4(1, -2, -3, -4)));
  164. CORE_TEST_IV2(FIV2(-1, 2), coreInvertSetIV2(FIV2(1, -2)));
  165. CORE_TEST_IV3(FIV3(-1, 2, 3), coreInvertSetIV3(FIV3(1, -2, -3)));
  166. CORE_TEST_IV4(CIV4(-1, 2, 3, 4), coreInvertSetIV4(CIV4(1, -2, -3, -4)));
  167. }
  168. static void testInvert() {
  169. CORE_TEST_V3(FV3(-1, 2, 3), coreInvertV3(CV30, FV3(1, -2, -3)));
  170. }
  171. static void testDot() {
  172. CORE_TEST_FLOAT(0, coreDotV2(FV2(-4, 2), FV2(-1, -2)), eps);
  173. CORE_TEST_FLOAT(9, coreDotV3(FV3(-4, 2, -3), FV3(-1, -2, -3)), eps);
  174. CORE_TEST_FLOAT(16, coreDotV4(CV4(-4, 2, -3, 1), CV4(-1, -2, -3, 7)), eps);
  175. }
  176. static void testSquareLength() {
  177. CORE_TEST_FLOAT(20, coreSquareLengthV2(FV2(-4, 2)), eps);
  178. CORE_TEST_FLOAT(29, coreSquareLengthV3(FV3(-4, 2, -3)), eps);
  179. CORE_TEST_FLOAT(54, coreSquareLengthV4(CV4(-4, 2, -3, 5)), eps);
  180. }
  181. static void testLength() {
  182. CORE_TEST_FLOAT(5, coreLengthV2(FV2(-3, 4)), eps);
  183. CORE_TEST_FLOAT(13, coreLengthV2(FV2(5, 12)), eps);
  184. CORE_TEST_FLOAT(3, coreLengthV3(FV3(-2, 2, -1)), eps);
  185. CORE_TEST_FLOAT(7, coreLengthV3(FV3(6, 2, -3)), eps);
  186. CORE_TEST_FLOAT(3, coreLengthV4(CV4(-2, 2, 0, -1)), eps);
  187. CORE_TEST_FLOAT(9, coreLengthV4(CV4(6, 0, -6, 3)), eps);
  188. }
  189. static void testNormalize() {
  190. {
  191. V2 v1 = {-15, 20};
  192. V2 v2;
  193. coreMulV2F(&v2, &v1, 1.0f / 25.0f);
  194. coreNormalizeV2(&v1);
  195. CORE_TEST_V2(&v2, &v1);
  196. V2 v3 = {15, 36};
  197. V2 v4;
  198. coreMulV2F(&v4, &v3, 1.0f / 39.0f);
  199. coreNormalizeV2(&v3);
  200. CORE_TEST_V2(&v4, &v3);
  201. }
  202. {
  203. V3 v1 = {-2, 2, -1};
  204. V3 v2;
  205. coreMulV3F(&v2, &v1, 1.0f / 3.0f);
  206. coreNormalizeV3(&v1);
  207. CORE_TEST_V3(&v2, &v1);
  208. V3 v3 = {6, 2, -3};
  209. V3 v4;
  210. coreMulV3F(&v4, &v3, 1.0f / 7.0f);
  211. coreNormalizeV3(&v3);
  212. CORE_TEST_V3(&v4, &v3);
  213. }
  214. {
  215. V4 v1 = {-2, 2, 0, -1};
  216. V4 v2;
  217. coreMulV4F(&v2, &v1, 1.0f / 3.0f);
  218. coreNormalizeV4(&v1);
  219. CORE_TEST_V4(&v2, &v1);
  220. V4 v3 = {6, 0, -6, 3};
  221. V4 v4;
  222. coreMulV4F(&v4, &v3, 1.0f / 9.0f);
  223. coreNormalizeV4(&v3);
  224. CORE_TEST_V4(&v4, &v3);
  225. }
  226. }
  227. static void testCast() {
  228. CORE_TEST_V2(FV2(-2.0f, 2.0f), coreConvertIV2(CV20, FIV2(-2, 2)));
  229. CORE_TEST_IV2(FIV2(-2, 2), coreConvertV2(CIV20, FV2(-2.5f, 2.6f)));
  230. CORE_TEST_V3(FV3(-2.0f, 2.0f, 9.0f), coreConvertIV3(CV30, FIV3(-2, 2, 9)));
  231. CORE_TEST_IV3(FIV3(-2, 2, 9), coreConvertV3(CIV30, FV3(-2.5f, 2.6f, 9.0f)));
  232. CORE_TEST_V4(CV4(-2.0f, 2.0f, 9.0f, 6.0f),
  233. coreConvertIV4(CV40, CIV4(-2, 2, 9, 6)));
  234. CORE_TEST_IV4(CIV4(-2, 2, 9, 3),
  235. coreConvertV4(CIV40, CV4(-2.5f, 2.6f, 9.0f, 3.2f)));
  236. }
  237. static void testToString() {
  238. char buffer[64];
  239. coreToStringV2(FV2(4, 5), buffer, sizeof(buffer));
  240. CORE_TEST_STRING("[4.000, 5.000]", buffer);
  241. coreToStringV3(FV3(4, 5, 6), buffer, sizeof(buffer));
  242. CORE_TEST_STRING("[4.000, 5.000, 6.000]", buffer);
  243. coreToStringV4(CV4(4, 5, 6, 7), buffer, sizeof(buffer));
  244. CORE_TEST_STRING("[4.000, 5.000, 6.000, 7.000]", buffer);
  245. coreToStringIV2(FIV2(4, 5), buffer, sizeof(buffer));
  246. CORE_TEST_STRING("[4, 5]", buffer);
  247. coreToStringIV3(FIV3(4, 5, 6), buffer, sizeof(buffer));
  248. CORE_TEST_STRING("[4, 5, 6]", buffer);
  249. coreToStringIV4(CIV4(4, 5, 6, 7), buffer, sizeof(buffer));
  250. CORE_TEST_STRING("[4, 5, 6, 7]", buffer);
  251. }
  252. void coreTestVector() {
  253. testSetAngles();
  254. testCross();
  255. testSetAdd();
  256. testAdd();
  257. testSetSub();
  258. testSub();
  259. testSetMul();
  260. testMul();
  261. testSetMulVector();
  262. testMulVector();
  263. testSetDiv();
  264. testDiv();
  265. testSetDivVector();
  266. testDivVector();
  267. testSetInvert();
  268. testInvert();
  269. testDot();
  270. testSquareLength();
  271. testLength();
  272. testNormalize();
  273. testCast();
  274. testToString();
  275. }