blob: 1634d56148a47bc1849d7823c2cb130892c4f8b3 [file] [log] [blame]
Amit Hilbuchc63ddb22019-01-02 10:13:58 -08001/*
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 Hilbuchc63ddb22019-01-02 10:13:58 -080016#include "rtc_base/gunit.h"
17#include "rtc_base/helpers.h"
Amit Hilbuchdbb49df2019-01-23 14:54:24 -080018#include "rtc_base/unique_id_generator.h"
Amit Hilbuchc63ddb22019-01-02 10:13:58 -080019#include "test/gmock.h"
20
21using ::testing::IsEmpty;
22using ::testing::Test;
23
Amit Hilbuchdbb49df2019-01-23 14:54:24 -080024namespace rtc {
Amit Hilbuchc63ddb22019-01-02 10:13:58 -080025
26template <typename Generator>
27class UniqueIdGeneratorTest : public Test {};
28
29using test_types = ::testing::Types<UniqueNumberGenerator<uint8_t>,
30 UniqueNumberGenerator<uint16_t>,
31 UniqueNumberGenerator<uint32_t>,
32 UniqueRandomIdGenerator,
33 UniqueStringGenerator>;
34
Mirko Bonadeic84f6612019-01-31 12:20:57 +010035TYPED_TEST_SUITE(UniqueIdGeneratorTest, test_types);
Amit Hilbuchc63ddb22019-01-02 10:13:58 -080036
37TYPED_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
52TYPED_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 Hilbuchae3df542019-01-07 12:13:08 -080079TYPED_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 Bonadei739baf02019-01-27 17:29:42 +010093 for (const typename Generator::value_type& value : known_values) {
Amit Hilbuchae3df542019-01-07 12:13:08 -080094 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 Hilbuchdbb49df2019-01-23 14:54:24 -0800110} // namespace rtc