test_matrix3.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (C) 2016 Intel Corporation. All rights reserved.
  3. *
  4. * This file is free software: you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This file is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. * See the GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "math_test.h"
  18. // given we are in the Math library, you're epected to know what
  19. // you're doing when directly comparing floats:
  20. #pragma GCC diagnostic push
  21. #pragma GCC diagnostic ignored "-Wfloat-equal"
  22. #define AP_EXPECT_IDENTITY_MATRIX(m_) {\
  23. EXPECT_NEAR(1.0f, m_.a.x, 1.0e-6); \
  24. EXPECT_NEAR(0.0f, m_.a.y, 1.0e-6); \
  25. EXPECT_NEAR(0.0f, m_.a.z, 1.0e-6); \
  26. EXPECT_NEAR(0.0f, m_.b.x, 1.0e-6); \
  27. EXPECT_NEAR(1.0f, m_.b.y, 1.0e-6); \
  28. EXPECT_NEAR(0.0f, m_.b.z, 1.0e-6); \
  29. EXPECT_NEAR(0.0f, m_.c.x, 1.0e-6); \
  30. EXPECT_NEAR(0.0f, m_.c.y, 1.0e-6); \
  31. EXPECT_NEAR(1.0f, m_.c.z, 1.0e-6); \
  32. }
  33. class TestParam {
  34. public:
  35. /**
  36. * The matrix for this param.
  37. */
  38. Matrix3f m;
  39. /**
  40. * The expected determinant for #m.
  41. */
  42. float det;
  43. };
  44. AP_GTEST_PRINTATBLE_PARAM_MEMBER(TestParam, m);
  45. class Matrix3fTest : public ::testing::TestWithParam<TestParam> {};
  46. static TestParam invertible[] = {
  47. {
  48. .m = {
  49. {1.0f, 2.0f, 3.0f},
  50. {4.0f, 6.0f, 2.0f},
  51. {9.0f, 18.0f, 27.0f}
  52. },
  53. .det = 0.0f,
  54. },
  55. };
  56. static TestParam non_invertible[] = {
  57. {
  58. .m = {
  59. { 6.0f, 2.0f, 20.0f},
  60. { 1.0f, -9.0f, 4.0f},
  61. {-4.0f, 7.0f, -27.0f}
  62. },
  63. .det = 732.0f,
  64. },
  65. {
  66. .m = {
  67. {-6.0f, -2.0f, -20.0f},
  68. {-1.0f, 9.0f, -4.0f},
  69. { 4.0f, -7.0f, 27.0f}
  70. },
  71. .det = -732.0f,
  72. },
  73. };
  74. TEST_P(Matrix3fTest, Determinants)
  75. {
  76. auto param = GetParam();
  77. EXPECT_FLOAT_EQ(param.det, param.m.det());
  78. }
  79. TEST_P(Matrix3fTest, Inverses)
  80. {
  81. auto param = GetParam();
  82. Matrix3f inv;
  83. bool success = param.m.inverse(inv);
  84. if (param.det == 0.0f) {
  85. EXPECT_FALSE(success);
  86. } else {
  87. ASSERT_TRUE(success);
  88. auto identity = inv * param.m;
  89. AP_EXPECT_IDENTITY_MATRIX(identity);
  90. }
  91. }
  92. INSTANTIATE_TEST_CASE_P(InvertibleMatrices,
  93. Matrix3fTest,
  94. ::testing::ValuesIn(invertible));
  95. INSTANTIATE_TEST_CASE_P(NonInvertibleMatrices,
  96. Matrix3fTest,
  97. ::testing::ValuesIn(non_invertible));
  98. AP_GTEST_MAIN()
  99. #pragma GCC diagnostic pop