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"
48 #include "gromacs/fatalerror/gmxassert.h"
50 #include "testutils/refdata.h"
55 class AbstractAnalysisData;
57 class AnalysisDataHandle;
62 class MockAnalysisModule;
64 //! Constant to use to signify end of data for AnalysisDataTestInput.
65 const real END_OF_DATA = std::numeric_limits<real>::max();
66 //! Constant to use to signify end of one data frame for AnalysisDataTestInput.
67 const real END_OF_FRAME = std::numeric_limits<real>::min();
68 //! Constant to use to signify end of one multipoint set for AnalysisDataTestInput.
69 const real MPSTOP = -std::numeric_limits<real>::max();
71 /*! \libinternal \brief
72 * Represents a single set of points in AnalysisDataTestInputFrame structure.
74 * If the data is not multipoint, each frame contains exactly one set of
75 * points. If there is more than one set of points, each of these sets is
76 * passed separately and AnalysisDataHandle::finishPointSet() called in
79 * \ingroup module_analysisdata
81 class AnalysisDataTestInputPointSet
84 AnalysisDataTestInputPointSet();
86 int size() const { return y_.size(); }
87 real y(int i) const { return y_[i]; }
88 real dy(int i) const { return 0.0; }
89 real present(int i) const { return true; }
90 const std::vector<real> &yvector() const { return y_; }
95 friend class AnalysisDataTestInput;
98 /*! \libinternal \brief
99 * Represents a single frame in AnalysisDataTestInput structure.
101 * \ingroup module_analysisdata
103 class AnalysisDataTestInputFrame
106 AnalysisDataTestInputFrame();
108 bool isMultipoint() const { return points_.size() > 1; }
110 int index() const { return index_; }
111 real x() const { return x_; }
112 real dx() const { return 0.0; }
114 int pointSetCount() const { return points_.size(); }
115 const AnalysisDataTestInputPointSet &points(int index = 0) const
117 GMX_ASSERT(index >= 0 && static_cast<size_t>(index) < points_.size(),
118 "Point set index out of range");
119 return points_[index];
125 std::vector<AnalysisDataTestInputPointSet> points_;
127 friend class AnalysisDataTestInput;
130 /*! \libinternal \brief
131 * Represents static input data for AbstractAnalysisData tests.
133 * Used to construct structured test input data from a static array of reals,
134 * and then typically used as input to methods in AnalysisDataTestFixture.
136 * \see AnalysisDataTestFixture
138 * \ingroup module_analysisdata
140 class AnalysisDataTestInput
144 * Constructs data representation from a simple array.
146 * \param[in] data Array to construct data from.
148 * The input array should consist of a set of frames, separated by a
149 * END_OF_FRAME marker. The first value for a frame is the X value,
150 * all following values are Y values.
151 * For multipoint data, one frame can contain several point sets,
152 * separated by MPSTOP markers. There should be no MPSTOP marker after
153 * the last point set, only an END_OF_FRAME marker. All point sets are
154 * assumed to start from column zero, but the sets may contain
155 * different number of columns. For non-multipoint data, all frames
156 * must containt the same number of columns.
157 * The final element in the array (after the last END_OF_FRAME) should
160 explicit AnalysisDataTestInput(const real *data);
161 ~AnalysisDataTestInput();
163 int frameCount() const { return frames_.size(); }
164 int columnCount() const { return columnCount_; }
165 bool isMultipoint() const { return bMultipoint_; }
166 const AnalysisDataTestInputFrame &frame(int index) const;
171 std::vector<AnalysisDataTestInputFrame> frames_;
174 /*! \libinternal \brief
175 * Test fixture for AbstractAnalysisData testing.
177 * This test fixture is designed to help writing tests for objects that
178 * derive from the AbstractAnalysisData class. Typical flow in such tests is
179 * that first the test creates the required data objects, then call static
180 * methods in this class to add mock modules (using
181 * AbstractAnalysisData::addModule()) to the data objects to check that they
182 * produce the correct data, and then invokes methods in the data object to
183 * produce the data to be checked. Static methods are also provided for
184 * pushing data from an AnalysisDataTestInput object to some generic types
185 * derived from AbstractAnalysisData.
187 * Methods addStaticCheckerModule(), addStaticColumnCheckerModule() and
188 * addStaticStorageCheckerModule() create and add mock modules that check the
189 * data against a given AnalysisDataTestInput instance.
191 * Method addReferenceCheckerModule() creates and adds a mock module that
192 * checks the output against reference data produced by a previous test
193 * execution (see TestReferenceData). Two versions are provided, a static
194 * method to be used with any TestReferenceChecker, and a non-static method
195 * that uses the protected \p data_ member.
197 * presentAllData() and presentDataFrame() are provided to push data from an
198 * AnalysisDataTestInput into an AnalysisData object. In typical tests, most
199 * checks are done during the these methods, by the added mock modules.
200 * setupArrayData() performs the same function for classes derived from
201 * AbstractAnalysisArrayData. In that case, the test should separately ensure
202 * that AbstractAnalysisArrayData::valuesReady() gets called.
205 * Support for errors and for arbitrary multipoint data.
207 * \see AnalysisDataTestInput
209 * \ingroup module_analysisdata
211 class AnalysisDataTestFixture : public ::testing::Test
214 AnalysisDataTestFixture();
217 * Adds all data from AnalysisDataTestInput into an AnalysisData.
219 static void presentAllData(const AnalysisDataTestInput &input,
222 * Adds a single frame from AnalysisDataTestInput into an AnalysisData.
224 static void presentDataFrame(const AnalysisDataTestInput &input, int row,
225 AnalysisDataHandle handle);
227 * Initializes an array data object from AnalysisDataTestInput.
229 * \tparam ArrayData Class derived from AbstractAnalysisArrayData.
231 * The ArrayData class should expose the setter methods
232 * (setColumnCount(), setRowCount(), allocateValues(), setValue())
233 * publicly or declare the fixture class as a friend.
234 * The X axis in \p data must be configured to match \p input before
235 * calling this method.
237 * Does not call AbstractAnalysisArrayData::valuesReady().
238 * The test must ensure that this method gets called, otherwise the
239 * mock modules never get called.
241 template <class ArrayData>
242 static void setupArrayData(const AnalysisDataTestInput &input,
246 * Adds a mock module that verifies output against
247 * AnalysisDataTestInput.
249 * \param[in] data Data to compare against.
250 * \param source Data object to verify.
252 * Creates a mock module that verifies that the
253 * AnalysisDataModuleInterface methods are called correctly by
254 * \p source. Parameters for the calls are verified against \p data.
255 * Adds the created module to \p source using \p data->addModule().
256 * Any exceptions from the called functions should be caught by the
259 * \see AbstractAnalysisData::addModule()
261 static void addStaticCheckerModule(const AnalysisDataTestInput &data,
262 AbstractAnalysisData *source);
264 * Adds a column mock module that verifies output against
265 * AnalysisDataTestInput.
267 * \param[in] data Data to compare against.
268 * \param[in] firstcol First column to check.
269 * \param[in] n Number of columns to check.
270 * \param source Data object to verify.
272 * Creates a mock module that verifies that the
273 * AnalysisDataModuleInterface methods are called correctly by
274 * \p source. Parameters for the calls are verified against \p data.
275 * Adds the created module to \p source using
276 * \p data->addColumnModule().
277 * Any exceptions from the called functions should be caught by the
280 * \see AbstractAnalysisData::addColumnModule()
282 static void addStaticColumnCheckerModule(const AnalysisDataTestInput &data,
284 AbstractAnalysisData *source);
286 * Adds a mock module that verifies output and storage against
287 * AnalysisDataTestInput.
289 * \param[in] data Data to compare against.
290 * \param[in] storageCount Number of previous frames to check
292 * \param source Data object to verify.
294 * Works like addStaticCheckerModule(), except that in addition, for
295 * each frame, the mock module also checks that previous frames can be
296 * accessed using AbstractAnalysisData::getDataFrame(). In the
297 * AnalysisDataModuleInterface::dataStarted() callback, the mock module
298 * calls AbstractAnalysisData::requestStorage() with \p storageCount as
301 static void addStaticStorageCheckerModule(const AnalysisDataTestInput &data,
303 AbstractAnalysisData *source);
305 * Adds a mock module that verifies output against reference data.
307 * \param[in] checker Reference data checker to use for comparison.
308 * \param source Data object to verify.
310 * Creates a mock module that verifies that the
311 * AnalysisDataModuleInterface methods are called correctly by
312 * \p source. Parameters for the calls are verified against reference
313 * data using \p checker.
314 * Adds the created module to \p source using \p data->addModule().
315 * Any exceptions from the called functions should be caught by the
318 * \see TestReferenceData
320 static void addReferenceCheckerModule(const TestReferenceChecker &checker,
321 AbstractAnalysisData *source);
324 * Adds a mock module that verifies output against reference data.
326 * \param[in] id Identifier for reference data compound to use.
327 * \param source Data object to verify.
329 * Creates a reference checker module using a compound checker with id
330 * \p id at the root level of \p data_.
332 * See the static overload for other details.
334 void addReferenceCheckerModule(const char *id,
335 AbstractAnalysisData *source);
338 gmx::test::TestReferenceData data_;
342 template <class ArrayData>
343 void AnalysisDataTestFixture::setupArrayData(const AnalysisDataTestInput &input,
346 GMX_RELEASE_ASSERT(!input.isMultipoint(),
347 "Array data cannot be initialized from multipoint data");
348 GMX_RELEASE_ASSERT(data->columnCount() == 0 || data->columnCount() == input.columnCount(),
349 "Mismatching input and target data");
350 GMX_RELEASE_ASSERT(data->rowCount() == 0 || data->rowCount() == input.frameCount(),
351 "Mismatching input and target data");
352 data->setColumnCount(input.columnCount());
353 data->setRowCount(input.frameCount());
354 data->allocateValues();
355 for (int row = 0; row < input.frameCount(); ++row)
357 const AnalysisDataTestInputFrame &frame = input.frame(row);
358 EXPECT_FLOAT_EQ(frame.x(), data->xvalue(row));
359 const AnalysisDataTestInputPointSet &points = frame.points();
360 for (int column = 0; column < input.columnCount(); ++column)
362 data->setValue(row, column, points.y(column));