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(RingBufferTest, SmallCapacity) { |
| 90 size_t capacity = 1; |
| 91 RingBuffer<int> q(capacity); |
| 92 EXPECT_TRUE(q.empty()); |
| 93 q.push_back(4711); |
| 94 EXPECT_FALSE(q.empty()); |
| 95 EXPECT_EQ(4711, q.front()); |
| 96 q.push_back(1024); |
| 97 EXPECT_FALSE(q.empty()); |
| 98 EXPECT_EQ(1024, q.front()); |
| 99 q.pop_front(); |
| 100 EXPECT_TRUE(q.empty()); |
| 101 } |
| 102 |
| 103 } // namespace webrtc |
OLD | NEW |