VectorTests.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. static void testV(const char* file, int line, const float* wanted,
  28. const float* actual, size_t n) {
  29. for(size_t i = 0; i < n; i++) {
  30. coreTestFloat(file, line, wanted[i], actual[i], eps);
  31. }
  32. }
  33. #define testV2(wanted, actual) \
  34. testV(__FILE__, __LINE__, (wanted)->data, (actual)->data, 2)
  35. #define testV3(wanted, actual) \
  36. testV(__FILE__, __LINE__, (wanted)->data, (actual)->data, 3)
  37. #define testV4(wanted, actual) \
  38. testV(__FILE__, __LINE__, (wanted)->data, (actual)->data, 4)
  39. static void testIV(const char* file, int line, const int* wanted,
  40. const int* actual, size_t n) {
  41. for(size_t i = 0; i < n; i++) {
  42. coreTestInt(file, line, wanted[i], actual[i]);
  43. }
  44. }
  45. #define testIV2(wanted, actual) \
  46. testIV(__FILE__, __LINE__, (wanted)->data, (actual)->data, 2)
  47. #define testIV3(wanted, actual) \
  48. testIV(__FILE__, __LINE__, (wanted)->data, (actual)->data, 3)
  49. #define testIV4(wanted, actual) \
  50. testIV(__FILE__, __LINE__, (wanted)->data, (actual)->data, 4)
  51. static void testSetAngles() {
  52. float root = sqrtf(2) * 0.5f;
  53. testV3(FV3(1, 0, 0), coreAngles(CV30, 0, 0));
  54. testV3(FV3(root, 0, -root), coreAngles(CV30, 45, 0));
  55. testV3(FV3(0, 0, -1), coreAngles(CV30, 90, 0));
  56. testV3(FV3(-root, 0, -root), coreAngles(CV30, 135, 0));
  57. testV3(FV3(-1, 0, 0), coreAngles(CV30, 180, 0));
  58. testV3(FV3(-root, 0, root), coreAngles(CV30, 225, 0));
  59. testV3(FV3(0, 0, 1), coreAngles(CV30, 270, 0));
  60. testV3(FV3(root, 0, root), coreAngles(CV30, 315, 0));
  61. testV3(FV3(0, 1, 0), coreAngles(CV30, 0, 90));
  62. testV3(FV3(0, 1, 0), coreAngles(CV30, 90, 90));
  63. testV3(FV3(0, 1, 0), coreAngles(CV30, 180, 90));
  64. testV3(FV3(0, 1, 0), coreAngles(CV30, 270, 90));
  65. testV3(FV3(0, -1, 0), coreAngles(CV30, 0, -90));
  66. testV3(FV3(0, -1, 0), coreAngles(CV30, 90, -90));
  67. testV3(FV3(0, -1, 0), coreAngles(CV30, 180, -90));
  68. testV3(FV3(0, -1, 0), coreAngles(CV30, 270, -90));
  69. testV3(FV3(root, root, 0), coreAngles(CV30, 0, 45));
  70. testV3(FV3(0, root, -root), coreAngles(CV30, 90, 45));
  71. testV3(FV3(-root, root, 0), coreAngles(CV30, 180, 45));
  72. testV3(FV3(0, root, root), coreAngles(CV30, 270, 45));
  73. testV3(FV3(root, -root, 0), coreAngles(CV30, 0, -45));
  74. testV3(FV3(0, -root, -root), coreAngles(CV30, 90, -45));
  75. testV3(FV3(-root, -root, 0), coreAngles(CV30, 180, -45));
  76. testV3(FV3(0, -root, root), coreAngles(CV30, 270, -45));
  77. testV3(FV3(0.5f, root, -0.5f), coreAngles(CV30, 45, 45));
  78. }
  79. static void testCross() {
  80. testV3(FV3(0, 0, 1), coreCross(CV30, FV3(1, 0, 0), FV3(0, 1, 0)));
  81. testV3(FV3(0, -1, 0), coreCross(CV30, FV3(1, 0, 0), FV3(0, 0, 1)));
  82. testV3(FV3(0, 0, -1), coreCross(CV30, FV3(0, 1, 0), FV3(1, 0, 0)));
  83. testV3(FV3(1, 0, 0), coreCross(CV30, FV3(0, 1, 0), FV3(0, 0, 1)));
  84. testV3(FV3(0, 1, 0), coreCross(CV30, FV3(0, 0, 1), FV3(1, 0, 0)));
  85. testV3(FV3(-1, 0, 0), coreCross(CV30, FV3(0, 0, 1), FV3(0, 1, 0)));
  86. }
  87. #define TEST_SET_ADD(T) \
  88. { \
  89. T v = {0}; \
  90. coreAddSet##T(&v, C##T(1, 2, 3, 4)); \
  91. test##T(C##T(1, 2, 3, 4), &v); \
  92. coreAddSet##T(&v, C##T(2, 3, 4, 5)); \
  93. test##T(C##T(3, 5, 7, 9), &v); \
  94. }
  95. static void testSetAdd() {
  96. TEST_SET_ADD(V2)
  97. TEST_SET_ADD(V3)
  98. TEST_SET_ADD(V4)
  99. }
  100. static void testAdd() {
  101. testV3(FV3(1, 2, 3), coreAddV3(CV30, CV30, FV3(1, 2, 3)));
  102. testV3(FV3(3, 5, 7), coreAddV3(CV30, FV3(1, 2, 3), FV3(2, 3, 4)));
  103. }
  104. #define TEST_SET_SUB(T) \
  105. { \
  106. T v = {0}; \
  107. coreSubSet##T(&v, C##T(1, 2, 3, 4)); \
  108. test##T(C##T(-1, -2, -3, -4), &v); \
  109. coreSubSet##T(&v, C##T(2, 3, 4, 5)); \
  110. test##T(C##T(-3, -5, -7, -9), &v); \
  111. }
  112. static void testSetSub() {
  113. TEST_SET_SUB(V2)
  114. TEST_SET_SUB(V3)
  115. TEST_SET_SUB(V4)
  116. }
  117. static void testSub() {
  118. testV3(FV3(1, 2, 3), coreSubV3(CV30, CV30, FV3(-1, -2, -3)));
  119. testV3(FV3(-1, -1, -1), coreSubV3(CV30, FV3(1, 2, 3), FV3(2, 3, 4)));
  120. }
  121. #define TEST_SET_MUL(T) \
  122. { \
  123. T v = *C##T(1, 2, 3, 4); \
  124. coreMulSet##T##F(&v, 3); \
  125. test##T(C##T(3, 6, 9, 12), &v); \
  126. coreMulSet##T##F(&v, -2); \
  127. test##T(C##T(-6, -12, -18, -24), &v); \
  128. }
  129. static void testSetMul() {
  130. TEST_SET_MUL(V2)
  131. TEST_SET_MUL(V3)
  132. TEST_SET_MUL(V4)
  133. }
  134. static void testMul() {
  135. testV3(FV3(3, 6, 9), coreMulV3F(CV30, FV3(1, 2, 3), 3));
  136. }
  137. #define TEST_SET_MUL_VECTOR(T) \
  138. { \
  139. T v = *C##T(1, 2, 3, 4); \
  140. coreMulSet##T(&v, C##T(2, 1, 3, 4)); \
  141. test##T(C##T(2, 2, 9, 16), &v); \
  142. coreMulSet##T(&v, C##T(-3, 4, -2, -2)); \
  143. test##T(C##T(-6, 8, -18, -32), &v); \
  144. }
  145. static void testSetMulVector() {
  146. TEST_SET_MUL_VECTOR(V2)
  147. TEST_SET_MUL_VECTOR(V3)
  148. TEST_SET_MUL_VECTOR(V4)
  149. }
  150. static void testMulVector() {
  151. testV3(FV3(-2, -2, -9), coreMulV3(CV30, FV3(2, 1, 3), FV3(-1, -2, -3)));
  152. testV3(FV3(2, 2, 9), coreMulV3(CV30, FV3(1, 2, 3), FV3(2, 1, 3)));
  153. }
  154. #define TEST_SET_DIV(T) \
  155. { \
  156. T v = *C##T(12, 24, 9, 27); \
  157. coreDivSet##T##F(&v, 3); \
  158. test##T(C##T(4, 8, 3, 9), &v); \
  159. coreDivSet##T##F(&v, -2); \
  160. test##T(C##T(-2, -4, -1.5f, -4.5f), &v); \
  161. }
  162. static void testSetDiv() {
  163. TEST_SET_DIV(V2);
  164. TEST_SET_DIV(V3);
  165. TEST_SET_DIV(V4);
  166. }
  167. static void testDiv() {
  168. testV3(FV3(-1, -2, -3), coreDivV3F(CV30, FV3(-3, -6, -9), 3));
  169. }
  170. #define TEST_SET_DIV_VECTOR(T) \
  171. { \
  172. T v = *C##T(3, 4, 6, 8); \
  173. coreDivSet##T(&v, C##T(2, 1, 3, 4)); \
  174. test##T(C##T(1.5f, 4, 2, 2), &v); \
  175. coreDivSet##T(&v, C##T(-3, 4, -2, -1)); \
  176. test##T(C##T(-0.5f, 1, -1, -2), &v); \
  177. }
  178. static void testSetDivVector() {
  179. TEST_SET_DIV_VECTOR(V2)
  180. TEST_SET_DIV_VECTOR(V3)
  181. TEST_SET_DIV_VECTOR(V4)
  182. }
  183. static void testDivVector() {
  184. testV3(FV3(-2, -0.5f, -1), coreDivV3(CV30, FV3(2, 1, 3), FV3(-1, -2, -3)));
  185. testV3(FV3(0.5f, 2, 1), coreDivV3(CV30, FV3(1, 2, 3), FV3(2, 1, 3)));
  186. }
  187. static void testSetInvert() {
  188. testV2(FV2(-1, 2), coreInvertSetV2(FV2(1, -2)));
  189. testV3(FV3(-1, 2, 3), coreInvertSetV3(FV3(1, -2, -3)));
  190. testV4(CV4(-1, 2, 3, 4), coreInvertSetV4(CV4(1, -2, -3, -4)));
  191. }
  192. static void testInvert() {
  193. testV3(FV3(-1, 2, 3), coreInvertV3(CV30, FV3(1, -2, -3)));
  194. }
  195. static void testDot() {
  196. CORE_TEST_FLOAT(0, coreDotV2(FV2(-4, 2), FV2(-1, -2)), eps);
  197. CORE_TEST_FLOAT(9, coreDotV3(FV3(-4, 2, -3), FV3(-1, -2, -3)), eps);
  198. CORE_TEST_FLOAT(16, coreDotV4(CV4(-4, 2, -3, 1), CV4(-1, -2, -3, 7)), eps);
  199. }
  200. static void testSquareLength() {
  201. CORE_TEST_FLOAT(20, coreSquareLengthV2(FV2(-4, 2)), eps);
  202. CORE_TEST_FLOAT(29, coreSquareLengthV3(FV3(-4, 2, -3)), eps);
  203. CORE_TEST_FLOAT(54, coreSquareLengthV4(CV4(-4, 2, -3, 5)), eps);
  204. }
  205. static void testLength() {
  206. CORE_TEST_FLOAT(5, coreLengthV2(FV2(-3, 4)), eps);
  207. CORE_TEST_FLOAT(13, coreLengthV2(FV2(5, 12)), eps);
  208. CORE_TEST_FLOAT(3, coreLengthV3(FV3(-2, 2, -1)), eps);
  209. CORE_TEST_FLOAT(7, coreLengthV3(FV3(6, 2, -3)), eps);
  210. CORE_TEST_FLOAT(3, coreLengthV4(CV4(-2, 2, 0, -1)), eps);
  211. CORE_TEST_FLOAT(9, coreLengthV4(CV4(6, 0, -6, 3)), eps);
  212. }
  213. static void testNormalize() {
  214. {
  215. V2 v1 = {-15, 20};
  216. V2 v2;
  217. coreMulV2F(&v2, &v1, 1.0f / 25.0f);
  218. coreNormalizeV2(&v1);
  219. testV2(&v2, &v1);
  220. V2 v3 = {15, 36};
  221. V2 v4;
  222. coreMulV2F(&v4, &v3, 1.0f / 39.0f);
  223. coreNormalizeV2(&v3);
  224. testV2(&v4, &v3);
  225. }
  226. {
  227. V3 v1 = {-2, 2, -1};
  228. V3 v2;
  229. coreMulV3F(&v2, &v1, 1.0f / 3.0f);
  230. coreNormalizeV3(&v1);
  231. testV3(&v2, &v1);
  232. V3 v3 = {6, 2, -3};
  233. V3 v4;
  234. coreMulV3F(&v4, &v3, 1.0f / 7.0f);
  235. coreNormalizeV3(&v3);
  236. testV3(&v4, &v3);
  237. }
  238. {
  239. V4 v1 = {-2, 2, 0, -1};
  240. V4 v2;
  241. coreMulV4F(&v2, &v1, 1.0f / 3.0f);
  242. coreNormalizeV4(&v1);
  243. testV4(&v2, &v1);
  244. V4 v3 = {6, 0, -6, 3};
  245. V4 v4;
  246. coreMulV4F(&v4, &v3, 1.0f / 9.0f);
  247. coreNormalizeV4(&v3);
  248. testV4(&v4, &v3);
  249. }
  250. }
  251. static void testCast() {
  252. testV2(FV2(-2.0f, 2.0f), coreConvertIV2(CV20, FIV2(-2, 2)));
  253. testIV2(FIV2(-2, 2), coreConvertV2(CIV20, FV2(-2.5f, 2.6f)));
  254. testV3(FV3(-2.0f, 2.0f, 9.0f), coreConvertIV3(CV30, FIV3(-2, 2, 9)));
  255. testIV3(FIV3(-2, 2, 9), coreConvertV3(CIV30, FV3(-2.5f, 2.6f, 9.0f)));
  256. testV4(CV4(-2.0f, 2.0f, 9.0f, 6.0f),
  257. coreConvertIV4(CV40, CIV4(-2, 2, 9, 6)));
  258. testIV4(CIV4(-2, 2, 9, 3),
  259. coreConvertV4(CIV40, CV4(-2.5f, 2.6f, 9.0f, 3.2f)));
  260. }
  261. static void testToString() {
  262. char buffer[64];
  263. coreToStringV2(FV2(4, 5), buffer, sizeof(buffer));
  264. CORE_TEST_STRING("[4.000, 5.000]", buffer);
  265. coreToStringV3(FV3(4, 5, 6), buffer, sizeof(buffer));
  266. CORE_TEST_STRING("[4.000, 5.000, 6.000]", buffer);
  267. coreToStringV4(CV4(4, 5, 6, 7), buffer, sizeof(buffer));
  268. CORE_TEST_STRING("[4.000, 5.000, 6.000, 7.000]", buffer);
  269. }
  270. void coreTestVector() {
  271. testSetAngles();
  272. testCross();
  273. testSetAdd();
  274. testAdd();
  275. testSetSub();
  276. testSub();
  277. testSetMul();
  278. testMul();
  279. testSetMulVector();
  280. testMulVector();
  281. testSetDiv();
  282. testDiv();
  283. testSetDivVector();
  284. testDivVector();
  285. testSetInvert();
  286. testInvert();
  287. testDot();
  288. testSquareLength();
  289. testLength();
  290. testNormalize();
  291. testCast();
  292. testToString();
  293. }