OLD | NEW |
1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2008 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 17 matching lines...) Expand all Loading... |
28 EXPECT_LE(0.0, number); | 28 EXPECT_LE(0.0, number); |
29 } | 29 } |
30 | 30 |
31 // Make sure that it is still appropriate to use RandGenerator in conjunction | 31 // Make sure that it is still appropriate to use RandGenerator in conjunction |
32 // with std::random_shuffle(). | 32 // with std::random_shuffle(). |
33 TEST(RandUtilTest, RandGeneratorForRandomShuffle) { | 33 TEST(RandUtilTest, RandGeneratorForRandomShuffle) { |
34 EXPECT_EQ(base::RandGenerator(1), 0U); | 34 EXPECT_EQ(base::RandGenerator(1), 0U); |
35 EXPECT_LE(std::numeric_limits<ptrdiff_t>::max(), | 35 EXPECT_LE(std::numeric_limits<ptrdiff_t>::max(), |
36 std::numeric_limits<int64>::max()); | 36 std::numeric_limits<int64>::max()); |
37 } | 37 } |
| 38 |
| 39 #if defined(OS_POSIX) |
| 40 // For unit testing purposes only. Do not use outside of tests. |
| 41 namespace base { |
| 42 extern std::string RandomBytesToGUIDString(const uint64 bytes[2]); |
| 43 } // base |
| 44 |
| 45 TEST(RandUtilTest, GUIDGeneratesAllZeroes) { |
| 46 uint64 bytes[] = { 0, 0 }; |
| 47 std::string clientid = base::RandomBytesToGUIDString(bytes); |
| 48 EXPECT_EQ("00000000-0000-0000-0000-000000000000", clientid); |
| 49 } |
| 50 |
| 51 TEST(RandUtilTest, GUIDGeneratesCorrectly) { |
| 52 uint64 bytes[] = { 0x0123456789ABCDEFULL, 0xFEDCBA9876543210ULL }; |
| 53 std::string clientid = base::RandomBytesToGUIDString(bytes); |
| 54 EXPECT_EQ("01234567-89AB-CDEF-FEDC-BA9876543210", clientid); |
| 55 } |
| 56 #endif |
| 57 |
| 58 TEST(RandUtilTest, GUIDCorrectlyFormatted) { |
| 59 const int kIterations = 10; |
| 60 for (int it = 0; it < kIterations; ++it) { |
| 61 std::string guid = base::GenerateGUID(); |
| 62 EXPECT_EQ(36U, guid.length()); |
| 63 std::string hexchars = "0123456789ABCDEF"; |
| 64 for (uint32 i = 0; i < guid.length(); ++i) { |
| 65 char current = guid.at(i); |
| 66 if (i == 8 || i == 13 || i == 18 || i == 23) { |
| 67 EXPECT_EQ('-', current); |
| 68 } else { |
| 69 EXPECT_TRUE(std::string::npos != hexchars.find(current)); |
| 70 } |
| 71 } |
| 72 } |
| 73 } |
| 74 |
| 75 TEST(RandUtilTest, GUIDBasicUniqueness) { |
| 76 const int kIterations = 10; |
| 77 for (int it = 0; it < kIterations; ++it) { |
| 78 std::string guid1 = base::GenerateGUID(); |
| 79 std::string guid2 = base::GenerateGUID(); |
| 80 EXPECT_EQ(36U, guid1.length()); |
| 81 EXPECT_EQ(36U, guid2.length()); |
| 82 EXPECT_NE(guid1, guid2); |
| 83 } |
| 84 } |
OLD | NEW |