OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (c) 2015 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 <limits> | |
13 #include <vector> | |
14 | |
15 #include "testing/gtest/include/gtest/gtest.h" | |
16 #include "webrtc/modules/bitrate_controller/send_time_history.h" | |
17 #include "webrtc/system_wrappers/interface/clock.h" | |
18 | |
19 namespace webrtc { | |
20 | |
21 static const int kDefaultHistoryLengthMs = 1000; | |
22 | |
23 class SendTimeHistoryTest : public ::testing::Test { | |
24 protected: | |
25 SendTimeHistoryTest() : history_(kDefaultHistoryLengthMs), clock_(0) {} | |
26 ~SendTimeHistoryTest() {} | |
27 | |
28 virtual void SetUp() {} | |
29 | |
30 virtual void TearDown() {} | |
31 | |
32 SendTimeHistory history_; | |
33 webrtc::SimulatedClock clock_; | |
34 }; | |
35 | |
36 TEST_F(SendTimeHistoryTest, AddRemoveOne) { | |
37 const uint16_t kSeqNo = 1; | |
38 const int64_t kTimestamp = 2; | |
39 history_.AddAndRemoveOldSendTimes(kSeqNo, kTimestamp); | |
40 | |
41 int64_t time = 0; | |
42 EXPECT_TRUE(history_.GetSendTime(kSeqNo, &time, false)); | |
43 EXPECT_EQ(kTimestamp, time); | |
44 | |
45 time = 0; | |
46 EXPECT_TRUE(history_.GetSendTime(kSeqNo, &time, true)); | |
47 EXPECT_EQ(kTimestamp, time); | |
48 | |
49 time = 0; | |
50 EXPECT_FALSE(history_.GetSendTime(kSeqNo, &time, true)); | |
51 } | |
52 | |
53 TEST_F(SendTimeHistoryTest, AddThenRemoveOutOfOrder) { | |
54 struct Timestamp { | |
55 Timestamp(uint16_t sequence_number, int64_t timestamp) | |
56 : sequence_number(sequence_number), timestamp(timestamp) {} | |
57 uint16_t sequence_number; | |
58 int64_t timestamp; | |
59 }; | |
60 std::vector<Timestamp> timestamps; | |
61 const size_t num_items = 100; | |
62 for (size_t i = 0; i < num_items; ++i) { | |
63 timestamps.push_back( | |
64 Timestamp(static_cast<uint16_t>(i), static_cast<int64_t>(i))); | |
65 } | |
66 std::vector<Timestamp> randomized_timestamps = timestamps; | |
67 std::random_shuffle(randomized_timestamps.begin(), | |
68 randomized_timestamps.end()); | |
69 for (size_t i = 0; i < num_items; ++i) { | |
70 history_.AddAndRemoveOldSendTimes(timestamps[i].sequence_number, | |
71 timestamps[i].timestamp); | |
72 } | |
73 for (size_t i = 0; i < num_items; ++i) { | |
74 int64_t timestamp; | |
75 EXPECT_TRUE(history_.GetSendTime(randomized_timestamps[i].sequence_number, | |
76 ×tamp, false)); | |
77 EXPECT_EQ(randomized_timestamps[i].timestamp, timestamp); | |
78 EXPECT_TRUE(history_.GetSendTime(randomized_timestamps[i].sequence_number, | |
79 ×tamp, true)); | |
80 } | |
81 for (size_t i = 0; i < num_items; ++i) { | |
82 int64_t timestamp; | |
83 EXPECT_FALSE( | |
84 history_.GetSendTime(timestamps[i].sequence_number, ×tamp, false)); | |
85 } | |
86 } | |
87 | |
88 TEST_F(SendTimeHistoryTest, HistorySize) { | |
89 const int kItems = kDefaultHistoryLengthMs / 100; | |
90 for (int i = 0; i < kItems; ++i) { | |
91 history_.AddAndRemoveOldSendTimes(i, i * 100); | |
92 } | |
93 int64_t timestamp; | |
94 for (int i = 0; i < kItems; ++i) { | |
95 EXPECT_TRUE(history_.GetSendTime(i, ×tamp, false)); | |
96 EXPECT_EQ(i * 100, timestamp); | |
97 } | |
98 history_.AddAndRemoveOldSendTimes(kItems, kItems * 100); | |
99 EXPECT_FALSE(history_.GetSendTime(0, ×tamp, false)); | |
100 for (int i = 1; i < (kItems + 1); ++i) { | |
101 EXPECT_TRUE(history_.GetSendTime(i, ×tamp, false)); | |
102 EXPECT_EQ(i * 100, timestamp); | |
103 } | |
104 } | |
105 | |
106 TEST_F(SendTimeHistoryTest, HistorySizeWithWraparound) { | |
107 const int kMaxSeqNo = std::numeric_limits<uint16_t>::max(); | |
108 history_.AddAndRemoveOldSendTimes(kMaxSeqNo - 2, 0); | |
109 history_.AddAndRemoveOldSendTimes(kMaxSeqNo - 1, 100); | |
110 history_.AddAndRemoveOldSendTimes(kMaxSeqNo, 200); | |
111 history_.AddAndRemoveOldSendTimes(0, 1000); | |
112 int64_t timestamp; | |
113 EXPECT_FALSE(history_.GetSendTime(kMaxSeqNo - 2, ×tamp, false)); | |
114 EXPECT_TRUE(history_.GetSendTime(kMaxSeqNo - 1, ×tamp, false)); | |
115 EXPECT_TRUE(history_.GetSendTime(kMaxSeqNo, ×tamp, false)); | |
116 EXPECT_TRUE(history_.GetSendTime(0, ×tamp, false)); | |
117 | |
118 // Create a gap (kMaxSeqNo - 1) -> 0. | |
119 EXPECT_TRUE(history_.GetSendTime(kMaxSeqNo, ×tamp, true)); | |
120 | |
121 history_.AddAndRemoveOldSendTimes(1, 1100); | |
122 | |
123 EXPECT_FALSE(history_.GetSendTime(kMaxSeqNo - 2, ×tamp, false)); | |
124 EXPECT_FALSE(history_.GetSendTime(kMaxSeqNo - 1, ×tamp, false)); | |
125 EXPECT_FALSE(history_.GetSendTime(kMaxSeqNo, ×tamp, false)); | |
126 EXPECT_TRUE(history_.GetSendTime(0, ×tamp, false)); | |
127 EXPECT_TRUE(history_.GetSendTime(1, ×tamp, false)); | |
128 } | |
129 | |
130 TEST_F(SendTimeHistoryTest, InterlievedGetAndRemove) { | |
131 const uint16_t kSeqNo = 1; | |
132 const int64_t kTimestamp = 2; | |
133 | |
134 history_.AddAndRemoveOldSendTimes(kSeqNo, kTimestamp); | |
135 history_.AddAndRemoveOldSendTimes(kSeqNo + 1, kTimestamp + 1); | |
136 | |
137 int64_t time = 0; | |
138 EXPECT_TRUE(history_.GetSendTime(kSeqNo, &time, true)); | |
139 EXPECT_EQ(kTimestamp, time); | |
140 | |
141 history_.AddAndRemoveOldSendTimes(kSeqNo + 2, kTimestamp + 2); | |
142 | |
143 EXPECT_TRUE(history_.GetSendTime(kSeqNo + 1, &time, true)); | |
144 EXPECT_EQ(kTimestamp + 1, time); | |
145 EXPECT_TRUE(history_.GetSendTime(kSeqNo + 2, &time, true)); | |
146 EXPECT_EQ(kTimestamp + 2, time); | |
147 } | |
148 | |
149 } // namespace webrtc | |
OLD | NEW |