PIVX Core  5.6.99
P2P Digital Currency
random_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2017 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include "random.h"
6 
7 #include "test/test_pivx.h"
8 
9 #include <boost/test/unit_test.hpp>
10 
11 #include <random>
12 #include <algorithm>
13 
15 
16 BOOST_AUTO_TEST_CASE(osrandom_tests)
17 {
19 }
20 
21 BOOST_AUTO_TEST_CASE(fastrandom_tests)
22 {
23  // Check that deterministic FastRandomContexts are deterministic
25  FastRandomContext ctx1(true);
26  FastRandomContext ctx2(true);
27 
28  for (int i = 10; i > 0; --i) {
29  BOOST_CHECK_EQUAL(GetRand(std::numeric_limits<uint64_t>::max()), uint64_t{10393729187455219830U});
30  BOOST_CHECK_EQUAL(GetRandInt(std::numeric_limits<int>::max()), int{769702006});
31  }
32  BOOST_CHECK_EQUAL(ctx1.rand32(), ctx2.rand32());
33  BOOST_CHECK_EQUAL(ctx1.rand32(), ctx2.rand32());
34  BOOST_CHECK_EQUAL(ctx1.rand64(), ctx2.rand64());
35  BOOST_CHECK_EQUAL(ctx1.randbits(3), ctx2.randbits(3));
36  BOOST_CHECK(ctx1.randbytes(17) == ctx2.randbytes(17));
37  BOOST_CHECK(ctx1.rand256() == ctx2.rand256());
38  BOOST_CHECK_EQUAL(ctx1.randbits(7), ctx2.randbits(7));
39  BOOST_CHECK(ctx1.randbytes(128) == ctx2.randbytes(128));
40  BOOST_CHECK_EQUAL(ctx1.rand32(), ctx2.rand32());
41  BOOST_CHECK_EQUAL(ctx1.randbits(3), ctx2.randbits(3));
42  BOOST_CHECK(ctx1.rand256() == ctx2.rand256());
43  BOOST_CHECK(ctx1.randbytes(50) == ctx2.randbytes(50));
44 
45  // Check that a nondeterministic ones are not
47  for (int i = 10; i > 0; --i) {
48  BOOST_CHECK(GetRand(std::numeric_limits<uint64_t>::max()) != uint64_t{10393729187455219830U});
49  BOOST_CHECK(GetRandInt(std::numeric_limits<int>::max()) != int{769702006});
50  }
51  {
52  FastRandomContext ctx3, ctx4;
53  BOOST_CHECK(ctx3.rand64() != ctx4.rand64()); // extremely unlikely to be equal
54  }
55  {
56  FastRandomContext ctx3, ctx4;
57  BOOST_CHECK(ctx3.rand256() != ctx4.rand256());
58  }
59  {
60  FastRandomContext ctx3, ctx4;
61  BOOST_CHECK(ctx3.randbytes(7) != ctx4.randbytes(7));
62  }
63 }
64 
65 BOOST_AUTO_TEST_CASE(fastrandom_randbits)
66 {
67  FastRandomContext ctx1;
68  FastRandomContext ctx2;
69  for (int bits = 0; bits < 63; ++bits) {
70  for (int j = 0; j < 1000; ++j) {
71  uint64_t rangebits = ctx1.randbits(bits);
72  BOOST_CHECK_EQUAL(rangebits >> bits, 0);
73  uint64_t range = ((uint64_t)1) << bits | rangebits;
74  uint64_t rand = ctx2.randrange(range);
75  BOOST_CHECK(rand < range);
76  }
77  }
78 }
79 
81 BOOST_AUTO_TEST_CASE(stdrandom_test)
82 {
84  std::uniform_int_distribution<int> distribution(3, 9);
85  for (int i = 0; i < 100; ++i) {
86  int x = distribution(ctx);
87  BOOST_CHECK(x >= 3);
88  BOOST_CHECK(x <= 9);
89 
90  std::vector<int> test{1,2,3,4,5,6,7,8,9,10};
91  std::shuffle(test.begin(), test.end(), ctx);
92  for (int j = 1; j <= 10; ++j) {
93  BOOST_CHECK(std::find(test.begin(), test.end(), j) != test.end());
94  }
95  Shuffle(test.begin(), test.end(), ctx);
96  for (int j = 1; j <= 10; ++j) {
97  BOOST_CHECK(std::find(test.begin(), test.end(), j) != test.end());
98  }
99  }
100 
101 }
102 
104 BOOST_AUTO_TEST_CASE(shuffle_stat_test)
105 {
106  FastRandomContext ctx(true);
107  uint32_t counts[5 * 5 * 5 * 5 * 5] = {0};
108  for (int i = 0; i < 12000; ++i) {
109  int data[5] = {0, 1, 2, 3, 4};
110  Shuffle(std::begin(data), std::end(data), ctx);
111  int pos = data[0] + data[1] * 5 + data[2] * 25 + data[3] * 125 + data[4] * 625;
112  ++counts[pos];
113  }
114  unsigned int sum = 0;
115  double chi_score = 0.0;
116  for (int i = 0; i < 5 * 5 * 5 * 5 * 5; ++i) {
117  int i1 = i % 5, i2 = (i / 5) % 5, i3 = (i / 25) % 5, i4 = (i / 125) % 5, i5 = i / 625;
118  uint32_t count = counts[i];
119  if (i1 == i2 || i1 == i3 || i1 == i4 || i1 == i5 || i2 == i3 || i2 == i4 || i2 == i5 || i3 == i4 || i3 == i5 || i4 == i5) {
120  BOOST_CHECK(count == 0);
121  } else {
122  chi_score += ((count - 100.0) * (count - 100.0)) / 100.0;
123  BOOST_CHECK(count > 50);
124  BOOST_CHECK(count < 150);
125  sum += count;
126  }
127  }
128  BOOST_CHECK(chi_score > 58.1411); // 99.9999% confidence interval
129  BOOST_CHECK(chi_score < 210.275);
130  BOOST_CHECK_EQUAL(sum, 12000);
131 }
132 
volatile double sum
Definition: Examples.cpp:22
Fast randomness source.
Definition: random.h:107
uint32_t rand32() noexcept
Generate a random 32-bit integer.
Definition: random.h:191
uint64_t rand64() noexcept
Generate a random 64-bit integer.
Definition: random.h:150
uint256 rand256() noexcept
generate a random uint256.
Definition: random.cpp:621
std::vector< unsigned char > randbytes(size_t len)
Generate random bytes.
Definition: random.cpp:632
uint64_t randbits(int bits) noexcept
Generate a random (bits)-bit integer.
Definition: random.h:159
uint64_t randrange(uint64_t range) noexcept
Generate a random integer in the range [0..range).
Definition: random.h:174
BOOST_AUTO_TEST_SUITE_END()
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
#define BOOST_CHECK(expr)
Definition: object.cpp:17
bool g_mock_deterministic_tests
Flag to make GetRand in random.h return the same number.
Definition: random.cpp:584
bool Random_SanityCheck()
Check that OS randomness is available and returning the requested number of bytes.
Definition: random.cpp:647
uint64_t GetRand(uint64_t nMax) noexcept
Definition: random.cpp:586
int GetRandInt(int nMax) noexcept
Definition: random.cpp:591
void Shuffle(I first, I last, R &&rng)
More efficient than using std::shuffle on a FastRandomContext.
Definition: random.h:217
BOOST_AUTO_TEST_CASE(osrandom_tests)
Basic testing setup.
Definition: test_pivx.h:51