Chromium Code Reviews| OLD | NEW |
|---|---|
| (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, 1000]) { | |
| 18 testQueue("int:$count", | |
| 19 create, | |
| 20 new List<int>.generate(count, (x)=>x), | |
|
Søren Gjesse
2014/01/06 08:05:21
Spaces on both sides of =>.
| |
| 21 count); | |
| 22 } | |
| 23 } | |
| 24 | |
| 25 void testCustom(PriorityQueue<C> create(comparator)) { | |
| 26 for (int count in [1, 5, 127, 128, 1000]) { | |
| 27 testQueue("Custom:$count/null", | |
| 28 () => create(null), | |
| 29 new List<C>.generate(count, (x)=> new C(x)), | |
|
Søren Gjesse
2014/01/06 08:05:21
Space before => (two more below).
| |
| 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)); | |
|
Søren Gjesse
2014/01/06 08:05:21
Why is the toList on the set always ordered?
| |
| 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); | |
|
Søren Gjesse
2014/01/06 08:05:21
Is this not just expect(all, equals(elements))?
| |
| 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 for (int i = 0, j = 0; i < elements.length; i++) { | |
| 94 int gray; | |
| 95 do { | |
| 96 gray = j ^ (j >> 1); | |
| 97 j++; | |
| 98 } while (gray >= elements.length); | |
| 99 q.add(elements[gray]); | |
| 100 } | |
| 101 testElements(); | |
| 102 | |
| 103 void addRec(int min, int max) { | |
| 104 int mid = min + ((max - min) >> 1); | |
| 105 q.add(elements[mid]); | |
| 106 if (mid > min) addRec(min, mid); | |
| 107 if (mid + 1 < max) addRec(mid + 1, max); | |
| 108 } | |
| 109 addRec(0, elements.length); | |
| 110 testElements(); | |
| 111 | |
| 112 // Test removeAll. | |
| 113 q.addAll(elements); | |
| 114 expect(q, hasLength(elements.length)); | |
| 115 Iterable all = q.removeAll(); | |
| 116 expect(q.isEmpty, isTrue); | |
| 117 expect(all, hasLength(elements.length)); | |
| 118 for (int i = 0; i < elements.length; i++) { | |
| 119 expect(all, contains(elements[i])); | |
| 120 } | |
| 121 | |
| 122 // Test the same element more than once in queue. | |
| 123 q.addAll(elements); | |
| 124 q.addAll(elements.reversed); | |
| 125 expect(q, hasLength(elements.length * 2)); | |
| 126 for (int i = 0; i < elements.length; i++) { | |
| 127 var element = elements[i]; | |
| 128 expect(q.contains(element), isTrue); | |
| 129 expect(q.removeFirst(), element); | |
| 130 expect(q.removeFirst(), element); | |
| 131 } | |
|
Søren Gjesse
2014/01/06 08:05:21
Test toSet in this case?
| |
| 132 | |
| 133 // Test queue with all same element. | |
| 134 var a = elements[0]; | |
| 135 for (int i = 0; i < elements.length; i++) { | |
| 136 q.add(a); | |
| 137 } | |
| 138 expect(q, hasLength(elements.length)); | |
| 139 expect(q.contains(a), isTrue); | |
| 140 expect(q.contains(notElement), isFalse); | |
| 141 q.removeAll().forEach((x) => expect(x, same(a))); | |
| 142 | |
| 143 // Test remove. | |
| 144 q.addAll(elements); | |
| 145 for (var element in elements.reversed) { | |
| 146 print("R: $element: $q"); | |
| 147 expect(q.remove(element), isTrue); | |
| 148 } | |
| 149 expect(q.isEmpty, isTrue); | |
| 150 } | |
| 151 | |
| 152 | |
| 153 // Custom class. | |
| 154 // Class is comparable, comparators match normal and inverse order. | |
|
Søren Gjesse
2014/01/06 08:05:21
Make these two comparators static members of C?
| |
| 155 int compare(C c1, C c2) => c1.value - c2.value; | |
| 156 int compareNeg(C c1, C c2) => c2.value - c1.value; | |
| 157 | |
| 158 class C { | |
| 159 final int value; | |
| 160 const C(this.value); | |
| 161 int get hashCode => value; | |
| 162 bool operator==(Object other) => other is C && value == other.value; | |
| 163 int compareTo(C other) => value - other.value; | |
| 164 String toString() => "C($value)"; | |
| 165 } | |
| OLD | NEW |