[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium 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 "net/base/priority_queue.h" |
| 6 | #include "testing/gtest/include/gtest/gtest.h" |
| 7 | |
| 8 | namespace net { |
| 9 | |
| 10 | namespace { |
| 11 | |
| 12 | typedef PriorityQueue<int>::Priority Priority; |
| 13 | const Priority kPriorities[] = { 2, 1, 2, 0, 4, 3, 1, 4, 0 }; |
| 14 | const Priority kNumPriorities = 5; // max(kPriorities) + 1 |
| 15 | const size_t kNumElements = arraysize(kPriorities); |
| 16 | const int kFirstMinOrder[kNumElements] = { 3, 8, 1, 6, 0, 2, 5, 4, 7 }; |
| 17 | const int kLastMaxOrder[kNumElements] = { 7, 4, 5, 2, 0, 6, 1, 8, 3 }; |
| 18 | const int kFirstMaxOrder[kNumElements] = { 4, 7, 5, 0, 2, 1, 6, 3, 8 }; |
| 19 | const int kLastMinOrder[kNumElements] = { 8, 3, 6, 1, 2, 0, 5, 7, 4 }; |
| 20 | |
[email protected] | 043324f4 | 2012-06-02 00:18:53 | [diff] [blame] | 21 | class PriorityQueueTest : public testing::Test { |
| 22 | protected: |
| 23 | PriorityQueueTest() : queue_(kNumPriorities) {} |
| 24 | |
| 25 | virtual void SetUp() OVERRIDE { |
| 26 | CheckEmpty(); |
| 27 | for (size_t i = 0; i < kNumElements; ++i) { |
| 28 | EXPECT_EQ(i, queue_.size()); |
| 29 | pointers_[i] = queue_.Insert(static_cast<int>(i), kPriorities[i]); |
| 30 | } |
| 31 | EXPECT_EQ(kNumElements, queue_.size()); |
| 32 | } |
| 33 | |
| 34 | void CheckEmpty() { |
| 35 | EXPECT_EQ(0u, queue_.size()); |
| 36 | EXPECT_TRUE(queue_.FirstMin().is_null()); |
| 37 | EXPECT_TRUE(queue_.LastMin().is_null()); |
| 38 | EXPECT_TRUE(queue_.FirstMax().is_null()); |
| 39 | EXPECT_TRUE(queue_.LastMax().is_null()); |
| 40 | } |
| 41 | |
| 42 | PriorityQueue<int> queue_; |
| 43 | PriorityQueue<int>::Pointer pointers_[kNumElements]; |
| 44 | }; |
| 45 | |
| 46 | TEST_F(PriorityQueueTest, AddAndClear) { |
| 47 | for (size_t i = 0; i < kNumElements; ++i) { |
| 48 | EXPECT_EQ(kPriorities[i], pointers_[i].priority()); |
| 49 | EXPECT_EQ(static_cast<int>(i), pointers_[i].value()); |
| 50 | } |
| 51 | queue_.Clear(); |
| 52 | CheckEmpty(); |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 53 | } |
| 54 | |
[email protected] | 043324f4 | 2012-06-02 00:18:53 | [diff] [blame] | 55 | TEST_F(PriorityQueueTest, FirstMinOrder) { |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 56 | for (size_t i = 0; i < kNumElements; ++i) { |
[email protected] | 043324f4 | 2012-06-02 00:18:53 | [diff] [blame] | 57 | EXPECT_EQ(kNumElements - i, queue_.size()); |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 58 | // Also check Equals. |
[email protected] | 043324f4 | 2012-06-02 00:18:53 | [diff] [blame] | 59 | EXPECT_TRUE(queue_.FirstMin().Equals(pointers_[kFirstMinOrder[i]])); |
| 60 | EXPECT_EQ(kFirstMinOrder[i], queue_.FirstMin().value()); |
| 61 | queue_.Erase(queue_.FirstMin()); |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 62 | } |
[email protected] | 043324f4 | 2012-06-02 00:18:53 | [diff] [blame] | 63 | CheckEmpty(); |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 64 | } |
| 65 | |
[email protected] | 043324f4 | 2012-06-02 00:18:53 | [diff] [blame] | 66 | TEST_F(PriorityQueueTest, LastMinOrder) { |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 67 | for (size_t i = 0; i < kNumElements; ++i) { |
[email protected] | 043324f4 | 2012-06-02 00:18:53 | [diff] [blame] | 68 | EXPECT_EQ(kLastMinOrder[i], queue_.LastMin().value()); |
| 69 | queue_.Erase(queue_.LastMin()); |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 70 | } |
[email protected] | 043324f4 | 2012-06-02 00:18:53 | [diff] [blame] | 71 | CheckEmpty(); |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 72 | } |
| 73 | |
[email protected] | 043324f4 | 2012-06-02 00:18:53 | [diff] [blame] | 74 | TEST_F(PriorityQueueTest, FirstMaxOrder) { |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 75 | for (size_t i = 0; i < kNumElements; ++i) { |
[email protected] | 043324f4 | 2012-06-02 00:18:53 | [diff] [blame] | 76 | EXPECT_EQ(kFirstMaxOrder[i], queue_.FirstMax().value()); |
| 77 | queue_.Erase(queue_.FirstMax()); |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 78 | } |
[email protected] | 043324f4 | 2012-06-02 00:18:53 | [diff] [blame] | 79 | CheckEmpty(); |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 80 | } |
| 81 | |
[email protected] | 043324f4 | 2012-06-02 00:18:53 | [diff] [blame] | 82 | TEST_F(PriorityQueueTest, LastMaxOrder) { |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 83 | for (size_t i = 0; i < kNumElements; ++i) { |
[email protected] | 043324f4 | 2012-06-02 00:18:53 | [diff] [blame] | 84 | EXPECT_EQ(kLastMaxOrder[i], queue_.LastMax().value()); |
| 85 | queue_.Erase(queue_.LastMax()); |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 86 | } |
[email protected] | 043324f4 | 2012-06-02 00:18:53 | [diff] [blame] | 87 | CheckEmpty(); |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 88 | } |
| 89 | |
[email protected] | 043324f4 | 2012-06-02 00:18:53 | [diff] [blame] | 90 | TEST_F(PriorityQueueTest, EraseFromMiddle) { |
| 91 | queue_.Erase(pointers_[2]); |
| 92 | queue_.Erase(pointers_[3]); |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 93 | |
| 94 | int expected_order[] = { 8, 1, 6, 0, 5, 4, 7 }; |
| 95 | |
| 96 | for (size_t i = 0; i < arraysize(expected_order); ++i) { |
[email protected] | 043324f4 | 2012-06-02 00:18:53 | [diff] [blame] | 97 | EXPECT_EQ(expected_order[i], queue_.FirstMin().value()); |
| 98 | queue_.Erase(queue_.FirstMin()); |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 99 | } |
[email protected] | 043324f4 | 2012-06-02 00:18:53 | [diff] [blame] | 100 | CheckEmpty(); |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | } // namespace |
| 104 | |
| 105 | } // namespace net |