Apply clang-format to source tree
[alexxy/gromacs.git] / src / gromacs / tools / tests / report_methods.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2018,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 functionality of the "report" tool to write system information.
38  *
39  * \author
40  */
41 #include "gmxpre.h"
42
43 #include "gromacs/tools/report_methods.h"
44
45 #include "gromacs/fileio/tpxio.h"
46 #include "gromacs/gmxpreprocess/grompp.h"
47 #include "gromacs/mdtypes/state.h"
48 #include "gromacs/utility/exceptions.h"
49 #include "gromacs/utility/stringstream.h"
50 #include "gromacs/utility/textwriter.h"
51
52 #include "testutils/cmdlinetest.h"
53 #include "testutils/refdata.h"
54 #include "testutils/testfilemanager.h"
55 #include "testutils/textblockmatchers.h"
56 #include "testutils/tprfilegenerator.h"
57
58 namespace gmx
59 {
60
61 namespace test
62 {
63
64 class ReportMethodsTest : public ::testing::Test
65 {
66 protected:
67     // TODO this is changed in newer googletest versions
68     //! Prepare shared resources.
69     static void SetUpTestCase() { s_tprFileHandle = new TprAndFileManager("lysozyme"); }
70     //! Clean up shared resources.
71     static void TearDownTestCase()
72     {
73         delete s_tprFileHandle;
74         s_tprFileHandle = nullptr;
75     }
76     //! Storage for opened file handles.
77     static TprAndFileManager* s_tprFileHandle;
78 };
79
80 TprAndFileManager* ReportMethodsTest::s_tprFileHandle = nullptr;
81
82 /*! \brief
83  * Reads a tpr for the test.
84  *
85  * Reads a tpr to have access to the system information for print out.
86  *
87  * \param[in] tprHandle Handle to the tpr to red in.
88  * \param[in] mtop Pointer to topology datastructure to populate.
89  * \param[in] ir Pointer to inputrec to populate.
90  */
91 static void readTprInput(const TprAndFileManager* tprHandle, gmx_mtop_t* mtop, t_inputrec* ir)
92 {
93     // read tpr into variables needed for output
94     {
95         t_state state;
96         read_tpx_state(tprHandle->tprName().c_str(), ir, &state, mtop);
97     }
98 }
99
100 TEST_F(ReportMethodsTest, WritesCorrectHeadersFormated)
101 {
102     gmx::StringOutputStream stream;
103     gmx::TextWriter         test(&stream);
104     writeHeader(&test, "Test", "Test", true);
105
106     std::string referenceString = "\\Test{Test}\n";
107
108     EXPECT_EQ(stream.toString(), referenceString);
109 }
110 TEST_F(ReportMethodsTest, WritesCorrectHeadersUnformatted)
111 {
112     gmx::StringOutputStream stream;
113     gmx::TextWriter         test(&stream);
114     writeHeader(&test, "Test", "Test", false);
115
116     std::string referenceString = "Test: Test\n";
117
118     EXPECT_EQ(stream.toString(), referenceString);
119 }
120
121 TEST_F(ReportMethodsTest, WritesCorrectInformation)
122 {
123     gmx_mtop_t top;
124     t_inputrec ir;
125     EXPECT_NO_THROW(readTprInput(s_tprFileHandle, &top, &ir));
126
127     // Test both formatted and unformatted output in the same test
128     {
129         gmx::StringOutputStream stream;
130         gmx::TextWriter         test(&stream);
131
132         writeSystemInformation(&test, top, true);
133
134         std::string referenceString =
135                 "\\subsection{Simulation system}\n"
136                 "A system of 1 molecules (156 atoms) was simulated.\n";
137
138         EXPECT_EQ(stream.toString(), referenceString);
139     }
140
141     {
142         gmx::StringOutputStream stream;
143         gmx::TextWriter         test(&stream);
144
145         writeSystemInformation(&test, top, false);
146
147         std::string referenceString =
148                 "subsection: Simulation system\n"
149                 "A system of 1 molecules (156 atoms) was simulated.\n";
150
151         EXPECT_EQ(stream.toString(), referenceString);
152     }
153
154     // Test for correct parameter information as well
155     {
156         gmx::StringOutputStream stream;
157         gmx::TextWriter         test(&stream);
158
159         writeParameterInformation(&test, ir, true);
160
161         std::string referenceString =
162                 "\\subsection{Simulation settings}\n"
163                 "A total of 0 ns were simulated with a time step of 1 fs.\n"
164                 "Neighbor searching was performed every 10 steps.\n"
165                 "The Cut-off algorithm was used for electrostatic interactions.\n"
166                 "with a cut-off of 1 nm.\nA single cut-off of 1.1 nm was used "
167                 "for Van der Waals interactions.\n";
168
169         EXPECT_EQ(stream.toString(), referenceString);
170     }
171 }
172
173 TEST_F(ReportMethodsTest, ToolEndToEndTest)
174 {
175     const char* const command[] = { "report-methods", "-s", s_tprFileHandle->tprName().c_str() };
176     CommandLine       cmdline(command);
177     EXPECT_EQ(0, gmx::test::CommandLineTestHelper::runModuleFactory(&gmx::ReportMethodsInfo::create,
178                                                                     &cmdline));
179 }
180
181 } // namespace test
182
183 } // namespace gmx