a31e6ead193dac9b32ffc6ee3139e5e8e708fca4
[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, 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{ { 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL,
127                                        0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL } };
128
129 /*! \brief Constant array of integers with bitpattern from Pi.
130  *
131  *  Reference key and counter input data for known answers test.
132  *  The 2x64 flavors of ThreeFry64 will use the first four values, while
133  *  the 4x64 version uses all eight.
134  */
135 const std::vector<uint64_t> bitsPi{ { 0x243f6a8885a308d3ULL, 0x13198a2e03707344ULL,
136                                       0xa4093822299f31d0ULL, 0x082efa98ec4e6c89ULL } };
137
138 // Test the known ansers for the ThreeFry random function when the argument
139 // is (1) all zero, (2) all ones, (3) the bits of pi, for a bunch of different flavors of ThreeFry.
140 INSTANTIATE_TEST_CASE_P(KnownAnswersTest, ThreeFry2x64Test, ::testing::Values(bitsZero, bitsOne, bitsPi));
141
142
143 // ThreeFry2x64 tests
144 TEST_F(ThreeFry2x64Test, Logical)
145 {
146     gmx::ThreeFry2x64<10> rngA(123456, gmx::RandomDomain::Other);
147     gmx::ThreeFry2x64<10> rngB(123456, gmx::RandomDomain::Other);
148     gmx::ThreeFry2x64<10> rngC(123456, gmx::RandomDomain::Other);
149
150     rngB(); // draw just once first, so block is the same, but index has changed
151     EXPECT_NE(rngA, rngB);
152     rngC();
153     rngC(); // two draws: next block, but index is the same
154     EXPECT_NE(rngA, rngC);
155     rngA();
156     EXPECT_EQ(rngA, rngB);
157     rngA();
158     EXPECT_EQ(rngA, rngC);
159 }
160
161 TEST_F(ThreeFry2x64Test, InternalCounterSequence)
162 {
163     gmx::test::TestReferenceData    data;
164     gmx::test::TestReferenceChecker checker(data.rootChecker());
165
166     // 66 bits of internal counter means the first four increments (giving 2*4=8 results)
167     // correspond to incrementing word 0, and then we should carry over to word 1.
168     gmx::ThreeFry2x64<66> rngA(123456, gmx::RandomDomain::Other);
169     std::vector<uint64_t> result;
170
171     result.reserve(16);
172     for (int i = 0; i < 16; i++)
173     {
174         result.push_back(rngA());
175     }
176     checker.checkSequence(result.begin(), result.end(), "ThreeFry2x64InternalCounterSequence");
177
178     // Make sure nothing goes wrong with the internal counter sequence when we use a full 64-bit word
179     gmx::ThreeFry2x64<64> rngB(123456, gmx::RandomDomain::Other);
180     for (int i = 0; i < 16; i++)
181     {
182         rngB();
183     }
184
185     // Use every single bit for the internal counter
186     gmx::ThreeFry2x64<128> rngC(123456, gmx::RandomDomain::Other);
187     for (int i = 0; i < 16; i++)
188     {
189         rngC();
190     }
191 }
192
193 TEST_F(ThreeFry2x64Test, Reseed)
194 {
195     gmx::ThreeFry2x64<10> rngA(123456, gmx::RandomDomain::Other);
196     gmx::ThreeFry2x64<10> rngB;
197
198     EXPECT_NE(rngA, rngB);
199     rngB.seed(123456, gmx::RandomDomain::Other);
200     EXPECT_EQ(rngA, rngB);
201     rngB();                                      // internal counter increments
202     rngB.seed(123456, gmx::RandomDomain::Other); // reseeding should reset random stream too
203     EXPECT_EQ(rngA, rngB);
204 }
205
206 TEST_F(ThreeFry2x64Test, Discard)
207 {
208     gmx::ThreeFry2x64<10> rngA(123456, gmx::RandomDomain::Other);
209     gmx::ThreeFry2x64<10> rngB(123456, gmx::RandomDomain::Other);
210
211     for (int i = 0; i < 9; i++)
212     {
213         rngA();
214     }
215     rngB.discard(9);
216     EXPECT_EQ(rngA, rngB);
217 }
218
219
220 TEST_F(ThreeFry2x64Test, InvalidCounter)
221 {
222     gmx::ThreeFry2x64<10> rngA(123456, gmx::RandomDomain::Other);
223
224     // Highest 10 bits of counter reserved for the internal counter.
225     EXPECT_THROW_GMX(rngA.restart(0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF), gmx::InternalError);
226 }
227
228 TEST_F(ThreeFry2x64Test, ExhaustInternalCounter)
229 {
230     gmx::ThreeFry2x64<2> rngA(123456, gmx::RandomDomain::Other);
231
232     // 2 bits for internal counter and 2 64-results per counter means 8 results are fine
233     for (int i = 0; i < 8; i++)
234     {
235         rngA();
236     }
237     // ... but the 9th time we have exhausted the internal counter space.
238     EXPECT_THROW_GMX(rngA(), gmx::InternalError);
239 }
240
241 } // namespace
242
243 } // namespace gmx