blob: da4023f50b019a0b99de634407ec82e3ea58cc8c [file] [log] [blame]
Vitaly Bukacbed2062015-08-17 12:54:05 -07001// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/rand_util.h"
6
Alex Vakulenko674f0eb2016-01-20 08:10:48 -08007#include <stddef.h>
8#include <stdint.h>
9
Vitaly Bukacbed2062015-08-17 12:54:05 -070010#include <algorithm>
11#include <limits>
Luis Hector Chavez637be792016-05-20 23:09:12 -070012#include <memory>
Vitaly Bukacbed2062015-08-17 12:54:05 -070013
Vitaly Buka8750b272015-08-18 18:39:08 -070014#include <gtest/gtest.h>
15
Vitaly Bukacbed2062015-08-17 12:54:05 -070016#include "base/logging.h"
Vitaly Bukacbed2062015-08-17 12:54:05 -070017#include "base/time/time.h"
Vitaly Bukacbed2062015-08-17 12:54:05 -070018
19namespace {
20
21const int kIntMin = std::numeric_limits<int>::min();
22const int kIntMax = std::numeric_limits<int>::max();
23
24} // namespace
25
Alex Vakulenko674f0eb2016-01-20 08:10:48 -080026TEST(RandUtilTest, RandInt) {
Vitaly Bukacbed2062015-08-17 12:54:05 -070027 EXPECT_EQ(base::RandInt(0, 0), 0);
28 EXPECT_EQ(base::RandInt(kIntMin, kIntMin), kIntMin);
29 EXPECT_EQ(base::RandInt(kIntMax, kIntMax), kIntMax);
Alex Vakulenko674f0eb2016-01-20 08:10:48 -080030
31 // Check that the DCHECKS in RandInt() don't fire due to internal overflow.
32 // There was a 50% chance of that happening, so calling it 40 times means
33 // the chances of this passing by accident are tiny (9e-13).
34 for (int i = 0; i < 40; ++i)
35 base::RandInt(kIntMin, kIntMax);
Vitaly Bukacbed2062015-08-17 12:54:05 -070036}
37
38TEST(RandUtilTest, RandDouble) {
39 // Force 64-bit precision, making sure we're not in a 80-bit FPU register.
40 volatile double number = base::RandDouble();
41 EXPECT_GT(1.0, number);
42 EXPECT_LE(0.0, number);
43}
44
45TEST(RandUtilTest, RandBytes) {
46 const size_t buffer_size = 50;
47 char buffer[buffer_size];
48 memset(buffer, 0, buffer_size);
49 base::RandBytes(buffer, buffer_size);
50 std::sort(buffer, buffer + buffer_size);
51 // Probability of occurrence of less than 25 unique bytes in 50 random bytes
52 // is below 10^-25.
53 EXPECT_GT(std::unique(buffer, buffer + buffer_size) - buffer, 25);
54}
55
56TEST(RandUtilTest, RandBytesAsString) {
57 std::string random_string = base::RandBytesAsString(1);
58 EXPECT_EQ(1U, random_string.size());
59 random_string = base::RandBytesAsString(145);
60 EXPECT_EQ(145U, random_string.size());
61 char accumulator = 0;
62 for (size_t i = 0; i < random_string.size(); ++i)
63 accumulator |= random_string[i];
64 // In theory this test can fail, but it won't before the universe dies of
65 // heat death.
66 EXPECT_NE(0, accumulator);
67}
68
69// Make sure that it is still appropriate to use RandGenerator in conjunction
70// with std::random_shuffle().
71TEST(RandUtilTest, RandGeneratorForRandomShuffle) {
72 EXPECT_EQ(base::RandGenerator(1), 0U);
73 EXPECT_LE(std::numeric_limits<ptrdiff_t>::max(),
Alex Vakulenko674f0eb2016-01-20 08:10:48 -080074 std::numeric_limits<int64_t>::max());
Vitaly Bukacbed2062015-08-17 12:54:05 -070075}
76
77TEST(RandUtilTest, RandGeneratorIsUniform) {
78 // Verify that RandGenerator has a uniform distribution. This is a
79 // regression test that consistently failed when RandGenerator was
80 // implemented this way:
81 //
82 // return base::RandUint64() % max;
83 //
84 // A degenerate case for such an implementation is e.g. a top of
85 // range that is 2/3rds of the way to MAX_UINT64, in which case the
86 // bottom half of the range would be twice as likely to occur as the
87 // top half. A bit of calculus care of jar@ shows that the largest
88 // measurable delta is when the top of the range is 3/4ths of the
89 // way, so that's what we use in the test.
Alex Vakulenko674f0eb2016-01-20 08:10:48 -080090 const uint64_t kTopOfRange =
91 (std::numeric_limits<uint64_t>::max() / 4ULL) * 3ULL;
92 const uint64_t kExpectedAverage = kTopOfRange / 2ULL;
93 const uint64_t kAllowedVariance = kExpectedAverage / 50ULL; // +/- 2%
Vitaly Bukacbed2062015-08-17 12:54:05 -070094 const int kMinAttempts = 1000;
95 const int kMaxAttempts = 1000000;
96
97 double cumulative_average = 0.0;
98 int count = 0;
99 while (count < kMaxAttempts) {
Alex Vakulenko674f0eb2016-01-20 08:10:48 -0800100 uint64_t value = base::RandGenerator(kTopOfRange);
Vitaly Bukacbed2062015-08-17 12:54:05 -0700101 cumulative_average = (count * cumulative_average + value) / (count + 1);
102
103 // Don't quit too quickly for things to start converging, or we may have
104 // a false positive.
105 if (count > kMinAttempts &&
106 kExpectedAverage - kAllowedVariance < cumulative_average &&
107 cumulative_average < kExpectedAverage + kAllowedVariance) {
108 break;
109 }
110
111 ++count;
112 }
113
114 ASSERT_LT(count, kMaxAttempts) << "Expected average was " <<
115 kExpectedAverage << ", average ended at " << cumulative_average;
116}
117
118TEST(RandUtilTest, RandUint64ProducesBothValuesOfAllBits) {
119 // This tests to see that our underlying random generator is good
120 // enough, for some value of good enough.
Alex Vakulenko674f0eb2016-01-20 08:10:48 -0800121 uint64_t kAllZeros = 0ULL;
122 uint64_t kAllOnes = ~kAllZeros;
123 uint64_t found_ones = kAllZeros;
124 uint64_t found_zeros = kAllOnes;
Vitaly Bukacbed2062015-08-17 12:54:05 -0700125
126 for (size_t i = 0; i < 1000; ++i) {
Alex Vakulenko674f0eb2016-01-20 08:10:48 -0800127 uint64_t value = base::RandUint64();
Vitaly Bukacbed2062015-08-17 12:54:05 -0700128 found_ones |= value;
129 found_zeros &= value;
130
131 if (found_zeros == kAllZeros && found_ones == kAllOnes)
132 return;
133 }
134
135 FAIL() << "Didn't achieve all bit values in maximum number of tries.";
136}