Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1 | // Copyright 2015 the V8 project authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "src/locked-queue-inl.h" |
| 6 | #include "testing/gtest/include/gtest/gtest.h" |
| 7 | |
| 8 | namespace { |
| 9 | |
| 10 | typedef int Record; |
| 11 | |
| 12 | } // namespace |
| 13 | |
| 14 | namespace v8 { |
| 15 | namespace internal { |
| 16 | |
| 17 | TEST(LockedQueue, ConstructorEmpty) { |
| 18 | LockedQueue<Record> queue; |
| 19 | EXPECT_TRUE(queue.IsEmpty()); |
| 20 | } |
| 21 | |
| 22 | |
| 23 | TEST(LockedQueue, SingleRecordEnqueueDequeue) { |
| 24 | LockedQueue<Record> queue; |
| 25 | EXPECT_TRUE(queue.IsEmpty()); |
| 26 | queue.Enqueue(1); |
| 27 | EXPECT_FALSE(queue.IsEmpty()); |
| 28 | Record a = -1; |
| 29 | bool success = queue.Dequeue(&a); |
| 30 | EXPECT_TRUE(success); |
| 31 | EXPECT_EQ(a, 1); |
| 32 | EXPECT_TRUE(queue.IsEmpty()); |
| 33 | } |
| 34 | |
| 35 | |
| 36 | TEST(LockedQueue, Peek) { |
| 37 | LockedQueue<Record> queue; |
| 38 | EXPECT_TRUE(queue.IsEmpty()); |
| 39 | queue.Enqueue(1); |
| 40 | EXPECT_FALSE(queue.IsEmpty()); |
| 41 | Record a = -1; |
| 42 | bool success = queue.Peek(&a); |
| 43 | EXPECT_TRUE(success); |
| 44 | EXPECT_EQ(a, 1); |
| 45 | EXPECT_FALSE(queue.IsEmpty()); |
| 46 | success = queue.Dequeue(&a); |
| 47 | EXPECT_TRUE(success); |
| 48 | EXPECT_EQ(a, 1); |
| 49 | EXPECT_TRUE(queue.IsEmpty()); |
| 50 | } |
| 51 | |
| 52 | |
| 53 | TEST(LockedQueue, PeekOnEmpty) { |
| 54 | LockedQueue<Record> queue; |
| 55 | EXPECT_TRUE(queue.IsEmpty()); |
| 56 | Record a = -1; |
| 57 | bool success = queue.Peek(&a); |
| 58 | EXPECT_FALSE(success); |
| 59 | } |
| 60 | |
| 61 | |
| 62 | TEST(LockedQueue, MultipleRecords) { |
| 63 | LockedQueue<Record> queue; |
| 64 | EXPECT_TRUE(queue.IsEmpty()); |
| 65 | queue.Enqueue(1); |
| 66 | EXPECT_FALSE(queue.IsEmpty()); |
| 67 | for (int i = 2; i <= 5; ++i) { |
| 68 | queue.Enqueue(i); |
| 69 | EXPECT_FALSE(queue.IsEmpty()); |
| 70 | } |
| 71 | Record rec = 0; |
| 72 | for (int i = 1; i <= 4; ++i) { |
| 73 | EXPECT_FALSE(queue.IsEmpty()); |
| 74 | queue.Dequeue(&rec); |
| 75 | EXPECT_EQ(i, rec); |
| 76 | } |
| 77 | for (int i = 6; i <= 12; ++i) { |
| 78 | queue.Enqueue(i); |
| 79 | EXPECT_FALSE(queue.IsEmpty()); |
| 80 | } |
| 81 | for (int i = 5; i <= 12; ++i) { |
| 82 | EXPECT_FALSE(queue.IsEmpty()); |
| 83 | queue.Dequeue(&rec); |
| 84 | EXPECT_EQ(i, rec); |
| 85 | } |
| 86 | EXPECT_TRUE(queue.IsEmpty()); |
| 87 | } |
| 88 | |
| 89 | } // namespace internal |
| 90 | } // namespace v8 |