LCOV - code coverage report
Current view: top level - src/test - random_tests.cpp (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 114 114 100.0 %
Date: 2026-06-25 07:23:43 Functions: 38 38 100.0 %

          Line data    Source code
       1             : // Copyright (c) 2017-2020 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/util/setup_common.h>
       8             : #include <util/time.h>
       9             : 
      10             : #include <boost/test/unit_test.hpp>
      11             : 
      12             : #include <algorithm>
      13             : 
      14         146 : BOOST_FIXTURE_TEST_SUITE(random_tests, BasicTestingSetup)
      15             : 
      16         149 : BOOST_AUTO_TEST_CASE(osrandom_tests)
      17             : {
      18           1 :     BOOST_CHECK(Random_SanityCheck());
      19           1 : }
      20             : 
      21         149 : BOOST_AUTO_TEST_CASE(fastrandom_tests)
      22             : {
      23             :     // Check that deterministic FastRandomContexts are deterministic
      24           1 :     g_mock_deterministic_tests = true;
      25           1 :     FastRandomContext ctx1(true);
      26           1 :     FastRandomContext ctx2(true);
      27             : 
      28          11 :     for (int i = 10; i > 0; --i) {
      29          10 :         BOOST_CHECK_EQUAL(GetRand<uint64_t>(), uint64_t{10393729187455219830U});
      30          10 :         BOOST_CHECK_EQUAL(GetRand<int>(), int{769702006});
      31          10 :         BOOST_CHECK_EQUAL(GetRandMicros(std::chrono::hours{1}).count(), 2917185654);
      32          10 :         BOOST_CHECK_EQUAL(GetRandMillis(std::chrono::hours{1}).count(), 2144374);
      33          10 :     }
      34             :     {
      35           1 :         constexpr SteadySeconds time_point{1s};
      36           1 :         FastRandomContext ctx{true};
      37           1 :         BOOST_CHECK_EQUAL(7, ctx.rand_uniform_delay(time_point, 9s).time_since_epoch().count());
      38           1 :         BOOST_CHECK_EQUAL(-6, ctx.rand_uniform_delay(time_point, -9s).time_since_epoch().count());
      39           1 :         BOOST_CHECK_EQUAL(1, ctx.rand_uniform_delay(time_point, 0s).time_since_epoch().count());
      40           1 :         BOOST_CHECK_EQUAL(1467825113502396065, ctx.rand_uniform_delay(time_point, 9223372036854775807s).time_since_epoch().count());
      41           1 :         BOOST_CHECK_EQUAL(-970181367944767837, ctx.rand_uniform_delay(time_point, -9223372036854775807s).time_since_epoch().count());
      42           1 :         BOOST_CHECK_EQUAL(24761, ctx.rand_uniform_delay(time_point, 9h).time_since_epoch().count());
      43           1 :     }
      44           1 :     BOOST_CHECK_EQUAL(ctx1.rand32(), ctx2.rand32());
      45           1 :     BOOST_CHECK_EQUAL(ctx1.rand32(), ctx2.rand32());
      46           1 :     BOOST_CHECK_EQUAL(ctx1.rand64(), ctx2.rand64());
      47           1 :     BOOST_CHECK_EQUAL(ctx1.randbits(3), ctx2.randbits(3));
      48           1 :     BOOST_CHECK(ctx1.randbytes(17) == ctx2.randbytes(17));
      49           1 :     BOOST_CHECK(ctx1.rand256() == ctx2.rand256());
      50           1 :     BOOST_CHECK_EQUAL(ctx1.randbits(7), ctx2.randbits(7));
      51           1 :     BOOST_CHECK(ctx1.randbytes(128) == ctx2.randbytes(128));
      52           1 :     BOOST_CHECK_EQUAL(ctx1.rand32(), ctx2.rand32());
      53           1 :     BOOST_CHECK_EQUAL(ctx1.randbits(3), ctx2.randbits(3));
      54           1 :     BOOST_CHECK(ctx1.rand256() == ctx2.rand256());
      55           1 :     BOOST_CHECK(ctx1.randbytes(50) == ctx2.randbytes(50));
      56             :     {
      57             :         struct MicroClock {
      58             :             using duration = std::chrono::microseconds;
      59             :         };
      60           1 :         FastRandomContext ctx{true};
      61             :         // Check with clock type
      62           1 :         BOOST_CHECK_EQUAL(47222, ctx.rand_uniform_duration<MicroClock>(1s).count());
      63             :         // Check with time-point type
      64           1 :         BOOST_CHECK_EQUAL(2782, ctx.rand_uniform_duration<SteadySeconds>(9h).count());
      65           1 :     }
      66             : 
      67             :     // Check that a nondeterministic ones are not
      68           1 :     g_mock_deterministic_tests = false;
      69          11 :     for (int i = 10; i > 0; --i) {
      70          10 :         BOOST_CHECK(GetRand<uint64_t>() != uint64_t{10393729187455219830U});
      71          10 :         BOOST_CHECK(GetRand<int>() != int{769702006});
      72          10 :         BOOST_CHECK(GetRandMicros(std::chrono::hours{1}) != std::chrono::microseconds{2917185654});
      73          10 :         BOOST_CHECK(GetRandMillis(std::chrono::hours{1}) != std::chrono::milliseconds{2144374});
      74          10 :     }
      75             : 
      76             :     {
      77           1 :         FastRandomContext ctx3, ctx4;
      78           1 :         BOOST_CHECK(ctx3.rand64() != ctx4.rand64()); // extremely unlikely to be equal
      79           1 :     }
      80             :     {
      81           1 :         FastRandomContext ctx3, ctx4;
      82           1 :         BOOST_CHECK(ctx3.rand256() != ctx4.rand256());
      83           1 :     }
      84             :     {
      85           1 :         FastRandomContext ctx3, ctx4;
      86           1 :         BOOST_CHECK(ctx3.randbytes(7) != ctx4.randbytes(7));
      87           1 :     }
      88           1 : }
      89             : 
      90         149 : BOOST_AUTO_TEST_CASE(fastrandom_randbits)
      91             : {
      92           1 :     FastRandomContext ctx1;
      93           1 :     FastRandomContext ctx2;
      94          64 :     for (int bits = 0; bits < 63; ++bits) {
      95       63063 :         for (int j = 0; j < 1000; ++j) {
      96       63000 :             uint64_t rangebits = ctx1.randbits(bits);
      97       63000 :             BOOST_CHECK_EQUAL(rangebits >> bits, 0U);
      98       63000 :             uint64_t range = (uint64_t{1}) << bits | rangebits;
      99       63000 :             uint64_t rand = ctx2.randrange(range);
     100       63000 :             BOOST_CHECK(rand < range);
     101       63000 :         }
     102          63 :     }
     103           1 : }
     104             : 
     105             : /** Does-it-compile test for compatibility with standard library RNG interface. */
     106         149 : BOOST_AUTO_TEST_CASE(stdrandom_test)
     107             : {
     108           1 :     FastRandomContext ctx;
     109           1 :     std::uniform_int_distribution<int> distribution(3, 9);
     110         101 :     for (int i = 0; i < 100; ++i) {
     111         100 :         int x = distribution(ctx);
     112         100 :         BOOST_CHECK(x >= 3);
     113         100 :         BOOST_CHECK(x <= 9);
     114             : 
     115         100 :         std::vector<int> test{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
     116         100 :         std::shuffle(test.begin(), test.end(), ctx);
     117        1100 :         for (int j = 1; j <= 10; ++j) {
     118        1000 :             BOOST_CHECK(std::find(test.begin(), test.end(), j) != test.end());
     119        1000 :         }
     120         100 :         Shuffle(test.begin(), test.end(), ctx);
     121        1100 :         for (int j = 1; j <= 10; ++j) {
     122        1000 :             BOOST_CHECK(std::find(test.begin(), test.end(), j) != test.end());
     123        1000 :         }
     124         100 :     }
     125           1 : }
     126             : 
     127             : /** Test that Shuffle reaches every permutation with equal probability. */
     128         149 : BOOST_AUTO_TEST_CASE(shuffle_stat_test)
     129             : {
     130           1 :     FastRandomContext ctx(true);
     131           1 :     uint32_t counts[5 * 5 * 5 * 5 * 5] = {0};
     132       12001 :     for (int i = 0; i < 12000; ++i) {
     133       12000 :         int data[5] = {0, 1, 2, 3, 4};
     134       12000 :         Shuffle(std::begin(data), std::end(data), ctx);
     135       12000 :         int pos = data[0] + data[1] * 5 + data[2] * 25 + data[3] * 125 + data[4] * 625;
     136       12000 :         ++counts[pos];
     137       12000 :     }
     138           1 :     unsigned int sum = 0;
     139           1 :     double chi_score = 0.0;
     140        3126 :     for (int i = 0; i < 5 * 5 * 5 * 5 * 5; ++i) {
     141        3125 :         int i1 = i % 5, i2 = (i / 5) % 5, i3 = (i / 25) % 5, i4 = (i / 125) % 5, i5 = i / 625;
     142        3125 :         uint32_t count = counts[i];
     143        3125 :         if (i1 == i2 || i1 == i3 || i1 == i4 || i1 == i5 || i2 == i3 || i2 == i4 || i2 == i5 || i3 == i4 || i3 == i5 || i4 == i5) {
     144        3005 :             BOOST_CHECK(count == 0);
     145        3005 :         } else {
     146         120 :             chi_score += ((count - 100.0) * (count - 100.0)) / 100.0;
     147         120 :             BOOST_CHECK(count > 50);
     148         120 :             BOOST_CHECK(count < 150);
     149         120 :             sum += count;
     150             :         }
     151        3125 :     }
     152           1 :     BOOST_CHECK(chi_score > 58.1411); // 99.9999% confidence interval
     153           1 :     BOOST_CHECK(chi_score < 210.275);
     154           1 :     BOOST_CHECK_EQUAL(sum, 12000U);
     155           1 : }
     156             : 
     157         146 : BOOST_AUTO_TEST_SUITE_END()

Generated by: LCOV version 1.16