Reimplement constant acceleration groups
[alexxy/gromacs.git] / src / testutils / testmatchers.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2018,2019,2020,2021, 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
36 /*! \internal \file
37  * \brief Implements floating-point matchers from testmatchers.h for
38  * use with Google Mock.
39  *
40  * \author Mark Abraham <mark.j.abraham@gmail.com>
41  * \ingroup module_testutils
42  */
43 #include "gmxpre.h"
44
45 #include "testutils/testmatchers.h"
46
47 #include <memory>
48
49 #include <gmock/gmock.h>
50
51 #include "gromacs/math/vectypes.h"
52
53 #include "testutils/testasserts.h"
54
55 namespace gmx
56 {
57 namespace test
58 {
59
60 /*! \brief Implementation class for RealEq matcher.
61  *
62  * See RealEq().
63  *
64  * The implementation is templated so that we can support all of real,
65  * float and double in the same build without duplication.
66  */
67 template<typename FloatType>
68 class FloatTypeMatcher : public testing::MatcherInterface<std::tuple<FloatType, FloatType>>
69 {
70 public:
71     //! Constructor
72     FloatTypeMatcher(const FloatingPointTolerance& tolerance) : tolerance_(tolerance) {}
73     //! Compare the two elements of \c arg, return whether they are equal, and comment on \c listener when they are not.
74     bool MatchAndExplain(std::tuple<FloatType, FloatType> arg, testing::MatchResultListener* listener) const override
75     {
76         const FloatType&        value1 = std::get<0>(arg);
77         const FloatType&        value2 = std::get<1>(arg);
78         FloatingPointDifference diff(value1, value2);
79         if (tolerance_.isWithin(diff))
80         {
81             return true;
82         }
83         if (listener->stream())
84         {
85             *listener->stream() << "  Actual value: " << value2 << std::endl
86                                 << "Expected value: " << value1 << std::endl
87                                 << "    Difference: " << diff.toString() << std::endl
88                                 << "     Tolerance: " << tolerance_.toString(diff);
89         }
90
91         return false;
92     }
93     //! Describe to a human what matching means.
94     void DescribeTo(::std::ostream* os) const override { *os << "matches within tolerance"; }
95     //! Describe to a human what failing to match means.
96     void DescribeNegationTo(::std::ostream* os) const override
97     {
98         *os << "does not match within tolerance";
99     }
100
101 private:
102     //! Tolerance used in matching
103     FloatingPointTolerance tolerance_;
104 };
105
106 testing::Matcher<std::tuple<float, float>> FloatEq(const FloatingPointTolerance& tolerance)
107 {
108     return testing::MakeMatcher(new FloatTypeMatcher<float>(tolerance));
109 }
110
111 testing::Matcher<std::tuple<double, double>> DoubleEq(const FloatingPointTolerance& tolerance)
112 {
113     return testing::MakeMatcher(new FloatTypeMatcher<double>(tolerance));
114 }
115
116 testing::Matcher<std::tuple<real, real>> RealEq(const FloatingPointTolerance& tolerance)
117 {
118     return testing::MakeMatcher(new FloatTypeMatcher<real>(tolerance));
119 }
120
121 /*! \brief Implementation class for RvecEq matcher
122  *
123  * See RvecEq().
124  */
125 template<typename FloatType>
126 class RVecMatcher :
127     public testing::MatcherInterface<std::tuple<BasicVector<FloatType>, BasicVector<FloatType>>>
128 {
129 public:
130     //! Convenience type
131     using VectorType = BasicVector<FloatType>;
132     //! Constructor
133     RVecMatcher(const FloatingPointTolerance& tolerance) : tolerance_(tolerance) {}
134     //! Compare the two elements of \c arg, return whether they are equal, and comment on \c listener when they are not.
135     bool MatchAndExplain(std::tuple<VectorType, VectorType> arg,
136                          testing::MatchResultListener*      listener) const override
137     {
138         const VectorType&           lhs = std::get<0>(arg);
139         const VectorType&           rhs = std::get<1>(arg);
140         FloatTypeMatcher<FloatType> floatTypeMatcher(tolerance_);
141         bool                        matches = true;
142         for (int d = 0; d < DIM; ++d)
143         {
144             auto floatTuple = std::make_tuple<FloatType, FloatType>(lhs[d], rhs[d]);
145             matches         = matches && floatTypeMatcher.MatchAndExplain(floatTuple, listener);
146         }
147         return matches;
148     }
149     //! Describe to a human what matching means.
150     void DescribeTo(::std::ostream* os) const override
151     {
152         *os << "matches all elements within tolerance";
153     }
154     //! Describe to a human what failing to match means.
155     void DescribeNegationTo(::std::ostream* os) const override
156     {
157         *os << "does not match all elements within tolerance";
158     }
159
160 private:
161     //! Tolerance used in matching
162     FloatingPointTolerance tolerance_;
163 };
164
165 // Currently there's no need for explicit float or double flavours of
166 // RVec comparison, but those would be simple to add later.
167 testing::Matcher<std::tuple<RVec, RVec>> RVecEq(const FloatingPointTolerance& tolerance)
168 {
169     return testing::MakeMatcher(new RVecMatcher<real>(tolerance));
170 }
171
172 } // namespace test
173 } // namespace gmx