Apply clang-format to source tree
[alexxy/gromacs.git] / src / gromacs / fileio / tests / mrcdensitymap.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 data structure.
38  *
39  * \author Christian Blau <blau@kth.se>
40  * \ingroup module_fileio
41  */
42 #include "gmxpre.h"
43
44 #include "gromacs/fileio/mrcdensitymap.h"
45
46 #include <string>
47
48 #include <gmock/gmock.h>
49 #include <gtest/gtest.h>
50
51 #include "gromacs/fileio/mrcdensitymapheader.h"
52 #include "gromacs/utility/inmemoryserializer.h"
53
54 #include "testutils/refdata.h"
55 #include "testutils/testasserts.h"
56 #include "testutils/testfilemanager.h"
57
58 namespace gmx
59 {
60 namespace test
61 {
62 namespace
63 {
64
65 TEST(MrcDensityMap, RoundTripIsIdempotent)
66 {
67     // write header and data to serializer, store the serialized data
68
69     MrcDensityMapHeader header{};
70     header.numColumnRowSection_ = { 1, 1, 1 };
71
72     std::vector<float> data(numberOfExpectedDataItems(header));
73
74     MrcDensityMapOfFloatWriter mrcDensityMapWriter(header, data);
75     InMemorySerializer         serializer;
76     mrcDensityMapWriter.write(&serializer);
77
78     const auto serializedFile = serializer.finishAndGetBuffer();
79
80     // read and write again
81     InMemoryDeserializer       deserializer(serializedFile, false);
82     MrcDensityMapOfFloatReader mrcDensityMapReader(&deserializer);
83
84     MrcDensityMapOfFloatWriter writerOfDeserializedOutput(mrcDensityMapReader.header(),
85                                                           mrcDensityMapReader.constView());
86     writerOfDeserializedOutput.write(&serializer);
87
88     const auto roundTripResult = serializer.finishAndGetBuffer();
89
90     // compare serialized data after reading and writing again to serialized data after one-time serialization
91     EXPECT_THAT(serializedFile, testing::Pointwise(testing::Eq(), roundTripResult));
92 }
93
94 TEST(MrcDensityMap, ThrowsFileIOErrorWhenFileNotPresent)
95 {
96     EXPECT_THROW(MrcDensityMapOfFloatFromFileReader(""), FileIOError);
97 }
98
99 TEST(MrcDensityMap, ReadsCoordinateTransformationFromFile)
100 {
101     RVec coordinate1(0, 0, 0);
102     RVec coordinate2(0, 0, 1);
103     RVec coordinate3(1, 0, 0);
104     RVec coordinate4(0, 1, 0);
105
106     MrcDensityMapOfFloatFromFileReader mrcFileReader(
107             TestFileManager::getInputFilePath("ellipsoid-density.mrc"));
108     TranslateAndScale coordinateTransformation(mrcFileReader.transformationToDensityLattice());
109
110     coordinateTransformation({ &coordinate1, &coordinate1 + 1 });
111     coordinateTransformation({ &coordinate2, &coordinate2 + 1 });
112
113     EXPECT_REAL_EQ(0, coordinate1[XX]);
114     EXPECT_REAL_EQ(-2, coordinate1[YY]);
115     EXPECT_REAL_EQ(0, coordinate1[ZZ]);
116
117     EXPECT_REAL_EQ(0, coordinate2[XX]);
118     EXPECT_REAL_EQ(-2, coordinate2[YY]);
119     EXPECT_REAL_EQ(1.25, coordinate2[ZZ]);
120
121     EXPECT_REAL_EQ(1, coordinate3[XX]);
122     EXPECT_REAL_EQ(0, coordinate3[YY]);
123     EXPECT_REAL_EQ(0, coordinate3[ZZ]);
124
125     EXPECT_REAL_EQ(0, coordinate4[XX]);
126     EXPECT_REAL_EQ(1, coordinate4[YY]);
127     EXPECT_REAL_EQ(0, coordinate4[ZZ]);
128 }
129
130 TEST(MrcDensityMap, ReadsDensityDataFromFile)
131 {
132     MrcDensityMapOfFloatFromFileReader mrcFileReader(
133             TestFileManager::getInputFilePath("ellipsoid-density.mrc"));
134     const auto densityData = mrcFileReader.densityDataCopy();
135
136     TestReferenceData    refData;
137     TestReferenceChecker checker(refData.rootChecker());
138     checker.checkSequence(begin(densityData.asConstView()), end(densityData.asConstView()),
139                           "data ellipsoid density");
140 }
141
142 } // namespace
143 } // namespace test
144 } // namespace gmx