[SyncFS] Build indexes from FileTracker entries on disk.
[chromium-blink-merge.git] / ui / gfx / geometry / matrix3_unittest.cc
blob0f57e8e7a95d4cc5253d640c87b58b4544cd4f0d
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include <cmath>
6 #include <limits>
8 #include "base/basictypes.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "ui/gfx/geometry/matrix3_f.h"
12 namespace gfx {
13 namespace {
15 TEST(Matrix3fTest, Constructors) {
16 Matrix3F zeros = Matrix3F::Zeros();
17 Matrix3F ones = Matrix3F::Ones();
18 Matrix3F identity = Matrix3F::Identity();
20 Matrix3F product_ones = Matrix3F::FromOuterProduct(
21 Vector3dF(1.0f, 1.0f, 1.0f), Vector3dF(1.0f, 1.0f, 1.0f));
22 Matrix3F product_zeros = Matrix3F::FromOuterProduct(
23 Vector3dF(1.0f, 1.0f, 1.0f), Vector3dF(0.0f, 0.0f, 0.0f));
24 EXPECT_EQ(ones, product_ones);
25 EXPECT_EQ(zeros, product_zeros);
27 for (int i = 0; i < 3; ++i) {
28 for (int j = 0; j < 3; ++j)
29 EXPECT_EQ(i == j ? 1.0f : 0.0f, identity.get(i, j));
33 TEST(Matrix3fTest, DataAccess) {
34 Matrix3F matrix = Matrix3F::Ones();
35 Matrix3F identity = Matrix3F::Identity();
37 EXPECT_EQ(Vector3dF(0.0f, 1.0f, 0.0f), identity.get_column(1));
38 matrix.set(0.0f, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f);
39 EXPECT_EQ(Vector3dF(2.0f, 5.0f, 8.0f), matrix.get_column(2));
40 matrix.set_column(0, Vector3dF(0.1f, 0.2f, 0.3f));
41 EXPECT_EQ(Vector3dF(0.1f, 0.2f, 0.3f), matrix.get_column(0));
43 EXPECT_EQ(0.1f, matrix.get(0, 0));
44 EXPECT_EQ(5.0f, matrix.get(1, 2));
47 TEST(Matrix3fTest, Determinant) {
48 EXPECT_EQ(1.0f, Matrix3F::Identity().Determinant());
49 EXPECT_EQ(0.0f, Matrix3F::Zeros().Determinant());
50 EXPECT_EQ(0.0f, Matrix3F::Ones().Determinant());
52 // Now for something non-trivial...
53 Matrix3F matrix = Matrix3F::Zeros();
54 matrix.set(0, 5, 6, 8, 7, 0, 1, 9, 0);
55 EXPECT_EQ(390.0f, matrix.Determinant());
56 matrix.set(2, 0, 3 * matrix.get(0, 0));
57 matrix.set(2, 1, 3 * matrix.get(0, 1));
58 matrix.set(2, 2, 3 * matrix.get(0, 2));
59 EXPECT_EQ(0, matrix.Determinant());
61 matrix.set(0.57f, 0.205f, 0.942f,
62 0.314f, 0.845f, 0.826f,
63 0.131f, 0.025f, 0.962f);
64 EXPECT_NEAR(0.3149f, matrix.Determinant(), 0.0001f);
67 TEST(Matrix3fTest, Inverse) {
68 Matrix3F identity = Matrix3F::Identity();
69 Matrix3F inv_identity = identity.Inverse();
70 EXPECT_EQ(identity, inv_identity);
72 Matrix3F singular = Matrix3F::Zeros();
73 singular.set(1.0f, 3.0f, 4.0f,
74 2.0f, 11.0f, 5.0f,
75 0.5f, 1.5f, 2.0f);
76 EXPECT_EQ(0, singular.Determinant());
77 EXPECT_EQ(Matrix3F::Zeros(), singular.Inverse());
79 Matrix3F regular = Matrix3F::Zeros();
80 regular.set(0.57f, 0.205f, 0.942f,
81 0.314f, 0.845f, 0.826f,
82 0.131f, 0.025f, 0.962f);
83 Matrix3F inv_regular = regular.Inverse();
84 regular.set(2.51540616f, -0.55138018f, -1.98968043f,
85 -0.61552266f, 1.34920184f, -0.55573636f,
86 -0.32653861f, 0.04002158f, 1.32488726f);
87 EXPECT_TRUE(regular.IsNear(inv_regular, 0.00001f));
90 TEST(Matrix3fTest, EigenvectorsIdentity) {
91 // This block tests the trivial case of eigenvalues of the identity matrix.
92 Matrix3F identity = Matrix3F::Identity();
93 Vector3dF eigenvals = identity.SolveEigenproblem(NULL);
94 EXPECT_EQ(Vector3dF(1.0f, 1.0f, 1.0f), eigenvals);
97 TEST(Matrix3fTest, EigenvectorsDiagonal) {
98 // This block tests the another trivial case of eigenvalues of a diagonal
99 // matrix. Here we expect values to be sorted.
100 Matrix3F matrix = Matrix3F::Zeros();
101 matrix.set(0, 0, 1.0f);
102 matrix.set(1, 1, -2.5f);
103 matrix.set(2, 2, 3.14f);
104 Matrix3F eigenvectors = Matrix3F::Zeros();
105 Vector3dF eigenvals = matrix.SolveEigenproblem(&eigenvectors);
106 EXPECT_EQ(Vector3dF(3.14f, 1.0f, -2.5f), eigenvals);
108 EXPECT_EQ(Vector3dF(0.0f, 0.0f, 1.0f), eigenvectors.get_column(0));
109 EXPECT_EQ(Vector3dF(1.0f, 0.0f, 0.0f), eigenvectors.get_column(1));
110 EXPECT_EQ(Vector3dF(0.0f, 1.0f, 0.0f), eigenvectors.get_column(2));
113 TEST(Matrix3fTest, EigenvectorsNiceNotPositive) {
114 // This block tests computation of eigenvectors of a matrix where nice
115 // round values are expected.
116 Matrix3F matrix = Matrix3F::Zeros();
117 // This is not a positive-definite matrix but eigenvalues and the first
118 // eigenvector should nonetheless be computed correctly.
119 matrix.set(3, 2, 4, 2, 0, 2, 4, 2, 3);
120 Matrix3F eigenvectors = Matrix3F::Zeros();
121 Vector3dF eigenvals = matrix.SolveEigenproblem(&eigenvectors);
122 EXPECT_EQ(Vector3dF(8.0f, -1.0f, -1.0f), eigenvals);
124 Vector3dF expected_principal(0.66666667f, 0.33333333f, 0.66666667f);
125 EXPECT_NEAR(0.0f,
126 (expected_principal - eigenvectors.get_column(0)).Length(),
127 0.000001f);
130 TEST(Matrix3fTest, EigenvectorsPositiveDefinite) {
131 // This block tests computation of eigenvectors of a matrix where output
132 // is not as nice as above, but it actually meets the definition.
133 Matrix3F matrix = Matrix3F::Zeros();
134 Matrix3F eigenvectors = Matrix3F::Zeros();
135 Matrix3F expected_eigenvectors = Matrix3F::Zeros();
136 matrix.set(1, -1, 2, -1, 4, 5, 2, 5, 0);
137 Vector3dF eigenvals = matrix.SolveEigenproblem(&eigenvectors);
138 Vector3dF expected_eigv(7.3996266f, 1.91197255f, -4.31159915f);
139 expected_eigv -= eigenvals;
140 EXPECT_NEAR(0, expected_eigv.LengthSquared(), 0.00001f);
141 expected_eigenvectors.set(0.04926317f, -0.92135662f, -0.38558414f,
142 0.82134249f, 0.25703273f, -0.50924521f,
143 0.56830419f, -0.2916096f, 0.76941158f);
144 EXPECT_TRUE(expected_eigenvectors.IsNear(eigenvectors, 0.00001f));