2 * This file is part of the GROMACS molecular simulation package.
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.
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.
36 * \brief Tests for the ThreeFry random engine
38 * \author Erik Lindahl <erik.lindahl@gmail.com>
39 * \ingroup module_random
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"
58 class ThreeFry2x64Test : public ::testing::TestWithParam<std::vector<uint64_t>>
62 TEST_P(ThreeFry2x64Test, Default)
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;
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<uint64_t> input = GetParam();
83 std::vector<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<uint64_t> input = GetParam();
99 std::vector<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<uint64_t> bitsZero{ { 0, 0, 0, 0 } };
120 /*! \brief Constant array of integers with all bits set to one.
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.
126 const std::vector<uint64_t> bitsOne{
127 { 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL }
130 /*! \brief Constant array of integers with bitpattern from Pi.
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.
136 const std::vector<uint64_t> bitsPi{
137 { 0x243f6a8885a308d3ULL, 0x13198a2e03707344ULL, 0xa4093822299f31d0ULL, 0x082efa98ec4e6c89ULL }
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));
145 // ThreeFry2x64 tests
146 TEST_F(ThreeFry2x64Test, Logical)
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);
152 rngB(); // draw just once first, so block is the same, but index has changed
153 EXPECT_NE(rngA, rngB);
155 rngC(); // two draws: next block, but index is the same
156 EXPECT_NE(rngA, rngC);
158 EXPECT_EQ(rngA, rngB);
160 EXPECT_EQ(rngA, rngC);
163 TEST_F(ThreeFry2x64Test, InternalCounterSequence)
165 gmx::test::TestReferenceData data;
166 gmx::test::TestReferenceChecker checker(data.rootChecker());
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;
174 for (int i = 0; i < 16; i++)
176 result.push_back(rngA());
178 checker.checkSequence(result.begin(), result.end(), "ThreeFry2x64InternalCounterSequence");
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++)
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++)
195 TEST_F(ThreeFry2x64Test, Reseed)
197 gmx::ThreeFry2x64<10> rngA(123456, gmx::RandomDomain::Other);
198 gmx::ThreeFry2x64<10> rngB;
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);
208 TEST_F(ThreeFry2x64Test, Discard)
210 gmx::ThreeFry2x64<10> rngA(123456, gmx::RandomDomain::Other);
211 gmx::ThreeFry2x64<10> rngB(123456, gmx::RandomDomain::Other);
213 for (int i = 0; i < 9; i++)
218 EXPECT_EQ(rngA, rngB);
222 TEST_F(ThreeFry2x64Test, InvalidCounter)
224 gmx::ThreeFry2x64<10> rngA(123456, gmx::RandomDomain::Other);
226 // Highest 10 bits of counter reserved for the internal counter.
227 EXPECT_THROW_GMX(rngA.restart(0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF), gmx::InternalError);
230 TEST_F(ThreeFry2x64Test, ExhaustInternalCounter)
232 gmx::ThreeFry2x64<2> rngA(123456, gmx::RandomDomain::Other);
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++)
239 // ... but the 9th time we have exhausted the internal counter space.
240 EXPECT_THROW_GMX(rngA(), gmx::InternalError);