Update bundled GoogleTest to current HEAD
[alexxy/gromacs.git] / src / gromacs / random / tests / threefry.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2015,2016,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 /*! \internal \file
36  * \brief Tests for the ThreeFry random engine
37  *
38  * \author Erik Lindahl <erik.lindahl@gmail.com>
39  * \ingroup module_random
40  */
41 #include "gmxpre.h"
42
43 #include "gromacs/random/threefry.h"
44
45 #include <gtest/gtest.h>
46
47 #include "gromacs/utility/exceptions.h"
48
49 #include "testutils/refdata.h"
50 #include "testutils/testasserts.h"
51
52 namespace gmx
53 {
54
55 namespace
56 {
57
58 class ThreeFry2x64Test : public ::testing::TestWithParam<std::vector<uint64_t>>
59 {
60 };
61
62 TEST_P(ThreeFry2x64Test, Default)
63 {
64     gmx::test::TestReferenceData    data;
65     gmx::test::TestReferenceChecker checker(data.rootChecker());
66     const std::vector<uint64_t>     input = GetParam();
67     std::vector<uint64_t>           result;
68
69     gmx::ThreeFry2x64<0> rng(input[2], input[3]);
70     rng.restart(input[0], input[1]);
71
72     result.push_back(rng());
73     result.push_back(rng());
74
75     checker.checkSequence(result.begin(), result.end(), "ThreeFry2x64");
76 }
77
78 TEST_P(ThreeFry2x64Test, Fast)
79 {
80     gmx::test::TestReferenceData    data;
81     gmx::test::TestReferenceChecker checker(data.rootChecker());
82     const std::vector<uint64_t>     input = GetParam();
83     std::vector<uint64_t>           result;
84
85     gmx::ThreeFry2x64Fast<0> rng(input[2], input[3]);
86     rng.restart(input[0], input[1]);
87
88     result.push_back(rng());
89     result.push_back(rng());
90
91     checker.checkSequence(result.begin(), result.end(), "ThreeFry2x64Fast");
92 }
93
94 TEST_P(ThreeFry2x64Test, Using40Rounds)
95 {
96     gmx::test::TestReferenceData    data;
97     gmx::test::TestReferenceChecker checker(data.rootChecker());
98     const std::vector<uint64_t>     input = GetParam();
99     std::vector<uint64_t>           result;
100
101     gmx::ThreeFry2x64General<40, 0> rng(input[2], input[3]);
102     rng.restart(input[0], input[1]);
103
104     result.push_back(rng());
105     result.push_back(rng());
106
107     checker.checkSequence(result.begin(), result.end(), "ThreeFry2x64Using40Rounds");
108 }
109
110
111 /*! \brief Constant array of integers with all bits zeroed.
112  *
113  *  Reference key and counter input data for known answers test.
114  *  The 2x64 flavors of ThreeFry64 will use the first four values, while
115  *  the 4x64 version uses all eight.
116  */
117 const std::vector<uint64_t> bitsZero{ { 0, 0, 0, 0 } };
118
119
120 /*! \brief Constant array of integers with all bits set to one.
121  *
122  *  Reference key and counter input data for known answers test.
123  *  The 2x64 flavors of ThreeFry64 will use the first four values, while
124  *  the 4x64 version uses all eight.
125  */
126 const std::vector<uint64_t> bitsOne{
127     { 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL }
128 };
129
130 /*! \brief Constant array of integers with bitpattern from Pi.
131  *
132  *  Reference key and counter input data for known answers test.
133  *  The 2x64 flavors of ThreeFry64 will use the first four values, while
134  *  the 4x64 version uses all eight.
135  */
136 const std::vector<uint64_t> bitsPi{
137     { 0x243f6a8885a308d3ULL, 0x13198a2e03707344ULL, 0xa4093822299f31d0ULL, 0x082efa98ec4e6c89ULL }
138 };
139
140 // Test the known ansers for the ThreeFry random function when the argument
141 // is (1) all zero, (2) all ones, (3) the bits of pi, for a bunch of different flavors of ThreeFry.
142 INSTANTIATE_TEST_SUITE_P(KnownAnswersTest, ThreeFry2x64Test, ::testing::Values(bitsZero, bitsOne, bitsPi));
143
144
145 // ThreeFry2x64 tests
146 TEST_F(ThreeFry2x64Test, Logical)
147 {
148     gmx::ThreeFry2x64<10> rngA(123456, gmx::RandomDomain::Other);
149     gmx::ThreeFry2x64<10> rngB(123456, gmx::RandomDomain::Other);
150     gmx::ThreeFry2x64<10> rngC(123456, gmx::RandomDomain::Other);
151
152     rngB(); // draw just once first, so block is the same, but index has changed
153     EXPECT_NE(rngA, rngB);
154     rngC();
155     rngC(); // two draws: next block, but index is the same
156     EXPECT_NE(rngA, rngC);
157     rngA();
158     EXPECT_EQ(rngA, rngB);
159     rngA();
160     EXPECT_EQ(rngA, rngC);
161 }
162
163 TEST_F(ThreeFry2x64Test, InternalCounterSequence)
164 {
165     gmx::test::TestReferenceData    data;
166     gmx::test::TestReferenceChecker checker(data.rootChecker());
167
168     // 66 bits of internal counter means the first four increments (giving 2*4=8 results)
169     // correspond to incrementing word 0, and then we should carry over to word 1.
170     gmx::ThreeFry2x64<66> rngA(123456, gmx::RandomDomain::Other);
171     std::vector<uint64_t> result;
172
173     result.reserve(16);
174     for (int i = 0; i < 16; i++)
175     {
176         result.push_back(rngA());
177     }
178     checker.checkSequence(result.begin(), result.end(), "ThreeFry2x64InternalCounterSequence");
179
180     // Make sure nothing goes wrong with the internal counter sequence when we use a full 64-bit word
181     gmx::ThreeFry2x64<64> rngB(123456, gmx::RandomDomain::Other);
182     for (int i = 0; i < 16; i++)
183     {
184         rngB();
185     }
186
187     // Use every single bit for the internal counter
188     gmx::ThreeFry2x64<128> rngC(123456, gmx::RandomDomain::Other);
189     for (int i = 0; i < 16; i++)
190     {
191         rngC();
192     }
193 }
194
195 TEST_F(ThreeFry2x64Test, Reseed)
196 {
197     gmx::ThreeFry2x64<10> rngA(123456, gmx::RandomDomain::Other);
198     gmx::ThreeFry2x64<10> rngB;
199
200     EXPECT_NE(rngA, rngB);
201     rngB.seed(123456, gmx::RandomDomain::Other);
202     EXPECT_EQ(rngA, rngB);
203     rngB();                                      // internal counter increments
204     rngB.seed(123456, gmx::RandomDomain::Other); // reseeding should reset random stream too
205     EXPECT_EQ(rngA, rngB);
206 }
207
208 TEST_F(ThreeFry2x64Test, Discard)
209 {
210     gmx::ThreeFry2x64<10> rngA(123456, gmx::RandomDomain::Other);
211     gmx::ThreeFry2x64<10> rngB(123456, gmx::RandomDomain::Other);
212
213     for (int i = 0; i < 9; i++)
214     {
215         rngA();
216     }
217     rngB.discard(9);
218     EXPECT_EQ(rngA, rngB);
219 }
220
221
222 TEST_F(ThreeFry2x64Test, InvalidCounter)
223 {
224     gmx::ThreeFry2x64<10> rngA(123456, gmx::RandomDomain::Other);
225
226     // Highest 10 bits of counter reserved for the internal counter.
227     EXPECT_THROW_GMX(rngA.restart(0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF), gmx::InternalError);
228 }
229
230 TEST_F(ThreeFry2x64Test, ExhaustInternalCounter)
231 {
232     gmx::ThreeFry2x64<2> rngA(123456, gmx::RandomDomain::Other);
233
234     // 2 bits for internal counter and 2 64-results per counter means 8 results are fine
235     for (int i = 0; i < 8; i++)
236     {
237         rngA();
238     }
239     // ... but the 9th time we have exhausted the internal counter space.
240     EXPECT_THROW_GMX(rngA(), gmx::InternalError);
241 }
242
243 } // namespace
244
245 } // namespace gmx