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. 4 5 #include "base/rand_util.h" 6 7 #include <stddef.h> 8 #include <stdint.h> 9 10 #include <algorithm> 11 #include <limits> 12 13 #include <gtest/gtest.h> 14 15 #include "base/logging.h" 16 #include "base/memory/scoped_ptr.h" 17 #include "base/time/time.h" 18 19 namespace { 20 21 const int kIntMin = std::numeric_limits<int>::min(); 22 const int kIntMax = std::numeric_limits<int>::max(); 23 24 } // namespace 25 26 TEST(RandUtilTest, RandInt) { 27 EXPECT_EQ(base::RandInt(0, 0), 0); 28 EXPECT_EQ(base::RandInt(kIntMin, kIntMin), kIntMin); 29 EXPECT_EQ(base::RandInt(kIntMax, kIntMax), kIntMax); 30 31 // Check that the DCHECKS in RandInt() don't fire due to internal overflow. 32 // There was a 50% chance of that happening, so calling it 40 times means 33 // the chances of this passing by accident are tiny (9e-13). 34 for (int i = 0; i < 40; ++i) 35 base::RandInt(kIntMin, kIntMax); 36 } 37 38 TEST(RandUtilTest, RandDouble) { 39 // Force 64-bit precision, making sure we're not in a 80-bit FPU register. 40 volatile double number = base::RandDouble(); 41 EXPECT_GT(1.0, number); 42 EXPECT_LE(0.0, number); 43 } 44 45 TEST(RandUtilTest, RandBytes) { 46 const size_t buffer_size = 50; 47 char buffer[buffer_size]; 48 memset(buffer, 0, buffer_size); 49 base::RandBytes(buffer, buffer_size); 50 std::sort(buffer, buffer + buffer_size); 51 // Probability of occurrence of less than 25 unique bytes in 50 random bytes 52 // is below 10^-25. 53 EXPECT_GT(std::unique(buffer, buffer + buffer_size) - buffer, 25); 54 } 55 56 TEST(RandUtilTest, RandBytesAsString) { 57 std::string random_string = base::RandBytesAsString(1); 58 EXPECT_EQ(1U, random_string.size()); 59 random_string = base::RandBytesAsString(145); 60 EXPECT_EQ(145U, random_string.size()); 61 char accumulator = 0; 62 for (size_t i = 0; i < random_string.size(); ++i) 63 accumulator |= random_string[i]; 64 // In theory this test can fail, but it won't before the universe dies of 65 // heat death. 66 EXPECT_NE(0, accumulator); 67 } 68 69 // Make sure that it is still appropriate to use RandGenerator in conjunction 70 // with std::random_shuffle(). 71 TEST(RandUtilTest, RandGeneratorForRandomShuffle) { 72 EXPECT_EQ(base::RandGenerator(1), 0U); 73 EXPECT_LE(std::numeric_limits<ptrdiff_t>::max(), 74 std::numeric_limits<int64_t>::max()); 75 } 76 77 TEST(RandUtilTest, RandGeneratorIsUniform) { 78 // Verify that RandGenerator has a uniform distribution. This is a 79 // regression test that consistently failed when RandGenerator was 80 // implemented this way: 81 // 82 // return base::RandUint64() % max; 83 // 84 // A degenerate case for such an implementation is e.g. a top of 85 // range that is 2/3rds of the way to MAX_UINT64, in which case the 86 // bottom half of the range would be twice as likely to occur as the 87 // top half. A bit of calculus care of jar@ shows that the largest 88 // measurable delta is when the top of the range is 3/4ths of the 89 // way, so that's what we use in the test. 90 const uint64_t kTopOfRange = 91 (std::numeric_limits<uint64_t>::max() / 4ULL) * 3ULL; 92 const uint64_t kExpectedAverage = kTopOfRange / 2ULL; 93 const uint64_t kAllowedVariance = kExpectedAverage / 50ULL; // +/- 2% 94 const int kMinAttempts = 1000; 95 const int kMaxAttempts = 1000000; 96 97 double cumulative_average = 0.0; 98 int count = 0; 99 while (count < kMaxAttempts) { 100 uint64_t value = base::RandGenerator(kTopOfRange); 101 cumulative_average = (count * cumulative_average + value) / (count + 1); 102 103 // Don't quit too quickly for things to start converging, or we may have 104 // a false positive. 105 if (count > kMinAttempts && 106 kExpectedAverage - kAllowedVariance < cumulative_average && 107 cumulative_average < kExpectedAverage + kAllowedVariance) { 108 break; 109 } 110 111 ++count; 112 } 113 114 ASSERT_LT(count, kMaxAttempts) << "Expected average was " << 115 kExpectedAverage << ", average ended at " << cumulative_average; 116 } 117 118 TEST(RandUtilTest, RandUint64ProducesBothValuesOfAllBits) { 119 // This tests to see that our underlying random generator is good 120 // enough, for some value of good enough. 121 uint64_t kAllZeros = 0ULL; 122 uint64_t kAllOnes = ~kAllZeros; 123 uint64_t found_ones = kAllZeros; 124 uint64_t found_zeros = kAllOnes; 125 126 for (size_t i = 0; i < 1000; ++i) { 127 uint64_t value = base::RandUint64(); 128 found_ones |= value; 129 found_zeros &= value; 130 131 if (found_zeros == kAllZeros && found_ones == kAllOnes) 132 return; 133 } 134 135 FAIL() << "Didn't achieve all bit values in maximum number of tries."; 136 } 137