OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 /// Tests priority queue implementations utilities. | 5 /// Tests priority queue implementations utilities. |
6 | 6 |
| 7 import "package:test/test.dart"; |
| 8 |
7 import "package:collection/priority_queue.dart"; | 9 import "package:collection/priority_queue.dart"; |
8 import "package:test/test.dart"; | |
9 | 10 |
10 void main() { | 11 void main() { |
11 testDefault(); | 12 testDefault(); |
12 testInt(() => new HeapPriorityQueue<int>()); | 13 testInt(() => new HeapPriorityQueue<int>()); |
13 testCustom((comparator) => new HeapPriorityQueue<C>(comparator)); | 14 testCustom((comparator) => new HeapPriorityQueue<C>(comparator)); |
14 } | 15 } |
15 | 16 |
16 void testDefault() { | 17 void testDefault() { |
17 test('new PriorityQueue() returns a HeapPriorityQueue', () { | 18 test('new PriorityQueue() returns a HeapPriorityQueue', () { |
18 expect(new PriorityQueue<int>(), | 19 expect(new PriorityQueue<int>(), |
(...skipping 22 matching lines...) Expand all Loading... |
41 () => create(compare), | 42 () => create(compare), |
42 new List<C>.generate(count, (x) => new C(x)), | 43 new List<C>.generate(count, (x) => new C(x)), |
43 new C(count)); | 44 new C(count)); |
44 testQueue("Custom:$count/compareNeg", | 45 testQueue("Custom:$count/compareNeg", |
45 () => create(compareNeg), | 46 () => create(compareNeg), |
46 new List<C>.generate(count, (x) => new C(count - x)), | 47 new List<C>.generate(count, (x) => new C(count - x)), |
47 new C(0)); | 48 new C(0)); |
48 } | 49 } |
49 } | 50 } |
50 | 51 |
51 /** | 52 /// Test that a queue behaves correctly. |
52 * Test that a queue behaves correctly. | 53 /// |
53 * | 54 /// The elements must be in priority order, from highest to lowest. |
54 * The elements must be in priority order, from highest to lowest. | |
55 */ | |
56 void testQueue(String name, PriorityQueue create(), List elements, notElement) { | 55 void testQueue(String name, PriorityQueue create(), List elements, notElement) { |
57 test(name, () => testQueueBody(create, elements, notElement)); | 56 test(name, () => testQueueBody(create, elements, notElement)); |
58 } | 57 } |
59 | 58 |
60 void testQueueBody(PriorityQueue create(), List elements, notElement) { | 59 void testQueueBody(PriorityQueue create(), List elements, notElement) { |
61 PriorityQueue q = create(); | 60 PriorityQueue q = create(); |
62 expect(q.isEmpty, isTrue); | 61 expect(q.isEmpty, isTrue); |
63 expect(q, hasLength(0)); | 62 expect(q, hasLength(0)); |
64 expect(() { q.first; }, throwsStateError); | 63 expect(() { q.first; }, throwsStateError); |
65 expect(() { q.removeFirst(); }, throwsStateError); | 64 expect(() { q.removeFirst(); }, throwsStateError); |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 int compareNeg(C c1, C c2) => c2.value - c1.value; | 166 int compareNeg(C c1, C c2) => c2.value - c1.value; |
168 | 167 |
169 class C implements Comparable { | 168 class C implements Comparable { |
170 final int value; | 169 final int value; |
171 const C(this.value); | 170 const C(this.value); |
172 int get hashCode => value; | 171 int get hashCode => value; |
173 bool operator==(Object other) => other is C && value == other.value; | 172 bool operator==(Object other) => other is C && value == other.value; |
174 int compareTo(C other) => value - other.value; | 173 int compareTo(C other) => value - other.value; |
175 String toString() => "C($value)"; | 174 String toString() => "C($value)"; |
176 } | 175 } |
OLD | NEW |