VectorTests.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #include <math.h>
  2. #include "../Tests.h"
  3. #include "core/Vector.h"
  4. const float eps = 0.0001f;
  5. #define V3 CoreVector3
  6. #define CV3(a, b, c) \
  7. &(V3) { \
  8. a, b, c \
  9. }
  10. #define CV30 CV3(0, 0, 0)
  11. #define IV3 CoreIntVector3
  12. #define CIV3(a, b, c) \
  13. &(IV3) { \
  14. a, b, c \
  15. }
  16. #define CIV30 CIV3(0, 0, 0)
  17. static void testVector3B(const char* file, int line, const V3* wanted,
  18. const V3* actual) {
  19. for(int i = 0; i < 3; i++) {
  20. coreTestFloat(file, line, wanted->data[i], actual->data[i], eps);
  21. }
  22. }
  23. #define testVector3(wanted, actual) \
  24. testVector3B(__FILE__, __LINE__, wanted, actual)
  25. static void testIntVector3(const IV3* wanted, const IV3* actual) {
  26. for(int i = 0; i < 3; i++) {
  27. CORE_TEST_INT(wanted->data[i], actual->data[i]);
  28. }
  29. }
  30. static void testSetAngles() {
  31. float root = sqrtf(2) * 0.5f;
  32. testVector3(CV3(1, 0, 0), coreAngles(CV30, 0, 0));
  33. testVector3(CV3(root, 0, -root), coreAngles(CV30, 45, 0));
  34. testVector3(CV3(0, 0, -1), coreAngles(CV30, 90, 0));
  35. testVector3(CV3(-root, 0, -root), coreAngles(CV30, 135, 0));
  36. testVector3(CV3(-1, 0, 0), coreAngles(CV30, 180, 0));
  37. testVector3(CV3(-root, 0, root), coreAngles(CV30, 225, 0));
  38. testVector3(CV3(0, 0, 1), coreAngles(CV30, 270, 0));
  39. testVector3(CV3(root, 0, root), coreAngles(CV30, 315, 0));
  40. testVector3(CV3(0, 1, 0), coreAngles(CV30, 0, 90));
  41. testVector3(CV3(0, 1, 0), coreAngles(CV30, 90, 90));
  42. testVector3(CV3(0, 1, 0), coreAngles(CV30, 180, 90));
  43. testVector3(CV3(0, 1, 0), coreAngles(CV30, 270, 90));
  44. testVector3(CV3(0, -1, 0), coreAngles(CV30, 0, -90));
  45. testVector3(CV3(0, -1, 0), coreAngles(CV30, 90, -90));
  46. testVector3(CV3(0, -1, 0), coreAngles(CV30, 180, -90));
  47. testVector3(CV3(0, -1, 0), coreAngles(CV30, 270, -90));
  48. testVector3(CV3(root, root, 0), coreAngles(CV30, 0, 45));
  49. testVector3(CV3(0, root, -root), coreAngles(CV30, 90, 45));
  50. testVector3(CV3(-root, root, 0), coreAngles(CV30, 180, 45));
  51. testVector3(CV3(0, root, root), coreAngles(CV30, 270, 45));
  52. testVector3(CV3(root, -root, 0), coreAngles(CV30, 0, -45));
  53. testVector3(CV3(0, -root, -root), coreAngles(CV30, 90, -45));
  54. testVector3(CV3(-root, -root, 0), coreAngles(CV30, 180, -45));
  55. testVector3(CV3(0, -root, root), coreAngles(CV30, 270, -45));
  56. testVector3(CV3(0.5f, root, -0.5f), coreAngles(CV30, 45, 45));
  57. }
  58. static void testCross() {
  59. testVector3(CV3(0, 0, 1), coreCross(CV30, CV3(1, 0, 0), CV3(0, 1, 0)));
  60. testVector3(CV3(0, -1, 0), coreCross(CV30, CV3(1, 0, 0), CV3(0, 0, 1)));
  61. testVector3(CV3(0, 0, -1), coreCross(CV30, CV3(0, 1, 0), CV3(1, 0, 0)));
  62. testVector3(CV3(1, 0, 0), coreCross(CV30, CV3(0, 1, 0), CV3(0, 0, 1)));
  63. testVector3(CV3(0, 1, 0), coreCross(CV30, CV3(0, 0, 1), CV3(1, 0, 0)));
  64. testVector3(CV3(-1, 0, 0), coreCross(CV30, CV3(0, 0, 1), CV3(0, 1, 0)));
  65. }
  66. static void testSetAdd() {
  67. V3 v = {0};
  68. coreAddSetV3(&v, CV3(1, 2, 3));
  69. testVector3(CV3(1, 2, 3), &v);
  70. coreAddSetV3(&v, CV3(2, 3, 4));
  71. testVector3(CV3(3, 5, 7), &v);
  72. }
  73. static void testAdd() {
  74. testVector3(CV3(1, 2, 3), coreAddV3(CV30, CV30, CV3(1, 2, 3)));
  75. testVector3(CV3(3, 5, 7), coreAddV3(CV30, CV3(1, 2, 3), CV3(2, 3, 4)));
  76. }
  77. static void testSetSub() {
  78. V3 v = {0};
  79. coreSubSetV3(&v, CV3(1, 2, 3));
  80. testVector3(CV3(-1, -2, -3), &v);
  81. coreSubSetV3(&v, CV3(2, 3, 4));
  82. testVector3(CV3(-3, -5, -7), &v);
  83. }
  84. static void testSub() {
  85. testVector3(CV3(1, 2, 3), coreSubV3(CV30, CV30, CV3(-1, -2, -3)));
  86. testVector3(CV3(-1, -1, -1), coreSubV3(CV30, CV3(1, 2, 3), CV3(2, 3, 4)));
  87. }
  88. static void testSetMul() {
  89. V3 v = {1, 2, 3};
  90. coreMulSetV3F(&v, 3);
  91. testVector3(CV3(3, 6, 9), &v);
  92. coreMulSetV3F(&v, -2);
  93. testVector3(CV3(-6, -12, -18), &v);
  94. }
  95. static void testMul() {
  96. testVector3(CV3(3, 6, 9), coreMulV3F(CV30, CV3(1, 2, 3), 3));
  97. }
  98. static void testSetMulVector() {
  99. V3 v = {1, 2, 3};
  100. coreMulSetV3(&v, CV3(2, 1, 3));
  101. testVector3(CV3(2, 2, 9), &v);
  102. coreMulSetV3(&v, CV3(-3, 4, -2));
  103. testVector3(CV3(-6, 8, -18), &v);
  104. }
  105. static void testMulVector() {
  106. testVector3(CV3(-2, -2, -9),
  107. coreMulV3(CV30, CV3(2, 1, 3), CV3(-1, -2, -3)));
  108. testVector3(CV3(2, 2, 9), coreMulV3(CV30, CV3(1, 2, 3), CV3(2, 1, 3)));
  109. }
  110. static void testSetDiv() {
  111. V3 v = {12, 24, 9};
  112. coreDivSetV3F(&v, 3);
  113. testVector3(CV3(4, 8, 3), &v);
  114. coreDivSetV3F(&v, -2);
  115. testVector3(CV3(-2, -4, -1.5f), &v);
  116. }
  117. static void testDiv() {
  118. testVector3(CV3(-1, -2, -3), coreDivV3F(CV30, CV3(-3, -6, -9), 3));
  119. }
  120. static void testSetDivVector() {
  121. V3 v = {3, 4, 6};
  122. coreDivSetV3(&v, CV3(2, 1, 3));
  123. testVector3(CV3(1.5f, 4, 2), &v);
  124. coreDivSetV3(&v, CV3(-3, 4, -2));
  125. testVector3(CV3(-0.5f, 1, -1), &v);
  126. }
  127. static void testDivVector() {
  128. testVector3(CV3(-2, -0.5f, -1),
  129. coreDivV3(CV30, CV3(2, 1, 3), CV3(-1, -2, -3)));
  130. testVector3(CV3(0.5f, 2, 1), coreDivV3(CV30, CV3(1, 2, 3), CV3(2, 1, 3)));
  131. }
  132. static void testSetInvert() {
  133. testVector3(CV3(-1, 2, 3), coreInvertSetV3(CV3(1, -2, -3)));
  134. }
  135. static void testInvert() {
  136. testVector3(CV3(-1, 2, 3), coreInvertV3(CV30, CV3(1, -2, -3)));
  137. }
  138. static void testDot() {
  139. CORE_TEST_FLOAT(9, coreDotV3(CV3(-4, 2, -3), CV3(-1, -2, -3)), eps);
  140. CORE_TEST_FLOAT(-22, coreDotV3(CV3(2, 2, -4), CV3(1, -2, 5)), eps);
  141. }
  142. static void testSquareLength() {
  143. CORE_TEST_FLOAT(29, coreSquareLengthV3(CV3(-4, 2, -3)), eps);
  144. CORE_TEST_FLOAT(24, coreSquareLengthV3(CV3(2, 2, -4)), eps);
  145. }
  146. static void testLength() {
  147. CORE_TEST_FLOAT(3, coreLengthV3(CV3(-2, 2, -1)), eps);
  148. CORE_TEST_FLOAT(7, coreLengthV3(CV3(6, 2, -3)), eps);
  149. }
  150. static void testNormalize() {
  151. V3 v1 = {-2, 2, -1};
  152. V3 v2;
  153. coreMulV3F(&v2, &v1, 1.0f / 3.0f);
  154. coreNormalizeV3(&v1);
  155. testVector3(&v2, &v1);
  156. V3 v3 = {6, 2, -3};
  157. V3 v4;
  158. coreMulV3F(&v4, &v3, 1.0f / 7.0f);
  159. coreNormalizeV3(&v3);
  160. testVector3(&v4, &v3);
  161. }
  162. static void testCast() {
  163. testVector3(CV3(-2.0f, 2.0f, 9.0f), coreConvertIV3(CV30, CIV3(-2, 2, 9)));
  164. testIntVector3(CIV3(-2, 2, 9),
  165. coreConvertV3(CIV30, CV3(-2.5f, 2.6f, 9.0f)));
  166. }
  167. static void testToString() {
  168. V3 v = {4, 5, 6};
  169. char buffer[64];
  170. coreToStringV3(&v, buffer, sizeof(buffer));
  171. CORE_TEST_STRING("[4.000, 5.000, 6.000]", buffer);
  172. }
  173. void coreTestVector() {
  174. testSetAngles();
  175. testCross();
  176. testSetAdd();
  177. testAdd();
  178. testSetSub();
  179. testSub();
  180. testSetMul();
  181. testMul();
  182. testSetMulVector();
  183. testMulVector();
  184. testSetDiv();
  185. testDiv();
  186. testSetDivVector();
  187. testDivVector();
  188. testSetInvert();
  189. testInvert();
  190. testDot();
  191. testSquareLength();
  192. testLength();
  193. testNormalize();
  194. testCast();
  195. testToString();
  196. }