| 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 <algorithm> |
| 7 #include <limits> | 8 #include <limits> |
| 8 | 9 |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 11 |
| 11 namespace { | 12 namespace { |
| 12 | 13 |
| 13 const int kIntMin = std::numeric_limits<int>::min(); | 14 const int kIntMin = std::numeric_limits<int>::min(); |
| 14 const int kIntMax = std::numeric_limits<int>::max(); | 15 const int kIntMax = std::numeric_limits<int>::max(); |
| 15 | 16 |
| 16 } // namespace | 17 } // namespace |
| 17 | 18 |
| 18 TEST(RandUtilTest, SameMinAndMax) { | 19 TEST(RandUtilTest, SameMinAndMax) { |
| 19 EXPECT_EQ(base::RandInt(0, 0), 0); | 20 EXPECT_EQ(base::RandInt(0, 0), 0); |
| 20 EXPECT_EQ(base::RandInt(kIntMin, kIntMin), kIntMin); | 21 EXPECT_EQ(base::RandInt(kIntMin, kIntMin), kIntMin); |
| 21 EXPECT_EQ(base::RandInt(kIntMax, kIntMax), kIntMax); | 22 EXPECT_EQ(base::RandInt(kIntMax, kIntMax), kIntMax); |
| 22 } | 23 } |
| 23 | 24 |
| 24 TEST(RandUtilTest, RandDouble) { | 25 TEST(RandUtilTest, RandDouble) { |
| 25 // Force 64-bit precision, making sure we're not in a 80-bit FPU register. | 26 // Force 64-bit precision, making sure we're not in a 80-bit FPU register. |
| 26 volatile double number = base::RandDouble(); | 27 volatile double number = base::RandDouble(); |
| 27 EXPECT_GT(1.0, number); | 28 EXPECT_GT(1.0, number); |
| 28 EXPECT_LE(0.0, number); | 29 EXPECT_LE(0.0, number); |
| 29 } | 30 } |
| 30 | 31 |
| 31 TEST(RandUtilTest, RandBytes) { | 32 TEST(RandUtilTest, RandBytes) { |
| 32 const size_t buffer_size = 145; | 33 const size_t buffer_size = 50; |
| 33 char buffer[buffer_size]; | 34 char buffer[buffer_size]; |
| 34 memset(buffer, 0, buffer_size); | 35 memset(buffer, 0, buffer_size); |
| 35 base::RandBytes(buffer, buffer_size); | 36 base::RandBytes(buffer, buffer_size); |
| 36 char accumulator = 0; | 37 std::sort(buffer, buffer + buffer_size); |
| 37 for(size_t i = 0; i < buffer_size; ++i) | 38 // Probability of occurrence of less than 25 unique bytes in 50 random bytes |
| 38 accumulator |= buffer[i]; | 39 // is below 10^-25. |
| 39 // In theory this test can fail, but it won't before the universe dies of | 40 EXPECT_GT(std::unique(buffer, buffer + buffer_size) - buffer, 25); |
| 40 // heat death. | |
| 41 EXPECT_NE(0, accumulator); | |
| 42 } | 41 } |
| 43 | 42 |
| 44 TEST(RandUtilTest, RandBytesAsString) { | 43 TEST(RandUtilTest, RandBytesAsString) { |
| 45 std::string random_string = base::RandBytesAsString(0); | 44 std::string random_string = base::RandBytesAsString(0); |
| 46 EXPECT_EQ(0U, random_string.size()); | 45 EXPECT_EQ(0U, random_string.size()); |
| 47 random_string = base::RandBytesAsString(145); | 46 random_string = base::RandBytesAsString(145); |
| 48 EXPECT_EQ(145U, random_string.size()); | 47 EXPECT_EQ(145U, random_string.size()); |
| 49 char accumulator = 0; | 48 char accumulator = 0; |
| 50 for (size_t i = 0; i < random_string.size(); ++i) | 49 for (size_t i = 0; i < random_string.size(); ++i) |
| 51 accumulator |= random_string[i]; | 50 accumulator |= random_string[i]; |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 uint64 value = base::RandUint64(); | 113 uint64 value = base::RandUint64(); |
| 115 found_ones |= value; | 114 found_ones |= value; |
| 116 found_zeros &= value; | 115 found_zeros &= value; |
| 117 | 116 |
| 118 if (found_zeros == kAllZeros && found_ones == kAllOnes) | 117 if (found_zeros == kAllZeros && found_ones == kAllOnes) |
| 119 return; | 118 return; |
| 120 } | 119 } |
| 121 | 120 |
| 122 FAIL() << "Didn't achieve all bit values in maximum number of tries."; | 121 FAIL() << "Didn't achieve all bit values in maximum number of tries."; |
| 123 } | 122 } |
| OLD | NEW |