MatrixTests.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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("TestMatrixEquals")
  9. {
  10. Matrix<3, 5> m1 = Matrix<3, 5>::sZero();
  11. Matrix<3, 5> m2 = Matrix<3, 5>::sZero();
  12. Matrix<3, 5> m3 = Matrix<3, 5>::sIdentity();
  13. CHECK(m1 == m2);
  14. CHECK(!(m1 != m2));
  15. CHECK(m1 != m3);
  16. CHECK(!(m1 == m3));
  17. }
  18. TEST_CASE("TestMatrixStream")
  19. {
  20. Matrix<3, 5> m1 = Matrix<3, 5>::sIdentity();
  21. std::stringstream ss;
  22. ss << m1;
  23. CHECK(ss.str() == "[1, 0, 0], [0, 1, 0], [0, 0, 1], [0, 0, 0], [0, 0, 0]");
  24. }
  25. TEST_CASE("TestMatrixZero")
  26. {
  27. Matrix<3, 5> m = Matrix<3, 5>::sZero();
  28. for (uint r = 0; r < 3; ++r)
  29. for (uint c = 0; c < 5; ++c)
  30. CHECK(m(r, c) == 0.0f);
  31. }
  32. TEST_CASE("TestMatrixIdentity")
  33. {
  34. Matrix<3, 5> m = Matrix<3, 5>::sIdentity();
  35. for (uint r = 0; r < 3; ++r)
  36. for (uint c = 0; c < 5; ++c)
  37. CHECK(m(r, c) == (r == c? 1.0f : 0.0f));
  38. }
  39. TEST_CASE("TestMatrixMultiply")
  40. {
  41. Matrix<3, 5> m1 = Matrix<3, 5>::sZero();
  42. Matrix<5, 4> m2 = Matrix<5, 4>::sZero();
  43. for (uint r = 0; r < 3; ++r)
  44. for (uint c = 0; c < 5; ++c)
  45. m1(r, c) = float(r * 5 + c + 1);
  46. for (uint r = 0; r < 5; ++r)
  47. for (uint c = 0; c < 4; ++c)
  48. m2(r, c) = float(r * 4 + c + 1);
  49. Matrix<3, 4> m3 = m1 * m2;
  50. CHECK(m3(0, 0) == 175.0f);
  51. CHECK(m3(1, 0) == 400.0f);
  52. CHECK(m3(2, 0) == 625.0f);
  53. CHECK(m3(0, 1) == 190.0f);
  54. CHECK(m3(1, 1) == 440.0f);
  55. CHECK(m3(2, 1) == 690.0f);
  56. CHECK(m3(0, 2) == 205.0f);
  57. CHECK(m3(1, 2) == 480.0f);
  58. CHECK(m3(2, 2) == 755.0f);
  59. CHECK(m3(0, 3) == 220.0f);
  60. CHECK(m3(1, 3) == 520.0f);
  61. CHECK(m3(2, 3) == 820.0f);
  62. }
  63. TEST_CASE("TestMatrixInversed")
  64. {
  65. Matrix<4, 4> mat = Matrix<4, 4>::sZero();
  66. mat(1, 0) = 4;
  67. mat(3, 0) = 8;
  68. mat(0, 1) = 2;
  69. mat(2, 1) = 16;
  70. mat(1, 2) = 16;
  71. mat(3, 2) = 4;
  72. mat(0, 3) = 8;
  73. mat(2, 3) = 2;
  74. Matrix<4, 4> inverse;
  75. CHECK(inverse.SetInversed(mat));
  76. Matrix<4, 4> identity = mat * inverse;
  77. CHECK(identity == Matrix<4, 4>::sIdentity());
  78. // Make non-invertible
  79. mat(1, 0) = 0;
  80. mat(3, 0) = 0;
  81. CHECK(!inverse.SetInversed(mat));
  82. }
  83. TEST_CASE("TestMatrix22Inversed")
  84. {
  85. // SetInverse is specialized for 2x2 matrices
  86. Matrix<2, 2> mat;
  87. mat(0, 0) = 1;
  88. mat(0, 1) = 2;
  89. mat(1, 0) = 3;
  90. mat(1, 1) = 4;
  91. Matrix<2, 2> inverse;
  92. CHECK(inverse.SetInversed(mat));
  93. Matrix<2, 2> identity = mat * inverse;
  94. CHECK(identity == Matrix<2, 2>::sIdentity());
  95. // Make non-invertible
  96. mat(0, 0) = 0;
  97. mat(1, 0) = 0;
  98. CHECK(!inverse.SetInversed(mat));
  99. }
  100. }