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

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

Issue 9924023: Readability review (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add copy constructor and assignment operator to Pointer. Created 8 years, 7 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
« net/base/priority_queue.h ('K') | « net/base/priority_queue.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/base/priority_queue.h" 5 #include "net/base/priority_queue.h"
6 #include "testing/gtest/include/gtest/gtest.h" 6 #include "testing/gtest/include/gtest/gtest.h"
7 7
8 namespace net { 8 namespace net {
9 9
10 namespace { 10 namespace {
11 11
12 typedef PriorityQueue<int>::Priority Priority; 12 typedef PriorityQueue<int>::Priority Priority;
13 const Priority kPriorities[] = { 2, 1, 2, 0, 4, 3, 1, 4, 0 }; 13 const Priority kPriorities[] = { 2, 1, 2, 0, 4, 3, 1, 4, 0 };
14 const Priority kNumPriorities = 5; // max(kPriorities) + 1 14 const Priority kNumPriorities = 5; // max(kPriorities) + 1
15 const size_t kNumElements = arraysize(kPriorities); 15 const size_t kNumElements = arraysize(kPriorities);
16 const int kFirstMinOrder[kNumElements] = { 3, 8, 1, 6, 0, 2, 5, 4, 7 }; 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 }; 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 }; 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 }; 19 const int kLastMinOrder[kNumElements] = { 8, 3, 6, 1, 2, 0, 5, 7, 4 };
20 20
21 void CheckEmpty(PriorityQueue<int>* queue) { 21 class PriorityQueueTest : public testing::Test {
22 EXPECT_EQ(0u, queue->size()); 22 protected:
23 EXPECT_TRUE(queue->FirstMin().is_null()); 23 PriorityQueueTest() : queue_(kNumPriorities) {}
24 EXPECT_TRUE(queue->LastMin().is_null()); 24
25 EXPECT_TRUE(queue->FirstMax().is_null()); 25 virtual void SetUp() OVERRIDE {
26 EXPECT_TRUE(queue->LastMax().is_null()); 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();
27 } 53 }
28 54
29 TEST(PriorityQueueTest, AddAndClear) { 55 TEST_F(PriorityQueueTest, FirstMinOrder) {
30 PriorityQueue<int> queue(kNumPriorities);
31 PriorityQueue<int>::Pointer pointers[kNumElements];
32
33 CheckEmpty(&queue);
34 for (size_t i = 0; i < kNumElements; ++i) { 56 for (size_t i = 0; i < kNumElements; ++i) {
35 EXPECT_EQ(i, queue.size()); 57 EXPECT_EQ(kNumElements - i, queue_.size());
36 pointers[i] = queue.Insert(static_cast<int>(i), kPriorities[i]); 58 // Also check Equals.
59 EXPECT_TRUE(queue_.FirstMin().Equals(pointers_[kFirstMinOrder[i]]));
60 EXPECT_EQ(kFirstMinOrder[i], queue_.FirstMin().value());
61 queue_.Erase(queue_.FirstMin());
37 } 62 }
38 EXPECT_EQ(kNumElements, queue.size()); 63 CheckEmpty();
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 } 64 }
48 65
49 TEST(PriorityQueueTest, FirstMinOrder) { 66 TEST_F(PriorityQueueTest, LastMinOrder) {
50 PriorityQueue<int> queue(kNumPriorities);
51 PriorityQueue<int>::Pointer pointers[kNumElements];
52
53 for (size_t i = 0; i < kNumElements; ++i) { 67 for (size_t i = 0; i < kNumElements; ++i) {
54 pointers[i] = queue.Insert(static_cast<int>(i), kPriorities[i]); 68 EXPECT_EQ(kLastMinOrder[i], queue_.LastMin().value());
69 queue_.Erase(queue_.LastMin());
55 } 70 }
56 71 CheckEmpty();
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 } 72 }
66 73
67 TEST(PriorityQueueTest, LastMinOrder) { 74 TEST_F(PriorityQueueTest, FirstMaxOrder) {
68 PriorityQueue<int> queue(kNumPriorities);
69
70 for (size_t i = 0; i < kNumElements; ++i) { 75 for (size_t i = 0; i < kNumElements; ++i) {
71 queue.Insert(static_cast<int>(i), kPriorities[i]); 76 EXPECT_EQ(kFirstMaxOrder[i], queue_.FirstMax().value());
77 queue_.Erase(queue_.FirstMax());
72 } 78 }
73 79 CheckEmpty();
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 }
80 81
81 TEST(PriorityQueueTest, FirstMaxOrder) { 82 TEST_F(PriorityQueueTest, LastMaxOrder) {
82 PriorityQueue<int> queue(kNumPriorities);
83
84 for (size_t i = 0; i < kNumElements; ++i) { 83 for (size_t i = 0; i < kNumElements; ++i) {
85 queue.Insert(static_cast<int>(i), kPriorities[i]); 84 EXPECT_EQ(kLastMaxOrder[i], queue_.LastMax().value());
85 queue_.Erase(queue_.LastMax());
86 } 86 }
87 87 CheckEmpty();
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 } 88 }
94 89
95 TEST(PriorityQueueTest, LastMaxOrder) { 90 TEST_F(PriorityQueueTest, EraseFromMiddle) {
96 PriorityQueue<int> queue(kNumPriorities); 91 queue_.Erase(pointers_[2]);
97 92 queue_.Erase(pointers_[3]);
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 93
120 int expected_order[] = { 8, 1, 6, 0, 5, 4, 7 }; 94 int expected_order[] = { 8, 1, 6, 0, 5, 4, 7 };
121 95
122 for (size_t i = 0; i < arraysize(expected_order); ++i) { 96 for (size_t i = 0; i < arraysize(expected_order); ++i) {
123 EXPECT_EQ(expected_order[i], queue.FirstMin().value()); 97 EXPECT_EQ(expected_order[i], queue_.FirstMin().value());
124 queue.Erase(queue.FirstMin()); 98 queue_.Erase(queue_.FirstMin());
125 } 99 }
126 CheckEmpty(&queue); 100 CheckEmpty();
127 } 101 }
128 102
129 } // namespace 103 } // namespace
130 104
131 } // namespace net 105 } // namespace net
132 106
107
OLDNEW
« net/base/priority_queue.h ('K') | « net/base/priority_queue.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698