Apply re-formatting to C++ in src/ tree.
[alexxy/gromacs.git] / src / gromacs / coordinateio / tests / coordinate_test.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 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 /*!\file
36  * \libinternal
37  * \brief
38  * Helper classes for coordinatefile and coordinateio tests
39  *
40  * \author Paul Bauer <paul.bauer.q@gmail.com>
41  * \inlibraryapi
42  * \ingroup module_coordinateio
43  */
44
45 #ifndef GMX_COORDINATEIO_TESTS_COORDINATEIO_H
46 #define GMX_COORDINATEIO_TESTS_COORDINATEIO_H
47
48 #include <utility>
49
50 #include <gtest/gtest.h>
51
52 #include "gromacs/coordinateio/coordinatefile.h"
53 #include "gromacs/coordinateio/ioutputadapter.h"
54 #include "gromacs/coordinateio/requirements.h"
55 #include "gromacs/options/options.h"
56 #include "gromacs/options/optionsassigner.h"
57 #include "gromacs/selection/selectioncollection.h"
58 #include "gromacs/selection/selectionoption.h"
59 #include "gromacs/selection/selectionoptionmanager.h"
60 #include "gromacs/trajectoryanalysis/topologyinformation.h"
61 #include "gromacs/utility/exceptions.h"
62 #include "gromacs/utility/smalloc.h"
63
64 #include "testutils/cmdlinetest.h"
65 #include "testutils/testasserts.h"
66 #include "testutils/testfilemanager.h"
67
68 namespace gmx
69 {
70
71 namespace test
72 {
73
74 /*!\brief
75  * Create minimal TrajectoryFrameWriter using the provided builder.
76  *
77  * \param[in] filename      Name of file to create object for.
78  * \param[in] topology      Reference to input top.
79  * \param[in] selection     Reference to input selection.
80  * \param[in] requirements  Requirements for constructing OutputManagar.
81  * \throws InconsistentInputError When builder can not create the CoordinateFile.
82  * \returns unique_ptr to new CoordinateFile object.
83  */
84 inline TrajectoryFrameWriterPointer createMinimalTrajectoryFrameWriter(const std::string& filename,
85                                                                        const TopologyInformation& topology,
86                                                                        const Selection& selection,
87                                                                        OutputRequirements requirements)
88 {
89     return createTrajectoryFrameWriter(topology.mtop(),
90                                        selection,
91                                        filename,
92                                        topology.hasTopology() ? topology.copyAtoms() : nullptr,
93                                        requirements);
94 }
95 /*! \libinternal \brief
96  * Helper class for tests that need an initialized selection.
97  */
98 class ModuleSelection
99 {
100 public:
101     ModuleSelection() : manager_(&sc_)
102     {
103         options_.addManager(&manager_);
104         sc_.setReferencePosType("atom");
105         sc_.setOutputPosType("atom");
106         top_.fillFromInputFile(TestFileManager::getInputFilePath("lysozyme.pdb"));
107         sc_.setTopology(top_.mtop(), 0);
108     }
109
110     /*! \brief
111      * Method to add a valid selection option to the Module, or an invalid
112      * one for testing.
113      *
114      * \param[in] sel Selection to add option to.
115      * \param[in] useValid Set if the added selection should be valid for the module.
116      */
117     void addOptionForSelection(Selection* sel, bool useValid);
118
119     /*! \brief
120      * Set the actual values for the selection.
121      *
122      * \param[in] options Option to set values for.
123      * \param[in] sel Selection to use.
124      * \param[in] useValid Set if the added selection should be valid for the module.
125      */
126     void setSelectionOptionValues(Options* options, Selection* sel, bool useValid);
127
128     /*! \brief
129      * Get pointer to options to set values.
130      *
131      * \returns Pointer to options.
132      */
133     Options* getOption() { return &options_; }
134
135 private:
136     //! Selection collection used for handling test selection.
137     SelectionCollection sc_;
138     //! Selection manager for test selection.
139     SelectionOptionManager manager_;
140     //! Options manager for test selection input.
141     Options options_;
142     //! Topology information needed for test selection atoms.
143     TopologyInformation top_;
144 };
145
146 inline void ModuleSelection::addOptionForSelection(Selection* sel, bool useValid)
147 {
148     if (useValid)
149     {
150         options_.addOption(SelectionOption("sel").store(sel).onlyAtoms());
151     }
152     else
153     {
154         options_.addOption(SelectionOption("sel").store(sel).dynamicMask());
155     }
156 }
157
158 inline void ModuleSelection::setSelectionOptionValues(Options* options, Selection* sel, bool useValid)
159 {
160     OptionsAssigner assigner(options);
161     assigner.start();
162     assigner.startOption("sel");
163     if (useValid)
164     {
165         assigner.appendValue("all");
166     }
167     else
168     {
169         assigner.appendValue("res_cog of all");
170     }
171     assigner.finishOption();
172     assigner.finish();
173
174     ASSERT_TRUE(sel->isValid());
175     EXPECT_NO_THROW(sc_.compile());
176 }
177
178 /*! \libinternal \brief
179  * Test fixture to test matching file types for modules.
180  */
181 class ModuleTest : public gmx::test::CommandLineTestBase, public ::testing::WithParamInterface<const char*>
182 {
183 public:
184     /*! \brief
185      * Run the builder to create an TrajectoryFrameWriter during tests.
186      *
187      * \param[in] filename Name for output file, to determine filetype during construction.
188      * \param[in] requirements Requirements for adding to the object.
189      * \returns The newly created object.
190      */
191     TrajectoryFrameWriterPointer runTest(const char* filename, const OutputRequirements& requirements)
192     {
193         return createMinimalTrajectoryFrameWriter(filename, dummyTopology_, dummySelection_, requirements);
194     }
195     //! Add topology information to test if needed.
196     void addTopology()
197     {
198         dummyTopology_.fillFromInputFile(TestFileManager::getInputFilePath("lysozyme.pdb"));
199     }
200     //! Dummy topology to use to create CoordinateFile.
201     TopologyInformation dummyTopology_;
202     //! Dummy selection.
203     Selection dummySelection_;
204 };
205
206 } // namespace test
207
208 } // namespace gmx
209
210 #endif