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