blob: 86d831b9aee4243c89cd2ca9157bf174de464792 [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"
16#include "pc/unique_id_generator.h"
17#include "rtc_base/gunit.h"
18#include "rtc_base/helpers.h"
19#include "test/gmock.h"
20
21using ::testing::IsEmpty;
22using ::testing::Test;
23
24namespace webrtc {
25
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
35TYPED_TEST_CASE(UniqueIdGeneratorTest, test_types);
36
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
79} // namespace webrtc