Merge branch release-2019 into release-2020
[gromacs.git] / src / testutils / refdata_checkers.h
blobf8c69f791a6cfd1a522fbb31d58db16b4fabda63
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2015,2016,2017,2018,2019, 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 checkEntry(const ReferenceDataEntry& entry,
72 const std::string& fullId) const = 0;
74 protected:
75 virtual ~IReferenceDataEntryChecker() {}
78 class NullChecker : public IReferenceDataEntryChecker
80 public:
81 void fillEntry(ReferenceDataEntry* /*entry*/) const override {}
82 ::testing::AssertionResult checkEntry(const ReferenceDataEntry& /*entry*/,
83 const std::string& /*fullId*/) const override
85 return ::testing::AssertionSuccess();
89 class ExactStringChecker : public IReferenceDataEntryChecker
91 public:
92 explicit ExactStringChecker(const std::string& value) : value_(value) {}
94 void fillEntry(ReferenceDataEntry* entry) const override { entry->setValue(value_); }
95 ::testing::AssertionResult checkEntry(const ReferenceDataEntry& entry, const std::string& fullId) const override
97 if (entry.value() == value_)
99 return ::testing::AssertionSuccess();
101 return ::testing::AssertionFailure() << " In item: " << fullId << std::endl
102 << " Actual: '" << value_ << "'" << std::endl
103 << "Reference: '" << entry.value() << "'";
106 private:
107 std::string value_;
110 class ExactStringBlockChecker : public IReferenceDataEntryChecker
112 public:
113 explicit ExactStringBlockChecker(const std::string& value) : value_(value) {}
115 void fillEntry(ReferenceDataEntry* entry) const override { entry->setTextBlockValue(value_); }
116 ::testing::AssertionResult checkEntry(const ReferenceDataEntry& entry, const std::string& fullId) const override
118 if (entry.value() == value_)
120 return ::testing::AssertionSuccess();
122 return ::testing::AssertionFailure() << " In item: " << fullId << std::endl
123 << " Actual: '" << value_ << "'" << std::endl
124 << "Reference: '" << entry.value() << "'";
127 private:
128 std::string value_;
132 //! Helper function to parse a floating-point reference data value.
133 static inline double convertDoubleReferenceValue(const std::string& value)
137 return fromString<double>(value);
139 catch (const InvalidInputError& ex)
141 GMX_THROW_WRAPPER_TESTEXCEPTION(ex);
145 template<typename FloatType>
146 class FloatingPointChecker : public IReferenceDataEntryChecker
148 public:
149 FloatingPointChecker(FloatType value, const FloatingPointTolerance& tolerance) :
150 value_(value),
151 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);
175 private:
176 FloatType value_;
177 FloatingPointTolerance tolerance_;
180 template<typename FloatType>
181 class FloatingPointFromStringChecker : public IReferenceDataEntryChecker
183 public:
184 FloatingPointFromStringChecker(const std::string& value, const FloatingPointTolerance& tolerance) :
185 value_(value),
186 tolerance_(tolerance)
190 void fillEntry(ReferenceDataEntry* entry) const override { entry->setValue(value_); }
191 ::testing::AssertionResult checkEntry(const ReferenceDataEntry& entry, const std::string& fullId) const override
193 FloatType value = fromString<FloatType>(value_);
194 FloatType refValue = static_cast<FloatType>(convertDoubleReferenceValue(entry.value()));
195 FloatingPointDifference diff(refValue, value);
196 if (tolerance_.isWithin(diff))
198 return ::testing::AssertionSuccess();
200 return ::testing::AssertionFailure() << " In item: " << fullId << std::endl
201 << " Actual: " << value << std::endl
202 << " Reference: " << entry.value() << std::endl
203 << "Difference: " << diff.toString() << std::endl
204 << " Tolerance: " << tolerance_.toString(diff);
207 private:
208 std::string value_;
209 FloatingPointTolerance tolerance_;
212 template<typename ValueType>
213 class ValueExtractor : public IReferenceDataEntryChecker
215 public:
216 explicit ValueExtractor(ValueType* value) : value_(value) {}
218 void fillEntry(ReferenceDataEntry* /*entry*/) const override
220 GMX_THROW(TestException("Extracting value from non-existent reference data entry"));
222 ::testing::AssertionResult checkEntry(const ReferenceDataEntry& entry,
223 const std::string& /*fullId*/) const override
225 extractValue(entry.value());
226 return ::testing::AssertionSuccess();
229 void extractValue(const std::string& value) const { *value_ = fromString<ValueType>(value); }
231 private:
232 ValueType* value_;
235 template<>
236 inline void ValueExtractor<std::string>::extractValue(const std::string& value) const
238 *value_ = value;
241 } // namespace test
242 } // namespace gmx
244 #endif