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

Side by Side Diff: third_party/WebKit/Source/platform/scheduler/base/intrusive_heap_unittest.cc

Issue 2419793002: [Reland] Optimize blink scheduler with an intrusive heap (Closed)
Patch Set: Fix bug in MoveHoleDown Created 4 years, 2 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
OLDNEW
(Empty)
1 // Copyright 2016 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 "platform/scheduler/base/intrusive_heap.h"
6 #include "testing/gtest/include/gtest/gtest.h"
7
8 namespace blink {
9 namespace scheduler {
10 namespace {
11
12 struct TestElement {
13 int key;
14 size_t* index;
15
16 bool operator<=(const TestElement& other) const { return key <= other.key; }
17
18 void SetHeapIndex(size_t i) {
19 if (index)
20 *index = i;
21 }
22 };
23
24 } // namespace
25
26 using IntrusiveHeapTest = ::testing::Test;
27
28 TEST(IntrusiveHeapTest, Basic) {
29 IntrusiveHeap<TestElement> heap;
30
31 EXPECT_TRUE(heap.empty());
32 EXPECT_EQ(0u, heap.size());
33 }
34
35 TEST(IntrusiveHeapTest, Min) {
36 IntrusiveHeap<TestElement> heap;
37
38 heap.insert({9, nullptr});
39 heap.insert({10, nullptr});
40 heap.insert({8, nullptr});
41 heap.insert({2, nullptr});
42 heap.insert({7, nullptr});
43 heap.insert({15, nullptr});
44 heap.insert({22, nullptr});
45 heap.insert({3, nullptr});
46
47 EXPECT_FALSE(heap.empty());
48 EXPECT_EQ(8u, heap.size());
49 EXPECT_EQ(2, heap.min().key);
50 }
51
52 TEST(IntrusiveHeapTest, InsertAscending) {
53 IntrusiveHeap<TestElement> heap;
54
55 for (int i = 0; i < 50; i++)
56 heap.insert({i, nullptr});
57
58 EXPECT_EQ(0, heap.min().key);
59 EXPECT_EQ(50u, heap.size());
60 }
61
62 TEST(IntrusiveHeapTest, InsertDescending) {
63 IntrusiveHeap<TestElement> heap;
64
65 for (int i = 0; i < 50; i++)
66 heap.insert({50 - i, nullptr});
67
68 EXPECT_EQ(1, heap.min().key);
69 EXPECT_EQ(50u, heap.size());
70 }
71
72 TEST(IntrusiveHeapTest, HeapIndex) {
73 IntrusiveHeap<TestElement> heap;
74 size_t index5 = 0;
75 size_t index4 = 0;
76 size_t index3 = 0;
77 size_t index2 = 0;
78 size_t index1 = 0;
79
80 heap.insert({15, &index5});
81 heap.insert({14, &index4});
82 heap.insert({13, &index3});
83 heap.insert({12, &index2});
84 heap.insert({11, &index1});
85
86 EXPECT_FALSE(heap.empty());
87 EXPECT_EQ(5u, heap.size());
88 EXPECT_EQ(1u, index1);
89 EXPECT_EQ(2u, index2);
90 EXPECT_EQ(5u, index3);
91 EXPECT_EQ(3u, index4);
92 EXPECT_EQ(4u, index5);
93 }
94
95 TEST(IntrusiveHeapTest, ExtractMinSimple) {
96 IntrusiveHeap<TestElement> heap;
97
98 heap.insert({15, nullptr});
99 heap.insert({14, nullptr});
100 heap.insert({13, nullptr});
101 heap.insert({12, nullptr});
102 heap.insert({11, nullptr});
103
104 EXPECT_FALSE(heap.empty());
105 EXPECT_EQ(5u, heap.size());
106 EXPECT_EQ(11, heap.extractMin().key);
107 EXPECT_EQ(12, heap.extractMin().key);
108 EXPECT_EQ(13, heap.extractMin().key);
109 EXPECT_EQ(14, heap.extractMin().key);
110 EXPECT_EQ(15, heap.extractMin().key);
111 EXPECT_TRUE(heap.empty());
112 }
113
114 TEST(IntrusiveHeapTest, ExtractMin) {
115 IntrusiveHeap<TestElement> heap;
116
117 for (int i = 0; i < 500; i++)
118 heap.insert({i, nullptr});
119
120 EXPECT_FALSE(heap.empty());
121 EXPECT_EQ(500u, heap.size());
122 for (int i = 0; i < 500; i++)
123 EXPECT_EQ(i, heap.extractMin().key);
124 EXPECT_TRUE(heap.empty());
125 }
126
127 TEST(IntrusiveHeapTest, EraseHeapIndex) {
128 IntrusiveHeap<TestElement> heap;
129
130 size_t index12;
131
132 heap.insert({15, nullptr});
133 heap.insert({14, nullptr});
134 heap.insert({13, nullptr});
135 heap.insert({12, &index12});
136 heap.insert({11, nullptr});
137
138 EXPECT_EQ(5u, heap.size());
139 heap.eraseHeapIndex(index12);
140 EXPECT_EQ(4u, heap.size());
141
142 EXPECT_EQ(11, heap.extractMin().key);
143 EXPECT_EQ(13, heap.extractMin().key);
144 EXPECT_EQ(14, heap.extractMin().key);
145 EXPECT_EQ(15, heap.extractMin().key);
146 EXPECT_TRUE(heap.empty());
147 }
148
149 TEST(IntrusiveHeapTest, ReplaceMin) {
150 IntrusiveHeap<TestElement> heap;
151
152 for (int i = 0; i < 500; i++)
153 heap.insert({500 - i, nullptr});
154
155 EXPECT_EQ(1, heap.min().key);
156
157 for (int i = 0; i < 500; i++)
158 heap.replaceMin({1000 + i, nullptr});
159
160 EXPECT_EQ(1000, heap.min().key);
161 }
162
163 TEST(IntrusiveHeapTest, ReplaceMinWithNonLeafNode) {
164 IntrusiveHeap<TestElement> heap;
165
166 for (int i = 0; i < 50; i++) {
167 heap.insert({i, nullptr});
168 heap.insert({200 + i, nullptr});
169 }
170
171 EXPECT_EQ(0, heap.min().key);
172
173 for (int i = 0; i < 50; i++)
174 heap.replaceMin({100 + i, nullptr});
175
176 for (int i = 0; i < 50; i++)
177 EXPECT_EQ((100 + i), heap.extractMin().key);
178 for (int i = 0; i < 50; i++)
179 EXPECT_EQ((200 + i), heap.extractMin().key);
180 EXPECT_TRUE(heap.empty());
181 }
182
183 } // namespace scheduler
184 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698