3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
9 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
10 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
11 * Copyright (c) 2001-2009, The GROMACS development team,
12 * check out http://www.gromacs.org for more information.
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * If you want to redistribute modifications, please consider that
20 * scientific software is very special. Version control is crucial -
21 * bugs must be traceable. We will be happy to consider code for
22 * inclusion in the official distribution, but derived work must not
23 * be called official GROMACS. Details are found in the README & COPYING
24 * files - if they are missing, get the official version at www.gromacs.org.
26 * To help us fund GROMACS development, we humbly ask that you cite
27 * the papers on the package - you can find them in the top README file.
29 * For more info, check our website at http://www.gromacs.org
31 /*! \libinternal \file
33 * Helper classes for testing classes that derive from AbstractAnalysisData.
35 * \author Teemu Murtola <teemu.murtola@cbr.su.se>
37 * \ingroup module_analysisdata
39 #ifndef GMX_ANALYSISDATA_TESTS_DATATEST_H
40 #define GMX_ANALYSISDATA_TESTS_DATATEST_H
45 #include <gtest/gtest.h>
47 #include "gromacs/legacyheaders/types/simple.h"
49 #include "testutils/refdata.h"
54 class AbstractAnalysisData;
56 class AnalysisDataHandle;
61 class MockAnalysisModule;
63 //! Constant to use to signify end of data for AnalysisDataTestInput.
64 const real END_OF_DATA = std::numeric_limits<real>::max();
65 //! Constant to use to signify end of one data frame for AnalysisDataTestInput.
66 const real END_OF_FRAME = std::numeric_limits<real>::min();
68 /*! \libinternal \brief
69 * Represents a single frame in AnalysisDataTestInput structure.
71 * \ingroup module_analysisdata
73 class AnalysisDataTestInputFrame
76 AnalysisDataTestInputFrame();
78 int index() const { return index_; }
79 real x() const { return x_; }
80 real dx() const { return 0.0; }
81 real y(int i) const { return y_[i]; }
82 const std::vector<real> &yvector() const { return y_; }
83 const real *yptr() const { return &y_[0]; }
84 const real *dyptr() const { return NULL; }
85 const bool *presentptr() const { return NULL; }
92 friend class AnalysisDataTestInput;
95 /*! \libinternal \brief
96 * Represents static input data for AbstractAnalysisData tests.
98 * Used to construct structured test input data from a static array of reals,
99 * and then typically used as input to methods in AnalysisDataTestFixture.
101 * \see AnalysisDataTestFixture
103 * \ingroup module_analysisdata
105 class AnalysisDataTestInput
109 * Constructs data representation from a simple array.
111 * \param[in] data Array to construct data from.
113 * The input array should consist of a set of frames, separated by a
114 * END_OF_FRAME marker. The first value for a frame is the X value,
115 * all following values are Y values.
116 * The final element in the array (after the last END_OF_FRAME) should
119 explicit AnalysisDataTestInput(const real *data);
120 ~AnalysisDataTestInput();
122 int frameCount() const { return frames_.size(); }
123 int columnCount() const { return columnCount_; }
124 const AnalysisDataTestInputFrame &frame(int index) const;
128 std::vector<AnalysisDataTestInputFrame> frames_;
131 /*! \libinternal \brief
132 * Test fixture for AbstractAnalysisData testing.
134 * This test fixture is designed to help writing tests for objects that
135 * derive from the AbstractAnalysisData class. Typical flow in such tests is
136 * that first the test creates the required data objects, then call static
137 * methods in this class to add mock modules (using
138 * AbstractAnalysisData::addModule()) to the data objects to check that they
139 * produce the correct data, and then invokes methods in the data object to
140 * produce the data to be checked. Static methods are also provided for
141 * pushing data from an AnalysisDataTestInput object to some generic types
142 * derived from AbstractAnalysisData.
144 * Methods addStaticCheckerModule(), addStaticColumnCheckerModule() and
145 * addStaticStorageCheckerModule() create and add mock modules that check the
146 * data against a given AnalysisDataTestInput instance.
148 * Method addReferenceCheckerModule() creates and adds a mock module that
149 * checks the output against reference data produced by a previous test
150 * execution (see TestReferenceData). Two versions are provided, a static
151 * method to be used with any TestReferenceChecker, and a non-static method
152 * that uses the protected \p data_ member.
154 * presentAllData() and presentDataFrame() are provided to push data from an
155 * AnalysisDataTestInput into an AnalysisData object. In typical tests, most
156 * checks are done during the these methods, by the added mock modules.
159 * Support for errors and for multipoint data.
161 * \see AnalysisDataTestInput
163 * \ingroup module_analysisdata
165 class AnalysisDataTestFixture : public ::testing::Test
168 AnalysisDataTestFixture();
171 * Adds all data from AnalysisDataTestInput into an AnalysisData.
173 static void presentAllData(const AnalysisDataTestInput &input,
176 * Adds a single frame from AnalysisDataTestInput into an AnalysisData.
178 static void presentDataFrame(const AnalysisDataTestInput &input, int row,
179 AnalysisDataHandle *handle);
182 * Adds a mock module that verifies output against
183 * AnalysisDataTestInput.
185 * \param[in] data Data to compare against.
186 * \param source Data object to verify.
188 * Creates a mock module that verifies that the
189 * AnalysisDataModuleInterface methods are called correctly by
190 * \p source. Parameters for the calls are verified against \p data.
191 * Adds the created module to \p source using \p data->addModule().
192 * Any exceptions from the called functions should be caught by the
195 * \see AbstractAnalysisData::addModule()
197 static void addStaticCheckerModule(const AnalysisDataTestInput &data,
198 AbstractAnalysisData *source);
200 * Adds a column mock module that verifies output against
201 * AnalysisDataTestInput.
203 * \param[in] data Data to compare against.
204 * \param[in] firstcol First column to check.
205 * \param[in] n Number of columns to check.
206 * \param source Data object to verify.
208 * Creates a mock module that verifies that the
209 * AnalysisDataModuleInterface methods are called correctly by
210 * \p source. Parameters for the calls are verified against \p data.
211 * Adds the created module to \p source using
212 * \p data->addColumnModule().
213 * Any exceptions from the called functions should be caught by the
216 * \see AbstractAnalysisData::addColumnModule()
218 static void addStaticColumnCheckerModule(const AnalysisDataTestInput &data,
220 AbstractAnalysisData *source);
222 * Adds a mock module that verifies output and storage against
223 * AnalysisDataTestInput.
225 * \param[in] data Data to compare against.
226 * \param[in] storageCount Number of previous frames to check
228 * \param source Data object to verify.
230 * Works like addStaticCheckerModule(), except that in addition, for
231 * each frame, the mock module also checks that previous frames can be
232 * accessed using AbstractAnalysisData::getDataWErr(). In the
233 * AnalysisDataModuleInterface::dataStarted() callback, the mock module
234 * calls AbstractAnalysisData::requestStorage() with \p storageCount as
237 static void addStaticStorageCheckerModule(const AnalysisDataTestInput &data,
239 AbstractAnalysisData *source);
241 * Adds a mock module that verifies output against reference data.
243 * \param[in] checker Reference data checker to use for comparison.
244 * \param source Data object to verify.
246 * Creates a mock module that verifies that the
247 * AnalysisDataModuleInterface methods are called correctly by
248 * \p source. Parameters for the calls are verified against reference
249 * data using \p checker.
250 * Adds the created module to \p source using \p data->addModule().
251 * Any exceptions from the called functions should be caught by the
254 * \see TestReferenceData
256 static void addReferenceCheckerModule(const TestReferenceChecker &checker,
257 AbstractAnalysisData *source);
260 * Adds a mock module that verifies output against reference data.
262 * \param[in] id Identifier for reference data compound to use.
263 * \param source Data object to verify.
265 * Creates a reference checker module using a compound checker with id
266 * \p id at the root level of \p data_.
268 * See the static overload for other details.
270 void addReferenceCheckerModule(const char *id,
271 AbstractAnalysisData *source);
274 gmx::test::TestReferenceData data_;