MatrixTests.cpp 1.9 KB

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