Remove cycle suppression
[gromacs.git] / src / testutils / refdata-checkers.h
blob9a5c4fd0a4ae145532b1beb1db5ae5f0cca3949a
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2015,2016,2017, 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 /*! \internal \file
36 * \brief
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
46 #include <cstdlib>
48 #include <limits>
49 #include <string>
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/refdata-impl.h"
59 #include "testutils/testasserts.h"
60 #include "testutils/testexceptions.h"
62 namespace gmx
64 namespace test
67 class IReferenceDataEntryChecker
69 public:
70 virtual void fillEntry(ReferenceDataEntry *entry) const = 0;
71 virtual ::testing::AssertionResult
72 checkEntry(const ReferenceDataEntry &entry, const std::string &fullId) const = 0;
74 protected:
75 virtual ~IReferenceDataEntryChecker() {}
78 class NullChecker : public IReferenceDataEntryChecker
80 public:
81 virtual void fillEntry(ReferenceDataEntry *) const {}
82 virtual ::testing::AssertionResult
83 checkEntry(const ReferenceDataEntry &, const std::string &) const
85 return ::testing::AssertionSuccess();
89 class ExactStringChecker : public IReferenceDataEntryChecker
91 public:
92 explicit ExactStringChecker(const std::string &value)
93 : value_(value)
97 virtual void fillEntry(ReferenceDataEntry *entry) const
99 entry->setValue(value_);
101 virtual ::testing::AssertionResult
102 checkEntry(const ReferenceDataEntry &entry, const std::string &fullId) const
104 if (entry.value() == value_)
106 return ::testing::AssertionSuccess();
108 return ::testing::AssertionFailure()
109 << " In item: " << fullId << std::endl
110 << " Actual: '" << value_ << "'" << std::endl
111 << "Reference: '" << entry.value() << "'";
114 private:
115 std::string value_;
118 class ExactStringBlockChecker : public IReferenceDataEntryChecker
120 public:
121 explicit ExactStringBlockChecker(const std::string &value)
122 : value_(value)
126 virtual void fillEntry(ReferenceDataEntry *entry) const
128 entry->setTextBlockValue(value_);
130 virtual ::testing::AssertionResult
131 checkEntry(const ReferenceDataEntry &entry, const std::string &fullId) const
133 if (entry.value() == value_)
135 return ::testing::AssertionSuccess();
137 return ::testing::AssertionFailure()
138 << " In item: " << fullId << std::endl
139 << " Actual: '" << value_ << "'" << std::endl
140 << "Reference: '" << entry.value() << "'";
143 private:
144 std::string value_;
148 //! Helper function to parse a floating-point reference data value.
149 static inline double convertDoubleReferenceValue(const std::string &value)
153 return fromString<double>(value);
155 catch (const InvalidInputError &ex)
157 GMX_THROW_WRAPPER_TESTEXCEPTION(ex);
161 template <typename FloatType>
162 class FloatingPointChecker : public IReferenceDataEntryChecker
164 public:
165 FloatingPointChecker(FloatType value, const FloatingPointTolerance &tolerance)
166 : value_(value), tolerance_(tolerance)
170 virtual void fillEntry(ReferenceDataEntry *entry) const
172 const int prec = std::numeric_limits<FloatType>::digits10 + 2;
173 entry->setValue(formatString("%.*g", prec, value_));
175 virtual ::testing::AssertionResult
176 checkEntry(const ReferenceDataEntry &entry, const std::string &fullId) const
178 FloatType refValue = static_cast<FloatType>(convertDoubleReferenceValue(entry.value()));
179 FloatingPointDifference diff(refValue, value_);
180 if (tolerance_.isWithin(diff))
182 return ::testing::AssertionSuccess();
184 return ::testing::AssertionFailure()
185 << " In item: " << fullId << std::endl
186 << " Actual: " << value_ << std::endl
187 << " Reference: " << refValue << std::endl
188 << "Difference: " << diff.toString() << std::endl
189 << " Tolerance: " << tolerance_.toString(diff);
192 private:
193 FloatType value_;
194 FloatingPointTolerance tolerance_;
197 template <typename FloatType>
198 class FloatingPointFromStringChecker : public IReferenceDataEntryChecker
200 public:
201 FloatingPointFromStringChecker(
202 const std::string &value, const FloatingPointTolerance &tolerance)
203 : value_(value), tolerance_(tolerance)
207 virtual void fillEntry(ReferenceDataEntry *entry) const
209 entry->setValue(value_);
211 virtual ::testing::AssertionResult
212 checkEntry(const ReferenceDataEntry &entry, const std::string &fullId) const
214 FloatType value = fromString<FloatType>(value_);
215 FloatType refValue = static_cast<FloatType>(convertDoubleReferenceValue(entry.value()));
216 FloatingPointDifference diff(refValue, value);
217 if (tolerance_.isWithin(diff))
219 return ::testing::AssertionSuccess();
221 return ::testing::AssertionFailure()
222 << " In item: " << fullId << std::endl
223 << " Actual: " << value << std::endl
224 << " Reference: " << entry.value() << std::endl
225 << "Difference: " << diff.toString() << std::endl
226 << " Tolerance: " << tolerance_.toString(diff);
229 private:
230 std::string value_;
231 FloatingPointTolerance tolerance_;
234 template <typename ValueType>
235 class ValueExtractor : public IReferenceDataEntryChecker
237 public:
238 explicit ValueExtractor(ValueType *value)
239 : value_(value)
243 virtual void fillEntry(ReferenceDataEntry *) const
245 GMX_THROW(TestException("Extracting value from non-existent reference data entry"));
247 virtual ::testing::AssertionResult
248 checkEntry(const ReferenceDataEntry &entry, const std::string &) const
250 extractValue(entry.value());
251 return ::testing::AssertionSuccess();
254 void extractValue(const std::string &value) const
256 *value_ = fromString<ValueType>(value);
259 private:
260 ValueType *value_;
263 template <> inline void
264 ValueExtractor<std::string>::extractValue(const std::string &value) const
266 *value_ = value;
269 } // namespace test
270 } // namespace gmx
272 #endif