Apply clang-format to source tree
[alexxy/gromacs.git] / src / gromacs / fileio / tests / mrcdensitymapheader.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 for mrc file header structure.
38  *
39  * \author Christian Blau <cblau@gwdg.de>
40  * \ingroup module_fileio
41  */
42
43 #include "gmxpre.h"
44
45 #include "gromacs/fileio/mrcdensitymapheader.h"
46
47 #include <array>
48 #include <vector>
49
50 #include <gmock/gmock.h>
51 #include <gtest/gtest.h>
52
53 #include "testutils/testasserts.h"
54 #include "testutils/testmatchers.h"
55
56 namespace gmx
57 {
58
59 TEST(MrcDensityMapHeaderTest, DataSizeIsZeroForDefaultHeader)
60 {
61     MrcDensityMapHeader header;
62     EXPECT_EQ(0, numberOfExpectedDataItems(header));
63 }
64
65 TEST(MrcDensityMapHeaderTest, DataSizeIsCorrect)
66 {
67     MrcDensityMapHeader header;
68     header.numColumnRowSection_ = { 1, 2, 3 };
69     EXPECT_EQ(6, numberOfExpectedDataItems(header));
70 }
71
72 TEST(MrcDensityMapHeaderTest, DataSizeThrowsWhenInvalid)
73 {
74     MrcDensityMapHeader header;
75     header.numColumnRowSection_ = { -1, 2, 3 };
76     EXPECT_THROW(numberOfExpectedDataItems(header), InternalError);
77 }
78 TEST(MrcDensityMapHeaderTest, GetsCorrectCoordinateTransformNoOriginGiven)
79 {
80     MrcDensityMapHeader header;
81
82     header.extent_                = { 100, 200, 300 };
83     header.columnRowSectionStart_ = { 50, 200, 0 };
84     header.cellLength_            = { 10, 20, 15 };
85
86     std::array<RVec, 2> testVectors = { RVec{ 0., 0., 0. }, RVec{ 1., 1., 1. } };
87     getCoordinateTransformationToLattice(header)(testVectors);
88
89     std::vector<RVec> expectedVectors = { { -50, -200, 0 }, { 50, -100, 200 } };
90     EXPECT_THAT(expectedVectors,
91                 testing::Pointwise(test::RVecEq(test::defaultFloatTolerance()), testVectors));
92 }
93
94 TEST(MrcDensityMapHeaderTest, GetsCorrectCoordinateTransformWithOriginDefined)
95 {
96     MrcDensityMapHeader header;
97     header.userDefinedFloat_[12] = 1.;
98     header.userDefinedFloat_[13] = 2.;
99     header.userDefinedFloat_[14] = 3.;
100     header.extent_               = { 100, 200, 300 };
101     // setting the columnRowSectionStart values that are to be ignored if userDefinedFloat_ is not zero
102     header.columnRowSectionStart_ = { 50, 200, 0 };
103     header.cellLength_            = { 10, 20, 15 };
104
105     std::array<RVec, 2> testVectors = { RVec{ 0., 0., 0. }, RVec{ 1., 1., 1. } };
106     getCoordinateTransformationToLattice(header)(testVectors);
107
108     std::vector<RVec> expectedVectors = { { -10, -20, -60 }, { 90, 80, 140 } };
109     EXPECT_THAT(expectedVectors,
110                 testing::Pointwise(test::RVecEq(test::defaultFloatTolerance()), testVectors));
111 }
112
113 TEST(MrcDensityMapHeaderTest, GetsCorrectCoordinateTransformWithStartValues)
114 {
115     MrcDensityMapHeader header;
116     header.userDefinedFloat_[12]  = 0;
117     header.userDefinedFloat_[13]  = 0;
118     header.userDefinedFloat_[14]  = 0;
119     header.extent_                = { 100, 200, 300 };
120     header.columnRowSectionStart_ = { 50, 200, 0 };
121     header.cellLength_            = { 10, 20, 15 };
122
123     std::array<RVec, 2> testVectors = { RVec{ 0., 0., 0. }, RVec{ 1., 1., 1. } };
124     getCoordinateTransformationToLattice(header)(testVectors);
125
126     std::vector<RVec> expectedVectors = { { -50, -200, 0 }, { 50, -100, 200 } };
127     EXPECT_THAT(expectedVectors,
128                 testing::Pointwise(test::RVecEq(test::defaultFloatTolerance()), testVectors));
129 }
130
131 TEST(MrcDensityMapHeaderTest, GetsCorrectExtents)
132 {
133     MrcDensityMapHeader header;
134     header.numColumnRowSection_ = { 100, 200, 300 };
135
136     const auto                      extents         = getDynamicExtents3D(header);
137     std::array<std::ptrdiff_t, DIM> expectedExtents = { 300, 200, 100 };
138     EXPECT_EQ(expectedExtents[XX], extents.extent(XX));
139     EXPECT_EQ(expectedExtents[YY], extents.extent(YY));
140     EXPECT_EQ(expectedExtents[ZZ], extents.extent(ZZ));
141 }
142
143 } // namespace gmx