Merge branch release-2016
[gromacs.git] / src / gromacs / random / tests / threefry.cpp
blob2984a5cc6446a4fa3fc9c9b771025e03d22e8430
1 /*
2 * This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
35 /*! \internal \file
36 * \brief Tests for the ThreeFry random engine
38 * \author Erik Lindahl <erik.lindahl@gmail.com>
39 * \ingroup module_random
41 #include "gmxpre.h"
43 #include "gromacs/random/threefry.h"
45 #include <gtest/gtest.h>
47 #include "gromacs/utility/exceptions.h"
49 #include "testutils/refdata.h"
50 #include "testutils/testasserts.h"
52 namespace gmx
55 namespace
58 class ThreeFry2x64Test : public ::testing::TestWithParam<std::vector<gmx_uint64_t> >
62 TEST_P(ThreeFry2x64Test, Default)
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;
69 gmx::ThreeFry2x64<0> rng(input[2], input[3]);
70 rng.restart(input[0], input[1]);
72 result.push_back(rng());
73 result.push_back(rng());
75 checker.checkSequence(result.begin(), result.end(), "ThreeFry2x64");
78 TEST_P(ThreeFry2x64Test, Fast)
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;
85 gmx::ThreeFry2x64Fast<0> rng(input[2], input[3]);
86 rng.restart(input[0], input[1]);
88 result.push_back(rng());
89 result.push_back(rng());
91 checker.checkSequence(result.begin(), result.end(), "ThreeFry2x64Fast");
94 TEST_P(ThreeFry2x64Test, Using40Rounds)
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;
101 gmx::ThreeFry2x64General<40, 0> rng(input[2], input[3]);
102 rng.restart(input[0], input[1]);
104 result.push_back(rng());
105 result.push_back(rng());
107 checker.checkSequence(result.begin(), result.end(), "ThreeFry2x64Using40Rounds");
111 /*! \brief Constant array of integers with all bits zeroed.
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.
117 const std::vector<gmx_uint64_t> bitsZero {{
118 0, 0, 0, 0
122 /*! \brief Constant array of integers with all bits set to one.
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.
128 const std::vector<gmx_uint64_t> bitsOne {{
129 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL,
130 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL
133 /*! \brief Constant array of integers with bitpattern from Pi.
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.
139 const std::vector<gmx_uint64_t> bitsPi {{
140 0x243f6a8885a308d3ULL, 0x13198a2e03707344ULL,
141 0xa4093822299f31d0ULL, 0x082efa98ec4e6c89ULL
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));
150 // ThreeFry2x64 tests
151 TEST_F(ThreeFry2x64Test, Logical)
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);
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);
167 TEST_F(ThreeFry2x64Test, InternalCounterSequence)
169 gmx::test::TestReferenceData data;
170 gmx::test::TestReferenceChecker checker(data.rootChecker());
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;
177 for (int i = 0; i < 16; i++)
179 result.push_back(rngA());
181 checker.checkSequence(result.begin(), result.end(), "ThreeFry2x64InternalCounterSequence");
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++)
187 rngB();
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++)
194 rngC();
198 TEST_F(ThreeFry2x64Test, Reseed)
200 gmx::ThreeFry2x64<10> rngA(123456, gmx::RandomDomain::Other);
201 gmx::ThreeFry2x64<10> rngB;
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);
211 TEST_F(ThreeFry2x64Test, Discard)
213 gmx::ThreeFry2x64<10> rngA(123456, gmx::RandomDomain::Other);
214 gmx::ThreeFry2x64<10> rngB(123456, gmx::RandomDomain::Other);
216 for (int i = 0; i < 9; i++)
218 rngA();
220 rngB.discard(9);
221 EXPECT_EQ(rngA, rngB);
225 TEST_F(ThreeFry2x64Test, InvalidCounter)
227 gmx::ThreeFry2x64<10> rngA(123456, gmx::RandomDomain::Other);
229 // Highest 10 bits of counter reserved for the internal counter.
230 EXPECT_THROW_GMX(rngA.restart(0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF), gmx::InternalError);
233 TEST_F(ThreeFry2x64Test, ExhaustInternalCounter)
235 gmx::ThreeFry2x64<2> rngA(123456, gmx::RandomDomain::Other);
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++)
240 rngA();
242 // ... but the 9th time we have exhausted the internal counter space.
243 EXPECT_THROW_GMX(rngA(), gmx::InternalError);
246 } // namespace anonymous
248 } // namespace gmx