abarth@chromium.org | c19cb31 | 2011-04-30 06:03:54 +0900 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 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 | |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
| 9 | |
dilmah@chromium.org | bd36573 | 2011-08-25 04:58:36 +0900 | [diff] [blame] | 10 | #include <algorithm> |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 11 | #include <limits> |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 12 | #include <memory> |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 13 | |
dalecurtis@chromium.org | 4f4a877 | 2014-01-23 11:14:28 +0900 | [diff] [blame] | 14 | #include "base/logging.h" |
dalecurtis@chromium.org | 4f4a877 | 2014-01-23 11:14:28 +0900 | [diff] [blame] | 15 | #include "base/time/time.h" |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 16 | #include "testing/gtest/include/gtest/gtest.h" |
| 17 | |
| 18 | namespace { |
| 19 | |
| 20 | const int kIntMin = std::numeric_limits<int>::min(); |
| 21 | const int kIntMax = std::numeric_limits<int>::max(); |
| 22 | |
| 23 | } // namespace |
| 24 | |
Nico Weber | 1796efe | 2015-10-30 05:42:58 +0900 | [diff] [blame] | 25 | TEST(RandUtilTest, RandInt) { |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 26 | EXPECT_EQ(base::RandInt(0, 0), 0); |
| 27 | EXPECT_EQ(base::RandInt(kIntMin, kIntMin), kIntMin); |
| 28 | EXPECT_EQ(base::RandInt(kIntMax, kIntMax), kIntMax); |
Nico Weber | 1796efe | 2015-10-30 05:42:58 +0900 | [diff] [blame] | 29 | |
| 30 | // Check that the DCHECKS in RandInt() don't fire due to internal overflow. |
| 31 | // There was a 50% chance of that happening, so calling it 40 times means |
| 32 | // the chances of this passing by accident are tiny (9e-13). |
| 33 | for (int i = 0; i < 40; ++i) |
| 34 | base::RandInt(kIntMin, kIntMax); |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 35 | } |
mark@chromium.org | c22c62b | 2008-09-30 23:26:33 +0900 | [diff] [blame] | 36 | |
| 37 | TEST(RandUtilTest, RandDouble) { |
abarth@chromium.org | c19cb31 | 2011-04-30 06:03:54 +0900 | [diff] [blame] | 38 | // Force 64-bit precision, making sure we're not in a 80-bit FPU register. |
| 39 | volatile double number = base::RandDouble(); |
| 40 | EXPECT_GT(1.0, number); |
| 41 | EXPECT_LE(0.0, number); |
| 42 | } |
| 43 | |
qsr@google.com | fbda4d5 | 2011-05-05 17:46:11 +0900 | [diff] [blame] | 44 | TEST(RandUtilTest, RandBytes) { |
dilmah@chromium.org | bd36573 | 2011-08-25 04:58:36 +0900 | [diff] [blame] | 45 | const size_t buffer_size = 50; |
qsr@google.com | fbda4d5 | 2011-05-05 17:46:11 +0900 | [diff] [blame] | 46 | char buffer[buffer_size]; |
| 47 | memset(buffer, 0, buffer_size); |
| 48 | base::RandBytes(buffer, buffer_size); |
dilmah@chromium.org | bd36573 | 2011-08-25 04:58:36 +0900 | [diff] [blame] | 49 | std::sort(buffer, buffer + buffer_size); |
| 50 | // Probability of occurrence of less than 25 unique bytes in 50 random bytes |
| 51 | // is below 10^-25. |
| 52 | EXPECT_GT(std::unique(buffer, buffer + buffer_size) - buffer, 25); |
qsr@google.com | fbda4d5 | 2011-05-05 17:46:11 +0900 | [diff] [blame] | 53 | } |
| 54 | |
abarth@chromium.org | c19cb31 | 2011-04-30 06:03:54 +0900 | [diff] [blame] | 55 | TEST(RandUtilTest, RandBytesAsString) { |
pkasting@chromium.org | d347937 | 2011-11-30 05:06:18 +0900 | [diff] [blame] | 56 | std::string random_string = base::RandBytesAsString(1); |
| 57 | EXPECT_EQ(1U, random_string.size()); |
abarth@chromium.org | c19cb31 | 2011-04-30 06:03:54 +0900 | [diff] [blame] | 58 | random_string = base::RandBytesAsString(145); |
| 59 | EXPECT_EQ(145U, random_string.size()); |
| 60 | char accumulator = 0; |
| 61 | for (size_t i = 0; i < random_string.size(); ++i) |
| 62 | accumulator |= random_string[i]; |
| 63 | // In theory this test can fail, but it won't before the universe dies of |
| 64 | // heat death. |
| 65 | EXPECT_NE(0, accumulator); |
mark@chromium.org | c22c62b | 2008-09-30 23:26:33 +0900 | [diff] [blame] | 66 | } |
isherman@chromium.org | 01eaa0b | 2010-08-31 06:07:05 +0900 | [diff] [blame] | 67 | |
| 68 | // Make sure that it is still appropriate to use RandGenerator in conjunction |
| 69 | // with std::random_shuffle(). |
| 70 | TEST(RandUtilTest, RandGeneratorForRandomShuffle) { |
| 71 | EXPECT_EQ(base::RandGenerator(1), 0U); |
| 72 | EXPECT_LE(std::numeric_limits<ptrdiff_t>::max(), |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 73 | std::numeric_limits<int64_t>::max()); |
isherman@chromium.org | 01eaa0b | 2010-08-31 06:07:05 +0900 | [diff] [blame] | 74 | } |
joi@chromium.org | edf24fc | 2011-05-31 02:39:09 +0900 | [diff] [blame] | 75 | |
| 76 | TEST(RandUtilTest, RandGeneratorIsUniform) { |
| 77 | // Verify that RandGenerator has a uniform distribution. This is a |
| 78 | // regression test that consistently failed when RandGenerator was |
| 79 | // implemented this way: |
| 80 | // |
| 81 | // return base::RandUint64() % max; |
| 82 | // |
| 83 | // A degenerate case for such an implementation is e.g. a top of |
| 84 | // range that is 2/3rds of the way to MAX_UINT64, in which case the |
| 85 | // bottom half of the range would be twice as likely to occur as the |
| 86 | // top half. A bit of calculus care of jar@ shows that the largest |
| 87 | // measurable delta is when the top of the range is 3/4ths of the |
| 88 | // way, so that's what we use in the test. |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 89 | const uint64_t kTopOfRange = |
| 90 | (std::numeric_limits<uint64_t>::max() / 4ULL) * 3ULL; |
| 91 | const uint64_t kExpectedAverage = kTopOfRange / 2ULL; |
| 92 | const uint64_t kAllowedVariance = kExpectedAverage / 50ULL; // +/- 2% |
joi@chromium.org | edf24fc | 2011-05-31 02:39:09 +0900 | [diff] [blame] | 93 | const int kMinAttempts = 1000; |
| 94 | const int kMaxAttempts = 1000000; |
| 95 | |
| 96 | double cumulative_average = 0.0; |
| 97 | int count = 0; |
| 98 | while (count < kMaxAttempts) { |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 99 | uint64_t value = base::RandGenerator(kTopOfRange); |
joi@chromium.org | edf24fc | 2011-05-31 02:39:09 +0900 | [diff] [blame] | 100 | cumulative_average = (count * cumulative_average + value) / (count + 1); |
| 101 | |
| 102 | // Don't quit too quickly for things to start converging, or we may have |
| 103 | // a false positive. |
| 104 | if (count > kMinAttempts && |
| 105 | kExpectedAverage - kAllowedVariance < cumulative_average && |
| 106 | cumulative_average < kExpectedAverage + kAllowedVariance) { |
| 107 | break; |
| 108 | } |
| 109 | |
| 110 | ++count; |
| 111 | } |
| 112 | |
| 113 | ASSERT_LT(count, kMaxAttempts) << "Expected average was " << |
| 114 | kExpectedAverage << ", average ended at " << cumulative_average; |
| 115 | } |
| 116 | |
| 117 | TEST(RandUtilTest, RandUint64ProducesBothValuesOfAllBits) { |
| 118 | // This tests to see that our underlying random generator is good |
| 119 | // enough, for some value of good enough. |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 120 | uint64_t kAllZeros = 0ULL; |
| 121 | uint64_t kAllOnes = ~kAllZeros; |
| 122 | uint64_t found_ones = kAllZeros; |
| 123 | uint64_t found_zeros = kAllOnes; |
joi@chromium.org | edf24fc | 2011-05-31 02:39:09 +0900 | [diff] [blame] | 124 | |
| 125 | for (size_t i = 0; i < 1000; ++i) { |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 126 | uint64_t value = base::RandUint64(); |
joi@chromium.org | edf24fc | 2011-05-31 02:39:09 +0900 | [diff] [blame] | 127 | found_ones |= value; |
| 128 | found_zeros &= value; |
| 129 | |
| 130 | if (found_zeros == kAllZeros && found_ones == kAllOnes) |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | FAIL() << "Didn't achieve all bit values in maximum number of tries."; |
| 135 | } |
dalecurtis@chromium.org | 4f4a877 | 2014-01-23 11:14:28 +0900 | [diff] [blame] | 136 | |
| 137 | // Benchmark test for RandBytes(). Disabled since it's intentionally slow and |
| 138 | // does not test anything that isn't already tested by the existing RandBytes() |
| 139 | // tests. |
| 140 | TEST(RandUtilTest, DISABLED_RandBytesPerf) { |
| 141 | // Benchmark the performance of |kTestIterations| of RandBytes() using a |
| 142 | // buffer size of |kTestBufferSize|. |
| 143 | const int kTestIterations = 10; |
| 144 | const size_t kTestBufferSize = 1 * 1024 * 1024; |
| 145 | |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 146 | std::unique_ptr<uint8_t[]> buffer(new uint8_t[kTestBufferSize]); |
charliea | ddf2b79 | 2015-01-27 02:35:41 +0900 | [diff] [blame] | 147 | const base::TimeTicks now = base::TimeTicks::Now(); |
dalecurtis@chromium.org | 4f4a877 | 2014-01-23 11:14:28 +0900 | [diff] [blame] | 148 | for (int i = 0; i < kTestIterations; ++i) |
| 149 | base::RandBytes(buffer.get(), kTestBufferSize); |
charliea | ddf2b79 | 2015-01-27 02:35:41 +0900 | [diff] [blame] | 150 | const base::TimeTicks end = base::TimeTicks::Now(); |
dalecurtis@chromium.org | 4f4a877 | 2014-01-23 11:14:28 +0900 | [diff] [blame] | 151 | |
| 152 | LOG(INFO) << "RandBytes(" << kTestBufferSize << ") took: " |
| 153 | << (end - now).InMicroseconds() << "µs"; |
| 154 | } |