MatrixTests.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include "UnitTestFramework.h"
  5. #include <Jolt/Math/Matrix.h>
  6. TEST_SUITE("MatrixTests")
  7. {
  8. TEST_CASE("TestMatrixZero")
  9. {
  10. Matrix<3, 5> m = Matrix<3, 5>::sZero();
  11. for (uint r = 0; r < 3; ++r)
  12. for (uint c = 0; c < 5; ++c)
  13. CHECK(m(r, c) == 0.0f);
  14. }
  15. TEST_CASE("TestMatrixIdentity")
  16. {
  17. Matrix<3, 5> m = Matrix<3, 5>::sIdentity();
  18. for (uint r = 0; r < 3; ++r)
  19. for (uint c = 0; c < 5; ++c)
  20. CHECK(m(r, c) == (r == c? 1.0f : 0.0f));
  21. }
  22. TEST_CASE("TestMatrixMultiply")
  23. {
  24. Matrix<3, 5> m1 = Matrix<3, 5>::sZero();
  25. Matrix<5, 4> m2 = Matrix<5, 4>::sZero();
  26. for (uint r = 0; r < 3; ++r)
  27. for (uint c = 0; c < 5; ++c)
  28. m1(r, c) = float(r * 5 + c + 1);
  29. for (uint r = 0; r < 5; ++r)
  30. for (uint c = 0; c < 4; ++c)
  31. m2(r, c) = float(r * 4 + c + 1);
  32. Matrix<3, 4> m3 = m1 * m2;
  33. CHECK(m3(0, 0) == 175.0f);
  34. CHECK(m3(1, 0) == 400.0f);
  35. CHECK(m3(2, 0) == 625.0f);
  36. CHECK(m3(0, 1) == 190.0f);
  37. CHECK(m3(1, 1) == 440.0f);
  38. CHECK(m3(2, 1) == 690.0f);
  39. CHECK(m3(0, 2) == 205.0f);
  40. CHECK(m3(1, 2) == 480.0f);
  41. CHECK(m3(2, 2) == 755.0f);
  42. CHECK(m3(0, 3) == 220.0f);
  43. CHECK(m3(1, 3) == 520.0f);
  44. CHECK(m3(2, 3) == 820.0f);
  45. }
  46. TEST_CASE("TestMatrixInversed")
  47. {
  48. Matrix<4, 4> mat = Matrix<4, 4>::sZero();
  49. mat(1, 0) = 4;
  50. mat(3, 0) = 8;
  51. mat(0, 1) = 2;
  52. mat(2, 1) = 16;
  53. mat(1, 2) = 16;
  54. mat(3, 2) = 4;
  55. mat(0, 3) = 8;
  56. mat(2, 3) = 2;
  57. Matrix<4, 4> inverse;
  58. CHECK(inverse.SetInversed(mat));
  59. Matrix<4, 4> identity = mat * inverse;
  60. CHECK(identity == Matrix<4, 4>::sIdentity());
  61. }
  62. TEST_CASE("TestMatrix22Inversed")
  63. {
  64. // SetInverse is specialized for 2x2 matrices
  65. Matrix<2, 2> mat;
  66. mat(0, 0) = 1;
  67. mat(0, 1) = 2;
  68. mat(1, 0) = 3;
  69. mat(1, 1) = 4;
  70. Matrix<2, 2> inverse;
  71. CHECK(inverse.SetInversed(mat));
  72. Matrix<2, 2> identity = mat * inverse;
  73. CHECK(identity == Matrix<2, 2>::sIdentity());
  74. }
  75. }