Correct nrdf for 1D/2D systems
[gromacs/AngularHB.git] / src / testutils / stringtest.cpp
blob7ce6008345f5ee323fcd8ee11bf075a04f09d39c
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2012,2013,2014,2015, 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 /*! \internal \file
36 * \brief
37 * Implements gmx::test::StringTestBase.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_testutils
42 #include "gmxpre.h"
44 #include "stringtest.h"
46 #include <algorithm>
47 #include <string>
48 #include <utility>
49 #include <vector>
51 #include <boost/scoped_ptr.hpp>
53 #include "gromacs/options/basicoptions.h"
54 #include "gromacs/options/options.h"
55 #include "gromacs/utility/exceptions.h"
56 #include "gromacs/utility/file.h"
57 #include "gromacs/utility/fileredirector.h"
59 #include "testutils/refdata.h"
60 #include "testutils/testexceptions.h"
61 #include "testutils/testfilemanager.h"
62 #include "testutils/testoptions.h"
64 namespace gmx
66 namespace test
69 namespace
71 //! Stores the -stdout flag value to print out values instead of checking them.
72 bool g_bWriteToStdOut = false;
74 /*! \brief
75 * Helper for checking a block of text, e.g., implementing the `-stdout`
76 * option.
78 * \ingroup module_testutils
80 void checkTextImpl(TestReferenceChecker *checker, const std::string &text,
81 const char *id)
83 if (g_bWriteToStdOut)
85 printf("%s:\n", id);
86 printf("%s[END]\n", text.c_str());
88 else
90 checker->checkStringBlock(text, id);
96 // TODO: Only add this option to those test binaries that actually need it
97 // (depending on the linker, it may or may not appear right now),
98 // or replace by a generic mechanism in TestReferenceData.
99 //! \cond
100 GMX_TEST_OPTIONS(StringTestOptions, options)
102 options->addOption(
103 BooleanOption("stdout")
104 .store(&g_bWriteToStdOut)
105 .description("Print the test string to stdout instead of checking against reference data"));
107 //! \endcond
109 /********************************************************************
110 * TestFileOutputRedirector
113 /*! \internal
114 * \brief
115 * Implementation of FileOutputRedirectorInterface for tests.
117 * This class redirects all output files to temporary files managed by a
118 * TestFileManager, and supports checking the contents of these files using the
119 * reference data framework.
121 * \ingroup module_testutils
123 class TestFileOutputRedirector : public FileOutputRedirectorInterface
125 public:
126 //! Initializes the redirector with the given file manager.
127 explicit TestFileOutputRedirector(TestFileManager *fileManager)
128 : fileManager_(*fileManager)
132 virtual File &standardOutput()
134 if (!stdoutFile_)
136 const std::string path = fileManager_.getTemporaryFilePath("stdout.txt");
137 stdoutFile_.reset(new File(path, "w"));
138 fileList_.push_back(FileListEntry("<stdout>", path));
140 return *stdoutFile_;
142 virtual FileInitializer openFileForWriting(const char *filename)
144 std::string suffix = filename;
145 std::replace(suffix.begin(), suffix.end(), '/', '_');
146 const std::string path = fileManager_.getTemporaryFilePath(suffix);
147 fileList_.push_back(FileListEntry(filename, path));
148 return FileInitializer(fileList_.back().second.c_str(), "w");
151 /*! \brief
152 * Checks the contents of all redirected files.
154 void checkRedirectedFiles(TestReferenceChecker *checker)
156 if (stdoutFile_)
158 stdoutFile_->close();
159 stdoutFile_.reset();
161 std::vector<FileListEntry>::const_iterator i;
162 for (i = fileList_.begin(); i != fileList_.end(); ++i)
164 const std::string text = File::readToString(i->second);
165 checkTextImpl(checker, text, i->first.c_str());
169 private:
170 typedef std::pair<std::string, std::string> FileListEntry;
172 TestFileManager &fileManager_;
173 boost::scoped_ptr<File> stdoutFile_;
174 std::vector<FileListEntry> fileList_;
177 /********************************************************************
178 * StringTestBase::Impl
181 class StringTestBase::Impl
183 public:
184 TestReferenceData data_;
185 boost::scoped_ptr<TestReferenceChecker> checker_;
186 boost::scoped_ptr<TestFileOutputRedirector> redirector_;
189 /********************************************************************
190 * StringTestBase
193 StringTestBase::StringTestBase()
194 : impl_(new Impl)
198 StringTestBase::~StringTestBase()
202 FileOutputRedirectorInterface &
203 StringTestBase::initOutputRedirector(TestFileManager *fileManager)
205 if (impl_->redirector_)
207 GMX_THROW(TestException("initOutputRedirector() called more than once"));
209 impl_->redirector_.reset(new TestFileOutputRedirector(fileManager));
210 return *impl_->redirector_;
213 TestReferenceChecker &
214 StringTestBase::checker()
216 if (!impl_->checker_)
218 impl_->checker_.reset(new TestReferenceChecker(impl_->data_.rootChecker()));
220 return *impl_->checker_;
223 void
224 StringTestBase::checkText(const std::string &text, const char *id)
226 checkTextImpl(&checker(), text, id);
229 void
230 StringTestBase::checkFileContents(const std::string &filename, const char *id)
232 const std::string text = File::readToString(filename);
233 checkText(text, id);
236 void
237 StringTestBase::checkRedirectedOutputFiles()
239 if (!impl_->redirector_)
241 GMX_THROW(TestException("initOutputRedirector() not called"));
243 impl_->redirector_->checkRedirectedFiles(&checker());
246 } // namespace test
247 } // namespace gmx