1 /* 2 * libjingle 3 * Copyright 2011, Google Inc. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 3. The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #if !defined(__arm__) 29 // For testing purposes, define faked versions of the atomic operations 30 #include "talk/base/basictypes.h" 31 namespace talk_base { 32 typedef uint32 Atomic32; 33 static inline void MemoryBarrier() { } 34 static inline void AtomicIncrement(volatile Atomic32* ptr) { 35 *ptr = *ptr + 1; 36 } 37 } 38 #define SKIP_ATOMIC_CHECK 39 #endif 40 41 #include "talk/base/atomicops.h" 42 #include "talk/base/gunit.h" 43 #include "talk/base/helpers.h" 44 #include "talk/base/logging.h" 45 46 TEST(FixedSizeLockFreeQueueTest, TestDefaultConstruct) { 47 talk_base::FixedSizeLockFreeQueue<int> queue; 48 EXPECT_EQ(0u, queue.capacity()); 49 EXPECT_EQ(0u, queue.Size()); 50 EXPECT_FALSE(queue.PushBack(1)); 51 int val; 52 EXPECT_FALSE(queue.PopFront(&val)); 53 } 54 55 TEST(FixedSizeLockFreeQueueTest, TestConstruct) { 56 talk_base::FixedSizeLockFreeQueue<int> queue(5); 57 EXPECT_EQ(5u, queue.capacity()); 58 EXPECT_EQ(0u, queue.Size()); 59 int val; 60 EXPECT_FALSE(queue.PopFront(&val)); 61 } 62 63 TEST(FixedSizeLockFreeQueueTest, TestPushPop) { 64 talk_base::FixedSizeLockFreeQueue<int> queue(2); 65 EXPECT_EQ(2u, queue.capacity()); 66 EXPECT_EQ(0u, queue.Size()); 67 EXPECT_TRUE(queue.PushBack(1)); 68 EXPECT_EQ(1u, queue.Size()); 69 EXPECT_TRUE(queue.PushBack(2)); 70 EXPECT_EQ(2u, queue.Size()); 71 EXPECT_FALSE(queue.PushBack(3)); 72 EXPECT_EQ(2u, queue.Size()); 73 int val; 74 EXPECT_TRUE(queue.PopFront(&val)); 75 EXPECT_EQ(1, val); 76 EXPECT_EQ(1u, queue.Size()); 77 EXPECT_TRUE(queue.PopFront(&val)); 78 EXPECT_EQ(2, val); 79 EXPECT_EQ(0u, queue.Size()); 80 EXPECT_FALSE(queue.PopFront(&val)); 81 EXPECT_EQ(0u, queue.Size()); 82 } 83 84 TEST(FixedSizeLockFreeQueueTest, TestResize) { 85 talk_base::FixedSizeLockFreeQueue<int> queue(2); 86 EXPECT_EQ(2u, queue.capacity()); 87 EXPECT_EQ(0u, queue.Size()); 88 EXPECT_TRUE(queue.PushBack(1)); 89 EXPECT_EQ(1u, queue.Size()); 90 91 queue.ClearAndResizeUnsafe(5); 92 EXPECT_EQ(5u, queue.capacity()); 93 EXPECT_EQ(0u, queue.Size()); 94 int val; 95 EXPECT_FALSE(queue.PopFront(&val)); 96 } 97