Math.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. #include <gtest/gtest.h>
  2. #include <iostream>
  3. #include "Math.h"
  4. #include "Util.h"
  5. const float RANGE_MIN = -1.00123;
  6. const float RANGE_MAX = 900.9990001;
  7. const float REALY_SMALL_FLOAT = 1.0e-3;
  8. /// Get random float and not zero cause of the divs
  9. static float randFloat()
  10. {
  11. float f;
  12. while(true)
  13. {
  14. f = Util::randRange(RANGE_MIN, RANGE_MAX);
  15. if(!isZero(f))
  16. {
  17. break;
  18. }
  19. }
  20. return f;
  21. }
  22. //======================================================================================================================
  23. // Alignment =
  24. //======================================================================================================================
  25. TEST(MathTests, Alignment)
  26. {
  27. const int FS = sizeof(float); // float size
  28. EXPECT_EQ(sizeof(Vec2), FS * 2);
  29. EXPECT_EQ(sizeof(Vec3), FS * 3);
  30. EXPECT_EQ(sizeof(Vec4), FS * 4);
  31. EXPECT_EQ(sizeof(Quat), FS * 4);
  32. EXPECT_EQ(sizeof(Euler), FS * 3);
  33. EXPECT_EQ(sizeof(Mat3), FS * 9);
  34. EXPECT_EQ(sizeof(Mat4), FS * 16);
  35. }
  36. //======================================================================================================================
  37. // Constructors =
  38. //======================================================================================================================
  39. template<typename Type>
  40. void testCommonContructors()
  41. {
  42. Type a, b;
  43. float f;
  44. float arr[sizeof(Type) / sizeof(float)];
  45. f = randFloat();
  46. for(uint i = 0; i < (sizeof(Type) / sizeof(float)); i++)
  47. {
  48. a[i] = f;
  49. b[i] = randFloat();
  50. arr[i] = b[i];
  51. }
  52. // float
  53. EXPECT_EQ(Type(f), a);
  54. // arr
  55. EXPECT_EQ(Type(arr), b);
  56. // Type
  57. EXPECT_EQ(Type(b), b);
  58. }
  59. TEST(MathTests, Constructors)
  60. {
  61. testCommonContructors<Vec2>();
  62. testCommonContructors<Vec3>();
  63. // Vec4
  64. testCommonContructors<Vec4>();
  65. }
  66. //======================================================================================================================
  67. // Arithmetic =
  68. //======================================================================================================================
  69. float addf(float a, float b)
  70. {
  71. return a + b;
  72. }
  73. float subf(float a, float b)
  74. {
  75. return a - b;
  76. }
  77. float mulf(float a, float b)
  78. {
  79. return a * b;
  80. }
  81. float divf(float a, float b)
  82. {
  83. return a / b;
  84. }
  85. template<
  86. typename Type,
  87. Type (Type::* op)(const Type&) const,
  88. Type& (Type::* compoundAssignment)(const Type&),
  89. float (* normalOp)(float, float)>
  90. void testOperators()
  91. {
  92. Type a, b, c;
  93. for(uint i = 0; i < (sizeof(Type) / sizeof(float)); i++)
  94. {
  95. a[i] = randFloat();
  96. b[i] = randFloat();
  97. c[i] = normalOp(a[i], b[i]);
  98. }
  99. EXPECT_EQ((a.*op)(b), c);
  100. (a.*compoundAssignment)(b);
  101. EXPECT_EQ(a, c);
  102. }
  103. template<
  104. typename Type,
  105. Type (Type::* op)(float) const,
  106. Type& (Type::* compoundAssignment)(float),
  107. Type (* opExtern)(float, const Type&),
  108. float (* normalOp)(float, float)>
  109. void testOperatorsWithFloat()
  110. {
  111. Type a, b, c;
  112. float f = randFloat();
  113. for(uint i = 0; i < (sizeof(Type) / sizeof(float)); i++)
  114. {
  115. a[i] = randFloat();
  116. b[i] = normalOp(a[i], f);
  117. c[i] = normalOp(f, a[i]);
  118. }
  119. EXPECT_EQ((a.*op)(f), b);
  120. EXPECT_EQ(opExtern(f, a), c);
  121. (a.*compoundAssignment)(f);
  122. EXPECT_EQ(a, b);
  123. }
  124. template<typename Type>
  125. void testCmpOperators()
  126. {
  127. Type a, b;
  128. for(uint i = 0; i < (sizeof(Type) / sizeof(float)); i++)
  129. {
  130. a[i] = b[i] = randFloat();
  131. }
  132. EXPECT_EQ(a == b, true);
  133. EXPECT_EQ(a != b, false);
  134. }
  135. template<typename Type>
  136. void testNegOperator()
  137. {
  138. Type a, b;
  139. for(int i = 0; i < (sizeof(Type) / sizeof(float)); i++)
  140. {
  141. a[i] = randFloat();
  142. b[i] = -a[i];
  143. }
  144. EXPECT_EQ(-a == b, true);
  145. }
  146. template<typename Type>
  147. void arithmeticOperations()
  148. {
  149. testOperators<Type, &Type::operator+, &Type::operator+=, &addf>();
  150. testOperators<Type, &Type::operator-, &Type::operator-=, &subf>();
  151. testOperators<Type, &Type::operator*, &Type::operator*=, &mulf>();
  152. testOperators<Type, &Type::operator/, &Type::operator/=, &divf>();
  153. testOperatorsWithFloat<Type, &Type::operator+, &Type::operator+=, &operator+, &addf>();
  154. testOperatorsWithFloat<Type, &Type::operator-, &Type::operator-=, &operator-, &subf>();
  155. testOperatorsWithFloat<Type, &Type::operator*, &Type::operator*=, &operator*, &mulf>();
  156. testOperatorsWithFloat<Type, &Type::operator/, &Type::operator/=, &operator/, &divf>();
  157. testCmpOperators<Type>();
  158. }
  159. TEST(MathTests, VectorArithmetic)
  160. {
  161. arithmeticOperations<Vec2>();
  162. arithmeticOperations<Vec3>();
  163. arithmeticOperations<Vec4>();
  164. }
  165. template<typename Type>
  166. void testDotProd()
  167. {
  168. Type a, b;
  169. float o = 0.0;
  170. for(uint i = 0; i < (sizeof(Type) / sizeof(float)); i++)
  171. {
  172. a[i] = randFloat();
  173. b[i] = randFloat();
  174. o += a[i] * b[i];
  175. }
  176. EXPECT_NEAR(a.dot(b), o, M::EPSILON);
  177. }
  178. TEST(MathTests, DotProducts)
  179. {
  180. testDotProd<Vec2>();
  181. testDotProd<Vec3>();
  182. testDotProd<Vec4>();
  183. }
  184. template<typename Type>
  185. void testLengthAndNormalize()
  186. {
  187. Type a;
  188. float o = 0.0;
  189. for(uint i = 0; i < (sizeof(Type) / sizeof(float)); i++)
  190. {
  191. a[i] = randFloat();
  192. o += a[i] * a[i];
  193. }
  194. o = sqrt(o);
  195. EXPECT_NEAR(a.getLength(), o, REALY_SMALL_FLOAT);
  196. EXPECT_NEAR(a.getNormalized().getLength(), 1.0, REALY_SMALL_FLOAT);
  197. //EXPECT_EQ(a.getNormalized() * a.getLength(), a);
  198. }
  199. TEST(MathTests, LengthsAndNormals)
  200. {
  201. testLengthAndNormalize<Vec2>();
  202. testLengthAndNormalize<Vec3>();
  203. testLengthAndNormalize<Vec4>();
  204. }