| 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 <stddef.h> |
| 8 #include <stdint.h> |
| 9 |
| 7 #include <algorithm> | 10 #include <algorithm> |
| 8 #include <limits> | 11 #include <limits> |
| 9 | 12 |
| 10 #include "base/logging.h" | 13 #include "base/logging.h" |
| 11 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 17 |
| 15 namespace { | 18 namespace { |
| 16 | 19 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 // In theory this test can fail, but it won't before the universe dies of | 63 // In theory this test can fail, but it won't before the universe dies of |
| 61 // heat death. | 64 // heat death. |
| 62 EXPECT_NE(0, accumulator); | 65 EXPECT_NE(0, accumulator); |
| 63 } | 66 } |
| 64 | 67 |
| 65 // Make sure that it is still appropriate to use RandGenerator in conjunction | 68 // Make sure that it is still appropriate to use RandGenerator in conjunction |
| 66 // with std::random_shuffle(). | 69 // with std::random_shuffle(). |
| 67 TEST(RandUtilTest, RandGeneratorForRandomShuffle) { | 70 TEST(RandUtilTest, RandGeneratorForRandomShuffle) { |
| 68 EXPECT_EQ(base::RandGenerator(1), 0U); | 71 EXPECT_EQ(base::RandGenerator(1), 0U); |
| 69 EXPECT_LE(std::numeric_limits<ptrdiff_t>::max(), | 72 EXPECT_LE(std::numeric_limits<ptrdiff_t>::max(), |
| 70 std::numeric_limits<int64>::max()); | 73 std::numeric_limits<int64_t>::max()); |
| 71 } | 74 } |
| 72 | 75 |
| 73 TEST(RandUtilTest, RandGeneratorIsUniform) { | 76 TEST(RandUtilTest, RandGeneratorIsUniform) { |
| 74 // Verify that RandGenerator has a uniform distribution. This is a | 77 // Verify that RandGenerator has a uniform distribution. This is a |
| 75 // regression test that consistently failed when RandGenerator was | 78 // regression test that consistently failed when RandGenerator was |
| 76 // implemented this way: | 79 // implemented this way: |
| 77 // | 80 // |
| 78 // return base::RandUint64() % max; | 81 // return base::RandUint64() % max; |
| 79 // | 82 // |
| 80 // A degenerate case for such an implementation is e.g. a top of | 83 // A degenerate case for such an implementation is e.g. a top of |
| 81 // range that is 2/3rds of the way to MAX_UINT64, in which case the | 84 // range that is 2/3rds of the way to MAX_UINT64, in which case the |
| 82 // bottom half of the range would be twice as likely to occur as the | 85 // bottom half of the range would be twice as likely to occur as the |
| 83 // top half. A bit of calculus care of jar@ shows that the largest | 86 // top half. A bit of calculus care of jar@ shows that the largest |
| 84 // measurable delta is when the top of the range is 3/4ths of the | 87 // measurable delta is when the top of the range is 3/4ths of the |
| 85 // way, so that's what we use in the test. | 88 // way, so that's what we use in the test. |
| 86 const uint64 kTopOfRange = (std::numeric_limits<uint64>::max() / 4ULL) * 3ULL; | 89 const uint64_t kTopOfRange = |
| 87 const uint64 kExpectedAverage = kTopOfRange / 2ULL; | 90 (std::numeric_limits<uint64_t>::max() / 4ULL) * 3ULL; |
| 88 const uint64 kAllowedVariance = kExpectedAverage / 50ULL; // +/- 2% | 91 const uint64_t kExpectedAverage = kTopOfRange / 2ULL; |
| 92 const uint64_t kAllowedVariance = kExpectedAverage / 50ULL; // +/- 2% |
| 89 const int kMinAttempts = 1000; | 93 const int kMinAttempts = 1000; |
| 90 const int kMaxAttempts = 1000000; | 94 const int kMaxAttempts = 1000000; |
| 91 | 95 |
| 92 double cumulative_average = 0.0; | 96 double cumulative_average = 0.0; |
| 93 int count = 0; | 97 int count = 0; |
| 94 while (count < kMaxAttempts) { | 98 while (count < kMaxAttempts) { |
| 95 uint64 value = base::RandGenerator(kTopOfRange); | 99 uint64_t value = base::RandGenerator(kTopOfRange); |
| 96 cumulative_average = (count * cumulative_average + value) / (count + 1); | 100 cumulative_average = (count * cumulative_average + value) / (count + 1); |
| 97 | 101 |
| 98 // Don't quit too quickly for things to start converging, or we may have | 102 // Don't quit too quickly for things to start converging, or we may have |
| 99 // a false positive. | 103 // a false positive. |
| 100 if (count > kMinAttempts && | 104 if (count > kMinAttempts && |
| 101 kExpectedAverage - kAllowedVariance < cumulative_average && | 105 kExpectedAverage - kAllowedVariance < cumulative_average && |
| 102 cumulative_average < kExpectedAverage + kAllowedVariance) { | 106 cumulative_average < kExpectedAverage + kAllowedVariance) { |
| 103 break; | 107 break; |
| 104 } | 108 } |
| 105 | 109 |
| 106 ++count; | 110 ++count; |
| 107 } | 111 } |
| 108 | 112 |
| 109 ASSERT_LT(count, kMaxAttempts) << "Expected average was " << | 113 ASSERT_LT(count, kMaxAttempts) << "Expected average was " << |
| 110 kExpectedAverage << ", average ended at " << cumulative_average; | 114 kExpectedAverage << ", average ended at " << cumulative_average; |
| 111 } | 115 } |
| 112 | 116 |
| 113 TEST(RandUtilTest, RandUint64ProducesBothValuesOfAllBits) { | 117 TEST(RandUtilTest, RandUint64ProducesBothValuesOfAllBits) { |
| 114 // This tests to see that our underlying random generator is good | 118 // This tests to see that our underlying random generator is good |
| 115 // enough, for some value of good enough. | 119 // enough, for some value of good enough. |
| 116 uint64 kAllZeros = 0ULL; | 120 uint64_t kAllZeros = 0ULL; |
| 117 uint64 kAllOnes = ~kAllZeros; | 121 uint64_t kAllOnes = ~kAllZeros; |
| 118 uint64 found_ones = kAllZeros; | 122 uint64_t found_ones = kAllZeros; |
| 119 uint64 found_zeros = kAllOnes; | 123 uint64_t found_zeros = kAllOnes; |
| 120 | 124 |
| 121 for (size_t i = 0; i < 1000; ++i) { | 125 for (size_t i = 0; i < 1000; ++i) { |
| 122 uint64 value = base::RandUint64(); | 126 uint64_t value = base::RandUint64(); |
| 123 found_ones |= value; | 127 found_ones |= value; |
| 124 found_zeros &= value; | 128 found_zeros &= value; |
| 125 | 129 |
| 126 if (found_zeros == kAllZeros && found_ones == kAllOnes) | 130 if (found_zeros == kAllZeros && found_ones == kAllOnes) |
| 127 return; | 131 return; |
| 128 } | 132 } |
| 129 | 133 |
| 130 FAIL() << "Didn't achieve all bit values in maximum number of tries."; | 134 FAIL() << "Didn't achieve all bit values in maximum number of tries."; |
| 131 } | 135 } |
| 132 | 136 |
| 133 // Benchmark test for RandBytes(). Disabled since it's intentionally slow and | 137 // Benchmark test for RandBytes(). Disabled since it's intentionally slow and |
| 134 // does not test anything that isn't already tested by the existing RandBytes() | 138 // does not test anything that isn't already tested by the existing RandBytes() |
| 135 // tests. | 139 // tests. |
| 136 TEST(RandUtilTest, DISABLED_RandBytesPerf) { | 140 TEST(RandUtilTest, DISABLED_RandBytesPerf) { |
| 137 // Benchmark the performance of |kTestIterations| of RandBytes() using a | 141 // Benchmark the performance of |kTestIterations| of RandBytes() using a |
| 138 // buffer size of |kTestBufferSize|. | 142 // buffer size of |kTestBufferSize|. |
| 139 const int kTestIterations = 10; | 143 const int kTestIterations = 10; |
| 140 const size_t kTestBufferSize = 1 * 1024 * 1024; | 144 const size_t kTestBufferSize = 1 * 1024 * 1024; |
| 141 | 145 |
| 142 scoped_ptr<uint8[]> buffer(new uint8[kTestBufferSize]); | 146 scoped_ptr<uint8_t[]> buffer(new uint8_t[kTestBufferSize]); |
| 143 const base::TimeTicks now = base::TimeTicks::Now(); | 147 const base::TimeTicks now = base::TimeTicks::Now(); |
| 144 for (int i = 0; i < kTestIterations; ++i) | 148 for (int i = 0; i < kTestIterations; ++i) |
| 145 base::RandBytes(buffer.get(), kTestBufferSize); | 149 base::RandBytes(buffer.get(), kTestBufferSize); |
| 146 const base::TimeTicks end = base::TimeTicks::Now(); | 150 const base::TimeTicks end = base::TimeTicks::Now(); |
| 147 | 151 |
| 148 LOG(INFO) << "RandBytes(" << kTestBufferSize << ") took: " | 152 LOG(INFO) << "RandBytes(" << kTestBufferSize << ") took: " |
| 149 << (end - now).InMicroseconds() << "µs"; | 153 << (end - now).InMicroseconds() << "µs"; |
| 150 } | 154 } |
| OLD | NEW |