Update instructions in containers.rst
[gromacs.git] / src / gromacs / coordinateio / tests / coordinate_test.h
blob98f278c38f793b07aa8a66409454d0532572275f
1 /*
2 * This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
35 /*!\file
36 * \libinternal
37 * \brief
38 * Helper classes for coordinatefile and coordinateio tests
40 * \author Paul Bauer <paul.bauer.q@gmail.com>
41 * \inlibraryapi
42 * \ingroup module_coordinateio
45 #ifndef GMX_COORDINATEIO_TESTS_COORDINATEIO_H
46 #define GMX_COORDINATEIO_TESTS_COORDINATEIO_H
48 #include <utility>
50 #include <gtest/gtest.h>
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"
64 #include "testutils/cmdlinetest.h"
65 #include "testutils/testasserts.h"
66 #include "testutils/testfilemanager.h"
68 namespace gmx
71 namespace test
74 /*!\brief
75 * Create minimal TrajectoryFrameWriter using the provided builder.
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.
84 inline TrajectoryFrameWriterPointer createMinimalTrajectoryFrameWriter(const std::string& filename,
85 const TopologyInformation& topology,
86 const Selection& selection,
87 OutputRequirements requirements)
89 return createTrajectoryFrameWriter(topology.mtop(), selection, filename,
90 topology.hasTopology() ? topology.copyAtoms() : nullptr,
91 requirements);
93 /*! \libinternal \brief
94 * Helper class for tests that need an initialized selection.
96 class ModuleSelection
98 public:
99 ModuleSelection() : manager_(&sc_)
101 options_.addManager(&manager_);
102 sc_.setReferencePosType("atom");
103 sc_.setOutputPosType("atom");
104 top_.fillFromInputFile(TestFileManager::getInputFilePath("lysozyme.pdb"));
105 sc_.setTopology(top_.mtop(), 0);
108 /*! \brief
109 * Method to add a valid selection option to the Module, or an invalid
110 * one for testing.
112 * \param[in] sel Selection to add option to.
113 * \param[in] useValid Set if the added selection should be valid for the module.
115 void addOptionForSelection(Selection* sel, bool useValid);
117 /*! \brief
118 * Set the actual values for the selection.
120 * \param[in] options Option to set values for.
121 * \param[in] sel Selection to use.
122 * \param[in] useValid Set if the added selection should be valid for the module.
124 void setSelectionOptionValues(Options* options, Selection* sel, bool useValid);
126 /*! \brief
127 * Get pointer to options to set values.
129 * \returns Pointer to options.
131 Options* getOption() { return &options_; }
133 private:
134 //! Selection collection used for handling test selection.
135 SelectionCollection sc_;
136 //! Selection manager for test selection.
137 SelectionOptionManager manager_;
138 //! Options manager for test selection input.
139 Options options_;
140 //! Topology information needed for test selection atoms.
141 TopologyInformation top_;
144 inline void ModuleSelection::addOptionForSelection(Selection* sel, bool useValid)
146 if (useValid)
148 options_.addOption(SelectionOption("sel").store(sel).onlyAtoms());
150 else
152 options_.addOption(SelectionOption("sel").store(sel).dynamicMask());
156 inline void ModuleSelection::setSelectionOptionValues(Options* options, Selection* sel, bool useValid)
158 OptionsAssigner assigner(options);
159 assigner.start();
160 assigner.startOption("sel");
161 if (useValid)
163 assigner.appendValue("all");
165 else
167 assigner.appendValue("res_cog of all");
169 assigner.finishOption();
170 assigner.finish();
172 ASSERT_TRUE(sel->isValid());
173 EXPECT_NO_THROW(sc_.compile());
176 /*! \libinternal \brief
177 * Test fixture to test matching file types for modules.
179 class ModuleTest : public gmx::test::CommandLineTestBase, public ::testing::WithParamInterface<const char*>
181 public:
182 /*! \brief
183 * Run the builder to create an TrajectoryFrameWriter during tests.
185 * \param[in] filename Name for output file, to determine filetype during construction.
186 * \param[in] requirements Requirements for adding to the object.
187 * \returns The newly created object.
189 TrajectoryFrameWriterPointer runTest(const char* filename, const OutputRequirements& requirements)
191 return createMinimalTrajectoryFrameWriter(filename, dummyTopology_, dummySelection_, requirements);
193 //! Add topology information to test if needed.
194 void addTopology()
196 dummyTopology_.fillFromInputFile(TestFileManager::getInputFilePath("lysozyme.pdb"));
198 //! Dummy topology to use to create CoordinateFile.
199 TopologyInformation dummyTopology_;
200 //! Dummy selection.
201 Selection dummySelection_;
204 } // namespace test
206 } // namespace gmx
208 #endif