[go: nahoru, domu]

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