f8c69f791a6cfd1a522fbb31d58db16b4fabda63
[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, 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/refdata_impl.h"
59 #include "testutils/testasserts.h"
60 #include "testutils/testexceptions.h"
61
62 namespace gmx
63 {
64 namespace test
65 {
66
67 class IReferenceDataEntryChecker
68 {
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;
73
74 protected:
75     virtual ~IReferenceDataEntryChecker() {}
76 };
77
78 class NullChecker : public IReferenceDataEntryChecker
79 {
80 public:
81     void                       fillEntry(ReferenceDataEntry* /*entry*/) const override {}
82     ::testing::AssertionResult checkEntry(const ReferenceDataEntry& /*entry*/,
83                                           const std::string& /*fullId*/) const override
84     {
85         return ::testing::AssertionSuccess();
86     }
87 };
88
89 class ExactStringChecker : public IReferenceDataEntryChecker
90 {
91 public:
92     explicit ExactStringChecker(const std::string& value) : value_(value) {}
93
94     void fillEntry(ReferenceDataEntry* entry) const override { entry->setValue(value_); }
95     ::testing::AssertionResult checkEntry(const ReferenceDataEntry& entry, const std::string& fullId) const override
96     {
97         if (entry.value() == value_)
98         {
99             return ::testing::AssertionSuccess();
100         }
101         return ::testing::AssertionFailure() << "  In item: " << fullId << std::endl
102                                              << "   Actual: '" << value_ << "'" << std::endl
103                                              << "Reference: '" << entry.value() << "'";
104     }
105
106 private:
107     std::string value_;
108 };
109
110 class ExactStringBlockChecker : public IReferenceDataEntryChecker
111 {
112 public:
113     explicit ExactStringBlockChecker(const std::string& value) : value_(value) {}
114
115     void fillEntry(ReferenceDataEntry* entry) const override { entry->setTextBlockValue(value_); }
116     ::testing::AssertionResult checkEntry(const ReferenceDataEntry& entry, const std::string& fullId) const override
117     {
118         if (entry.value() == value_)
119         {
120             return ::testing::AssertionSuccess();
121         }
122         return ::testing::AssertionFailure() << "  In item: " << fullId << std::endl
123                                              << "   Actual: '" << value_ << "'" << std::endl
124                                              << "Reference: '" << entry.value() << "'";
125     }
126
127 private:
128     std::string value_;
129 };
130
131
132 //! Helper function to parse a floating-point reference data value.
133 static inline double convertDoubleReferenceValue(const std::string& value)
134 {
135     try
136     {
137         return fromString<double>(value);
138     }
139     catch (const InvalidInputError& ex)
140     {
141         GMX_THROW_WRAPPER_TESTEXCEPTION(ex);
142     }
143 }
144
145 template<typename FloatType>
146 class FloatingPointChecker : public IReferenceDataEntryChecker
147 {
148 public:
149     FloatingPointChecker(FloatType value, const FloatingPointTolerance& tolerance) :
150         value_(value),
151         tolerance_(tolerance)
152     {
153     }
154
155     void fillEntry(ReferenceDataEntry* entry) const override
156     {
157         const int prec = std::numeric_limits<FloatType>::digits10 + 2;
158         entry->setValue(formatString("%.*g", prec, value_));
159     }
160     ::testing::AssertionResult checkEntry(const ReferenceDataEntry& entry, const std::string& fullId) const override
161     {
162         FloatType refValue = static_cast<FloatType>(convertDoubleReferenceValue(entry.value()));
163         FloatingPointDifference diff(refValue, value_);
164         if (tolerance_.isWithin(diff))
165         {
166             return ::testing::AssertionSuccess();
167         }
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);
173     }
174
175 private:
176     FloatType              value_;
177     FloatingPointTolerance tolerance_;
178 };
179
180 template<typename FloatType>
181 class FloatingPointFromStringChecker : public IReferenceDataEntryChecker
182 {
183 public:
184     FloatingPointFromStringChecker(const std::string& value, const FloatingPointTolerance& tolerance) :
185         value_(value),
186         tolerance_(tolerance)
187     {
188     }
189
190     void fillEntry(ReferenceDataEntry* entry) const override { entry->setValue(value_); }
191     ::testing::AssertionResult checkEntry(const ReferenceDataEntry& entry, const std::string& fullId) const override
192     {
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))
197         {
198             return ::testing::AssertionSuccess();
199         }
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);
205     }
206
207 private:
208     std::string            value_;
209     FloatingPointTolerance tolerance_;
210 };
211
212 template<typename ValueType>
213 class ValueExtractor : public IReferenceDataEntryChecker
214 {
215 public:
216     explicit ValueExtractor(ValueType* value) : value_(value) {}
217
218     void fillEntry(ReferenceDataEntry* /*entry*/) const override
219     {
220         GMX_THROW(TestException("Extracting value from non-existent reference data entry"));
221     }
222     ::testing::AssertionResult checkEntry(const ReferenceDataEntry& entry,
223                                           const std::string& /*fullId*/) const override
224     {
225         extractValue(entry.value());
226         return ::testing::AssertionSuccess();
227     }
228
229     void extractValue(const std::string& value) const { *value_ = fromString<ValueType>(value); }
230
231 private:
232     ValueType* value_;
233 };
234
235 template<>
236 inline void ValueExtractor<std::string>::extractValue(const std::string& value) const
237 {
238     *value_ = value;
239 }
240
241 } // namespace test
242 } // namespace gmx
243
244 #endif