OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/rand_util.h" | 5 #include "base/rand_util.h" |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 | 8 |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 | 10 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
54 EXPECT_NE(0, accumulator); | 54 EXPECT_NE(0, accumulator); |
55 } | 55 } |
56 | 56 |
57 // Make sure that it is still appropriate to use RandGenerator in conjunction | 57 // Make sure that it is still appropriate to use RandGenerator in conjunction |
58 // with std::random_shuffle(). | 58 // with std::random_shuffle(). |
59 TEST(RandUtilTest, RandGeneratorForRandomShuffle) { | 59 TEST(RandUtilTest, RandGeneratorForRandomShuffle) { |
60 EXPECT_EQ(base::RandGenerator(1), 0U); | 60 EXPECT_EQ(base::RandGenerator(1), 0U); |
61 EXPECT_LE(std::numeric_limits<ptrdiff_t>::max(), | 61 EXPECT_LE(std::numeric_limits<ptrdiff_t>::max(), |
62 std::numeric_limits<int64>::max()); | 62 std::numeric_limits<int64>::max()); |
63 } | 63 } |
64 | |
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: | |
69 // | |
70 // return base::RandUint64() % max; | |
71 // | |
72 // The worst case for such an implementation is e.g. a top of range | |
73 // that is 2/3rds of the way to MAX_UINT64, in which case the bottom | |
jar (doing other things)
2011/05/28 17:26:40
You got me to wondering about this ratio, and it w
| |
74 // half of the range would be twice as likely to occur as the top | |
75 // half, assuming a naive modulus implementation of RandGenerator. | |
76 const uint64 kTopOfRange = (std::numeric_limits<uint64>::max() / 3L) * 2L; | |
77 const uint64 kExpectedAverage = kTopOfRange / 2L; | |
78 const uint64 kAllowedVariance = kExpectedAverage / 50L; // +/- 2% | |
79 const int kMinAttempts = 1000; | |
80 const int kMaxAttempts = 1000000; | |
81 | |
82 double cumulative_average = 0.0; | |
83 int count = 0; | |
84 while (count < kMaxAttempts) { | |
85 uint64 value = base::RandGenerator(kTopOfRange); | |
86 cumulative_average = (count * cumulative_average + value) / (count + 1); | |
87 | |
88 // Don't quit too quickly for things to start converging, or we may have | |
89 // a false positive. | |
90 if (count > kMinAttempts && | |
91 kExpectedAverage - kAllowedVariance < cumulative_average && | |
92 cumulative_average < kExpectedAverage + kAllowedVariance) { | |
93 break; | |
94 } | |
95 | |
96 ++count; | |
97 } | |
98 | |
99 ASSERT_LT(count, kMaxAttempts) << "Expected average was " << | |
100 kExpectedAverage << ", average ended at " << cumulative_average; | |
101 } | |
OLD | NEW |