MathCommon.ut.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #ifndef MATH_COMMON_UT_H
  2. #define MATH_COMMON_UT_H
  3. #include "Util.h"
  4. /// Get random float and not zero cause of the divs
  5. inline float randFloat()
  6. {
  7. const float RANGE_MIN = -1.00123;
  8. const float RANGE_MAX = 900.9990001;
  9. float f;
  10. while(true)
  11. {
  12. f = Util::randRange(RANGE_MIN, RANGE_MAX);
  13. if(!isZero(f))
  14. {
  15. break;
  16. }
  17. }
  18. return f;
  19. }
  20. //======================================================================================================================
  21. // testCommonContructors =
  22. //======================================================================================================================
  23. template<typename Type>
  24. void testCommonContructors()
  25. {
  26. Type a, b;
  27. float f;
  28. float arr[sizeof(Type) / sizeof(float)];
  29. f = randFloat();
  30. for(uint i = 0; i < (sizeof(Type) / sizeof(float)); i++)
  31. {
  32. a[i] = f;
  33. b[i] = randFloat();
  34. arr[i] = b[i];
  35. }
  36. // float
  37. EXPECT_EQ(Type(f), a);
  38. // arr
  39. EXPECT_EQ(Type(arr), b);
  40. // Copy
  41. EXPECT_EQ(Type(b), b);
  42. }
  43. //======================================================================================================================
  44. // Test operators =
  45. //======================================================================================================================
  46. inline float addf(float a, float b)
  47. {
  48. return a + b;
  49. }
  50. inline float subf(float a, float b)
  51. {
  52. return a - b;
  53. }
  54. inline float mulf(float a, float b)
  55. {
  56. return a * b;
  57. }
  58. inline float divf(float a, float b)
  59. {
  60. return a / b;
  61. }
  62. template<
  63. typename Type,
  64. Type (Type::* op)(const Type&) const,
  65. Type& (Type::* compoundAssignment)(const Type&),
  66. float (* normalOp)(float, float)>
  67. void testOperators()
  68. {
  69. Type a, b, c;
  70. for(uint i = 0; i < (sizeof(Type) / sizeof(float)); i++)
  71. {
  72. a[i] = randFloat();
  73. b[i] = randFloat();
  74. c[i] = normalOp(a[i], b[i]);
  75. }
  76. EXPECT_EQ((a.*op)(b), c);
  77. (a.*compoundAssignment)(b);
  78. EXPECT_EQ(a, c);
  79. }
  80. /// @tparam op eg Type + float
  81. /// @tparam compoundAssignment eg Type += float
  82. /// @tparam opExtern eg float + Type
  83. /// @tparam normalOp The normal function
  84. template<
  85. typename Type,
  86. Type (Type::* op)(float) const,
  87. Type& (Type::* compoundAssignment)(float),
  88. Type (* opExtern)(float, const Type&),
  89. float (* normalOp)(float, float)>
  90. void testOperatorsWithFloat()
  91. {
  92. Type a, b, c;
  93. float f = randFloat();
  94. for(uint i = 0; i < (sizeof(Type) / sizeof(float)); i++)
  95. {
  96. a[i] = randFloat();
  97. b[i] = normalOp(a[i], f);
  98. c[i] = normalOp(f, a[i]);
  99. }
  100. EXPECT_EQ((a.*op)(f), b);
  101. EXPECT_EQ(opExtern(f, a), c);
  102. (a.*compoundAssignment)(f);
  103. EXPECT_EQ(a, b);
  104. }
  105. template<typename Type>
  106. void testCmpOperators()
  107. {
  108. Type a, b;
  109. for(uint i = 0; i < (sizeof(Type) / sizeof(float)); i++)
  110. {
  111. a[i] = b[i] = randFloat();
  112. }
  113. EXPECT_EQ(a == b, true);
  114. EXPECT_EQ(a != b, false);
  115. }
  116. template<typename Type>
  117. void testNegOperator()
  118. {
  119. Type a, b;
  120. for(int i = 0; i < (sizeof(Type) / sizeof(float)); i++)
  121. {
  122. a[i] = randFloat();
  123. b[i] = -a[i];
  124. }
  125. EXPECT_EQ(-a == b, true);
  126. }
  127. #endif