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