Merge release-5-0 into master
[alexxy/gromacs.git] / src / testutils / stringtest.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
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 gmx::test::StringTestBase.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \ingroup module_testutils
41  */
42 #include "gmxpre.h"
43
44 #include "stringtest.h"
45
46 #include <algorithm>
47 #include <string>
48 #include <utility>
49 #include <vector>
50
51 #include <boost/scoped_ptr.hpp>
52
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"
58
59 #include "testutils/refdata.h"
60 #include "testutils/testexceptions.h"
61 #include "testutils/testfilemanager.h"
62 #include "testutils/testoptions.h"
63
64 namespace gmx
65 {
66 namespace test
67 {
68
69 namespace
70 {
71 //! Stores the -stdout flag value to print out values instead of checking them.
72 bool g_bWriteToStdOut = false;
73
74 /*! \brief
75  * Helper for checking a block of text, e.g., implementing the `-stdout`
76  * option.
77  *
78  * \ingroup module_testutils
79  */
80 void checkTextImpl(TestReferenceChecker *checker, const std::string &text,
81                    const char *id)
82 {
83     if (g_bWriteToStdOut)
84     {
85         printf("%s:\n", id);
86         printf("%s[END]\n", text.c_str());
87     }
88     else
89     {
90         checker->checkStringBlock(text, id);
91     }
92 }
93
94 }
95
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)
101 {
102     options->addOption(
103             BooleanOption("stdout")
104                 .store(&g_bWriteToStdOut)
105                 .description("Print the test string to stdout instead of checking against reference data"));
106 }
107 //! \endcond
108
109 /********************************************************************
110  * TestFileOutputRedirector
111  */
112
113 /*! \internal
114  * \brief
115  * Implementation of FileOutputRedirectorInterface for tests.
116  *
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.
120  *
121  * \ingroup module_testutils
122  */
123 class TestFileOutputRedirector : public FileOutputRedirectorInterface
124 {
125     public:
126         //! Initializes the redirector with the given file manager.
127         explicit TestFileOutputRedirector(TestFileManager *fileManager)
128             : fileManager_(*fileManager)
129         {
130         }
131
132         virtual File &standardOutput()
133         {
134             if (!stdoutFile_)
135             {
136                 const std::string path = fileManager_.getTemporaryFilePath("stdout.txt");
137                 stdoutFile_.reset(new File(path, "w"));
138                 fileList_.push_back(FileListEntry("<stdout>", path));
139             }
140             return *stdoutFile_;
141         }
142         virtual FileInitializer openFileForWriting(const char *filename)
143         {
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");
149         }
150
151         /*! \brief
152          * Checks the contents of all redirected files.
153          */
154         void checkRedirectedFiles(TestReferenceChecker *checker)
155         {
156             if (stdoutFile_)
157             {
158                 stdoutFile_->close();
159                 stdoutFile_.reset();
160             }
161             std::vector<FileListEntry>::const_iterator i;
162             for (i = fileList_.begin(); i != fileList_.end(); ++i)
163             {
164                 const std::string text = File::readToString(i->second);
165                 checkTextImpl(checker, text, i->first.c_str());
166             }
167         }
168
169     private:
170         typedef std::pair<std::string, std::string> FileListEntry;
171
172         TestFileManager            &fileManager_;
173         boost::scoped_ptr<File>     stdoutFile_;
174         std::vector<FileListEntry>  fileList_;
175 };
176
177 /********************************************************************
178  * StringTestBase::Impl
179  */
180
181 class StringTestBase::Impl
182 {
183     public:
184         TestReferenceData                           data_;
185         boost::scoped_ptr<TestReferenceChecker>     checker_;
186         boost::scoped_ptr<TestFileOutputRedirector> redirector_;
187 };
188
189 /********************************************************************
190  * StringTestBase
191  */
192
193 StringTestBase::StringTestBase()
194     : impl_(new Impl)
195 {
196 }
197
198 StringTestBase::~StringTestBase()
199 {
200 }
201
202 FileOutputRedirectorInterface &
203 StringTestBase::initOutputRedirector(TestFileManager *fileManager)
204 {
205     if (impl_->redirector_)
206     {
207         GMX_THROW(TestException("initOutputRedirector() called more than once"));
208     }
209     impl_->redirector_.reset(new TestFileOutputRedirector(fileManager));
210     return *impl_->redirector_;
211 }
212
213 TestReferenceChecker &
214 StringTestBase::checker()
215 {
216     if (!impl_->checker_)
217     {
218         impl_->checker_.reset(new TestReferenceChecker(impl_->data_.rootChecker()));
219     }
220     return *impl_->checker_;
221 }
222
223 void
224 StringTestBase::checkText(const std::string &text, const char *id)
225 {
226     checkTextImpl(&checker(), text, id);
227 }
228
229 void
230 StringTestBase::checkFileContents(const std::string &filename, const char *id)
231 {
232     const std::string text = File::readToString(filename);
233     checkText(text, id);
234 }
235
236 void
237 StringTestBase::checkRedirectedOutputFiles()
238 {
239     if (!impl_->redirector_)
240     {
241         GMX_THROW(TestException("initOutputRedirector() not called"));
242     }
243     impl_->redirector_->checkRedirectedFiles(&checker());
244 }
245
246 } // namespace test
247 } // namespace gmx