2
0

Vectors.ut.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include <gtest/gtest.h>
  2. #include <iostream>
  3. #include "Math.h"
  4. #include "MathCommon.ut.h"
  5. const float REALY_SMALL_FLOAT = 1.0e-3;
  6. //======================================================================================================================
  7. // Alignment =
  8. //======================================================================================================================
  9. TEST(MathTests, Alignment)
  10. {
  11. const int FS = sizeof(float); // float size
  12. EXPECT_EQ(sizeof(Vec2), FS * 2);
  13. EXPECT_EQ(sizeof(Vec3), FS * 3);
  14. EXPECT_EQ(sizeof(Vec4), FS * 4);
  15. EXPECT_EQ(sizeof(Quat), FS * 4);
  16. EXPECT_EQ(sizeof(Euler), FS * 3);
  17. EXPECT_EQ(sizeof(Mat3), FS * 9);
  18. EXPECT_EQ(sizeof(Mat4), FS * 16);
  19. }
  20. TEST(MathTests, VectorConstructors)
  21. {
  22. testCommonContructors<Vec2>();
  23. testCommonContructors<Vec3>();
  24. testCommonContructors<Vec4>();
  25. }
  26. //======================================================================================================================
  27. // Operators =
  28. //======================================================================================================================
  29. template<typename Type>
  30. void arithmeticOperations()
  31. {
  32. testOperators<Type, &Type::operator+, &Type::operator+=, &addf>();
  33. testOperators<Type, &Type::operator-, &Type::operator-=, &subf>();
  34. testOperators<Type, &Type::operator*, &Type::operator*=, &mulf>();
  35. testOperators<Type, &Type::operator/, &Type::operator/=, &divf>();
  36. testOperatorsWithFloat<Type, &Type::operator+, &Type::operator+=, &operator+, &addf>();
  37. testOperatorsWithFloat<Type, &Type::operator-, &Type::operator-=, &operator-, &subf>();
  38. testOperatorsWithFloat<Type, &Type::operator*, &Type::operator*=, &operator*, &mulf>();
  39. testOperatorsWithFloat<Type, &Type::operator/, &Type::operator/=, &operator/, &divf>();
  40. testCmpOperators<Type>();
  41. }
  42. TEST(MathTests, VectorArithmetic)
  43. {
  44. arithmeticOperations<Vec2>();
  45. arithmeticOperations<Vec3>();
  46. arithmeticOperations<Vec4>();
  47. }
  48. //======================================================================================================================
  49. // Misc =
  50. //======================================================================================================================
  51. template<typename Type>
  52. void testDotProd()
  53. {
  54. Type a, b;
  55. float o = 0.0;
  56. for(uint i = 0; i < (sizeof(Type) / sizeof(float)); i++)
  57. {
  58. a[i] = randFloat();
  59. b[i] = randFloat();
  60. o += a[i] * b[i];
  61. }
  62. EXPECT_NEAR(a.dot(b), o, M::EPSILON);
  63. }
  64. TEST(MathTests, DotProducts)
  65. {
  66. testDotProd<Vec2>();
  67. testDotProd<Vec3>();
  68. testDotProd<Vec4>();
  69. }
  70. template<typename Type>
  71. void testLengthAndNormalize()
  72. {
  73. Type a;
  74. float o = 0.0;
  75. for(uint i = 0; i < (sizeof(Type) / sizeof(float)); i++)
  76. {
  77. a[i] = randFloat();
  78. o += a[i] * a[i];
  79. }
  80. o = sqrt(o);
  81. EXPECT_NEAR(a.getLength(), o, REALY_SMALL_FLOAT);
  82. EXPECT_NEAR(a.getNormalized().getLength(), 1.0, REALY_SMALL_FLOAT);
  83. //EXPECT_EQ(a.getNormalized() * a.getLength(), a);
  84. }
  85. TEST(MathTests, LengthsAndNormals)
  86. {
  87. testLengthAndNormalize<Vec2>();
  88. testLengthAndNormalize<Vec3>();
  89. testLengthAndNormalize<Vec4>();
  90. }