Matrices.ut.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include <gtest/gtest.h>
  2. #include <iostream>
  3. #include "Math.h"
  4. #include "MathCommon.ut.h"
  5. /// Test nxn matrix multiplication
  6. template<typename Type>
  7. void testMatrixMul()
  8. {
  9. int dimention = log2(sizeof(Type) / sizeof(float));
  10. Type r, a, b;
  11. // Fill
  12. for(int i = 0; i < dimention; i++)
  13. {
  14. for(int j = 0; j < dimention; j++)
  15. {
  16. a(i, j) = randFloat();
  17. b(i, j) = randFloat();
  18. }
  19. }
  20. // Calc r = a * b as usual
  21. for(int i = 0; i < dimention; i++)
  22. {
  23. for(int j = 0; j < dimention; j++)
  24. {
  25. r(i, j) = 0.0;
  26. for(int k = 0; k < dimention; k++)
  27. {
  28. r(i, j) += a(i, k) * b(k, j);
  29. }
  30. }
  31. }
  32. EXPECT_EQ(r, a * b);
  33. }
  34. template<typename Type>
  35. void arithmeticOperations()
  36. {
  37. testOperators<Type, &Type::operator+, &Type::operator+=, &addf>();
  38. testMatrixMul<Type>();
  39. testOperators<Type, &Type::operator-, &Type::operator-=, &subf>();
  40. testOperatorsWithFloat<Type, &Type::operator+, &Type::operator+=, &operator+, &addf>();
  41. testOperatorsWithFloat<Type, &Type::operator-, &Type::operator-=, &operator-, &subf>();
  42. testOperatorsWithFloat<Type, &Type::operator*, &Type::operator*=, &operator*, &mulf>();
  43. testOperatorsWithFloat<Type, &Type::operator/, &Type::operator/=, &operator/, &divf>();
  44. testCmpOperators<Type>();
  45. }
  46. TEST(MathTests, MatrixOperators)
  47. {
  48. arithmeticOperations<Mat3>();
  49. arithmeticOperations<Mat4>();
  50. }