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>, |
| 32 | UniqueRandomIdGenerator, |
| 33 | UniqueStringGenerator>; |
| 34 | |
Mirko Bonadei | c84f661 | 2019-01-31 12:20:57 +0100 | [diff] [blame^] | 35 | TYPED_TEST_SUITE(UniqueIdGeneratorTest, test_types); |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 36 | |
| 37 | TYPED_TEST(UniqueIdGeneratorTest, ElementsDoNotRepeat) { |
| 38 | typedef TypeParam Generator; |
| 39 | const size_t num_elements = 255; |
| 40 | Generator generator; |
| 41 | std::vector<typename Generator::value_type> values; |
| 42 | for (size_t i = 0; i < num_elements; i++) { |
| 43 | values.push_back(generator()); |
| 44 | } |
| 45 | |
| 46 | EXPECT_EQ(num_elements, values.size()); |
| 47 | // Use a set to check uniqueness. |
| 48 | std::set<typename Generator::value_type> set(values.begin(), values.end()); |
| 49 | EXPECT_EQ(values.size(), set.size()) << "Returned values were not unique."; |
| 50 | } |
| 51 | |
| 52 | TYPED_TEST(UniqueIdGeneratorTest, KnownElementsAreNotGenerated) { |
| 53 | typedef TypeParam Generator; |
| 54 | const size_t num_elements = 100; |
| 55 | rtc::InitRandom(0); |
| 56 | Generator generator1; |
| 57 | std::vector<typename Generator::value_type> known_values; |
| 58 | for (size_t i = 0; i < num_elements; i++) { |
| 59 | known_values.push_back(generator1()); |
| 60 | } |
| 61 | EXPECT_EQ(num_elements, known_values.size()); |
| 62 | |
| 63 | rtc::InitRandom(0); |
| 64 | Generator generator2(known_values); |
| 65 | |
| 66 | std::vector<typename Generator::value_type> values; |
| 67 | for (size_t i = 0; i < num_elements; i++) { |
| 68 | values.push_back(generator2()); |
| 69 | } |
| 70 | EXPECT_THAT(values, ::testing::SizeIs(num_elements)); |
| 71 | std::sort(values.begin(), values.end()); |
| 72 | std::sort(known_values.begin(), known_values.end()); |
| 73 | std::vector<typename Generator::value_type> intersection; |
| 74 | std::set_intersection(values.begin(), values.end(), known_values.begin(), |
| 75 | known_values.end(), std::back_inserter(intersection)); |
| 76 | EXPECT_THAT(intersection, IsEmpty()); |
| 77 | } |
| 78 | |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 79 | TYPED_TEST(UniqueIdGeneratorTest, AddedElementsAreNotGenerated) { |
| 80 | typedef TypeParam Generator; |
| 81 | const size_t num_elements = 100; |
| 82 | rtc::InitRandom(0); |
| 83 | Generator generator1; |
| 84 | std::vector<typename Generator::value_type> known_values; |
| 85 | for (size_t i = 0; i < num_elements; i++) { |
| 86 | known_values.push_back(generator1()); |
| 87 | } |
| 88 | EXPECT_EQ(num_elements, known_values.size()); |
| 89 | |
| 90 | rtc::InitRandom(0); |
| 91 | Generator generator2; |
| 92 | |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 93 | for (const typename Generator::value_type& value : known_values) { |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 94 | generator2.AddKnownId(value); |
| 95 | } |
| 96 | |
| 97 | std::vector<typename Generator::value_type> values; |
| 98 | for (size_t i = 0; i < num_elements; i++) { |
| 99 | values.push_back(generator2()); |
| 100 | } |
| 101 | EXPECT_THAT(values, ::testing::SizeIs(num_elements)); |
| 102 | std::sort(values.begin(), values.end()); |
| 103 | std::sort(known_values.begin(), known_values.end()); |
| 104 | std::vector<typename Generator::value_type> intersection; |
| 105 | std::set_intersection(values.begin(), values.end(), known_values.begin(), |
| 106 | known_values.end(), std::back_inserter(intersection)); |
| 107 | EXPECT_THAT(intersection, IsEmpty()); |
| 108 | } |
| 109 | |
Amit Hilbuch | dbb49df | 2019-01-23 14:54:24 -0800 | [diff] [blame] | 110 | } // namespace rtc |