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