Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(54)

Side by Side Diff: net/base/priority_queue_unittest.cc

Issue 9113022: Adds PriorityQueue and PrioritizedDispatcher. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Removed changes to net/dns/ that don't belong to this CL. Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « net/base/priority_queue.h ('k') | net/net.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
21 void CheckEmpty(PriorityQueue<int>* queue) {
22 EXPECT_EQ(0u, queue->size());
23 EXPECT_TRUE(queue->FirstMin().is_null());
24 EXPECT_TRUE(queue->LastMin().is_null());
25 EXPECT_TRUE(queue->FirstMax().is_null());
26 EXPECT_TRUE(queue->LastMax().is_null());
27 }
28
29 TEST(PriorityQueueTest, AddAndClear) {
30 PriorityQueue<int> queue(kNumPriorities);
31 PriorityQueue<int>::Pointer pointers[kNumElements];
32
33 CheckEmpty(&queue);
34 for (size_t i = 0; i < kNumElements; ++i) {
35 EXPECT_EQ(i, queue.size());
36 pointers[i] = queue.Insert(static_cast<int>(i), kPriorities[i]);
37 }
38 EXPECT_EQ(kNumElements, queue.size());
39
40 for (size_t i = 0; i < kNumElements; ++i) {
41 EXPECT_EQ(kPriorities[i], pointers[i].priority());
42 EXPECT_EQ(static_cast<int>(i), pointers[i].value());
43 }
44
45 queue.Clear();
46 CheckEmpty(&queue);
47 }
48
49 TEST(PriorityQueueTest, FirstMinOrder) {
50 PriorityQueue<int> queue(kNumPriorities);
51 PriorityQueue<int>::Pointer pointers[kNumElements];
52
53 for (size_t i = 0; i < kNumElements; ++i) {
54 pointers[i] = queue.Insert(static_cast<int>(i), kPriorities[i]);
55 }
56
57 for (size_t i = 0; i < kNumElements; ++i) {
58 EXPECT_EQ(kNumElements - i, queue.size());
59 // Also check Equals.
60 EXPECT_TRUE(queue.FirstMin().Equals(pointers[kFirstMinOrder[i]]));
61 EXPECT_EQ(kFirstMinOrder[i], queue.FirstMin().value());
62 queue.Erase(queue.FirstMin());
63 }
64 CheckEmpty(&queue);
65 }
66
67 TEST(PriorityQueueTest, LastMinOrder) {
68 PriorityQueue<int> queue(kNumPriorities);
69
70 for (size_t i = 0; i < kNumElements; ++i) {
71 queue.Insert(static_cast<int>(i), kPriorities[i]);
72 }
73
74 for (size_t i = 0; i < kNumElements; ++i) {
75 EXPECT_EQ(kLastMinOrder[i], queue.LastMin().value());
76 queue.Erase(queue.LastMin());
77 }
78 CheckEmpty(&queue);
79 }
80
81 TEST(PriorityQueueTest, FirstMaxOrder) {
82 PriorityQueue<int> queue(kNumPriorities);
83
84 for (size_t i = 0; i < kNumElements; ++i) {
85 queue.Insert(static_cast<int>(i), kPriorities[i]);
86 }
87
88 for (size_t i = 0; i < kNumElements; ++i) {
89 EXPECT_EQ(kFirstMaxOrder[i], queue.FirstMax().value());
90 queue.Erase(queue.FirstMax());
91 }
92 CheckEmpty(&queue);
93 }
94
95 TEST(PriorityQueueTest, LastMaxOrder) {
96 PriorityQueue<int> queue(kNumPriorities);
97
98 for (size_t i = 0; i < kNumElements; ++i) {
99 queue.Insert(static_cast<int>(i), kPriorities[i]);
100 }
101
102 for (size_t i = 0; i < kNumElements; ++i) {
103 EXPECT_EQ(kLastMaxOrder[i], queue.LastMax().value());
104 queue.Erase(queue.LastMax());
105 }
106 CheckEmpty(&queue);
107 }
108
109 TEST(PriorityQueueTest, EraseFromMiddle) {
110 PriorityQueue<int> queue(kNumPriorities);
111 PriorityQueue<int>::Pointer pointers[kNumElements];
112
113 for (size_t i = 0; i < kNumElements; ++i) {
114 pointers[i] = queue.Insert(static_cast<int>(i), kPriorities[i]);
115 }
116
117 queue.Erase(pointers[2]);
118 queue.Erase(pointers[3]);
119
120 int expected_order[] = { 8, 1, 6, 0, 5, 4, 7 };
121
122 for (size_t i = 0; i < arraysize(expected_order); ++i) {
123 EXPECT_EQ(expected_order[i], queue.FirstMin().value());
124 queue.Erase(queue.FirstMin());
125 }
126 CheckEmpty(&queue);
127 }
128
129 } // namespace
130
131 } // namespace net
132
OLDNEW
« no previous file with comments | « net/base/priority_queue.h ('k') | net/net.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698