Tidy: modernize-use-nullptr
[alexxy/gromacs.git] / src / gromacs / trajectoryanalysis / tests / moduletest.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012,2013,2014,2015,2017, 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  * Implements classes in moduletest.h.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \ingroup module_trajectoryanalysis
41  */
42 #include "gmxpre.h"
43
44 #include "moduletest.h"
45
46 #include <map>
47 #include <string>
48 #include <vector>
49
50 #include "gromacs/commandline/cmdlineoptionsmodule.h"
51 #include "gromacs/trajectoryanalysis/analysismodule.h"
52 #include "gromacs/trajectoryanalysis/cmdlinerunner.h"
53
54 #include "gromacs/analysisdata/tests/datatest.h"
55 #include "testutils/cmdlinetest.h"
56 #include "testutils/refdata.h"
57 #include "testutils/testasserts.h"
58
59 namespace gmx
60 {
61 namespace test
62 {
63
64 /********************************************************************
65  * AbstractTrajectoryAnalysisModuleTestFixture::Impl
66  */
67
68 class AbstractTrajectoryAnalysisModuleTestFixture::Impl
69 {
70     public:
71         struct DatasetInfo
72         {
73             DatasetInfo()
74                 : bCheck(true), tolerance(defaultRealTolerance())
75             {
76             }
77
78             bool                   bCheck;
79             FloatingPointTolerance tolerance;
80         };
81
82         typedef std::map<std::string, DatasetInfo> DatasetList;
83
84         explicit Impl(AbstractTrajectoryAnalysisModuleTestFixture *parent);
85
86         TrajectoryAnalysisModule &module();
87         void ensureModuleCreated();
88         bool hasCheckedDatasets() const;
89
90         AbstractTrajectoryAnalysisModuleTestFixture    &parent_;
91         TrajectoryAnalysisModulePointer                 module_;
92         DatasetList                                     datasets_;
93         bool                                            bDatasetsIncluded_;
94 };
95
96 AbstractTrajectoryAnalysisModuleTestFixture::Impl::Impl(
97         AbstractTrajectoryAnalysisModuleTestFixture *parent)
98     : parent_(*parent), bDatasetsIncluded_(false)
99 {
100 }
101
102 TrajectoryAnalysisModule &
103 AbstractTrajectoryAnalysisModuleTestFixture::Impl::module()
104 {
105     ensureModuleCreated();
106     return *module_;
107 }
108
109 void
110 AbstractTrajectoryAnalysisModuleTestFixture::Impl::ensureModuleCreated()
111 {
112     if (module_.get() == nullptr)
113     {
114         module_ = parent_.createModule();
115         const std::vector<std::string>          &datasetNames(module_->datasetNames());
116         datasets_.clear();
117         std::vector<std::string>::const_iterator i;
118         for (i = datasetNames.begin(); i != datasetNames.end(); ++i)
119         {
120             datasets_[*i] = DatasetInfo();
121         }
122     }
123 }
124
125 bool
126 AbstractTrajectoryAnalysisModuleTestFixture::Impl::hasCheckedDatasets() const
127 {
128     DatasetList::const_iterator dataset;
129     for (dataset = datasets_.begin(); dataset != datasets_.end(); ++dataset)
130     {
131         if (dataset->second.bCheck)
132         {
133             return true;
134         }
135     }
136     return false;
137 }
138
139 /********************************************************************
140  * AbstractTrajectoryAnalysisModuleTestFixture
141  */
142
143 AbstractTrajectoryAnalysisModuleTestFixture::AbstractTrajectoryAnalysisModuleTestFixture()
144     : impl_(new Impl(this))
145 {
146 }
147
148 AbstractTrajectoryAnalysisModuleTestFixture::~AbstractTrajectoryAnalysisModuleTestFixture()
149 {
150 }
151
152 void
153 AbstractTrajectoryAnalysisModuleTestFixture::setTopology(const char *filename)
154 {
155     setInputFile("-s", filename);
156 }
157
158 void
159 AbstractTrajectoryAnalysisModuleTestFixture::setTrajectory(const char *filename)
160 {
161     setInputFile("-f", filename);
162 }
163
164 void
165 AbstractTrajectoryAnalysisModuleTestFixture::includeDataset(const char *name)
166 {
167     impl_->ensureModuleCreated();
168     if (!impl_->bDatasetsIncluded_)
169     {
170         Impl::DatasetList::iterator i;
171         for (i = impl_->datasets_.begin(); i != impl_->datasets_.end(); ++i)
172         {
173             i->second.bCheck = false;
174         }
175     }
176     Impl::DatasetList::iterator dataset = impl_->datasets_.find(name);
177     const bool                  bFound  = (dataset != impl_->datasets_.end());
178     GMX_RELEASE_ASSERT(bFound, "Attempted to include a non-existent dataset");
179     dataset->second.bCheck = true;
180 }
181
182 void
183 AbstractTrajectoryAnalysisModuleTestFixture::excludeDataset(const char *name)
184 {
185     impl_->ensureModuleCreated();
186     Impl::DatasetList::iterator dataset = impl_->datasets_.find(name);
187     const bool                  bFound  = (dataset != impl_->datasets_.end());
188     GMX_RELEASE_ASSERT(bFound, "Attempted to exclude a non-existent dataset");
189     dataset->second.bCheck = false;
190 }
191
192 void
193 AbstractTrajectoryAnalysisModuleTestFixture::setDatasetTolerance(
194         const char *name, const FloatingPointTolerance &tolerance)
195 {
196     impl_->ensureModuleCreated();
197     Impl::DatasetList::iterator dataset = impl_->datasets_.find(name);
198     const bool                  bFound  = (dataset != impl_->datasets_.end());
199     GMX_RELEASE_ASSERT(bFound, "Attempted to set a tolerance for a non-existent dataset");
200     dataset->second.tolerance = tolerance;
201 }
202
203 void
204 AbstractTrajectoryAnalysisModuleTestFixture::runTest(const CommandLine &args)
205 {
206     TrajectoryAnalysisModule &module  = impl_->module();
207     CommandLine              &cmdline = commandLine();
208     cmdline.merge(args);
209
210     TestReferenceChecker rootChecker(this->rootChecker());
211     rootChecker.checkString(args.toString(), "CommandLine");
212
213     if (impl_->hasCheckedDatasets())
214     {
215         TestReferenceChecker               dataChecker(
216                 rootChecker.checkCompound("OutputData", "Data"));
217         Impl::DatasetList::const_iterator  dataset;
218         for (dataset = impl_->datasets_.begin();
219              dataset != impl_->datasets_.end();
220              ++dataset)
221         {
222             if (dataset->second.bCheck)
223             {
224                 const char *const     name = dataset->first.c_str();
225                 AbstractAnalysisData &data = module.datasetFromName(name);
226                 AnalysisDataTestFixture::addReferenceCheckerModule(
227                         dataChecker, name, &data, dataset->second.tolerance);
228             }
229         }
230     }
231
232     ICommandLineOptionsModulePointer runner(
233             TrajectoryAnalysisCommandLineRunner::createModule(std::move(impl_->module_)));
234     int rc = 0;
235     EXPECT_NO_THROW_GMX(rc = CommandLineTestHelper::runModuleDirect(std::move(runner), &cmdline));
236     EXPECT_EQ(0, rc);
237
238     checkOutputFiles();
239 }
240
241 } // namespace test
242 } // namespace gmx