Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include "algorithm" |
| 12 | #include "string" |
| 13 | #include "vector" |
| 14 | |
| 15 | #include "api/array_view.h" |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 16 | #include "rtc_base/gunit.h" |
| 17 | #include "rtc_base/helpers.h" |
Amit Hilbuch | dbb49df | 2019-01-23 14:54:24 -0800 | [diff] [blame] | 18 | #include "rtc_base/unique_id_generator.h" |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 19 | #include "test/gmock.h" |
| 20 | |
| 21 | using ::testing::IsEmpty; |
| 22 | using ::testing::Test; |
| 23 | |
Amit Hilbuch | dbb49df | 2019-01-23 14:54:24 -0800 | [diff] [blame] | 24 | namespace rtc { |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 25 | |
| 26 | template <typename Generator> |
| 27 | class UniqueIdGeneratorTest : public Test {}; |
| 28 | |
| 29 | using test_types = ::testing::Types<UniqueNumberGenerator<uint8_t>, |
| 30 | UniqueNumberGenerator<uint16_t>, |
| 31 | UniqueNumberGenerator<uint32_t>, |
Elad Alon | efc9a14 | 2019-02-08 23:35:59 +0100 | [diff] [blame^] | 32 | UniqueNumberGenerator<int>, |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 33 | UniqueRandomIdGenerator, |
| 34 | UniqueStringGenerator>; |
| 35 | |
Mirko Bonadei | c84f661 | 2019-01-31 12:20:57 +0100 | [diff] [blame] | 36 | TYPED_TEST_SUITE(UniqueIdGeneratorTest, test_types); |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 37 | |
| 38 | TYPED_TEST(UniqueIdGeneratorTest, ElementsDoNotRepeat) { |
| 39 | typedef TypeParam Generator; |
| 40 | const size_t num_elements = 255; |
| 41 | Generator generator; |
| 42 | std::vector<typename Generator::value_type> values; |
| 43 | for (size_t i = 0; i < num_elements; i++) { |
| 44 | values.push_back(generator()); |
| 45 | } |
| 46 | |
| 47 | EXPECT_EQ(num_elements, values.size()); |
| 48 | // Use a set to check uniqueness. |
| 49 | std::set<typename Generator::value_type> set(values.begin(), values.end()); |
| 50 | EXPECT_EQ(values.size(), set.size()) << "Returned values were not unique."; |
| 51 | } |
| 52 | |
| 53 | TYPED_TEST(UniqueIdGeneratorTest, KnownElementsAreNotGenerated) { |
| 54 | typedef TypeParam Generator; |
| 55 | const size_t num_elements = 100; |
| 56 | rtc::InitRandom(0); |
| 57 | Generator generator1; |
| 58 | std::vector<typename Generator::value_type> known_values; |
| 59 | for (size_t i = 0; i < num_elements; i++) { |
| 60 | known_values.push_back(generator1()); |
| 61 | } |
| 62 | EXPECT_EQ(num_elements, known_values.size()); |
| 63 | |
| 64 | rtc::InitRandom(0); |
| 65 | Generator generator2(known_values); |
| 66 | |
| 67 | std::vector<typename Generator::value_type> values; |
| 68 | for (size_t i = 0; i < num_elements; i++) { |
| 69 | values.push_back(generator2()); |
| 70 | } |
| 71 | EXPECT_THAT(values, ::testing::SizeIs(num_elements)); |
| 72 | std::sort(values.begin(), values.end()); |
| 73 | std::sort(known_values.begin(), known_values.end()); |
| 74 | std::vector<typename Generator::value_type> intersection; |
| 75 | std::set_intersection(values.begin(), values.end(), known_values.begin(), |
| 76 | known_values.end(), std::back_inserter(intersection)); |
| 77 | EXPECT_THAT(intersection, IsEmpty()); |
| 78 | } |
| 79 | |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 80 | TYPED_TEST(UniqueIdGeneratorTest, AddedElementsAreNotGenerated) { |
| 81 | typedef TypeParam Generator; |
| 82 | const size_t num_elements = 100; |
| 83 | rtc::InitRandom(0); |
| 84 | Generator generator1; |
| 85 | std::vector<typename Generator::value_type> known_values; |
| 86 | for (size_t i = 0; i < num_elements; i++) { |
| 87 | known_values.push_back(generator1()); |
| 88 | } |
| 89 | EXPECT_EQ(num_elements, known_values.size()); |
| 90 | |
| 91 | rtc::InitRandom(0); |
| 92 | Generator generator2; |
| 93 | |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 94 | for (const typename Generator::value_type& value : known_values) { |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 95 | generator2.AddKnownId(value); |
| 96 | } |
| 97 | |
| 98 | std::vector<typename Generator::value_type> values; |
| 99 | for (size_t i = 0; i < num_elements; i++) { |
| 100 | values.push_back(generator2()); |
| 101 | } |
| 102 | EXPECT_THAT(values, ::testing::SizeIs(num_elements)); |
| 103 | std::sort(values.begin(), values.end()); |
| 104 | std::sort(known_values.begin(), known_values.end()); |
| 105 | std::vector<typename Generator::value_type> intersection; |
| 106 | std::set_intersection(values.begin(), values.end(), known_values.begin(), |
| 107 | known_values.end(), std::back_inserter(intersection)); |
| 108 | EXPECT_THAT(intersection, IsEmpty()); |
| 109 | } |
| 110 | |
Elad Alon | efc9a14 | 2019-02-08 23:35:59 +0100 | [diff] [blame^] | 111 | TYPED_TEST(UniqueIdGeneratorTest, AddKnownIdOnNewIdReturnsTrue) { |
| 112 | typedef TypeParam Generator; |
| 113 | |
| 114 | rtc::InitRandom(0); |
| 115 | Generator generator1; |
| 116 | const typename Generator::value_type id = generator1(); |
| 117 | |
| 118 | rtc::InitRandom(0); |
| 119 | Generator generator2; |
| 120 | EXPECT_TRUE(generator2.AddKnownId(id)); |
| 121 | } |
| 122 | |
| 123 | TYPED_TEST(UniqueIdGeneratorTest, AddKnownIdCalledAgainForSameIdReturnsFalse) { |
| 124 | typedef TypeParam Generator; |
| 125 | |
| 126 | rtc::InitRandom(0); |
| 127 | Generator generator1; |
| 128 | const typename Generator::value_type id = generator1(); |
| 129 | |
| 130 | rtc::InitRandom(0); |
| 131 | Generator generator2; |
| 132 | ASSERT_TRUE(generator2.AddKnownId(id)); |
| 133 | EXPECT_FALSE(generator2.AddKnownId(id)); |
| 134 | } |
| 135 | |
| 136 | TYPED_TEST(UniqueIdGeneratorTest, |
| 137 | AddKnownIdOnIdProvidedAsKnownToCtorReturnsFalse) { |
| 138 | typedef TypeParam Generator; |
| 139 | |
| 140 | rtc::InitRandom(0); |
| 141 | Generator generator1; |
| 142 | const typename Generator::value_type id = generator1(); |
| 143 | std::vector<typename Generator::value_type> known_values = {id}; |
| 144 | |
| 145 | rtc::InitRandom(0); |
| 146 | Generator generator2(known_values); |
| 147 | EXPECT_FALSE(generator2.AddKnownId(id)); |
| 148 | } |
| 149 | |
Amit Hilbuch | dbb49df | 2019-01-23 14:54:24 -0800 | [diff] [blame] | 150 | } // namespace rtc |