Apply clang-format to source tree
[alexxy/gromacs.git] / src / gromacs / math / tests / matrix.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2019, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35 /*! \internal \file
36  * \brief
37  * Tests matrices
38  *
39  * \author Christian Blau <cblau@gwdg.de>
40  * \ingroup module_math
41  */
42 #include "gmxpre.h"
43
44 #include "gromacs/math/matrix.h"
45
46 #include <string>
47 #include <vector>
48
49 #include <gmock/gmock.h>
50 #include <gtest/gtest.h>
51
52 #include "gromacs/math/vec.h"
53
54 #include "testutils/testasserts.h"
55
56 namespace gmx
57 {
58
59 namespace test
60 {
61
62 namespace
63 {
64
65 class MatrixTest : public ::testing::Test
66 {
67 public:
68     MatrixTest() { std::fill(begin(matrix_), end(matrix_), testNumber_ - 1); }
69
70 protected:
71     Matrix3x3 matrix_;
72     real      testNumber_ = 42;
73 };
74
75 TEST_F(MatrixTest, canSetFromArray)
76 {
77     std::array<real, 3 * 3> arr = { { 1, 2, 3, 4, 5, 6, 7, 8, 9 } };
78     Matrix3x3               newMatrix(arr);
79     EXPECT_EQ(newMatrix(0, 0), 1);
80     EXPECT_EQ(newMatrix(0, 1), 2);
81     EXPECT_EQ(newMatrix(0, 2), 3);
82     EXPECT_EQ(newMatrix(1, 0), 4);
83     EXPECT_EQ(newMatrix(1, 1), 5);
84     EXPECT_EQ(newMatrix(1, 2), 6);
85     EXPECT_EQ(newMatrix(2, 0), 7);
86     EXPECT_EQ(newMatrix(2, 1), 8);
87     EXPECT_EQ(newMatrix(2, 2), 9);
88 }
89
90 TEST_F(MatrixTest, canSetStaticallyFromList)
91 {
92     Matrix3x3 newMatrix = { { 1, 2, 3, 4, 5, 6, 7, 8, 9 } };
93     EXPECT_EQ(newMatrix(0, 0), 1);
94     EXPECT_EQ(newMatrix(0, 1), 2);
95     EXPECT_EQ(newMatrix(0, 2), 3);
96     EXPECT_EQ(newMatrix(1, 0), 4);
97     EXPECT_EQ(newMatrix(1, 1), 5);
98     EXPECT_EQ(newMatrix(1, 2), 6);
99     EXPECT_EQ(newMatrix(2, 0), 7);
100     EXPECT_EQ(newMatrix(2, 1), 8);
101     EXPECT_EQ(newMatrix(2, 2), 9);
102 }
103
104 TEST_F(MatrixTest, canConstructAndFill)
105 {
106     for (const auto& x : matrix_)
107     {
108         EXPECT_EQ(testNumber_ - 1, x);
109     }
110 }
111
112 TEST_F(MatrixTest, canSetValues)
113 {
114     matrix_(1, 1) = testNumber_;
115     EXPECT_EQ(testNumber_, matrix_(1, 1));
116 }
117
118 TEST_F(MatrixTest, canCopyAssign)
119 {
120     Matrix3x3 other;
121     other = matrix_;
122     using ::testing::Eq;
123     using ::testing::Pointwise;
124     EXPECT_THAT(other.toArrayRef(), Pointwise(Eq(), matrix_.toArrayRef()));
125 }
126
127 TEST_F(MatrixTest, canSwap)
128 {
129     Matrix3x3 other;
130     matrix_(0, 0) = testNumber_;
131     other.swap(matrix_);
132     EXPECT_EQ(testNumber_, other(0, 0));
133     for (auto x = begin(other) + 1; x != end(other); ++x)
134     {
135         EXPECT_EQ(testNumber_ - 1, *x);
136     }
137 }
138
139 TEST_F(MatrixTest, staticMultiDimArrayExtent)
140 {
141     EXPECT_EQ(matrix_.extent(0), 3);
142     EXPECT_EQ(matrix_.extent(1), 3);
143 }
144
145 TEST_F(MatrixTest, determinantWorks)
146 {
147     const Matrix3x3 mat = { { 1.0, 2.0, 3.0, 0.0, 1.0, 4.0, 5.0, 6.0, 0.0 } };
148     EXPECT_EQ(determinant(mat), 1);
149 }
150
151 TEST_F(MatrixTest, noninvertableDeterminantIsZero)
152 {
153     const Matrix3x3 mat = { { 1, 0, 0, 0, 1, 0, 0, 0, 0 } };
154     EXPECT_EQ(determinant(mat), 0);
155 }
156
157 TEST_F(MatrixTest, determinantOfDiagonalMatrix)
158 {
159     const Matrix3x3 mat = { { 2, 0, 0, 0, 3, 0, 0, 0, 4 } };
160     EXPECT_EQ(determinant(mat), 24);
161 }
162
163 TEST_F(MatrixTest, traceWorks)
164 {
165     const Matrix3x3 mat = { { 1.5, 9, 9, 9, 2.0, 9, 9, 9, 0.25 } };
166     EXPECT_EQ(trace(mat), 3.75);
167 }
168
169 TEST_F(MatrixTest, transposeWorks)
170 {
171     const Matrix3x3 asymmetricMat = { { 1, 2, 3, 4, 5, 6, 7, 8, 9 } };
172
173     const Matrix3x3 transposedAsymmetricMat = transpose(asymmetricMat);
174     EXPECT_EQ(asymmetricMat(0, 0), transposedAsymmetricMat(0, 0));
175     EXPECT_EQ(asymmetricMat(0, 1), transposedAsymmetricMat(1, 0));
176     EXPECT_EQ(asymmetricMat(0, 2), transposedAsymmetricMat(2, 0));
177     EXPECT_EQ(asymmetricMat(1, 0), transposedAsymmetricMat(0, 1));
178     EXPECT_EQ(asymmetricMat(1, 1), transposedAsymmetricMat(1, 1));
179     EXPECT_EQ(asymmetricMat(1, 2), transposedAsymmetricMat(2, 1));
180     EXPECT_EQ(asymmetricMat(2, 0), transposedAsymmetricMat(0, 2));
181     EXPECT_EQ(asymmetricMat(2, 1), transposedAsymmetricMat(1, 2));
182     EXPECT_EQ(asymmetricMat(2, 2), transposedAsymmetricMat(2, 2));
183 }
184
185 TEST_F(MatrixTest, transposeOfSymmetricMatrix)
186 {
187     const Matrix3x3 symmetricMat           = { { 1, 2, 3, 2, 5, 6, 3, 6, 9 } };
188     const Matrix3x3 transposedSymmetricMat = transpose(symmetricMat);
189     for (int i = 0; i < 3; i++)
190     {
191         for (int j = 0; j < 3; j++)
192         {
193             EXPECT_EQ(symmetricMat(i, j), transposedSymmetricMat(i, j));
194         }
195     }
196 }
197
198 TEST_F(MatrixTest, canCreateFromLegacyMatrix)
199 {
200     matrix          legacyMatrix = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
201     const Matrix3x3 fromLegacy   = createMatrix3x3FromLegacyMatrix(legacyMatrix);
202     for (int i = 0; i < 3; i++)
203     {
204         for (int j = 0; j < 3; j++)
205         {
206             EXPECT_EQ(fromLegacy(i, j), legacyMatrix[i][j]);
207         }
208     }
209 }
210
211 TEST_F(MatrixTest, canFillLegacyMatrix)
212 {
213     matrix legacyMatrix = { { -2 } };
214     fillLegacyMatrix(matrix_, legacyMatrix);
215     for (int i = 0; i < 3; i++)
216     {
217         for (int j = 0; j < 3; j++)
218         {
219             EXPECT_EQ(legacyMatrix[i][j], matrix_(i, j));
220         }
221     }
222 }
223
224 } // namespace
225
226 } // namespace test
227
228 } // namespace gmx