Merge release-5-0 into master
[alexxy/gromacs.git] / src / testutils / testasserts.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2014, 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  * Implements floating-point comparison routines from testasserts.h.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \ingroup module_testutils
41  */
42 #include "testutils/testasserts.h"
43
44 #include <cmath>
45
46 #include <limits>
47
48 #include <gtest/gtest.h>
49
50 #include "gromacs/utility/basedefinitions.h"
51 #include "gromacs/utility/stringutil.h"
52
53 namespace gmx
54 {
55
56 namespace test
57 {
58
59 namespace
60 {
61
62 using ::testing::internal::FloatingPoint;
63
64 //! \internal \addtogroup module_testutils
65 //! \{
66
67 /*! \name Helper functions for computing floating-point differences
68  *
69  * These routines are used to initialize FloatingPointDifference.
70  * They peek into some internal types from Google Test (gtest-internal.h),
71  * and duplicate some other functionality from there, but that is likely
72  * a better alternative than just copying all that code here.
73  */
74 //! \{
75
76 /*! \brief
77  * Computes biased integer representation for a floating-point value.
78  *
79  * This moves the integer representation from a sign-and-magnitude
80  * representation to a biased representation where the 0x8000... represents
81  * zero, and the order of the integer values matches the order of the
82  * floating-point values.
83  */
84 template <typename FloatType>
85 typename FloatingPoint<FloatType>::Bits
86 floatingPointToBiasedInteger(const FloatingPoint<FloatType> &value)
87 {
88     if (value.sign_bit())
89     {
90         return ~value.bits() + 1;
91     }
92     else
93     {
94         return value.bits() | FloatingPoint<FloatType>::kSignBitMask;
95     }
96 }
97
98 /*! \brief
99  * Computes difference in ULPs between two numbers, treating also values of
100  * different sign.
101  */
102 template <typename FloatType>
103 gmx_uint64_t calculateUlpDifference(const FloatingPoint<FloatType> &value1,
104                                     const FloatingPoint<FloatType> &value2)
105 {
106     typename FloatingPoint<FloatType>::Bits biased1
107         = floatingPointToBiasedInteger(value1);
108     typename FloatingPoint<FloatType>::Bits biased2
109         = floatingPointToBiasedInteger(value2);
110     return biased1 > biased2 ? biased1 - biased2 : biased2 - biased1;
111 }
112
113 /*! \brief
114  * Helper to implement the constructors for FloatingPointDifference.
115  */
116 template <typename FloatType>
117 void initDifference(FloatType raw1, FloatType raw2, double *absoluteDifference,
118                     gmx_uint64_t *ulpDifference, bool *bSignDifference)
119 {
120     FloatingPoint<FloatType> value1(raw1);
121     FloatingPoint<FloatType> value2(raw2);
122
123     if (value1.is_nan() || value2.is_nan())
124     {
125         *absoluteDifference = std::numeric_limits<double>::quiet_NaN();
126         *bSignDifference    = false;
127         *ulpDifference      = 0;
128         return;
129     }
130     *absoluteDifference = std::fabs(raw1 - raw2);
131     *bSignDifference    = (value1.sign_bit() != value2.sign_bit());
132     *ulpDifference      = calculateUlpDifference(value1, value2);
133 }
134
135 //! \}
136 //! \}
137
138 }       // namespace
139
140 /********************************************************************
141  * FloatingPointDifference
142  */
143
144 FloatingPointDifference::FloatingPointDifference(float value1, float value2)
145 {
146     initDifference(value1, value2,
147                    &absoluteDifference_, &ulpDifference_, &bSignDifference_);
148     bDouble_ = false;
149 }
150
151 FloatingPointDifference::FloatingPointDifference(double value1, double value2)
152 {
153     initDifference(value1, value2,
154                    &absoluteDifference_, &ulpDifference_, &bSignDifference_);
155     bDouble_ = true;
156 }
157
158 bool FloatingPointDifference::isNaN() const
159 {
160     return FloatingPoint<double>(absoluteDifference_).is_nan();
161 }
162
163 std::string FloatingPointDifference::toString() const
164 {
165     return formatString("%g (%" GMX_PRIu64 " %s-prec. ULPs)%s",
166                         absoluteDifference_, ulpDifference_,
167                         bDouble_ ? "double" : "single",
168                         bSignDifference_ ? ", signs differ" : "");
169 }
170
171 /********************************************************************
172  * FloatingPointTolerance
173  */
174
175 bool FloatingPointTolerance::isWithin(
176         const FloatingPointDifference &difference) const
177 {
178     if (difference.isNaN())
179     {
180         return false;
181     }
182
183     if (bSignMustMatch_ && difference.signsDiffer())
184     {
185         return false;
186     }
187
188     if (difference.asAbsolute() < absoluteTolerance_)
189     {
190         return true;
191     }
192
193     if (ulpTolerance_ >= 0
194         && difference.asUlps() <= static_cast<gmx_uint64_t>(ulpTolerance_))
195     {
196         return true;
197     }
198     return false;
199 }
200
201 std::string FloatingPointTolerance::toString() const
202 {
203     std::string result;
204     if (absoluteTolerance_ > 0.0)
205     {
206         result.append(formatString("abs. %g", absoluteTolerance_));
207     }
208     if (ulpTolerance_ >= 0)
209     {
210         if (!result.empty())
211         {
212             result.append(", ");
213         }
214         result.append(formatString("%d ULPs", ulpTolerance_));
215     }
216     if (bSignMustMatch_)
217     {
218         if (!result.empty())
219         {
220             result.append(", ");
221         }
222         result.append("sign must match");
223     }
224     return result;
225 }
226
227 } // namespace test
228 } // namespace gmx