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