blob: 384125b74a5b46d789d05bc7f0615130960baf94 [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
7#include <algorithm>
8#include <limits>
9
Vitaly Buka8750b272015-08-18 18:39:08 -070010#include <gtest/gtest.h>
11
Vitaly Bukacbed2062015-08-17 12:54:05 -070012#include "base/logging.h"
13#include "base/memory/scoped_ptr.h"
14#include "base/time/time.h"
Vitaly Bukacbed2062015-08-17 12:54:05 -070015
16namespace {
17
18const int kIntMin = std::numeric_limits<int>::min();
19const int kIntMax = std::numeric_limits<int>::max();
20
21} // namespace
22
23TEST(RandUtilTest, SameMinAndMax) {
24 EXPECT_EQ(base::RandInt(0, 0), 0);
25 EXPECT_EQ(base::RandInt(kIntMin, kIntMin), kIntMin);
26 EXPECT_EQ(base::RandInt(kIntMax, kIntMax), kIntMax);
27}
28
29TEST(RandUtilTest, RandDouble) {
30 // Force 64-bit precision, making sure we're not in a 80-bit FPU register.
31 volatile double number = base::RandDouble();
32 EXPECT_GT(1.0, number);
33 EXPECT_LE(0.0, number);
34}
35
36TEST(RandUtilTest, RandBytes) {
37 const size_t buffer_size = 50;
38 char buffer[buffer_size];
39 memset(buffer, 0, buffer_size);
40 base::RandBytes(buffer, buffer_size);
41 std::sort(buffer, buffer + buffer_size);
42 // Probability of occurrence of less than 25 unique bytes in 50 random bytes
43 // is below 10^-25.
44 EXPECT_GT(std::unique(buffer, buffer + buffer_size) - buffer, 25);
45}
46
47TEST(RandUtilTest, RandBytesAsString) {
48 std::string random_string = base::RandBytesAsString(1);
49 EXPECT_EQ(1U, random_string.size());
50 random_string = base::RandBytesAsString(145);
51 EXPECT_EQ(145U, random_string.size());
52 char accumulator = 0;
53 for (size_t i = 0; i < random_string.size(); ++i)
54 accumulator |= random_string[i];
55 // In theory this test can fail, but it won't before the universe dies of
56 // heat death.
57 EXPECT_NE(0, accumulator);
58}
59
60// Make sure that it is still appropriate to use RandGenerator in conjunction
61// with std::random_shuffle().
62TEST(RandUtilTest, RandGeneratorForRandomShuffle) {
63 EXPECT_EQ(base::RandGenerator(1), 0U);
64 EXPECT_LE(std::numeric_limits<ptrdiff_t>::max(),
65 std::numeric_limits<int64>::max());
66}
67
68TEST(RandUtilTest, RandGeneratorIsUniform) {
69 // Verify that RandGenerator has a uniform distribution. This is a
70 // regression test that consistently failed when RandGenerator was
71 // implemented this way:
72 //
73 // return base::RandUint64() % max;
74 //
75 // A degenerate case for such an implementation is e.g. a top of
76 // range that is 2/3rds of the way to MAX_UINT64, in which case the
77 // bottom half of the range would be twice as likely to occur as the
78 // top half. A bit of calculus care of jar@ shows that the largest
79 // measurable delta is when the top of the range is 3/4ths of the
80 // way, so that's what we use in the test.
81 const uint64 kTopOfRange = (std::numeric_limits<uint64>::max() / 4ULL) * 3ULL;
82 const uint64 kExpectedAverage = kTopOfRange / 2ULL;
83 const uint64 kAllowedVariance = kExpectedAverage / 50ULL; // +/- 2%
84 const int kMinAttempts = 1000;
85 const int kMaxAttempts = 1000000;
86
87 double cumulative_average = 0.0;
88 int count = 0;
89 while (count < kMaxAttempts) {
90 uint64 value = base::RandGenerator(kTopOfRange);
91 cumulative_average = (count * cumulative_average + value) / (count + 1);
92
93 // Don't quit too quickly for things to start converging, or we may have
94 // a false positive.
95 if (count > kMinAttempts &&
96 kExpectedAverage - kAllowedVariance < cumulative_average &&
97 cumulative_average < kExpectedAverage + kAllowedVariance) {
98 break;
99 }
100
101 ++count;
102 }
103
104 ASSERT_LT(count, kMaxAttempts) << "Expected average was " <<
105 kExpectedAverage << ", average ended at " << cumulative_average;
106}
107
108TEST(RandUtilTest, RandUint64ProducesBothValuesOfAllBits) {
109 // This tests to see that our underlying random generator is good
110 // enough, for some value of good enough.
111 uint64 kAllZeros = 0ULL;
112 uint64 kAllOnes = ~kAllZeros;
113 uint64 found_ones = kAllZeros;
114 uint64 found_zeros = kAllOnes;
115
116 for (size_t i = 0; i < 1000; ++i) {
117 uint64 value = base::RandUint64();
118 found_ones |= value;
119 found_zeros &= value;
120
121 if (found_zeros == kAllZeros && found_ones == kAllOnes)
122 return;
123 }
124
125 FAIL() << "Didn't achieve all bit values in maximum number of tries.";
126}