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 | |
12 #include <list> | |
13 | |
14 #include "testing/gtest/include/gtest/gtest.h" | |
15 #include "webrtc/base/random.h" | |
16 #include "webrtc/call/ringbuffer.h" | |
17 | |
18 namespace webrtc { | |
19 | |
20 // Verify that the ring buffer works as a simple queue. | |
21 TEST(RingBufferTest, SimpleQueue) { | |
22 size_t capacity = 100; | |
23 RingBuffer<int> q(capacity); | |
24 EXPECT_TRUE(q.empty()); | |
25 for (size_t i = 0; i < capacity; i++) { | |
26 q.push_back(static_cast<int>(i)); | |
27 EXPECT_FALSE(q.empty()); | |
28 } | |
29 | |
30 for (size_t i = 0; i < capacity; i++) { | |
31 EXPECT_FALSE(q.empty()); | |
32 EXPECT_EQ(static_cast<int>(i), q.front()); | |
33 q.pop_front(); | |
34 } | |
35 EXPECT_TRUE(q.empty()); | |
36 } | |
37 | |
38 // Do a "random" sequence of queue operations and verify that the | |
39 // result is consistent with the same operation performed on a std::list. | |
40 TEST(RingBufferTest, ConsistentWithStdList) { | |
41 Random prng(987654321ull); | |
42 size_t capacity = 10; | |
43 RingBuffer<int> q(capacity); | |
44 std::list<int> l; | |
45 EXPECT_TRUE(q.empty()); | |
46 for (size_t i = 0; i < 100 * capacity; i++) { | |
47 bool insert = prng.Rand<bool>(); | |
48 if ((insert && l.size() < capacity) || l.size() == 0) { | |
49 int x = prng.Rand<int>(); | |
50 l.push_back(x); | |
51 q.push_back(x); | |
52 EXPECT_FALSE(q.empty()); | |
53 } else { | |
54 EXPECT_FALSE(q.empty()); | |
55 EXPECT_EQ(l.front(), q.front()); | |
56 l.pop_front(); | |
57 q.pop_front(); | |
58 } | |
59 } | |
60 while (!l.empty()) { | |
61 EXPECT_FALSE(q.empty()); | |
62 EXPECT_EQ(l.front(), q.front()); | |
63 l.pop_front(); | |
64 q.pop_front(); | |
65 } | |
66 EXPECT_TRUE(q.empty()); | |
67 } | |
68 | |
69 // Test that the ringbuffer starts reusing elements from the front | |
70 // when the queue becomes full. | |
71 TEST(RingBufferTest, OverwriteOldElements) { | |
72 size_t capacity = 100; | |
73 int insertions = 3 * capacity + 25; | |
74 RingBuffer<int> q(capacity); | |
75 EXPECT_TRUE(q.empty()); | |
76 for (int i = 0; i < insertions; i++) { | |
77 q.push_back(i); | |
78 EXPECT_FALSE(q.empty()); | |
79 } | |
80 | |
81 for (int i = insertions - capacity; i < insertions; i++) { | |
82 EXPECT_FALSE(q.empty()); | |
83 EXPECT_EQ(i, q.front()); | |
84 q.pop_front(); | |
85 } | |
86 EXPECT_TRUE(q.empty()); | |
87 } | |
88 | |
89 // Test that the ringbuffer uses swap when pushing an rvalue reference. | |
90 TEST(RingBufferTest, RvaluePushback) { | |
91 int capacity = 100; | |
stefan-webrtc
2016/04/19 08:23:04
size_t
terelius
2016/04/19 09:21:56
Done.
| |
92 int insertions = 3 * capacity + 25; | |
stefan-webrtc
2016/04/19 08:23:04
size_t
terelius
2016/04/19 09:21:56
Done.
| |
93 RingBuffer<int> q(static_cast<size_t>(capacity)); | |
94 EXPECT_TRUE(q.empty()); | |
95 for (int i = 0; i < insertions; i++) { | |
96 int tmp = i; | |
97 q.push_back(std::move(tmp)); | |
98 if (i > capacity) { | |
99 // There are capacity+1 elements in the buffer since we use one | |
100 // extra element as a sentinel. Thus, the element we swap out | |
101 // should be i-(capacity-1). | |
102 EXPECT_EQ(i - capacity - 1, tmp); | |
103 } | |
104 EXPECT_FALSE(q.empty()); | |
105 } | |
106 | |
107 for (int i = insertions - capacity; i < insertions; i++) { | |
108 EXPECT_FALSE(q.empty()); | |
109 EXPECT_EQ(i, q.front()); | |
110 q.pop_front(); | |
111 } | |
112 EXPECT_TRUE(q.empty()); | |
113 } | |
114 | |
115 TEST(RingBufferTest, SmallCapacity) { | |
116 size_t capacity = 1; | |
117 RingBuffer<int> q(capacity); | |
118 EXPECT_TRUE(q.empty()); | |
119 q.push_back(4711); | |
120 EXPECT_FALSE(q.empty()); | |
121 EXPECT_EQ(4711, q.front()); | |
122 q.push_back(1024); | |
123 EXPECT_FALSE(q.empty()); | |
124 EXPECT_EQ(1024, q.front()); | |
125 q.pop_front(); | |
126 EXPECT_TRUE(q.empty()); | |
127 } | |
128 | |
129 } // namespace webrtc | |
OLD | NEW |