2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2015,2016,2017,2018,2019,2020,2021, 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.
37 * Declares internal helper classes for the reference data framework to check
38 * reference data values of different types.
40 * \author Teemu Murtola <teemu.murtola@gmail.com>
41 * \ingroup module_testutils
43 #ifndef GMX_TESTUTILS_REFDATA_CHECKERS_H
44 #define GMX_TESTUTILS_REFDATA_CHECKERS_H
51 #include <gtest/gtest.h>
53 #include "gromacs/utility/basedefinitions.h"
54 #include "gromacs/utility/exceptions.h"
55 #include "gromacs/utility/strconvert.h"
56 #include "gromacs/utility/stringutil.h"
58 #include "testutils/testasserts.h"
59 #include "testutils/testexceptions.h"
61 #include "refdata_impl.h"
68 class IReferenceDataEntryChecker
71 virtual void fillEntry(ReferenceDataEntry* entry) const = 0;
72 virtual ::testing::AssertionResult checkEntry(const ReferenceDataEntry& entry,
73 const std::string& fullId) const = 0;
76 virtual ~IReferenceDataEntryChecker() {}
79 class NullChecker : public IReferenceDataEntryChecker
82 void fillEntry(ReferenceDataEntry* /*entry*/) const override {}
83 ::testing::AssertionResult checkEntry(const ReferenceDataEntry& /*entry*/,
84 const std::string& /*fullId*/) const override
86 return ::testing::AssertionSuccess();
90 class ExactStringChecker : public IReferenceDataEntryChecker
93 explicit ExactStringChecker(const std::string& value) : value_(value) {}
95 void fillEntry(ReferenceDataEntry* entry) const override { entry->setValue(value_); }
96 ::testing::AssertionResult checkEntry(const ReferenceDataEntry& entry, const std::string& fullId) const override
98 if (entry.value() == value_)
100 return ::testing::AssertionSuccess();
102 return ::testing::AssertionFailure() << " In item: " << fullId << std::endl
103 << " Actual: '" << value_ << "'" << std::endl
104 << "Reference: '" << entry.value() << "'";
111 class ExactStringBlockChecker : public IReferenceDataEntryChecker
114 explicit ExactStringBlockChecker(const std::string& value) : value_(value) {}
116 void fillEntry(ReferenceDataEntry* entry) const override { entry->setTextBlockValue(value_); }
117 ::testing::AssertionResult checkEntry(const ReferenceDataEntry& entry, const std::string& fullId) const override
119 if (entry.value() == value_)
121 return ::testing::AssertionSuccess();
123 return ::testing::AssertionFailure() << " In item: " << fullId << std::endl
124 << " Actual: '" << value_ << "'" << std::endl
125 << "Reference: '" << entry.value() << "'";
133 //! Helper function to parse a floating-point reference data value.
134 static inline double convertDoubleReferenceValue(const std::string& value)
138 return fromString<double>(value);
140 catch (const InvalidInputError& ex)
142 GMX_THROW_WRAPPER_TESTEXCEPTION(ex);
146 template<typename FloatType>
147 class FloatingPointChecker : public IReferenceDataEntryChecker
150 FloatingPointChecker(FloatType value, const FloatingPointTolerance& tolerance) :
151 value_(value), tolerance_(tolerance)
155 void fillEntry(ReferenceDataEntry* entry) const override
157 const int prec = std::numeric_limits<FloatType>::digits10 + 2;
158 entry->setValue(formatString("%.*g", prec, value_));
160 ::testing::AssertionResult checkEntry(const ReferenceDataEntry& entry, const std::string& fullId) const override
162 FloatType refValue = static_cast<FloatType>(convertDoubleReferenceValue(entry.value()));
163 FloatingPointDifference diff(refValue, value_);
164 if (tolerance_.isWithin(diff))
166 return ::testing::AssertionSuccess();
168 return ::testing::AssertionFailure() << " In item: " << fullId << std::endl
169 << " Actual: " << value_ << std::endl
170 << " Reference: " << refValue << std::endl
171 << "Difference: " << diff.toString() << std::endl
172 << " Tolerance: " << tolerance_.toString(diff);
177 FloatingPointTolerance tolerance_;
180 template<typename FloatType>
181 class FloatingPointFromStringChecker : public IReferenceDataEntryChecker
184 FloatingPointFromStringChecker(const std::string& value, const FloatingPointTolerance& tolerance) :
185 value_(value), tolerance_(tolerance)
189 void fillEntry(ReferenceDataEntry* entry) const override { entry->setValue(value_); }
190 ::testing::AssertionResult checkEntry(const ReferenceDataEntry& entry, const std::string& fullId) const override
192 FloatType value = fromString<FloatType>(value_);
193 FloatType refValue = static_cast<FloatType>(convertDoubleReferenceValue(entry.value()));
194 FloatingPointDifference diff(refValue, value);
195 if (tolerance_.isWithin(diff))
197 return ::testing::AssertionSuccess();
199 return ::testing::AssertionFailure() << " In item: " << fullId << std::endl
200 << " Actual: " << value << std::endl
201 << " Reference: " << entry.value() << std::endl
202 << "Difference: " << diff.toString() << std::endl
203 << " Tolerance: " << tolerance_.toString(diff);
208 FloatingPointTolerance tolerance_;
211 template<typename ValueType>
212 class ValueExtractor : public IReferenceDataEntryChecker
215 explicit ValueExtractor(ValueType* value) : value_(value) {}
217 void fillEntry(ReferenceDataEntry* /*entry*/) const override
219 GMX_THROW(TestException("Extracting value from non-existent reference data entry"));
221 ::testing::AssertionResult checkEntry(const ReferenceDataEntry& entry,
222 const std::string& /*fullId*/) const override
224 extractValue(entry.value());
225 return ::testing::AssertionSuccess();
228 void extractValue(const std::string& value) const { *value_ = fromString<ValueType>(value); }
235 inline void ValueExtractor<std::string>::extractValue(const std::string& value) const