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

Side by Side Diff: pkg/collection/test/priority_queue_test.dart

Issue 110483006: Add priority queue to package:collection. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Improve test layout/comments. Created 6 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 | « pkg/collection/pubspec.yaml ('k') | sdk/lib/core/comparable.dart » ('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) 2013, 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 /// Tests priority queue implementations utilities.
6
7 import "dart:collection";
8 import "package:collection/priority_queue.dart";
9 import "package:unittest/unittest.dart";
10
11 void main() {
12 testInt(() => new HeapPriorityQueue<int>());
13 testCustom((comparator) => new HeapPriorityQueue<C>(comparator));
14 }
15
16 void testInt(PriorityQueue<int> create()) {
17 for (int count in [1, 5, 127, 128, 400]) {
18 testQueue("int:$count",
19 create,
20 new List<int>.generate(count, (x) => x),
21 count);
22 }
23 }
24
25 void testCustom(PriorityQueue<C> create(comparator)) {
26 for (int count in [1, 5, 127, 128, 400]) {
27 testQueue("Custom:$count/null",
28 () => create(null),
29 new List<C>.generate(count, (x) => new C(x)),
30 new C(count));
31 testQueue("Custom:$count/compare",
32 () => create(compare),
33 new List<C>.generate(count, (x) => new C(x)),
34 new C(count));
35 testQueue("Custom:$count/compareNeg",
36 () => create(compareNeg),
37 new List<C>.generate(count, (x) => new C(count - x)),
38 new C(0));
39 }
40 }
41
42 /**
43 * Test that a queue behaves correctly.
44 *
45 * The elements must be in priority order, from highest to lowest.
46 */
47 void testQueue(String name, PriorityQueue create(), List elements, notElement) {
48 test(name, () => testQueueBody(create, elements, notElement));
49 }
50
51 void testQueueBody(PriorityQueue create(), List elements, notElement) {
52 PriorityQueue q = create();
53 expect(q.isEmpty, isTrue);
54 expect(q, hasLength(0));
55 expect(() { q.first; }, throwsStateError);
56 expect(() { q.removeFirst(); }, throwsStateError);
57
58 // Tests removeFirst, first, contains, toList and toSet.
59 void testElements() {
60 expect(q.isNotEmpty, isTrue);
61 expect(q, hasLength(elements.length));
62
63 expect(q.toList(), equals(elements));
64 expect(q.toSet().toList(), equals(elements));
65
66 for (int i = 0; i < elements.length; i++) {
67 expect(q.contains(elements[i]), isTrue);
68 }
69 expect(q.contains(notElement), isFalse);
70
71 List all = [];
72 while (!q.isEmpty) {
73 var expected = q.first;
74 var actual = q.removeFirst();
75 expect(actual, same(expected));
76 all.add(actual);
77 }
78
79 expect(all.length, elements.length);
80 for (int i = 0; i < all.length; i++) {
81 expect(all[i], same(elements[i]));
82 }
83
84 expect(q.isEmpty, isTrue);
85 }
86
87 q.addAll(elements);
88 testElements();
89
90 q.addAll(elements.reversed);
91 testElements();
92
93 // Add elements in a non-linear order (gray order).
94 for (int i = 0, j = 0; i < elements.length; i++) {
95 int gray;
96 do {
97 gray = j ^ (j >> 1);
98 j++;
99 } while (gray >= elements.length);
100 q.add(elements[gray]);
101 }
102 testElements();
103
104 // Add elements by picking the middle element first, and then recursing
105 // on each side.
106 void addRec(int min, int max) {
107 int mid = min + ((max - min) >> 1);
108 q.add(elements[mid]);
109 if (mid + 1 < max) addRec(mid + 1, max);
110 if (mid > min) addRec(min, mid);
111 }
112 addRec(0, elements.length);
113 testElements();
114
115 // Test removeAll.
116 q.addAll(elements);
117 expect(q, hasLength(elements.length));
118 Iterable all = q.removeAll();
119 expect(q.isEmpty, isTrue);
120 expect(all, hasLength(elements.length));
121 for (int i = 0; i < elements.length; i++) {
122 expect(all, contains(elements[i]));
123 }
124
125 // Test the same element more than once in queue.
126 q.addAll(elements);
127 q.addAll(elements.reversed);
128 expect(q, hasLength(elements.length * 2));
129 for (int i = 0; i < elements.length; i++) {
130 var element = elements[i];
131 expect(q.contains(element), isTrue);
132 expect(q.removeFirst(), element);
133 expect(q.removeFirst(), element);
134 }
135
136 // Test queue with all same element.
137 var a = elements[0];
138 for (int i = 0; i < elements.length; i++) {
139 q.add(a);
140 }
141 expect(q, hasLength(elements.length));
142 expect(q.contains(a), isTrue);
143 expect(q.contains(notElement), isFalse);
144 q.removeAll().forEach((x) => expect(x, same(a)));
145
146 // Test remove.
147 q.addAll(elements);
148 for (var element in elements.reversed) {
149 expect(q.remove(element), isTrue);
150 }
151 expect(q.isEmpty, isTrue);
152 }
153
154
155 // Custom class.
156 // Class is comparable, comparators match normal and inverse order.
157 int compare(C c1, C c2) => c1.value - c2.value;
158 int compareNeg(C c1, C c2) => c2.value - c1.value;
159
160 class C implements Comparable {
161 final int value;
162 const C(this.value);
163 int get hashCode => value;
164 bool operator==(Object other) => other is C && value == other.value;
165 int compareTo(C other) => value - other.value;
166 String toString() => "C($value)";
167 }
OLDNEW
« no previous file with comments | « pkg/collection/pubspec.yaml ('k') | sdk/lib/core/comparable.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698