2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2012,2013, 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
42 #include "moduletest.h"
48 #include "gromacs/trajectoryanalysis/analysismodule.h"
49 #include "gromacs/trajectoryanalysis/cmdlinerunner.h"
50 #include "gromacs/utility/file.h"
52 #include "testutils/cmdlinetest.h"
53 #include "testutils/datatest.h"
54 #include "testutils/refdata.h"
55 #include "testutils/testasserts.h"
56 #include "testutils/testfilemanager.h"
63 /********************************************************************
64 * AbstractTrajectoryAnalysisModuleTestFixture::Impl
67 class AbstractTrajectoryAnalysisModuleTestFixture::Impl
72 OutputFileInfo(const char *option, const std::string &path)
73 : option(option), path(path)
81 typedef std::set<std::string> DatasetNames;
82 typedef std::vector<OutputFileInfo> OutputFileList;
84 explicit Impl(AbstractTrajectoryAnalysisModuleTestFixture *parent);
86 TrajectoryAnalysisModule &module();
87 void ensureModuleCreated();
89 AbstractTrajectoryAnalysisModuleTestFixture &parent_;
90 TrajectoryAnalysisModulePointer module_;
91 TestReferenceData data_;
93 TestFileManager tempFiles_;
94 DatasetNames moduleDatasets_;
95 DatasetNames outputDatasets_;
96 OutputFileList outputFiles_;
97 bool bDatasetsIncluded_;
100 AbstractTrajectoryAnalysisModuleTestFixture::Impl::Impl(
101 AbstractTrajectoryAnalysisModuleTestFixture *parent)
102 : parent_(*parent), bDatasetsIncluded_(false)
104 cmdline_.append("module");
107 TrajectoryAnalysisModule &
108 AbstractTrajectoryAnalysisModuleTestFixture::Impl::module()
110 ensureModuleCreated();
115 AbstractTrajectoryAnalysisModuleTestFixture::Impl::ensureModuleCreated()
117 if (module_.get() == NULL)
119 module_ = parent_.createModule();
120 const std::vector<std::string> &datasetNames(module_->datasetNames());
121 moduleDatasets_.clear();
122 moduleDatasets_.insert(datasetNames.begin(), datasetNames.end());
123 outputDatasets_ = moduleDatasets_;
127 /********************************************************************
128 * AbstractTrajectoryAnalysisModuleTestFixture
131 AbstractTrajectoryAnalysisModuleTestFixture::AbstractTrajectoryAnalysisModuleTestFixture()
132 : impl_(new Impl(this))
136 AbstractTrajectoryAnalysisModuleTestFixture::~AbstractTrajectoryAnalysisModuleTestFixture()
141 AbstractTrajectoryAnalysisModuleTestFixture::setTopology(const char *filename)
143 impl_->cmdline_.append("-s");
144 impl_->cmdline_.append(TestFileManager::getInputFilePath(filename));
148 AbstractTrajectoryAnalysisModuleTestFixture::setTrajectory(const char *filename)
150 impl_->cmdline_.append("-f");
151 impl_->cmdline_.append(TestFileManager::getInputFilePath(filename));
155 AbstractTrajectoryAnalysisModuleTestFixture::setOutputFile(const char *option,
156 const char *filename)
158 std::string fullFilename = impl_->tempFiles_.getTemporaryFilePath(filename);
159 impl_->cmdline_.append(option);
160 impl_->cmdline_.append(fullFilename);
161 impl_->outputFiles_.push_back(Impl::OutputFileInfo(option, fullFilename));
165 AbstractTrajectoryAnalysisModuleTestFixture::includeDataset(const char *name)
167 impl_->ensureModuleCreated();
168 if (!impl_->bDatasetsIncluded_)
170 impl_->outputDatasets_.clear();
172 bool bFound = (impl_->moduleDatasets_.find(name) != impl_->moduleDatasets_.end());
173 GMX_RELEASE_ASSERT(bFound, "Attempted to include a non-existent dataset");
174 impl_->outputDatasets_.insert(name);
178 AbstractTrajectoryAnalysisModuleTestFixture::excludeDataset(const char *name)
180 impl_->ensureModuleCreated();
181 bool bFound = (impl_->outputDatasets_.erase(name) > 0);
182 GMX_RELEASE_ASSERT(bFound, "Attempted to exclude a non-existent dataset");
186 AbstractTrajectoryAnalysisModuleTestFixture::runTest(const CommandLine &args)
188 TrajectoryAnalysisModule &module = impl_->module();
189 // Skip first argument if it is the module name.
190 int firstArg = (args.arg(0)[0] == '-' ? 0 : 1);
191 for (int i = firstArg; i < args.argc(); ++i)
193 impl_->cmdline_.append(args.arg(i));
196 TestReferenceChecker rootChecker(impl_->data_.rootChecker());
198 rootChecker.checkString(args.toString(), "CommandLine");
200 if (!impl_->outputDatasets_.empty())
202 TestReferenceChecker dataChecker(
203 rootChecker.checkCompound("OutputData", "Data"));
204 Impl::DatasetNames::const_iterator dataset;
205 for (dataset = impl_->outputDatasets_.begin();
206 dataset != impl_->outputDatasets_.end();
209 const char *name = dataset->c_str();
210 AbstractAnalysisData &dataset = module.datasetFromName(name);
211 AnalysisDataTestFixture::addReferenceCheckerModule(
212 dataChecker, name, &dataset);
216 TrajectoryAnalysisCommandLineRunner runner(&module);
217 runner.setUseDefaultGroups(false);
219 EXPECT_NO_THROW_GMX(rc = runner.run(impl_->cmdline_.argc(), impl_->cmdline_.argv()));
222 if (!impl_->outputFiles_.empty())
224 TestReferenceChecker outputChecker(
225 rootChecker.checkCompound("OutputFiles", "Files"));
226 Impl::OutputFileList::const_iterator outfile;
227 for (outfile = impl_->outputFiles_.begin();
228 outfile != impl_->outputFiles_.end();
231 std::string output = File::readToString(outfile->path);
232 outputChecker.checkStringBlock(output, outfile->option.c_str());