2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2011,2012,2013,2014,2015,2016, 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 /*! \libinternal \file
37 * Functionality for writing tests that can produce their own reference data.
39 * See \ref page_refdata for more details.
41 * \author Teemu Murtola <teemu.murtola@gmail.com>
43 * \ingroup module_testutils
45 #ifndef GMX_TESTUTILS_REFDATA_H
46 #define GMX_TESTUTILS_REFDATA_H
52 #include "gromacs/utility/basedefinitions.h"
53 #include "gromacs/utility/classhelpers.h"
58 class IOptionsContainer
;
63 class FloatingPointTolerance
;
65 /*! \libinternal \brief
66 * Mode of operation for reference data handling.
68 * There should be no need to use this type outside the test utility module.
70 * \ingroup module_testutils
72 enum ReferenceDataMode
75 * Compare to existing reference data.
77 * If reference data does not exist, or if the test results differ from
78 * those in the reference data, the test fails.
82 * Create missing reference data.
84 * If reference data does not exist for a test, that test behaves as if
85 * ::erefdataUpdateAll had been specified. Tests for which reference data
86 * exists, behave like with ::erefdataCompare.
88 erefdataCreateMissing
,
90 * Update reference data that does not pass comparison.
92 * Tests utilizing reference data should always pass in this mode unless
93 * there is an I/O error.
95 erefdataUpdateChanged
,
97 * Update reference data, overwriting old data.
99 * Tests utilizing reference data should always pass in this mode unless
100 * there is an I/O error.
105 /*! \libinternal \brief
106 * Initializes reference data handling.
108 * Adds command-line options to \p options to set the reference data mode.
109 * By default, ::erefdataCompare is used, but ``--ref-data create`` or
110 * ``--ref-data update`` can be used to change it.
112 * This function is automatically called by initTestUtils().
114 * \ingroup module_testutils
116 void initReferenceData(IOptionsContainer
*options
);
118 class TestReferenceChecker
;
122 class TestReferenceDataImpl
;
125 /*! \libinternal \brief
126 * Handles creation of and comparison to test reference data.
128 * See \ref page_refdata for an overview of the functionality.
130 * This class provides functionality to use the same code to generate reference
131 * data and then on later runs compare the results of the code against that
132 * reference. The mode in which the class operates (writing reference data or
133 * comparing against existing data) is set using a command-line option that
134 * is automatically managed when using the testutils module to implement tests.
135 * Tests only need to create an instance of TestReferenceData, obtain a
136 * TestReferenceChecker using the rootChecker() method and use the various
137 * check*() methods in TestReferenceChecker to indicate values to check. If
138 * the test is running in reference data creation mode, it will produce an XML
139 * file with the values recorder. In comparison mode, it will read that same
140 * XML file and produce a Google Test non-fatal assertion for every discrepancy
141 * it detects with the reference data (including missing reference data file or
142 * individual item). Exceptions derived from TestException are thrown for I/O
143 * errors and syntax errors in the reference data.
145 * Simple example (using Google Test):
147 int functionToTest(int param);
149 TEST(MyTest, SimpleTest)
151 gmx::test::TestReferenceData data;
153 gmx::test::TestReferenceChecker checker(data.rootChecker());
154 checker.checkInteger(functionToTest(3), "ValueWith3");
155 checker.checkInteger(functionToTest(5), "ValueWith5");
156 gmx::test::TestReferenceChecker compound(
157 checker.checkCompound("CustomCompound", "Item"));
158 compound.checkInteger(function2ToTest(3), "ValueWith3");
159 compound.checkInteger(function2ToTest(5), "ValueWith5");
160 checker.checkInteger(functionToTest(4), "ValueWith4");
164 * If rootChecker() is never called, no comparison is done (i.e., missing
165 * reference data file is not reported as an error, nor is empty reference data
166 * file created in write mode).
168 * For floating-point comparisons, the reference data should be generated in
169 * double precision (currently, no warning is provided even if this is not the
170 * case, but the double precision tests will then very likely fail).
173 * \ingroup module_testutils
175 class TestReferenceData
179 * Initializes the reference data in the global mode.
183 * Initializes the reference data in a specific mode.
185 * This function is only useful for self-testing the reference data
186 * framework. As such, it also puts the framework in a state where it
187 * logs additional internal information for failures to help diagnosing
188 * problems in the framework, and stores the reference data in a
189 * temporary directory instead of the source tree.
190 * The default constructor should be used in tests utilizing this class.
192 explicit TestReferenceData(ReferenceDataMode mode
);
194 * Frees reference data structures.
196 * The reference data is written out if necessary automatically when
199 ~TestReferenceData();
202 * Returns a root-level checker object for comparisons.
204 * Each call returns an independent instance.
206 TestReferenceChecker
rootChecker();
209 std::shared_ptr
<internal::TestReferenceDataImpl
> impl_
;
211 GMX_DISALLOW_COPY_AND_ASSIGN(TestReferenceData
);
214 /*! \libinternal \brief
215 * Handles comparison to test reference data.
217 * Every check*() method takes an id string as the last parameter. This id is
218 * used to uniquely identify the value in the reference data, and it makes the
219 * output XML more human-friendly and more robust to errors. The id can be
220 * NULL; in this case, multiple elements with no id are created, and they will
221 * be matched in the same order as in which they are created. The
222 * checkCompound() method can be used to create a set of reference values
223 * grouped together. In this case, all check*() calls using the returned child
224 * TestReferenceChecker object will create the reference data within this
225 * group, and the ids only need to be unique within the compound. Compounds
228 * For usage example, see TestReferenceData.
230 * Copies of this class behave have independent internal state.
233 * \ingroup module_testutils
235 class TestReferenceChecker
239 * Creates a checker that cannot be used for checking.
241 * Attempting to call the check methods generates an assert.
242 * It is possible to check whether the checker is initialized by
244 * This constructor exists to allow declaring checker variables that
245 * will receive their value later without resorting to dynamic
248 TestReferenceChecker();
249 //! Creates a deep copy of the other checker.
250 explicit TestReferenceChecker(const TestReferenceChecker
&other
);
251 //! Moves the checker.
252 TestReferenceChecker(TestReferenceChecker
&&other
);
253 ~TestReferenceChecker();
255 //! Prevents implicit copying during assignment.
256 TestReferenceChecker
&operator=(const TestReferenceChecker
&) = delete;
257 //! Assigns a test reference checker.
258 TestReferenceChecker
&operator=(TestReferenceChecker
&&other
);
260 //! Returns whether the checker is initialized.
261 bool isValid() const;
262 //! Allows testing whether the checker is initialized directly with if.
263 explicit operator bool() const { return isValid(); }
266 * Sets the tolerance for floating-point comparisons.
268 * All following floating-point comparisons using this checker will use
269 * the new tolerance. Child checkers created with checkCompound()
270 * will inherit the tolerance from their parent checker at the time
271 * checkCompound() is called.
275 void setDefaultTolerance(const FloatingPointTolerance
&tolerance
);
278 * Checks whether a data item is present.
280 * \param[in] bPresent Whether to check for presence or absence.
281 * \param[in] id Unique identifier of the item to check.
282 * \returns true if bPresent was true and the data item was found.
284 * If \p bPresent is true, checks that a data item with \p id is
285 * present, otherwise checks that the data item is absent.
286 * If the check fails, a non-fatal Google Test assertion is generated.
288 * If reference data is being written, the check always succeeds and the
289 * return value is \p bPresent.
291 * The main use of this method is to assign meaning for missing
292 * reference data. Example use:
294 if (checker.checkPresent(bHaveVelocities, "Velocities"))
296 // <check the velocities>
300 bool checkPresent(bool bPresent
, const char *id
);
303 * Initializes comparison of a group of related data items.
305 * \param[in] type Informational type for the compound.
306 * \param[in] id Unique identifier for the compound among its
308 * \returns Checker to use for comparison within the compound.
310 * All checks performed with the returned checker only
311 * need to have unique ids within the compound, not globally.
313 * Compound structures can be nested.
315 TestReferenceChecker
checkCompound(const char *type
, const char *id
);
317 //! Check a single boolean value.
318 void checkBoolean(bool value
, const char *id
);
319 //! Check a single string value.
320 void checkString(const char *value
, const char *id
);
321 //! Check a single string value.
322 void checkString(const std::string
&value
, const char *id
);
324 * Check a multi-line string value.
326 * This method works as checkString(), but should be used for long
327 * strings that may contain, e.g., newlines. Typically used to check
328 * formatted output, and attempts to make the output XML such that it
329 * is easier to edit by hand to set the desired output formatting.
331 void checkTextBlock(const std::string
&value
, const char *id
);
332 //! Check a single integer value.
333 void checkInteger(int value
, const char *id
);
334 //! Check a single int64 value.
335 void checkInt64(gmx_int64_t value
, const char *id
);
336 //! Check a single uint64 value.
337 void checkUInt64(gmx_uint64_t value
, const char *id
);
338 //! Check a single single-precision floating point value.
339 void checkFloat(float value
, const char *id
);
340 //! Check a single double-precision floating point value.
341 void checkDouble(double value
, const char *id
);
342 //! Check a single floating point value.
343 void checkReal(float value
, const char *id
);
344 //! Check a single floating point value.
345 void checkReal(double value
, const char *id
);
346 //! Check a vector of three integer values.
347 void checkVector(const int value
[3], const char *id
);
348 //! Check a vector of three single-precision floating point values.
349 void checkVector(const float value
[3], const char *id
);
350 //! Check a vector of three double-precision floating point values.
351 void checkVector(const double value
[3], const char *id
);
352 //! Check a single floating-point value from a string.
353 void checkRealFromString(const std::string
&value
, const char *id
);
355 /*! \name Overloaded versions of simple checker methods
357 * These methods provide overloads under a single name for all the
358 * methods checkBoolean(), checkString(), checkReal() and checkVector().
359 * They are provided mainly to allow template implementations (such as
360 * checkSequence()). Typically callers should use the individually
361 * named versions for greater clarity.
364 //! Check a single boolean value.
365 void checkValue(bool value
, const char *id
)
367 checkBoolean(value
, id
);
369 //! Check a single string value.
370 void checkValue(const char *value
, const char *id
)
372 checkString(value
, id
);
374 //! Check a single string value.
375 void checkValue(const std::string
&value
, const char *id
)
377 checkString(value
, id
);
379 //! Check a single integer value.
380 void checkValue(int value
, const char *id
)
382 checkInteger(value
, id
);
384 //! Check a single integer value.
385 void checkValue(gmx_int64_t value
, const char *id
)
387 checkInt64(value
, id
);
389 //! Check a single integer value.
390 void checkValue(gmx_uint64_t value
, const char *id
)
392 checkUInt64(value
, id
);
394 //! Check a single single-precision floating point value.
395 void checkValue(float value
, const char *id
)
397 checkFloat(value
, id
);
399 //! Check a single double-precision floating point value.
400 void checkValue(double value
, const char *id
)
402 checkDouble(value
, id
);
404 //! Check a vector of three integer values.
405 void checkValue(const int value
[3], const char *id
)
407 checkVector(value
, id
);
409 //! Check a vector of three single-precision floating point values.
410 void checkValue(const float value
[3], const char *id
)
412 checkVector(value
, id
);
414 //! Check a vector of three double-precision floating point values.
415 void checkValue(const double value
[3], const char *id
)
417 checkVector(value
, id
);
422 * Generic method to check a sequence of simple values.
424 * \tparam Iterator Input iterator that allows multiple (two) passes.
425 * Value type must be one of those accepted by checkValue(), or
426 * implicitly convertible to one.
427 * \param[in] begin Iterator to the start of the range to check.
428 * \param[in] end Iterator to the end of the range to check.
429 * \param[in] id Unique identifier for the sequence among its
432 template <class Iterator
>
433 void checkSequence(Iterator begin
, Iterator end
, const char *id
)
435 typename
std::iterator_traits
<Iterator
>::difference_type length
436 = std::distance(begin
, end
);
437 TestReferenceChecker
compound(checkSequenceCompound(id
, length
));
438 for (Iterator i
= begin
; i
!= end
; ++i
)
440 compound
.checkValue(*i
, NULL
);
444 * Generic method to check a sequence of custom values.
446 * \tparam Iterator Input iterator that allows multiple (two) passes.
447 * \tparam ItemChecker Functor to check an individual value. Signature
448 * void(TestReferenceChecker *, const T &), where T is the value
449 * type of \p Iterator.
450 * \param[in] begin Iterator to the start of the range to check.
451 * \param[in] end Iterator to the end of the range to check.
452 * \param[in] id Unique identifier for the sequence among its
454 * \param[in] checkItem Functor to check an individual item.
456 * This method creates a compound checker \c compound within which all
457 * values of the sequence are checked. Calls \c checkItem(&compound, *i)
458 * with that compound for each iterator \c i in the range [begin, end).
459 * \p checkItem should use the various check methods in the passed
460 * checker to check each value.
462 * This method can be used to check a sequence made of compound types.
463 * Typically \p checkItem will create a compound within the passed
464 * checker to check different aspects of the value that was passed
465 * to it. Either NULL or a unique identifier string must be used for
466 * the id value of that compound. */
467 template <class Iterator
, class ItemChecker
>
468 void checkSequence(Iterator begin
, Iterator end
, const char *id
,
469 ItemChecker checkItem
)
471 typename
std::iterator_traits
<Iterator
>::difference_type length
472 = std::distance(begin
, end
);
473 TestReferenceChecker
compound(checkSequenceCompound(id
, length
));
474 for (Iterator i
= begin
; i
!= end
; ++i
)
476 checkItem(&compound
, *i
);
480 * Check an array of values.
482 * \tparam T Type of values to check. Should be one of those accepted
483 * by checkValue(), or implicitly convertible to one.
485 * \param[in] length Number of values to check.
486 * \param[in] values Pointer to the first value to check.
487 * \param[in] id Unique identifier for the sequence among its
490 * This is a convenience method that delegates all work to
493 template <typename T
>
494 void checkSequenceArray(size_t length
, const T
*values
, const char *id
)
496 checkSequence(values
, values
+ length
, id
);
499 * Convenience method for checking that a sequence is empty.
501 * \param[in] id Unique identifier for the sequence among its
504 * This method provides a convenient solution for a case where there is
505 * implicitly a sequence to be checked, but there is no pointer
506 * available to the values since the sequence is empty.
507 * Since this method does not require the type of the values, it can be
508 * used in such cases easily.
510 void checkEmptySequence(const char *id
);
512 * Initializes a compound for a sequence of items.
514 * \param[in] id Unique identifier for the sequence among its
516 * \param[in] length Number of items that will be in the sequence.
517 * \returns Checker to use for comparison within the sequence.
519 * This method can be used to check custom sequences where
520 * checkSequence() is not appropriate.
522 TestReferenceChecker
checkSequenceCompound(const char *id
, size_t length
);
528 * Constructs a checker with a specific internal state.
530 * Is private to only allow users of this class to create instances
531 * using TestReferenceData::rootChecker() or checkCompound()
534 explicit TestReferenceChecker(Impl
*impl
);
536 PrivateImplPointer
<Impl
> impl_
;
539 * Needed to expose the constructor only to TestReferenceData.
541 friend class TestReferenceData
;