Separate the testutils module.
[alexxy/gromacs.git] / src / testutils / refdata_checkers.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2015,2016,2017,2018,2019,2020, 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.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35 /*! \internal \file
36  * \brief
37  * Declares internal helper classes for the reference data framework to check
38  * reference data values of different types.
39  *
40  * \author Teemu Murtola <teemu.murtola@gmail.com>
41  * \ingroup module_testutils
42  */
43 #ifndef GMX_TESTUTILS_REFDATA_CHECKERS_H
44 #define GMX_TESTUTILS_REFDATA_CHECKERS_H
45
46 #include <cstdlib>
47
48 #include <limits>
49 #include <string>
50
51 #include <gtest/gtest.h>
52
53 #include "gromacs/utility/basedefinitions.h"
54 #include "gromacs/utility/exceptions.h"
55 #include "gromacs/utility/strconvert.h"
56 #include "gromacs/utility/stringutil.h"
57
58 #include "testutils/testasserts.h"
59 #include "testutils/testexceptions.h"
60
61 #include "refdata_impl.h"
62
63 namespace gmx
64 {
65 namespace test
66 {
67
68 class IReferenceDataEntryChecker
69 {
70 public:
71     virtual void                       fillEntry(ReferenceDataEntry* entry) const  = 0;
72     virtual ::testing::AssertionResult checkEntry(const ReferenceDataEntry& entry,
73                                                   const std::string&        fullId) const = 0;
74
75 protected:
76     virtual ~IReferenceDataEntryChecker() {}
77 };
78
79 class NullChecker : public IReferenceDataEntryChecker
80 {
81 public:
82     void                       fillEntry(ReferenceDataEntry* /*entry*/) const override {}
83     ::testing::AssertionResult checkEntry(const ReferenceDataEntry& /*entry*/,
84                                           const std::string& /*fullId*/) const override
85     {
86         return ::testing::AssertionSuccess();
87     }
88 };
89
90 class ExactStringChecker : public IReferenceDataEntryChecker
91 {
92 public:
93     explicit ExactStringChecker(const std::string& value) : value_(value) {}
94
95     void fillEntry(ReferenceDataEntry* entry) const override { entry->setValue(value_); }
96     ::testing::AssertionResult checkEntry(const ReferenceDataEntry& entry, const std::string& fullId) const override
97     {
98         if (entry.value() == value_)
99         {
100             return ::testing::AssertionSuccess();
101         }
102         return ::testing::AssertionFailure() << "  In item: " << fullId << std::endl
103                                              << "   Actual: '" << value_ << "'" << std::endl
104                                              << "Reference: '" << entry.value() << "'";
105     }
106
107 private:
108     std::string value_;
109 };
110
111 class ExactStringBlockChecker : public IReferenceDataEntryChecker
112 {
113 public:
114     explicit ExactStringBlockChecker(const std::string& value) : value_(value) {}
115
116     void fillEntry(ReferenceDataEntry* entry) const override { entry->setTextBlockValue(value_); }
117     ::testing::AssertionResult checkEntry(const ReferenceDataEntry& entry, const std::string& fullId) const override
118     {
119         if (entry.value() == value_)
120         {
121             return ::testing::AssertionSuccess();
122         }
123         return ::testing::AssertionFailure() << "  In item: " << fullId << std::endl
124                                              << "   Actual: '" << value_ << "'" << std::endl
125                                              << "Reference: '" << entry.value() << "'";
126     }
127
128 private:
129     std::string value_;
130 };
131
132
133 //! Helper function to parse a floating-point reference data value.
134 static inline double convertDoubleReferenceValue(const std::string& value)
135 {
136     try
137     {
138         return fromString<double>(value);
139     }
140     catch (const InvalidInputError& ex)
141     {
142         GMX_THROW_WRAPPER_TESTEXCEPTION(ex);
143     }
144 }
145
146 template<typename FloatType>
147 class FloatingPointChecker : public IReferenceDataEntryChecker
148 {
149 public:
150     FloatingPointChecker(FloatType value, const FloatingPointTolerance& tolerance) :
151         value_(value),
152         tolerance_(tolerance)
153     {
154     }
155
156     void fillEntry(ReferenceDataEntry* entry) const override
157     {
158         const int prec = std::numeric_limits<FloatType>::digits10 + 2;
159         entry->setValue(formatString("%.*g", prec, value_));
160     }
161     ::testing::AssertionResult checkEntry(const ReferenceDataEntry& entry, const std::string& fullId) const override
162     {
163         FloatType refValue = static_cast<FloatType>(convertDoubleReferenceValue(entry.value()));
164         FloatingPointDifference diff(refValue, value_);
165         if (tolerance_.isWithin(diff))
166         {
167             return ::testing::AssertionSuccess();
168         }
169         return ::testing::AssertionFailure() << "   In item: " << fullId << std::endl
170                                              << "    Actual: " << value_ << std::endl
171                                              << " Reference: " << refValue << std::endl
172                                              << "Difference: " << diff.toString() << std::endl
173                                              << " Tolerance: " << tolerance_.toString(diff);
174     }
175
176 private:
177     FloatType              value_;
178     FloatingPointTolerance tolerance_;
179 };
180
181 template<typename FloatType>
182 class FloatingPointFromStringChecker : public IReferenceDataEntryChecker
183 {
184 public:
185     FloatingPointFromStringChecker(const std::string& value, const FloatingPointTolerance& tolerance) :
186         value_(value),
187         tolerance_(tolerance)
188     {
189     }
190
191     void fillEntry(ReferenceDataEntry* entry) const override { entry->setValue(value_); }
192     ::testing::AssertionResult checkEntry(const ReferenceDataEntry& entry, const std::string& fullId) const override
193     {
194         FloatType value    = fromString<FloatType>(value_);
195         FloatType refValue = static_cast<FloatType>(convertDoubleReferenceValue(entry.value()));
196         FloatingPointDifference diff(refValue, value);
197         if (tolerance_.isWithin(diff))
198         {
199             return ::testing::AssertionSuccess();
200         }
201         return ::testing::AssertionFailure() << "   In item: " << fullId << std::endl
202                                              << "    Actual: " << value << std::endl
203                                              << " Reference: " << entry.value() << std::endl
204                                              << "Difference: " << diff.toString() << std::endl
205                                              << " Tolerance: " << tolerance_.toString(diff);
206     }
207
208 private:
209     std::string            value_;
210     FloatingPointTolerance tolerance_;
211 };
212
213 template<typename ValueType>
214 class ValueExtractor : public IReferenceDataEntryChecker
215 {
216 public:
217     explicit ValueExtractor(ValueType* value) : value_(value) {}
218
219     void fillEntry(ReferenceDataEntry* /*entry*/) const override
220     {
221         GMX_THROW(TestException("Extracting value from non-existent reference data entry"));
222     }
223     ::testing::AssertionResult checkEntry(const ReferenceDataEntry& entry,
224                                           const std::string& /*fullId*/) const override
225     {
226         extractValue(entry.value());
227         return ::testing::AssertionSuccess();
228     }
229
230     void extractValue(const std::string& value) const { *value_ = fromString<ValueType>(value); }
231
232 private:
233     ValueType* value_;
234 };
235
236 template<>
237 inline void ValueExtractor<std::string>::extractValue(const std::string& value) const
238 {
239     *value_ = value;
240 }
241
242 } // namespace test
243 } // namespace gmx
244
245 #endif