2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2012,2013,2014, 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.
37 * Implements classes in moduletest.h.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_trajectoryanalysis
44 #include "moduletest.h"
50 #include "gromacs/trajectoryanalysis/analysismodule.h"
51 #include "gromacs/trajectoryanalysis/cmdlinerunner.h"
52 #include "gromacs/utility/file.h"
53 #include "gromacs/utility/stringutil.h"
55 #include "gromacs/analysisdata/tests/datatest.h"
56 #include "testutils/cmdlinetest.h"
57 #include "testutils/refdata.h"
58 #include "testutils/testasserts.h"
59 #include "testutils/testfilemanager.h"
66 /********************************************************************
67 * AbstractTrajectoryAnalysisModuleTestFixture::Impl
70 class AbstractTrajectoryAnalysisModuleTestFixture::Impl
76 : bCheck(true), tolerance(defaultRealTolerance())
81 FloatingPointTolerance tolerance;
86 OutputFileInfo(const char *option, const std::string &path)
87 : option(option), path(path)
95 typedef std::map<std::string, DatasetInfo> DatasetList;
96 typedef std::vector<OutputFileInfo> OutputFileList;
98 explicit Impl(AbstractTrajectoryAnalysisModuleTestFixture *parent);
100 TrajectoryAnalysisModule &module();
101 void ensureModuleCreated();
102 bool hasCheckedDatasets() const;
104 AbstractTrajectoryAnalysisModuleTestFixture &parent_;
105 TrajectoryAnalysisModulePointer module_;
106 TestReferenceData data_;
107 CommandLine cmdline_;
108 TestFileManager tempFiles_;
109 DatasetList datasets_;
110 OutputFileList outputFiles_;
111 bool bDatasetsIncluded_;
114 AbstractTrajectoryAnalysisModuleTestFixture::Impl::Impl(
115 AbstractTrajectoryAnalysisModuleTestFixture *parent)
116 : parent_(*parent), bDatasetsIncluded_(false)
118 cmdline_.append("module");
121 TrajectoryAnalysisModule &
122 AbstractTrajectoryAnalysisModuleTestFixture::Impl::module()
124 ensureModuleCreated();
129 AbstractTrajectoryAnalysisModuleTestFixture::Impl::ensureModuleCreated()
131 if (module_.get() == NULL)
133 module_ = parent_.createModule();
134 const std::vector<std::string> &datasetNames(module_->datasetNames());
136 std::vector<std::string>::const_iterator i;
137 for (i = datasetNames.begin(); i != datasetNames.end(); ++i)
139 datasets_[*i] = DatasetInfo();
145 AbstractTrajectoryAnalysisModuleTestFixture::Impl::hasCheckedDatasets() const
147 DatasetList::const_iterator dataset;
148 for (dataset = datasets_.begin(); dataset != datasets_.end(); ++dataset)
150 if (dataset->second.bCheck)
158 /********************************************************************
159 * AbstractTrajectoryAnalysisModuleTestFixture
162 AbstractTrajectoryAnalysisModuleTestFixture::AbstractTrajectoryAnalysisModuleTestFixture()
163 : impl_(new Impl(this))
167 AbstractTrajectoryAnalysisModuleTestFixture::~AbstractTrajectoryAnalysisModuleTestFixture()
172 AbstractTrajectoryAnalysisModuleTestFixture::setTopology(const char *filename)
174 impl_->cmdline_.append("-s");
175 impl_->cmdline_.append(TestFileManager::getInputFilePath(filename));
179 AbstractTrajectoryAnalysisModuleTestFixture::setTrajectory(const char *filename)
181 impl_->cmdline_.append("-f");
182 impl_->cmdline_.append(TestFileManager::getInputFilePath(filename));
186 AbstractTrajectoryAnalysisModuleTestFixture::setOutputFile(const char *option,
187 const char *filename)
189 std::string fullFilename = impl_->tempFiles_.getTemporaryFilePath(filename);
190 impl_->cmdline_.append(option);
191 impl_->cmdline_.append(fullFilename);
192 impl_->outputFiles_.push_back(Impl::OutputFileInfo(option, fullFilename));
196 AbstractTrajectoryAnalysisModuleTestFixture::setOutputFileNoTest(
197 const char *option, const char *extension)
199 std::string fullFilename = impl_->tempFiles_.getTemporaryFilePath(
200 formatString("%d.%s", impl_->cmdline_.argc(), extension));
201 impl_->cmdline_.append(option);
202 impl_->cmdline_.append(fullFilename);
206 AbstractTrajectoryAnalysisModuleTestFixture::includeDataset(const char *name)
208 impl_->ensureModuleCreated();
209 if (!impl_->bDatasetsIncluded_)
211 Impl::DatasetList::iterator i;
212 for (i = impl_->datasets_.begin(); i != impl_->datasets_.end(); ++i)
214 i->second.bCheck = false;
217 Impl::DatasetList::iterator dataset = impl_->datasets_.find(name);
218 const bool bFound = (dataset != impl_->datasets_.end());
219 GMX_RELEASE_ASSERT(bFound, "Attempted to include a non-existent dataset");
220 dataset->second.bCheck = true;
224 AbstractTrajectoryAnalysisModuleTestFixture::excludeDataset(const char *name)
226 impl_->ensureModuleCreated();
227 Impl::DatasetList::iterator dataset = impl_->datasets_.find(name);
228 const bool bFound = (dataset != impl_->datasets_.end());
229 GMX_RELEASE_ASSERT(bFound, "Attempted to exclude a non-existent dataset");
230 dataset->second.bCheck = false;
234 AbstractTrajectoryAnalysisModuleTestFixture::setDatasetTolerance(
235 const char *name, const FloatingPointTolerance &tolerance)
237 impl_->ensureModuleCreated();
238 Impl::DatasetList::iterator dataset = impl_->datasets_.find(name);
239 const bool bFound = (dataset != impl_->datasets_.end());
240 GMX_RELEASE_ASSERT(bFound, "Attempted to set a tolerance for a non-existent dataset");
241 dataset->second.tolerance = tolerance;
245 AbstractTrajectoryAnalysisModuleTestFixture::runTest(const CommandLine &args)
247 TrajectoryAnalysisModule &module = impl_->module();
248 // Skip first argument if it is the module name.
249 int firstArg = (args.arg(0)[0] == '-' ? 0 : 1);
250 for (int i = firstArg; i < args.argc(); ++i)
252 impl_->cmdline_.append(args.arg(i));
255 TestReferenceChecker rootChecker(impl_->data_.rootChecker());
257 rootChecker.checkString(args.toString(), "CommandLine");
259 if (impl_->hasCheckedDatasets())
261 TestReferenceChecker dataChecker(
262 rootChecker.checkCompound("OutputData", "Data"));
263 Impl::DatasetList::const_iterator dataset;
264 for (dataset = impl_->datasets_.begin();
265 dataset != impl_->datasets_.end();
268 if (dataset->second.bCheck)
270 const char *const name = dataset->first.c_str();
271 AbstractAnalysisData &data = module.datasetFromName(name);
272 AnalysisDataTestFixture::addReferenceCheckerModule(
273 dataChecker, name, &data, dataset->second.tolerance);
278 TrajectoryAnalysisCommandLineRunner runner(&module);
279 runner.setUseDefaultGroups(false);
281 EXPECT_NO_THROW_GMX(rc = runner.run(impl_->cmdline_.argc(), impl_->cmdline_.argv()));
284 if (!impl_->outputFiles_.empty())
286 TestReferenceChecker outputChecker(
287 rootChecker.checkCompound("OutputFiles", "Files"));
288 Impl::OutputFileList::const_iterator outfile;
289 for (outfile = impl_->outputFiles_.begin();
290 outfile != impl_->outputFiles_.end();
293 std::string output = File::readToString(outfile->path);
294 outputChecker.checkStringBlock(output, outfile->option.c_str());