Index: net/base/priority_queue_unittest.cc |
diff --git a/net/base/priority_queue_unittest.cc b/net/base/priority_queue_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..1439a990e43e2e487fba99d0feaef41953912eee |
--- /dev/null |
+++ b/net/base/priority_queue_unittest.cc |
@@ -0,0 +1,127 @@ |
+// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "net/base/priority_queue.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace net { |
+ |
+namespace { |
+ |
+TEST(PriorityQueueTest, OrderEraseAndClear) { |
+ PriorityQueue<size_t> queue; |
+ RequestPriority priorities[] = { |
+ LOW, |
+ MEDIUM, |
+ LOW, |
+ HIGHEST, |
+ IDLE, |
+ LOWEST, |
+ MEDIUM, |
+ IDLE, |
+ HIGHEST, |
+ }; |
+ const size_t kNumPriorities = arraysize(priorities); |
+ size_t expected_order[kNumPriorities] = { 3, 8, 1, 6, 0, 2, 5, 4, 7 }; |
+ size_t oldest_lowest[kNumPriorities] = { 4, 7, 5, 0, 2, 1, 6, 3, 8 }; |
+ PriorityQueue<size_t>::Pointer pointers[kNumPriorities]; |
+ |
+ // Check queue is empty. |
+ EXPECT_EQ(0u, queue.size()); |
+ EXPECT_TRUE(queue.First().is_null()); |
+ EXPECT_TRUE(queue.Last().is_null()); |
mmenke
2011/12/21 16:22:58
Should check OldestLowest(), too, and make sure it
szym
2011/12/28 01:24:10
Done.
|
+ |
+ for (size_t i = 0; i < kNumPriorities; ++i) { |
+ EXPECT_EQ(i, queue.size()); |
+ pointers[i] = queue.Insert(i, priorities[i]); |
+ } |
+ EXPECT_EQ(kNumPriorities, queue.size()); |
+ |
+ for (size_t i = 0; i < kNumPriorities; ++i) { |
+ EXPECT_EQ(priorities[i], pointers[i].priority()); |
+ EXPECT_EQ(i, pointers[i].value()); |
+ } |
+ |
+ // Test forward direction. |
+ for (size_t i = 0; i < kNumPriorities; ++i) { |
+ EXPECT_EQ(expected_order[i], queue.First().value()); |
+ EXPECT_EQ(kNumPriorities - i, queue.size()); |
+ queue.Erase(queue.First()); |
+ } |
+ EXPECT_EQ(0u, queue.size()); |
+ EXPECT_TRUE(queue.First().is_null()); |
+ EXPECT_TRUE(queue.Last().is_null()); |
+ |
+ for (size_t i = 0; i < kNumPriorities; ++i) { |
mmenke
2011/12/21 16:22:58
Just to make the test a little simpler and self-co
szym
2011/12/28 01:24:10
Done.
|
+ pointers[i] = queue.Insert(i, priorities[i]); |
mmenke
2011/12/21 16:22:58
You don't use pointers[i] in after populating them
szym
2011/12/28 01:24:10
Done.
|
+ } |
+ |
+ // Test backward direction. |
+ for (size_t i = 0; i < kNumPriorities; ++i) { |
+ EXPECT_EQ(expected_order[kNumPriorities - i - 1], queue.Last().value()); |
+ queue.Erase(queue.Last()); |
+ } |
+ EXPECT_EQ(0u, queue.size()); |
+ |
+ for (size_t i = 0; i < kNumPriorities; ++i) { |
+ pointers[i] = queue.Insert(i, priorities[i]); |
+ } |
+ |
+ // Test OldestLowest. |
+ for (size_t i = 0; i < kNumPriorities; ++i) { |
+ EXPECT_EQ(oldest_lowest[i], queue.OldestLowest().value()); |
+ queue.Erase(queue.OldestLowest()); |
+ } |
+ EXPECT_EQ(0u, queue.size()); |
+ |
+ // Add them once more to test Clear. |
+ for (size_t i = 0; i < kNumPriorities; ++i) { |
+ pointers[i] = queue.Insert(i, priorities[i]); |
+ } |
+ EXPECT_EQ(kNumPriorities, queue.size()); |
+ |
+ queue.Clear(); |
+ |
+ EXPECT_EQ(0u, queue.size()); |
+} |
+ |
+TEST(PriorityQueueTest, UpdatePriority) { |
+ PriorityQueue<size_t> queue; |
+ RequestPriority priorities[] = { |
+ LOW, |
+ MEDIUM, |
+ LOW, |
+ HIGHEST, |
+ IDLE, |
+ LOWEST, |
+ MEDIUM, |
+ IDLE, |
+ HIGHEST, |
+ }; |
+ const size_t kNumPriorities = arraysize(priorities); |
+ PriorityQueue<size_t>::Pointer pointers[kNumPriorities]; |
+ |
+ for (size_t i = 0; i < kNumPriorities; ++i) { |
+ pointers[i] = queue.Insert(i, priorities[i]); |
+ } |
+ |
+ // Current order: 3, 8, 1, 6, 0, 2, 5, 4, 7. |
+ pointers[2] = queue.Move(pointers[2], HIGHEST); |
+ EXPECT_EQ(HIGHEST, pointers[2].priority()); |
+ EXPECT_EQ(2u, pointers[2].value()); |
+ queue.Move(pointers[3], IDLE); |
+ queue.Move(pointers[1], MEDIUM); // No change. |
+ size_t expected_order[kNumPriorities] = { 8, 2, 1, 6, 0, 5, 4, 7, 3 }; |
+ |
+ for (size_t i = 0; i < kNumPriorities; ++i) { |
+ EXPECT_EQ(expected_order[i], queue.First().value()); |
+ queue.Erase(queue.First()); |
+ } |
+ EXPECT_EQ(0u, queue.size()); |
+} |
+ |
+} // namespace |
+ |
+} // namespace net |
+ |