Apply clang-format to source tree
[alexxy/gromacs.git] / src / gromacs / math / tests / exponentialmovingaverage.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 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  * Tests for the exponential moving average.
38  *
39  * \author Christian Blau <blau@kth.se>
40  * \ingroup module_math
41  */
42 #include "gmxpre.h"
43
44 #include "gromacs/math/exponentialmovingaverage.h"
45
46 #include <gtest/gtest.h>
47
48 #include "testutils/testasserts.h"
49
50 namespace gmx
51 {
52
53 namespace test
54 {
55
56 namespace
57 {
58
59 TEST(ExponentialMovingAverage, ThrowsWhenLagTimeIsZero)
60 {
61     EXPECT_THROW_GMX(ExponentialMovingAverage(0), InconsistentInputError);
62 }
63
64 TEST(ExponentialMovingAverage, ThrowsWhenLagTimeIsNegative)
65 {
66     EXPECT_THROW_GMX(ExponentialMovingAverage(-10), InconsistentInputError);
67 }
68
69 TEST(ExponentialMovingAverage, LagTimeOneYieldsInstantaneousValue)
70 {
71     const real               lagTime = 1;
72     ExponentialMovingAverage exponentialMovingAverage(lagTime);
73
74     exponentialMovingAverage.updateWithDataPoint(10);
75     EXPECT_REAL_EQ(10, exponentialMovingAverage.biasCorrectedAverage());
76
77     exponentialMovingAverage.updateWithDataPoint(-10);
78     EXPECT_REAL_EQ(-10, exponentialMovingAverage.biasCorrectedAverage());
79 }
80
81 TEST(ExponentialMovingAverage, YieldsCorrectValue)
82 {
83     const real               lagTime = 100;
84     ExponentialMovingAverage exponentialMovingAverage(lagTime);
85
86     exponentialMovingAverage.updateWithDataPoint(10);
87     EXPECT_REAL_EQ(10, exponentialMovingAverage.biasCorrectedAverage());
88
89     exponentialMovingAverage.updateWithDataPoint(-10);
90     EXPECT_REAL_EQ(-0.050251256281406857, exponentialMovingAverage.biasCorrectedAverage());
91
92     exponentialMovingAverage.updateWithDataPoint(0);
93     EXPECT_REAL_EQ(-0.03333221103666531, exponentialMovingAverage.biasCorrectedAverage());
94 }
95
96 TEST(ExponentialMovingAverage, SetAverageCorrectly)
97 {
98     const real               lagTime = 100;
99     ExponentialMovingAverage exponentialMovingAverage(lagTime);
100
101     exponentialMovingAverage.updateWithDataPoint(10);
102     EXPECT_REAL_EQ(10, exponentialMovingAverage.biasCorrectedAverage());
103
104     ExponentialMovingAverageState thisState = exponentialMovingAverage.state();
105
106     ExponentialMovingAverage other(lagTime, thisState);
107
108     other.updateWithDataPoint(-10);
109     EXPECT_REAL_EQ(-0.050251256281406857, other.biasCorrectedAverage());
110
111     other.updateWithDataPoint(0);
112     EXPECT_REAL_EQ(-0.03333221103666531, other.biasCorrectedAverage());
113 }
114
115 TEST(ExponentialMovingAverage, DeterminesCorrectlyIfIncreasing)
116 {
117     const real               lagTime = 100;
118     ExponentialMovingAverage exponentialMovingAverage(lagTime);
119
120     exponentialMovingAverage.updateWithDataPoint(10);
121     exponentialMovingAverage.updateWithDataPoint(10);
122
123     EXPECT_FALSE(exponentialMovingAverage.increasing());
124
125     exponentialMovingAverage.updateWithDataPoint(-10);
126
127     EXPECT_FALSE(exponentialMovingAverage.increasing());
128
129     exponentialMovingAverage.updateWithDataPoint(100);
130     EXPECT_TRUE(exponentialMovingAverage.increasing());
131 }
132
133
134 TEST(ExponentialMovingAverage, InverseLagTimeCorrect)
135 {
136     const real               lagTime = 2.;
137     ExponentialMovingAverage exponentialMovingAverage(lagTime);
138     EXPECT_REAL_EQ(0.5, exponentialMovingAverage.inverseTimeConstant());
139 }
140
141 TEST(ExponentialMovingAverage, RoundTripAsKeyValueTree)
142 {
143     KeyValueTreeBuilder           builder;
144     const real                    weightedSum   = 9;
145     const real                    weightedCount = 1;
146     const bool                    increasing    = true;
147     ExponentialMovingAverageState state         = { weightedSum, weightedCount, increasing };
148     exponentialMovingAverageStateAsKeyValueTree(builder.rootObject(), state);
149     state                     = {};
150     KeyValueTreeObject result = builder.build();
151     state                     = exponentialMovingAverageStateFromKeyValueTree(result);
152     EXPECT_EQ(weightedSum, state.weightedSum_);
153     EXPECT_EQ(weightedCount, state.weightedCount_);
154     EXPECT_EQ(increasing, state.increasing_);
155 }
156
157 } // namespace
158
159 } // namespace test
160
161 } // namespace gmx