147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org/*
247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *  Copyright 2011 The WebRTC Project Authors. All rights reserved.
347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *
447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *  Use of this source code is governed by a BSD-style license
547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *  that can be found in the LICENSE file in the root of the source
647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *  tree. An additional intellectual property rights grant can be found
747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *  in the file PATENTS.  All contributing project authors may
847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *  be found in the AUTHORS file in the root of the source tree.
947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org */
1047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
1147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#if !defined(__arm__)
1247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// For testing purposes, define faked versions of the atomic operations
1347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include "webrtc/base/basictypes.h"
1447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgnamespace rtc {
1547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgtypedef uint32 Atomic32;
1647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgstatic inline void MemoryBarrier() { }
1747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgstatic inline void AtomicIncrement(volatile Atomic32* ptr) {
1847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  *ptr = *ptr + 1;
1947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
2047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
2147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#define SKIP_ATOMIC_CHECK
2247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#endif
2347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
2447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include "webrtc/base/atomicops.h"
2547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include "webrtc/base/gunit.h"
2647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include "webrtc/base/helpers.h"
2747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include "webrtc/base/logging.h"
2847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
2947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgTEST(FixedSizeLockFreeQueueTest, TestDefaultConstruct) {
3047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  rtc::FixedSizeLockFreeQueue<int> queue;
3147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_EQ(0u, queue.capacity());
3247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_EQ(0u, queue.Size());
3347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_FALSE(queue.PushBack(1));
3447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  int val;
3547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_FALSE(queue.PopFront(&val));
3647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
3747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
3847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgTEST(FixedSizeLockFreeQueueTest, TestConstruct) {
3947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  rtc::FixedSizeLockFreeQueue<int> queue(5);
4047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_EQ(5u, queue.capacity());
4147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_EQ(0u, queue.Size());
4247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  int val;
4347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_FALSE(queue.PopFront(&val));
4447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
4547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
4647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgTEST(FixedSizeLockFreeQueueTest, TestPushPop) {
4747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  rtc::FixedSizeLockFreeQueue<int> queue(2);
4847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_EQ(2u, queue.capacity());
4947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_EQ(0u, queue.Size());
5047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(queue.PushBack(1));
5147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_EQ(1u, queue.Size());
5247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(queue.PushBack(2));
5347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_EQ(2u, queue.Size());
5447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_FALSE(queue.PushBack(3));
5547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_EQ(2u, queue.Size());
5647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  int val;
5747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(queue.PopFront(&val));
5847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_EQ(1, val);
5947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_EQ(1u, queue.Size());
6047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(queue.PopFront(&val));
6147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_EQ(2, val);
6247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_EQ(0u, queue.Size());
6347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_FALSE(queue.PopFront(&val));
6447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_EQ(0u, queue.Size());
6547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
6647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
6747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgTEST(FixedSizeLockFreeQueueTest, TestResize) {
6847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  rtc::FixedSizeLockFreeQueue<int> queue(2);
6947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_EQ(2u, queue.capacity());
7047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_EQ(0u, queue.Size());
7147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_TRUE(queue.PushBack(1));
7247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_EQ(1u, queue.Size());
7347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
7447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  queue.ClearAndResizeUnsafe(5);
7547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_EQ(5u, queue.capacity());
7647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_EQ(0u, queue.Size());
7747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  int val;
7847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  EXPECT_FALSE(queue.PopFront(&val));
7947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
80