| OLD | NEW | 
|   1 /* |   1 /* | 
|   2  *  Copyright 2004 The WebRTC Project Authors. All rights reserved. |   2  *  Copyright 2004 The WebRTC Project Authors. All rights reserved. | 
|   3  * |   3  * | 
|   4  *  Use of this source code is governed by a BSD-style license |   4  *  Use of this source code is governed by a BSD-style license | 
|   5  *  that can be found in the LICENSE file in the root of the source |   5  *  that can be found in the LICENSE file in the root of the source | 
|   6  *  tree. An additional intellectual property rights grant can be found |   6  *  tree. An additional intellectual property rights grant can be found | 
|   7  *  in the file PATENTS.  All contributing project authors may |   7  *  in the file PATENTS.  All contributing project authors may | 
|   8  *  be found in the AUTHORS file in the root of the source tree. |   8  *  be found in the AUTHORS file in the root of the source tree. | 
|   9  */ |   9  */ | 
|  10  |  10  | 
|  11 #include <string> |  11 #include <string> | 
|  12  |  12  | 
|  13 #include "webrtc/base/gunit.h" |  13 #include "webrtc/base/gunit.h" | 
|  14 #include "webrtc/base/helpers.h" |  14 #include "webrtc/base/helpers.h" | 
|  15 #include "webrtc/base/ssladapter.h" |  15 #include "webrtc/base/ssladapter.h" | 
|  16  |  16  | 
|  17 namespace rtc { |  17 namespace rtc { | 
|  18  |  18  | 
|  19 class RandomTest : public testing::Test {}; |  19 class RandomTest : public testing::Test {}; | 
|  20  |  20  | 
|  21 TEST_F(RandomTest, TestCreateRandomId) { |  21 TEST_F(RandomTest, TestCreateRandomId) { | 
|  22   CreateRandomId(); |  22   CreateRandomId(); | 
 |  23   uint32_t id; | 
 |  24   EXPECT_TRUE(CreateRandomId(&id)); | 
|  23 } |  25 } | 
|  24  |  26  | 
|  25 TEST_F(RandomTest, TestCreateRandomDouble) { |  27 TEST_F(RandomTest, TestCreateRandomDouble) { | 
|  26   for (int i = 0; i < 100; ++i) { |  28   for (int i = 0; i < 100; ++i) { | 
|  27     double r = CreateRandomDouble(); |  29     double r = CreateRandomDouble(); | 
|  28     EXPECT_GE(r, 0.0); |  30     EXPECT_GE(r, 0.0); | 
|  29     EXPECT_LT(r, 1.0); |  31     EXPECT_LT(r, 1.0); | 
 |  32     EXPECT_TRUE(CreateRandomDouble(&r)); | 
 |  33     EXPECT_GE(r, 0.0); | 
 |  34     EXPECT_LT(r, 1.0); | 
|  30   } |  35   } | 
|  31 } |  36 } | 
|  32  |  37  | 
|  33 TEST_F(RandomTest, TestCreateNonZeroRandomId) { |  38 TEST_F(RandomTest, TestCreateNonZeroRandomId) { | 
|  34   EXPECT_NE(0U, CreateRandomNonZeroId()); |  39   EXPECT_NE(0U, CreateRandomNonZeroId()); | 
 |  40   uint32_t id; | 
 |  41   EXPECT_TRUE(CreateRandomNonZeroId(&id)); | 
 |  42   EXPECT_NE(0U, id); | 
|  35 } |  43 } | 
|  36  |  44  | 
|  37 TEST_F(RandomTest, TestCreateRandomString) { |  45 TEST_F(RandomTest, TestCreateRandomString) { | 
|  38   std::string random = CreateRandomString(256); |  46   std::string random = CreateRandomString(256); | 
|  39   EXPECT_EQ(256U, random.size()); |  47   EXPECT_EQ(256U, random.size()); | 
|  40   std::string random2; |  48   std::string random2; | 
|  41   EXPECT_TRUE(CreateRandomString(256, &random2)); |  49   EXPECT_TRUE(CreateRandomString(256, &random2)); | 
|  42   EXPECT_NE(random, random2); |  50   EXPECT_NE(random, random2); | 
|  43   EXPECT_EQ(256U, random2.size()); |  51   EXPECT_EQ(256U, random2.size()); | 
|  44 } |  52 } | 
|  45  |  53  | 
|  46 TEST_F(RandomTest, TestCreateRandomUuid) { |  54 TEST_F(RandomTest, TestCreateRandomUuid) { | 
|  47   std::string random = CreateRandomUuid(); |  55   std::string random = CreateRandomUuid(); | 
|  48   EXPECT_EQ(36U, random.size()); |  56   EXPECT_EQ(36U, random.size()); | 
 |  57   random.clear(); | 
 |  58   EXPECT_TRUE(CreateRandomUuid(&random)); | 
 |  59   EXPECT_EQ(36U, random.size()); | 
|  49 } |  60 } | 
|  50  |  61  | 
|  51 TEST_F(RandomTest, TestCreateRandomForTest) { |  62 TEST_F(RandomTest, TestCreateRandomForTest) { | 
|  52   // Make sure we get the output we expect. |  63   // Make sure we get the output we expect. | 
|  53   SetRandomTestMode(true); |  64   SetRandomTestMode(true); | 
|  54   EXPECT_EQ(2154761789U, CreateRandomId()); |  65   EXPECT_EQ(2154761789U, CreateRandomId()); | 
|  55   EXPECT_EQ("h0ISP4S5SJKH/9EY", CreateRandomString(16)); |  66   EXPECT_EQ("h0ISP4S5SJKH/9EY", CreateRandomString(16)); | 
|  56   EXPECT_EQ("41706e92-cdd3-46d9-a22d-8ff1737ffb11", CreateRandomUuid()); |  67   EXPECT_EQ("41706e92-cdd3-46d9-a22d-8ff1737ffb11", CreateRandomUuid()); | 
|  57  |  68  | 
|  58   // Reset and make sure we get the same output. |  69   // Reset and make sure we get the same output. | 
|  59   SetRandomTestMode(true); |  70   SetRandomTestMode(true); | 
|  60   EXPECT_EQ(2154761789U, CreateRandomId()); |  71   EXPECT_EQ(2154761789U, CreateRandomId()); | 
|  61   EXPECT_EQ("h0ISP4S5SJKH/9EY", CreateRandomString(16)); |  72   EXPECT_EQ("h0ISP4S5SJKH/9EY", CreateRandomString(16)); | 
|  62   EXPECT_EQ("41706e92-cdd3-46d9-a22d-8ff1737ffb11", CreateRandomUuid()); |  73   EXPECT_EQ("41706e92-cdd3-46d9-a22d-8ff1737ffb11", CreateRandomUuid()); | 
|  63  |  74  | 
|  64   // Test different character sets. |  75   // Test different character sets. | 
|  65   SetRandomTestMode(true); |  76   SetRandomTestMode(true); | 
|  66   std::string str; |  77   std::string str; | 
|  67   EXPECT_TRUE(CreateRandomString(16, "a", &str)); |  78   EXPECT_TRUE(CreateRandomString(16, "a", &str)); | 
|  68   EXPECT_EQ("aaaaaaaaaaaaaaaa", str); |  79   EXPECT_EQ("aaaaaaaaaaaaaaaa", str); | 
|  69   EXPECT_TRUE(CreateRandomString(16, "abc", &str)); |  80   EXPECT_TRUE(CreateRandomString(16, "abc", &str)); | 
|  70   EXPECT_EQ("acbccaaaabbaacbb", str); |  81   EXPECT_EQ("acbccaaaabbaacbb", str); | 
|  71  |  82  | 
|  72   // Turn off test mode for other tests. |  83   // Turn off test mode for other tests. | 
|  73   SetRandomTestMode(false); |  84   SetRandomTestMode(false); | 
|  74 } |  85 } | 
|  75  |  86  | 
|  76 }  // namespace rtc |  87 }  // namespace rtc | 
| OLD | NEW |