OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 TEST(PriorityQueueTest, OrderEraseAndClear) { |
| 13 PriorityQueue<size_t> queue; |
| 14 RequestPriority priorities[] = { |
| 15 LOW, |
| 16 MEDIUM, |
| 17 LOW, |
| 18 HIGHEST, |
| 19 IDLE, |
| 20 LOWEST, |
| 21 MEDIUM, |
| 22 IDLE, |
| 23 HIGHEST, |
| 24 }; |
| 25 const size_t kNumPriorities = arraysize(priorities); |
| 26 size_t expected_order[kNumPriorities] = { 3, 8, 1, 6, 0, 2, 5, 4, 7 }; |
| 27 size_t oldest_lowest[kNumPriorities] = { 4, 7, 5, 0, 2, 1, 6, 3, 8 }; |
| 28 PriorityQueue<size_t>::Pointer pointers[kNumPriorities]; |
| 29 |
| 30 // Check queue is empty. |
| 31 EXPECT_EQ(0u, queue.size()); |
| 32 EXPECT_TRUE(queue.First().is_null()); |
| 33 EXPECT_TRUE(queue.Last().is_null()); |
| 34 |
| 35 for (size_t i = 0; i < kNumPriorities; ++i) { |
| 36 EXPECT_EQ(i, queue.size()); |
| 37 pointers[i] = queue.Insert(i, priorities[i]); |
| 38 } |
| 39 EXPECT_EQ(kNumPriorities, queue.size()); |
| 40 |
| 41 for (size_t i = 0; i < kNumPriorities; ++i) { |
| 42 EXPECT_EQ(priorities[i], pointers[i].priority()); |
| 43 EXPECT_EQ(i, pointers[i].value()); |
| 44 } |
| 45 |
| 46 // Test forward direction. |
| 47 for (size_t i = 0; i < kNumPriorities; ++i) { |
| 48 EXPECT_EQ(expected_order[i], queue.First().value()); |
| 49 EXPECT_EQ(kNumPriorities - i, queue.size()); |
| 50 queue.Erase(queue.First()); |
| 51 } |
| 52 EXPECT_EQ(0u, queue.size()); |
| 53 EXPECT_TRUE(queue.First().is_null()); |
| 54 EXPECT_TRUE(queue.Last().is_null()); |
| 55 |
| 56 for (size_t i = 0; i < kNumPriorities; ++i) { |
| 57 pointers[i] = queue.Insert(i, priorities[i]); |
| 58 } |
| 59 |
| 60 // Test backward direction. |
| 61 for (size_t i = 0; i < kNumPriorities; ++i) { |
| 62 EXPECT_EQ(expected_order[kNumPriorities - i - 1], queue.Last().value()); |
| 63 queue.Erase(queue.Last()); |
| 64 } |
| 65 EXPECT_EQ(0u, queue.size()); |
| 66 |
| 67 for (size_t i = 0; i < kNumPriorities; ++i) { |
| 68 pointers[i] = queue.Insert(i, priorities[i]); |
| 69 } |
| 70 |
| 71 // Test OldestLowest. |
| 72 for (size_t i = 0; i < kNumPriorities; ++i) { |
| 73 EXPECT_EQ(oldest_lowest[i], queue.OldestLowest().value()); |
| 74 queue.Erase(queue.OldestLowest()); |
| 75 } |
| 76 EXPECT_EQ(0u, queue.size()); |
| 77 |
| 78 // Add them once more to test Clear. |
| 79 for (size_t i = 0; i < kNumPriorities; ++i) { |
| 80 pointers[i] = queue.Insert(i, priorities[i]); |
| 81 } |
| 82 EXPECT_EQ(kNumPriorities, queue.size()); |
| 83 |
| 84 queue.Clear(); |
| 85 |
| 86 EXPECT_EQ(0u, queue.size()); |
| 87 } |
| 88 |
| 89 TEST(PriorityQueueTest, UpdatePriority) { |
| 90 PriorityQueue<size_t> queue; |
| 91 RequestPriority priorities[] = { |
| 92 LOW, |
| 93 MEDIUM, |
| 94 LOW, |
| 95 HIGHEST, |
| 96 IDLE, |
| 97 LOWEST, |
| 98 MEDIUM, |
| 99 IDLE, |
| 100 HIGHEST, |
| 101 }; |
| 102 const size_t kNumPriorities = arraysize(priorities); |
| 103 PriorityQueue<size_t>::Pointer pointers[kNumPriorities]; |
| 104 |
| 105 for (size_t i = 0; i < kNumPriorities; ++i) { |
| 106 pointers[i] = queue.Insert(i, priorities[i]); |
| 107 } |
| 108 |
| 109 // Current order: 3, 8, 1, 6, 0, 2, 5, 4, 7. |
| 110 pointers[2] = queue.Move(pointers[2], HIGHEST); |
| 111 EXPECT_EQ(HIGHEST, pointers[2].priority()); |
| 112 EXPECT_EQ(2u, pointers[2].value()); |
| 113 queue.Move(pointers[3], IDLE); |
| 114 queue.Move(pointers[1], MEDIUM); // No change. |
| 115 size_t expected_order[kNumPriorities] = { 8, 2, 1, 6, 0, 5, 4, 7, 3 }; |
| 116 |
| 117 for (size_t i = 0; i < kNumPriorities; ++i) { |
| 118 EXPECT_EQ(expected_order[i], queue.First().value()); |
| 119 queue.Erase(queue.First()); |
| 120 } |
| 121 EXPECT_EQ(0u, queue.size()); |
| 122 } |
| 123 |
| 124 } // namespace |
| 125 |
| 126 } // namespace net |
| 127 |
OLD | NEW |