henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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 | #if !defined(__arm__) |
| 12 | // For testing purposes, define faked versions of the atomic operations |
| 13 | #include "webrtc/base/basictypes.h" |
| 14 | namespace rtc { |
| 15 | typedef uint32 Atomic32; |
| 16 | static inline void MemoryBarrier() { } |
| 17 | static inline void AtomicIncrement(volatile Atomic32* ptr) { |
| 18 | *ptr = *ptr + 1; |
| 19 | } |
| 20 | } |
| 21 | #define SKIP_ATOMIC_CHECK |
| 22 | #endif |
| 23 | |
| 24 | #include "webrtc/base/atomicops.h" |
| 25 | #include "webrtc/base/gunit.h" |
| 26 | #include "webrtc/base/helpers.h" |
| 27 | #include "webrtc/base/logging.h" |
| 28 | |
| 29 | TEST(FixedSizeLockFreeQueueTest, TestDefaultConstruct) { |
| 30 | rtc::FixedSizeLockFreeQueue<int> queue; |
| 31 | EXPECT_EQ(0u, queue.capacity()); |
| 32 | EXPECT_EQ(0u, queue.Size()); |
| 33 | EXPECT_FALSE(queue.PushBack(1)); |
| 34 | int val; |
| 35 | EXPECT_FALSE(queue.PopFront(&val)); |
| 36 | } |
| 37 | |
| 38 | TEST(FixedSizeLockFreeQueueTest, TestConstruct) { |
| 39 | rtc::FixedSizeLockFreeQueue<int> queue(5); |
| 40 | EXPECT_EQ(5u, queue.capacity()); |
| 41 | EXPECT_EQ(0u, queue.Size()); |
| 42 | int val; |
| 43 | EXPECT_FALSE(queue.PopFront(&val)); |
| 44 | } |
| 45 | |
| 46 | TEST(FixedSizeLockFreeQueueTest, TestPushPop) { |
| 47 | rtc::FixedSizeLockFreeQueue<int> queue(2); |
| 48 | EXPECT_EQ(2u, queue.capacity()); |
| 49 | EXPECT_EQ(0u, queue.Size()); |
| 50 | EXPECT_TRUE(queue.PushBack(1)); |
| 51 | EXPECT_EQ(1u, queue.Size()); |
| 52 | EXPECT_TRUE(queue.PushBack(2)); |
| 53 | EXPECT_EQ(2u, queue.Size()); |
| 54 | EXPECT_FALSE(queue.PushBack(3)); |
| 55 | EXPECT_EQ(2u, queue.Size()); |
| 56 | int val; |
| 57 | EXPECT_TRUE(queue.PopFront(&val)); |
| 58 | EXPECT_EQ(1, val); |
| 59 | EXPECT_EQ(1u, queue.Size()); |
| 60 | EXPECT_TRUE(queue.PopFront(&val)); |
| 61 | EXPECT_EQ(2, val); |
| 62 | EXPECT_EQ(0u, queue.Size()); |
| 63 | EXPECT_FALSE(queue.PopFront(&val)); |
| 64 | EXPECT_EQ(0u, queue.Size()); |
| 65 | } |
| 66 | |
| 67 | TEST(FixedSizeLockFreeQueueTest, TestResize) { |
| 68 | rtc::FixedSizeLockFreeQueue<int> queue(2); |
| 69 | EXPECT_EQ(2u, queue.capacity()); |
| 70 | EXPECT_EQ(0u, queue.Size()); |
| 71 | EXPECT_TRUE(queue.PushBack(1)); |
| 72 | EXPECT_EQ(1u, queue.Size()); |
| 73 | |
| 74 | queue.ClearAndResizeUnsafe(5); |
| 75 | EXPECT_EQ(5u, queue.capacity()); |
| 76 | EXPECT_EQ(0u, queue.Size()); |
| 77 | int val; |
| 78 | EXPECT_FALSE(queue.PopFront(&val)); |
| 79 | } |