Flutter Engine
The Flutter Engine
priority_heap_test.cc
Go to the documentation of this file.
1// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5#include "vm/unit_test.h"
6
8
9namespace dart {
10
11UNIT_TEST_CASE(PRIORITY_HEAP_WITH_INDEX__INCREASING) {
13
15 for (word i = 0; i < kSize; i++) {
16 heap.Insert(i, 10 + i);
17 }
18 ASSERT(heap.min_heap_size() == kSize);
19 for (word i = 0; i < kSize; i++) {
20 EXPECT(!heap.IsEmpty());
21 EXPECT_EQ(i, heap.Minimum().priority);
22 EXPECT_EQ(10 + i, heap.Minimum().value);
23 EXPECT(heap.ContainsValue(10 + i));
24 heap.RemoveMinimum();
25 EXPECT(!heap.ContainsValue(10 + i));
26 }
27 EXPECT(heap.IsEmpty());
28}
29
30UNIT_TEST_CASE(PRIORITY_HEAP_WITH_INDEX__DECREASING) {
32
34 for (word i = kSize - 1; i >= 0; i--) {
35 heap.Insert(i, 10 + i);
36 }
37 ASSERT(heap.min_heap_size() == kSize);
38 for (word i = 0; i < kSize; i++) {
39 EXPECT(!heap.IsEmpty());
40 EXPECT_EQ(i, heap.Minimum().priority);
41 EXPECT_EQ(10 + i, heap.Minimum().value);
42 EXPECT(heap.ContainsValue(10 + i));
43 heap.RemoveMinimum();
44 EXPECT(!heap.ContainsValue(10 + i));
45 }
46 EXPECT(heap.IsEmpty());
47}
48
49UNIT_TEST_CASE(PRIORITY_HEAP_WITH_INDEX__DELETE_BY_VALUES) {
51
53 for (word i = kSize - 1; i >= 0; i--) {
54 heap.Insert(i, 10 + i);
55 }
56
57 ASSERT(heap.min_heap_size() == kSize);
58
59 EXPECT(heap.RemoveByValue(10 + 0));
60 EXPECT(!heap.RemoveByValue(10 + 0));
61
62 EXPECT(heap.RemoveByValue(10 + 5));
63 EXPECT(!heap.RemoveByValue(10 + 5));
64
65 EXPECT(heap.RemoveByValue(10 + kSize - 1));
66 EXPECT(!heap.RemoveByValue(10 + kSize - 1));
67
68 for (word i = 0; i < kSize; i++) {
69 // Jump over the removed [i]s in the loop.
70 if (i != 0 && i != 5 && i != (kSize - 1)) {
71 EXPECT(!heap.IsEmpty());
72 EXPECT_EQ(i, heap.Minimum().priority);
73 EXPECT_EQ(10 + i, heap.Minimum().value);
74 EXPECT(heap.ContainsValue(10 + i));
75 heap.RemoveMinimum();
76 EXPECT(!heap.ContainsValue(10 + i));
77 }
78 }
79 EXPECT(heap.IsEmpty());
80}
81
82UNIT_TEST_CASE(PRIORITY_HEAP_WITH_INDEX__GROW_SHRINK) {
83 const word kSize = 1024;
85
87 for (word i = 0; i < kSize; i++) {
88 heap.Insert(i, 10 + i);
89 }
90
91 ASSERT(heap.min_heap_size() == kSize);
92
93 for (word i = 0; i < kSize; i++) {
94 EXPECT(!heap.IsEmpty());
95 EXPECT_EQ(i, heap.Minimum().priority);
96 EXPECT_EQ(10 + i, heap.Minimum().value);
97 EXPECT(heap.ContainsValue(10 + i));
98 heap.RemoveMinimum();
99 EXPECT(!heap.ContainsValue(10 + i));
100 }
101
102 EXPECT(heap.IsEmpty());
103 ASSERT(heap.min_heap_size() == kMinimumSize);
104
105 for (word i = 0; i < kSize; i++) {
106 heap.Insert(i, 10 + i);
107 }
108
109 for (word i = 0; i < kSize; i++) {
110 EXPECT(!heap.IsEmpty());
111 EXPECT_EQ(i, heap.Minimum().priority);
112 EXPECT_EQ(10 + i, heap.Minimum().value);
113 EXPECT(heap.ContainsValue(10 + i));
114 heap.RemoveMinimum();
115 EXPECT(!heap.ContainsValue(10 + i));
116 }
117
118 EXPECT(heap.IsEmpty());
119 ASSERT(heap.min_heap_size() == kMinimumSize);
120}
121
122UNIT_TEST_CASE(PRIORITY_HEAP_WITH_INDEX__CHANGE_PRIORITY) {
124
126 for (word i = 0; i < kSize; i++) {
127 if (i % 2 == 0) {
128 heap.Insert(i, 10 + i);
129 }
130 }
131 ASSERT(heap.min_heap_size() == kSize);
132 for (word i = 0; i < kSize; i++) {
133 bool was_inserted = i % 2 == 0;
134 bool increase = i % 3 == 0;
135 word new_priority = i + (increase ? 100 : -100);
136
137 EXPECT(was_inserted != heap.InsertOrChangePriority(new_priority, 10 + i));
138 }
139
140 for (word i = 0; i < kSize; i++) {
141 bool increase = i % 3 == 0;
142 if (!increase) {
143 word expected_priority = i + (increase ? 100 : -100);
144 EXPECT(!heap.IsEmpty());
145 EXPECT_EQ(expected_priority, heap.Minimum().priority);
146 EXPECT_EQ(10 + i, heap.Minimum().value);
147 EXPECT(heap.ContainsValue(10 + i));
148 heap.RemoveMinimum();
149 EXPECT(!heap.ContainsValue(10 + i));
150 }
151 }
152 for (word i = 0; i < kSize; i++) {
153 bool increase = i % 3 == 0;
154 if (increase) {
155 word expected_priority = i + (increase ? 100 : -100);
156 EXPECT(!heap.IsEmpty());
157 EXPECT_EQ(expected_priority, heap.Minimum().priority);
158 EXPECT_EQ(10 + i, heap.Minimum().value);
159 EXPECT(heap.ContainsValue(10 + i));
160 heap.RemoveMinimum();
161 EXPECT(!heap.ContainsValue(10 + i));
162 }
163 }
164 EXPECT(heap.IsEmpty());
165}
166
167} // namespace dart.
#define EXPECT(type, expectedAlignment, expectedSize)
bool RemoveByValue(const V &value)
const Entry & Minimum() const
bool ContainsValue(const V &value)
void Insert(const P &priority, const V &value)
bool InsertOrChangePriority(const P &priority, const V &value)
bool IsEmpty() const
#define ASSERT(E)
Definition: dart_vm.cc:33
UNIT_TEST_CASE(PRIORITY_HEAP_WITH_INDEX__INCREASING)
intptr_t word
Definition: globals.h:500