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

Side by Side Diff: pkg/collection_helpers/test/algorithms_test.dart

Issue 23567044: Add collection-helpers library. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 3 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
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 algorithm utilities.
6
7 import "dart:collection";
8 import "package:collection_helpers/all.dart";
9 import "package:unittest/unittest.dart";
10 import 'dart:math';
11
12 void main() {
13 void testShuffle(List list) {
14 List copy = list.toList();
15 shuffle(list);
16 expect(new UnorderedIterableEquality().equals(list, copy), isTrue);
17 }
18
19 test("Shuffle 0", (){
20 testShuffle([]);
21 });
22 test("Shuffle 1", (){
23 testShuffle([1]);
24 });
25 test("Shuffle 3", (){
26 testShuffle([1, 2, 3]);
27 });
28 test("Shuffle 10", (){
29 testShuffle([1, 2, 3, 4, 5, 1, 3, 5, 7, 9]);
30 });
31 test("Shuffle shuffles", () {
32 List l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
33 List c = l.toList();
34 int count = 0;
35 do {
36 shuffle(l);
37 if (!const ListEquality().equals(c, l)) return;
38 // Odds of not changing the order should be one in ~ 16! ~= 2e+13.
39 // Repeat this 10 times, and the odds of accidentally shuffling to the
40 // same result every time is disappearingly tiny.
41 count++;
42 // If this happens even once, it's ok to report it.
43 print("Failed shuffle $count times");
44 if (count == 10) fail("Shuffle didn't change order.");
45 } while (true);
46 });
47 test("Shuffle sublist", (){
48 List l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
49 List c = l.toList();
50 shuffle(l, 4, 12);
51 expect(const IterableEquality().equals(l.getRange(0, 4),
52 c.getRange(0, 4)), isTrue);
53 expect(const IterableEquality().equals(l.getRange(12, 16),
54 c.getRange(12, 16)), isTrue);
55 expect(const UnorderedIterableEquality().equals(l.getRange(4, 12),
56 c.getRange(4, 12)),
57 isTrue);
58
59 });
60
61 test("binsearch0", () {
62 expect(binarySearch([], 2), equals(-1));
63 expect(binarySearch([], 2, location:true), equals(0));
64 });
65
66 test("binsearch1", () {
67 expect(binarySearch([5], 2), equals(-1));
68 expect(binarySearch([5], 5), equals(0));
69 expect(binarySearch([5], 7), equals(-1));
70 expect(binarySearch([5], 2, location:true), equals(0));
71 expect(binarySearch([5], 5, location:true), equals(0));
72 expect(binarySearch([5], 7, location:true), equals(1));
73 });
74
75 test("binsearch3", () {
76 expect(binarySearch([0, 5, 10], -1), equals(-1));
77 expect(binarySearch([0, 5, 10], 0), equals(0));
78 expect(binarySearch([0, 5, 10], 2), equals(-1));
79 expect(binarySearch([0, 5, 10], 5), equals(1));
80 expect(binarySearch([0, 5, 10], 7), equals(-1));
81 expect(binarySearch([0, 5, 10], 10), equals(2));
82 expect(binarySearch([0, 5, 10], 12), equals(-1));
83 expect(binarySearch([0, 5, 10], -1, location: true), equals(0));
84 expect(binarySearch([0, 5, 10], 0, location:true), equals(0));
85 expect(binarySearch([0, 5, 10], 2, location:true), equals(1));
86 expect(binarySearch([0, 5, 10], 5, location:true), equals(1));
87 expect(binarySearch([0, 5, 10], 7, location:true), equals(2));
88 expect(binarySearch([0, 5, 10], 10, location:true), equals(2));
89 expect(binarySearch([0, 5, 10], 12, location:true), equals(3));
90 });
91
92 test("binsearchCompare0", () {
93 expect(binarySearch([], new C(2), compare: compareC), equals(-1));
94 expect(binarySearch([], new C(2), location:true, compare: compareC),
95 equals(0));
96 });
97
98 test("binsearchCompare1", () {
99 var l1 = [new C(5)];
100 expect(binarySearch(l1, new C(2), compare: compareC), equals(-1));
101 expect(binarySearch(l1, new C(5), compare: compareC), equals(0));
102 expect(binarySearch(l1, new C(7), compare: compareC), equals(-1));
103 expect(binarySearch(l1, new C(2), location:true, compare: compareC),
104 equals(0));
105 expect(binarySearch(l1, new C(5), location:true, compare: compareC),
106 equals(0));
107 expect(binarySearch(l1, new C(7), location:true, compare: compareC),
108 equals(1));
109 });
110
111 test("binsearchCompare3", () {
112 var l3 = [new C(0), new C(5), new C(10)];
113 expect(binarySearch(l3, new C(-1), compare: compareC), equals(-1));
114 expect(binarySearch(l3, new C(0), compare: compareC), equals(0));
115 expect(binarySearch(l3, new C(2), compare: compareC), equals(-1));
116 expect(binarySearch(l3, new C(5), compare: compareC), equals(1));
117 expect(binarySearch(l3, new C(7), compare: compareC), equals(-1));
118 expect(binarySearch(l3, new C(10), compare: compareC), equals(2));
119 expect(binarySearch(l3, new C(12), compare: compareC), equals(-1));
120 expect(binarySearch(l3, new C(-1), location: true, compare: compareC),
121 equals(0));
122 expect(binarySearch(l3, new C(0), location:true, compare: compareC),
123 equals(0));
124 expect(binarySearch(l3, new C(2), location:true, compare: compareC),
125 equals(1));
126 expect(binarySearch(l3, new C(5), location:true, compare: compareC),
127 equals(1));
128 expect(binarySearch(l3, new C(7), location:true, compare: compareC),
129 equals(2));
130 expect(binarySearch(l3, new C(10), location:true, compare: compareC),
131 equals(2));
132 expect(binarySearch(l3, new C(12), location:true, compare: compareC),
133 equals(3)); });
134
135 test("insertionSortRandom", () {
136 Random random = new Random();
137 for (int i = 0; i < 25; i++) {
138 List list = new List(i);
139 for (int j = 0; j < i; j++) {
140 list[j] = random.nextInt(25); // Expect some equal elements.
141 }
142 insertionSort(list);
143 for (int j = 1; j < i; j++) {
144 expect(list[j - 1], lessThanOrEqualTo(list[j]));
145 }
146 }
147 });
148
149 test("insertionSortSubRanges", () {
150 List l = [6, 5, 4, 3, 2, 1];
151 insertionSort(l, start: 2, end: 4);
152 expect(l, equals([6, 5, 3, 4, 2, 1]));
153 insertionSort(l, start: 1, end: 1);
154 expect(l, equals([6, 5, 3, 4, 2, 1]));
155 insertionSort(l, start: 4, end: 6);
156 expect(l, equals([6, 5, 3, 4, 1, 2]));
157 insertionSort(l, start: 0, end: 2);
158 expect(l, equals([5, 6, 3, 4, 1, 2]));
159 insertionSort(l, start: 0, end: 6);
160 expect(l, equals([1, 2, 3, 4, 5, 6]));
161 });
162
163 test("insertionSortSpecialCases", () {
164 List l = [6, 6, 6, 6, 6, 6];
165 insertionSort(l);
166 expect(l, equals([6, 6, 6, 6, 6, 6]));
167
168 l = [6, 6, 3, 3, 0, 0];
169 insertionSort(l);
170 expect(l, equals([0, 0, 3, 3, 6, 6]));
171 });
172
173 test("MergeSortRandom", () {
174 Random random = new Random();
175 for (int i = 0; i < 250; i += 1) {
176 List list = new List(i);
177 for (int j = 0; j < i; j++) {
178 list[j] = random.nextInt(i); // Expect some equal elements.
179 }
180 mergeSort(list);
181 for (int j = 1; j < i; j++) {
182 expect(list[j - 1], lessThanOrEqualTo(list[j]));
183 }
184 }
185 });
186
187 test("MergeSortPreservesOrder", () {
188 Random random = new Random();
189 // Small case where only insertion call is called,
190 // larger case where the internal moving insertion sort is used
191 // larger cases with multiple splittings, numbers just around a power of 2.
192 for (int size in [8, 50, 511, 512, 513]) {
193 List list = new List(size);
194 // Class OC compares using id.
195 // With size elements with id's in the range 0..size/4, a number of
196 // collisions are guaranteed. These should be sorted so that the "order"
197 // part of the objects are still in order.
198 for (int i = 0; i < size; i++) {
199 list[i] = new OC(random.nextInt(size >> 2), i);
200 }
201 mergeSort(list);
202 OC prev = list[0];
203 for (int i = 1; i < size; i++) {
204 OC next = list[i];
205 expect(prev.id, lessThanOrEqualTo(next.id));
206 if (next.id == prev.id) {
207 expect(prev.order, lessThanOrEqualTo(next.order));
208 }
209 prev = next;
210 }
211 // Reverse compare on part of list.
212 List copy = list.toList();
213 int min = size >> 2;
214 int max = size - min;
215 mergeSort(list, start: min, end: max, compare: (a, b) => b.compareTo(a));
216 prev = list[min];
217 for (int i = min + 1; i < max; i++) {
218 OC next = list[i];
219 expect(prev.id, greaterThanOrEqualTo(next.id));
220 if (next.id == prev.id) {
221 expect(prev.order, lessThanOrEqualTo(next.order));
222 }
223 prev = next;
224 }
225 // Equals on OC objects is identity, so this means the parts before min,
226 // and the parts after max, didn't change at all.
227 expect(list.sublist(0, min), equals(copy.sublist(0, min)));
228 expect(list.sublist(max), equals(copy.sublist(max)));
229 }
230 });
231
232 test("MergeSortSpecialCases", () {
233 for (int size in [511, 512, 513]) {
234 // All equal.
235 List list = new List(size);
236 for (int i = 0; i < size; i++) {
237 list[i] = new OC(0, i);
238 }
239 mergeSort(list);
240 for (int i = 0; i < size; i++) {
241 expect(list[i].order, equals(i));
242 }
243 // All but one equal, first.
244 list[0] = new OC(1, 0);
245 for (int i = 1; i < size; i++) {
246 list[i] = new OC(0, i);
247 }
248 mergeSort(list);
249 for (int i = 0; i < size - 1; i++) {
250 expect(list[i].order, equals(i + 1));
251 }
252 expect(list[size - 1].order, equals(0));
253
254 // All but one equal, last.
255 for (int i = 0; i < size - 1; i++) {
256 list[i] = new OC(0, i);
257 }
258 list[size - 1] = new OC(-1, size - 1);
259 mergeSort(list);
260 expect(list[0].order, equals(size - 1));
261 for (int i = 1; i < size; i++) {
262 expect(list[i].order, equals(i - 1));
263 }
264
265 // Reversed.
266 for (int i = 0; i < size; i++) {
267 list[i] = new OC(size - 1 - i, i);
268 }
269 mergeSort(list);
270 for (int i = 0; i < size; i++) {
271 expect(list[i].id, equals(i));
272 expect(list[i].order, equals(size - 1 - i));
273 }
274 }
275 });
276
277 test("Reverse", () {
278 List l = [6, 5, 4, 3, 2, 1];
279 reverse(l, 2, 4);
280 expect(l, equals([6, 5, 3, 4, 2, 1]));
281 reverse(l, 1, 1);
282 expect(l, equals([6, 5, 3, 4, 2, 1]));
283 reverse(l, 4, 6);
284 expect(l, equals([6, 5, 3, 4, 1, 2]));
285 reverse(l, 0, 2);
286 expect(l, equals([5, 6, 3, 4, 1, 2]));
287 reverse(l, 0, 6);
288 expect(l, equals([2, 1, 4, 3, 6, 5]));
289 });
290 }
291
292 class C {
293 final int id;
294 C(this.id);
295 }
296 int compareC(C one, C other) => one.id - other.id;
297
298 class OC implements Comparable<OC> {
299 final int id;
300 final int order;
301 OC(this.id, this.order);
302 int compareTo(OC other) => id - other.id;
303 String toString() => "OC[$id,$order]";
304 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698