APM:Libraries
test_matrix3.cpp
Go to the documentation of this file.
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 
18 #include "math_test.h"
19 
20 #define AP_EXPECT_IDENTITY_MATRIX(m_) {\
21  EXPECT_NEAR(1.0f, m_.a.x, 1.0e-6); \
22  EXPECT_NEAR(0.0f, m_.a.y, 1.0e-6); \
23  EXPECT_NEAR(0.0f, m_.a.z, 1.0e-6); \
24  EXPECT_NEAR(0.0f, m_.b.x, 1.0e-6); \
25  EXPECT_NEAR(1.0f, m_.b.y, 1.0e-6); \
26  EXPECT_NEAR(0.0f, m_.b.z, 1.0e-6); \
27  EXPECT_NEAR(0.0f, m_.c.x, 1.0e-6); \
28  EXPECT_NEAR(0.0f, m_.c.y, 1.0e-6); \
29  EXPECT_NEAR(1.0f, m_.c.z, 1.0e-6); \
30 }
31 
32 class TestParam {
33 public:
41  float det;
42 };
43 
45 
46 class Matrix3fTest : public ::testing::TestWithParam<TestParam> {};
47 
48 static TestParam invertible[] = {
49  {
50  .m = {
51  {1.0f, 2.0f, 3.0f},
52  {4.0f, 6.0f, 2.0f},
53  {9.0f, 18.0f, 27.0f}
54  },
55  .det = 0.0f,
56  },
57 };
58 
60  {
61  .m = {
62  { 6.0f, 2.0f, 20.0f},
63  { 1.0f, -9.0f, 4.0f},
64  {-4.0f, 7.0f, -27.0f}
65  },
66  .det = 732.0f,
67  },
68  {
69  .m = {
70  {-6.0f, -2.0f, -20.0f},
71  {-1.0f, 9.0f, -4.0f},
72  { 4.0f, -7.0f, 27.0f}
73  },
74  .det = -732.0f,
75  },
76 };
77 
78 TEST_P(Matrix3fTest, Determinants)
79 {
80  auto param = GetParam();
81  EXPECT_FLOAT_EQ(param.det, param.m.det());
82 }
83 
84 TEST_P(Matrix3fTest, Inverses)
85 {
86  auto param = GetParam();
87  Matrix3f inv;
88  bool success = param.m.inverse(inv);
89 
90  if (param.det == 0.0f) {
91  EXPECT_FALSE(success);
92  } else {
93  ASSERT_TRUE(success);
94  auto identity = inv * param.m;
95  AP_EXPECT_IDENTITY_MATRIX(identity);
96  }
97 }
98 
99 INSTANTIATE_TEST_CASE_P(InvertibleMatrices,
100  Matrix3fTest,
101  ::testing::ValuesIn(invertible));
102 
103 INSTANTIATE_TEST_CASE_P(NonInvertibleMatrices,
104  Matrix3fTest,
105  ::testing::ValuesIn(non_invertible));
106 
107 AP_GTEST_MAIN()
AP_GTEST_PRINTATBLE_PARAM_MEMBER(TestParam, m)
INSTANTIATE_TEST_CASE_P(InvertibleMatrices, Matrix3fTest, ::testing::ValuesIn(invertible))
Matrix3f m
T det() const
Definition: matrix3.cpp:197
bool inverse(Matrix3< T > &inv) const
Definition: matrix3.cpp:205
TEST_P(Matrix3fTest, Determinants)
#define AP_EXPECT_IDENTITY_MATRIX(m_)
static TestParam invertible[]
static TestParam non_invertible[]