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