Use more forward declarations to removed header dependencies
[alexxy/gromacs.git] / src / gromacs / coordinateio / tests / setatoms.cpp
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 /*!\internal
36  * \file
37  * \brief
38  * Tests for gmx::SetAtoms
39  *
40  * \author Paul Bauer <paul.bauer.q@gmail.com>
41  * \ingroup module_coordinateio
42  */
43
44
45 #include "gmxpre.h"
46
47 #include <utility>
48
49 #include "gromacs/coordinateio/outputadapters/setatoms.h"
50 #include "gromacs/fileio/trxio.h"
51 #include "gromacs/trajectory/trajectoryframe.h"
52 #include "gromacs/trajectoryanalysis/topologyinformation.h"
53
54 #include "gromacs/coordinateio/tests/coordinate_test.h"
55 #include "testutils/testfilemanager.h"
56
57 namespace gmx
58 {
59
60 namespace test
61 {
62
63 /*!\brief
64  * Test fixture to prepare a setatoms object to pass data through.
65  */
66 class SetAtomsTest : public gmx::test::CommandLineTestBase
67 {
68 public:
69     //! Prepare test fixture topology to have atoms available.
70     SetAtomsTest()
71     {
72         topology_.fillFromInputFile(TestFileManager::getInputFilePath("lysozyme.pdb"));
73     }
74
75     /*! \brief
76      * Get access to the method for changing atom information.
77      *
78      * \param[in] type Type to use for setting up method.
79      * \param[in] haveAtomsAvailable Decide if method shouöd have atoms stored or not.
80      */
81     SetAtoms* setAtoms(ChangeAtomsType type, bool haveAtomsAvailable)
82     {
83         if (!setAtoms_)
84         {
85             if (haveAtomsAvailable)
86             {
87                 setAtoms_ = std::make_unique<SetAtoms>(type, topology_.copyAtoms());
88             }
89             else
90             {
91                 setAtoms_ = std::make_unique<SetAtoms>(type, nullptr);
92             }
93         }
94         return setAtoms_.get();
95     }
96     //! Get access to a t_atoms struct to use in the dummy t_trxframe.
97     t_atoms* getAtomsCopy() { return const_cast<t_atoms*>(topology_.atoms()); }
98
99 private:
100     //! Object to use for tests
101     SetAtomsPointer setAtoms_;
102     //! Local topology to get atoms from
103     TopologyInformation topology_;
104 };
105
106 TEST_F(SetAtomsTest, RemovesExistingAtoms)
107 {
108     t_trxframe frame;
109     clear_trxframe(&frame, true);
110
111     frame.bAtoms = true;
112     frame.atoms  = getAtomsCopy();
113
114     EXPECT_NE(frame.atoms, nullptr);
115
116     SetAtoms* method = setAtoms(ChangeAtomsType::Never, true);
117     EXPECT_NO_THROW(method->processFrame(0, &frame));
118
119     EXPECT_FALSE(frame.bAtoms);
120     EXPECT_EQ(frame.atoms, nullptr);
121 }
122
123 TEST_F(SetAtomsTest, AddsNewAtoms)
124 {
125     t_trxframe frame;
126     clear_trxframe(&frame, true);
127
128     frame.bAtoms = false;
129     frame.atoms  = nullptr;
130
131     SetAtoms* method = setAtoms(ChangeAtomsType::AlwaysFromStructure, true);
132     EXPECT_NO_THROW(method->processFrame(0, &frame));
133
134     EXPECT_TRUE(frame.bAtoms);
135     EXPECT_NE(frame.atoms, nullptr);
136 }
137
138 TEST_F(SetAtomsTest, ThrowsOnRequiredAtomsNotAvailable)
139 {
140     t_trxframe frame;
141     clear_trxframe(&frame, true);
142
143     frame.bAtoms = false;
144     frame.atoms  = nullptr;
145
146     SetAtoms* method = setAtoms(ChangeAtomsType::Always, false);
147     EXPECT_THROW(method->processFrame(0, &frame), InconsistentInputError);
148 }
149
150 TEST_F(SetAtomsTest, WillUseOldAtomsWhenNoNewAvailable)
151 {
152     t_trxframe frame;
153     clear_trxframe(&frame, true);
154
155     frame.bAtoms = true;
156     frame.atoms  = getAtomsCopy();
157
158     EXPECT_NE(frame.atoms, nullptr);
159
160     SetAtoms* method = setAtoms(ChangeAtomsType::Always, false);
161     EXPECT_NO_THROW(method->processFrame(0, &frame));
162 }
163
164 TEST_F(SetAtomsTest, ThrowsWhenUserAtomReplacementNotPossible)
165 {
166     t_trxframe frame;
167     clear_trxframe(&frame, true);
168
169     frame.bAtoms = true;
170     frame.atoms  = getAtomsCopy();
171
172     EXPECT_NE(frame.atoms, nullptr);
173
174     SetAtoms* method = setAtoms(ChangeAtomsType::AlwaysFromStructure, false);
175     EXPECT_THROW(method->processFrame(0, &frame), InconsistentInputError);
176 }
177
178 } // namespace test
179
180 } // namespace gmx