2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 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.
37 * \brief Implements floating-point matchers from testmatchers.h for
38 * use with Google Mock.
40 * \author Mark Abraham <mark.j.abraham@gmail.com>
41 * \ingroup module_testutils
45 #include "testmatchers.h"
49 #include <gmock/gmock.h>
51 #include "testutils/testasserts.h"
58 /*! \brief Implementation class for RealEq matcher.
62 * The implementation is templated so that we can support all of real,
63 * float and double in the same build without duplication.
65 template<typename FloatType>
66 class FloatTypeMatcher : public testing::MatcherInterface<std::tuple<FloatType, FloatType>>
70 FloatTypeMatcher(const FloatingPointTolerance& tolerance) : tolerance_(tolerance) {}
71 //! Compare the two elements of \c arg, return whether they are equal, and comment on \c listener when they are not.
72 bool MatchAndExplain(std::tuple<FloatType, FloatType> arg, testing::MatchResultListener* listener) const override
74 const FloatType& value1 = std::get<0>(arg);
75 const FloatType& value2 = std::get<1>(arg);
76 FloatingPointDifference diff(value1, value2);
77 if (tolerance_.isWithin(diff))
81 *listener->stream() << " Actual value: " << value2 << std::endl
82 << "Expected value: " << value1 << std::endl
83 << " Difference: " << diff.toString() << std::endl
84 << " Tolerance: " << tolerance_.toString(diff);
87 //! Describe to a human what matching means.
88 void DescribeTo(::std::ostream* os) const override { *os << "matches within tolerance"; }
89 //! Describe to a human what failing to match means.
90 void DescribeNegationTo(::std::ostream* os) const override
92 *os << "does not match within tolerance";
96 //! Tolerance used in matching
97 FloatingPointTolerance tolerance_;
100 testing::Matcher<std::tuple<float, float>> FloatEq(const FloatingPointTolerance& tolerance)
102 return testing::MakeMatcher(new FloatTypeMatcher<float>(tolerance));
105 testing::Matcher<std::tuple<double, double>> DoubleEq(const FloatingPointTolerance& tolerance)
107 return testing::MakeMatcher(new FloatTypeMatcher<double>(tolerance));
110 testing::Matcher<std::tuple<real, real>> RealEq(const FloatingPointTolerance& tolerance)
112 return testing::MakeMatcher(new FloatTypeMatcher<real>(tolerance));
115 /*! \brief Implementation class for RvecEq matcher
119 template<typename FloatType>
121 public testing::MatcherInterface<std::tuple<BasicVector<FloatType>, BasicVector<FloatType>>>
125 using VectorType = BasicVector<FloatType>;
127 RVecMatcher(const FloatingPointTolerance& tolerance) : tolerance_(tolerance) {}
128 //! Compare the two elements of \c arg, return whether they are equal, and comment on \c listener when they are not.
129 bool MatchAndExplain(std::tuple<VectorType, VectorType> arg,
130 testing::MatchResultListener* listener) const override
132 const VectorType& lhs = std::get<0>(arg);
133 const VectorType& rhs = std::get<1>(arg);
134 FloatTypeMatcher<FloatType> floatTypeMatcher(tolerance_);
136 for (int d = 0; d < DIM; ++d)
138 auto floatTuple = std::make_tuple<FloatType, FloatType>(lhs[d], rhs[d]);
139 matches = matches && floatTypeMatcher.MatchAndExplain(floatTuple, listener);
143 //! Describe to a human what matching means.
144 void DescribeTo(::std::ostream* os) const override
146 *os << "matches all elements within tolerance";
148 //! Describe to a human what failing to match means.
149 void DescribeNegationTo(::std::ostream* os) const override
151 *os << "does not match all elements within tolerance";
155 //! Tolerance used in matching
156 FloatingPointTolerance tolerance_;
159 // Currently there's no need for explicit float or double flavours of
160 // RVec comparison, but those would be simple to add later.
161 testing::Matcher<std::tuple<RVec, RVec>> RVecEq(const FloatingPointTolerance& tolerance)
163 return testing::MakeMatcher(new RVecMatcher<real>(tolerance));