| 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 testInt(() => new HeapPriorityQueue<int>()); | 12 testInt(() => new HeapPriorityQueue<int>()); |
| 12 testCustom((comparator) => new HeapPriorityQueue<C>(comparator)); | 13 testCustom((comparator) => new HeapPriorityQueue<C>(comparator)); |
| 13 } | 14 } |
| 14 | 15 |
| 15 void testInt(PriorityQueue<int> create()) { | 16 void testInt(PriorityQueue<int> create()) { |
| 16 for (int count in [1, 5, 127, 128]) { | 17 for (int count in [1, 5, 127, 128]) { |
| 17 testQueue("int:$count", | 18 testQueue("int:$count", |
| 18 create, | 19 create, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 31 () => create(compare), | 32 () => create(compare), |
| 32 new List<C>.generate(count, (x) => new C(x)), | 33 new List<C>.generate(count, (x) => new C(x)), |
| 33 new C(count)); | 34 new C(count)); |
| 34 testQueue("Custom:$count/compareNeg", | 35 testQueue("Custom:$count/compareNeg", |
| 35 () => create(compareNeg), | 36 () => create(compareNeg), |
| 36 new List<C>.generate(count, (x) => new C(count - x)), | 37 new List<C>.generate(count, (x) => new C(count - x)), |
| 37 new C(0)); | 38 new C(0)); |
| 38 } | 39 } |
| 39 } | 40 } |
| 40 | 41 |
| 41 /** | 42 /// Test that a queue behaves correctly. |
| 42 * Test that a queue behaves correctly. | 43 /// |
| 43 * | 44 /// The elements must be in priority order, from highest to lowest. |
| 44 * The elements must be in priority order, from highest to lowest. | |
| 45 */ | |
| 46 void testQueue(String name, PriorityQueue create(), List elements, notElement) { | 45 void testQueue(String name, PriorityQueue create(), List elements, notElement) { |
| 47 test(name, () => testQueueBody(create, elements, notElement)); | 46 test(name, () => testQueueBody(create, elements, notElement)); |
| 48 } | 47 } |
| 49 | 48 |
| 50 void testQueueBody(PriorityQueue create(), List elements, notElement) { | 49 void testQueueBody(PriorityQueue create(), List elements, notElement) { |
| 51 PriorityQueue q = create(); | 50 PriorityQueue q = create(); |
| 52 expect(q.isEmpty, isTrue); | 51 expect(q.isEmpty, isTrue); |
| 53 expect(q, hasLength(0)); | 52 expect(q, hasLength(0)); |
| 54 expect(() { q.first; }, throwsStateError); | 53 expect(() { q.first; }, throwsStateError); |
| 55 expect(() { q.removeFirst(); }, throwsStateError); | 54 expect(() { q.removeFirst(); }, throwsStateError); |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 int compareNeg(C c1, C c2) => c2.value - c1.value; | 156 int compareNeg(C c1, C c2) => c2.value - c1.value; |
| 158 | 157 |
| 159 class C implements Comparable { | 158 class C implements Comparable { |
| 160 final int value; | 159 final int value; |
| 161 const C(this.value); | 160 const C(this.value); |
| 162 int get hashCode => value; | 161 int get hashCode => value; |
| 163 bool operator==(Object other) => other is C && value == other.value; | 162 bool operator==(Object other) => other is C && value == other.value; |
| 164 int compareTo(C other) => value - other.value; | 163 int compareTo(C other) => value - other.value; |
| 165 String toString() => "C($value)"; | 164 String toString() => "C($value)"; |
| 166 } | 165 } |
| OLD | NEW |