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 <limits> | 7 #include <limits> |
8 | 8 |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 | 10 |
(...skipping 10 matching lines...) Expand all Loading... |
21 EXPECT_EQ(base::RandInt(kIntMax, kIntMax), kIntMax); | 21 EXPECT_EQ(base::RandInt(kIntMax, kIntMax), kIntMax); |
22 } | 22 } |
23 | 23 |
24 TEST(RandUtilTest, RandDouble) { | 24 TEST(RandUtilTest, RandDouble) { |
25 // Force 64-bit precision, making sure we're not in a 80-bit FPU register. | 25 // Force 64-bit precision, making sure we're not in a 80-bit FPU register. |
26 volatile double number = base::RandDouble(); | 26 volatile double number = base::RandDouble(); |
27 EXPECT_GT(1.0, number); | 27 EXPECT_GT(1.0, number); |
28 EXPECT_LE(0.0, number); | 28 EXPECT_LE(0.0, number); |
29 } | 29 } |
30 | 30 |
| 31 TEST(RandUtilTest, RandBytes) { |
| 32 const size_t buffer_size = 145; |
| 33 char buffer[buffer_size]; |
| 34 memset(buffer, 0, buffer_size); |
| 35 base::RandBytes(buffer, buffer_size); |
| 36 char accumulator = 0; |
| 37 for(size_t i = 0; i < buffer_size; ++i) |
| 38 accumulator |= buffer[i]; |
| 39 // In theory this test can fail, but it won't before the universe dies of |
| 40 // heat death. |
| 41 EXPECT_NE(0, accumulator); |
| 42 } |
| 43 |
31 TEST(RandUtilTest, RandBytesAsString) { | 44 TEST(RandUtilTest, RandBytesAsString) { |
32 std::string random_string = base::RandBytesAsString(0); | 45 std::string random_string = base::RandBytesAsString(0); |
33 EXPECT_EQ(0U, random_string.size()); | 46 EXPECT_EQ(0U, random_string.size()); |
34 random_string = base::RandBytesAsString(145); | 47 random_string = base::RandBytesAsString(145); |
35 EXPECT_EQ(145U, random_string.size()); | 48 EXPECT_EQ(145U, random_string.size()); |
36 char accumulator = 0; | 49 char accumulator = 0; |
37 for (size_t i = 0; i < random_string.size(); ++i) | 50 for (size_t i = 0; i < random_string.size(); ++i) |
38 accumulator |= random_string[i]; | 51 accumulator |= random_string[i]; |
39 // In theory this test can fail, but it won't before the universe dies of | 52 // In theory this test can fail, but it won't before the universe dies of |
40 // heat death. | 53 // heat death. |
41 EXPECT_NE(0, accumulator); | 54 EXPECT_NE(0, accumulator); |
42 } | 55 } |
43 | 56 |
44 // Make sure that it is still appropriate to use RandGenerator in conjunction | 57 // Make sure that it is still appropriate to use RandGenerator in conjunction |
45 // with std::random_shuffle(). | 58 // with std::random_shuffle(). |
46 TEST(RandUtilTest, RandGeneratorForRandomShuffle) { | 59 TEST(RandUtilTest, RandGeneratorForRandomShuffle) { |
47 EXPECT_EQ(base::RandGenerator(1), 0U); | 60 EXPECT_EQ(base::RandGenerator(1), 0U); |
48 EXPECT_LE(std::numeric_limits<ptrdiff_t>::max(), | 61 EXPECT_LE(std::numeric_limits<ptrdiff_t>::max(), |
49 std::numeric_limits<int64>::max()); | 62 std::numeric_limits<int64>::max()); |
50 } | 63 } |
OLD | NEW |