Debian Codegraph @

closure-library/closure/goog/iter/iter_test.js

raw
1 // Copyright 2007 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 goog.provide('goog.iterTest');
16 goog.setTestOnly('goog.iterTest');
17
18 goog.require('goog.iter');
19 goog.require('goog.iter.Iterator');
20 goog.require('goog.iter.StopIteration');
21 goog.require('goog.testing.jsunit');
22
23 function ArrayIterator(array) {
24 this.array_ = array;
25 this.current_ = 0;
26 }
27 goog.inherits(ArrayIterator, goog.iter.Iterator);
28
29 ArrayIterator.prototype.next = function() {
30 if (this.current_ >= this.array_.length) {
31 throw goog.iter.StopIteration;
32 }
33 return this.array_[this.current_++];
34 };
35
36 function testForEach() {
37 var s = '';
38 var iter = new ArrayIterator(['a', 'b', 'c', 'd']);
39 goog.iter.forEach(iter, function(val, index, iter2) {
40 assertEquals(iter, iter2);
41 assertEquals('index should be undefined', 'undefined', typeof index);
42 s += val;
43 });
44 assertEquals('abcd', s);
45 }
46
47 function testJoin() {
48 var iter = new ArrayIterator(['a', 'b', 'c', 'd']);
49 assertEquals('abcd', goog.iter.join(iter, ''));
50
51 iter = new ArrayIterator(['a', 'b', 'c', 'd']);
52 assertEquals('a,b,c,d', goog.iter.join(iter, ','));
53
54 // make sure everything is treated as strings
55 iter = new ArrayIterator([0, 1, 2, 3]);
56 assertEquals('0123', goog.iter.join(iter, ''));
57
58 iter = new ArrayIterator([0, 1, 2, 3]);
59 assertEquals('0919293', goog.iter.join(iter, 9));
60
61 // Joining an empty iterator should result in an empty string
62 iter = new ArrayIterator([]);
63 assertEquals('', goog.iter.join(iter, ','));
64 }
65
66 function testRange() {
67 var iter = goog.iter.range(0, 5, 1);
68 assertEquals('01234', goog.iter.join(iter, ''));
69
70 iter = goog.iter.range(0, 5, 2);
71 assertEquals('024', goog.iter.join(iter, ''));
72
73 iter = goog.iter.range(0, 5, 5);
74 assertEquals('0', goog.iter.join(iter, ''));
75
76 iter = goog.iter.range(0, 5, 10);
77 assertEquals('0', goog.iter.join(iter, ''));
78
79 // negative step
80 var iter = goog.iter.range(5, 0, -1);
81 assertEquals('54321', goog.iter.join(iter, ''));
82
83
84 iter = goog.iter.range(5, 0, -2);
85 assertEquals('531', goog.iter.join(iter, ''));
86
87 iter = goog.iter.range(5, 0, -5);
88 assertEquals('5', goog.iter.join(iter, ''));
89
90 iter = goog.iter.range(5, 0, -10);
91 assertEquals('5', goog.iter.join(iter, ''));
92
93 // wrong direction should result in empty iterator
94 iter = goog.iter.range(0, 5, -1);
95 assertEquals('', goog.iter.join(iter, ''));
96
97 iter = goog.iter.range(5, 0, 1);
98 assertEquals('', goog.iter.join(iter, ''));
99
100 // a step of 0 is not allowed
101 goog.iter.range(0, 5, 0);
102
103 // test the opt args
104 iter = goog.iter.range(0, 5);
105 assertEquals('01234', goog.iter.join(iter, ''));
106
107 iter = goog.iter.range(5);
108 assertEquals('01234', goog.iter.join(iter, ''));
109 }
110
111 function testFilter() {
112 var iter = goog.iter.range(5);
113 var iter2 = goog.iter.filter(iter, function(val, index, iter3) {
114 assertEquals(iter, iter3);
115 assertEquals('index should be undefined', 'undefined', typeof index);
116 return val > 1;
117 });
118
119 assertEquals('234', goog.iter.join(iter2, ''));
120
121 // Chaining filters
122 iter = goog.iter.range(10);
123 var sb = [];
124 var evens = goog.iter.filter(iter, function(v) {
125 sb.push('a' + v);
126 return v % 2 == 0;
127 });
128 var evens2 = goog.iter.filter(evens, function(v) {
129 sb.push('b' + v);
130 return v >= 5;
131 });
132
133 assertEquals('68', goog.iter.join(evens2, ''));
134 // Note the order here. The next calls are done lazily.
135 assertEquals('a0b0a1a2b2a3a4b4a5a6b6a7a8b8a9', sb.join(''));
136 }
137
138 function testFilterFalse() {
139 var iter = goog.iter.range(5);
140 var iter2 = goog.iter.filterFalse(iter, function(val, index, iter3) {
141 assertEquals(iter, iter3);
142 assertEquals('index should be undefined', 'undefined', typeof index);
143 return val < 2;
144 });
145
146 assertEquals('234', goog.iter.join(iter2, ''));
147
148 // Chaining filters
149 iter = goog.iter.range(10);
150 var sb = [];
151 var odds = goog.iter.filterFalse(iter, function(v) {
152 sb.push('a' + v);
153 return v % 2 == 0;
154 });
155 var odds2 = goog.iter.filterFalse(odds, function(v) {
156 sb.push('b' + v);
157 return v <= 5;
158 });
159
160 assertEquals('79', goog.iter.join(odds2, ''));
161 // Note the order here. The next calls are done lazily.
162 assertEquals('a0a1b1a2a3b3a4a5b5a6a7b7a8a9b9', sb.join(''));
163 }
164
165 function testMap() {
166 var iter = goog.iter.range(4);
167 var iter2 = goog.iter.map(iter, function(val, index, iter3) {
168 assertEquals(iter, iter3);
169 assertEquals('index should be undefined', 'undefined', typeof index);
170 return val * val;
171 });
172 assertEquals('0149', goog.iter.join(iter2, ''));
173 }
174
175 function testReduce() {
176 var iter = goog.iter.range(1, 5);
177 assertEquals(
178 10, // 1 + 2 + 3 + 4
179 goog.iter.reduce(iter, function(val, el) { return val + el; }, 0));
180 }
181
182 function testReduce2() {
183 var iter = goog.iter.range(1, 5);
184 assertEquals(
185 24, // 4!
186 goog.iter.reduce(iter, function(val, el) { return val * el; }, 1));
187 }
188
189 function testSome() {
190 var iter = goog.iter.range(5);
191 var b = goog.iter.some(iter, function(val, index, iter3) {
192 assertEquals(iter, iter3);
193 assertEquals('index should be undefined', 'undefined', typeof index);
194 return val > 1;
195 });
196 assertTrue(b);
197 iter = goog.iter.range(5);
198 b = goog.iter.some(iter, function(val, index, iter3) {
199 assertEquals(iter, iter3);
200 assertEquals('index should be undefined', 'undefined', typeof index);
201 return val > 100;
202 });
203 assertFalse(b);
204 }
205
206
207 function testEvery() {
208 var iter = goog.iter.range(5);
209 var b = goog.iter.every(iter, function(val, index, iter3) {
210 assertEquals(iter, iter3);
211 assertEquals('index should be undefined', 'undefined', typeof index);
212 return val >= 0;
213 });
214 assertTrue(b);
215 iter = goog.iter.range(5);
216 b = goog.iter.every(iter, function(val, index, iter3) {
217 assertEquals(iter, iter3);
218 assertEquals('index should be undefined', 'undefined', typeof index);
219 return val > 1;
220 });
221 assertFalse(b);
222 }
223
224 function testChain() {
225 var iter = goog.iter.range(0, 2);
226 var iter2 = goog.iter.range(2, 4);
227 var iter3 = goog.iter.range(4, 6);
228 var iter4 = goog.iter.chain(iter, iter2, iter3);
229
230 assertEquals('012345', goog.iter.join(iter4, ''));
231
232 // empty iter
233 iter = new goog.iter.Iterator;
234 iter2 = goog.iter.chain(iter);
235 assertEquals('', goog.iter.join(iter2, ''));
236
237 // no args
238 iter2 = goog.iter.chain();
239 assertEquals('', goog.iter.join(iter2, ''));
240
241 // arrays
242 var arr = [0, 1];
243 var arr2 = [2, 3];
244 var arr3 = [4, 5];
245 iter = goog.iter.chain(arr, arr2, arr3);
246 assertEquals('012345', goog.iter.join(iter, ''));
247 }
248
249 function testChainFromIterable() {
250 var arg = [0, 1];
251 var arg2 = [2, 3];
252 var arg3 = goog.iter.range(4, 6);
253 var iter = goog.iter.chainFromIterable([arg, arg2, arg3]);
254 assertEquals('012345', goog.iter.join(iter, ''));
255 }
256
257 function testChainFromIterable2() {
258 var arg = goog.iter.zip([0, 3], [1, 4], [2, 5]);
259 var iter = goog.iter.chainFromIterable(arg);
260 assertEquals('012345', goog.iter.join(iter, ''));
261 }
262
263 function testDropWhile() {
264 var iter = goog.iter.range(10);
265 var iter2 = goog.iter.dropWhile(iter, function(val, index, iter3) {
266 assertEquals(iter, iter3);
267 assertEquals('index should be undefined', 'undefined', typeof index);
268 return val < 5;
269 });
270
271 assertEquals('56789', goog.iter.join(iter2, ''));
272 }
273
274 function testDropWhile2() {
275 var iter = goog.iter.range(10);
276 var iter2 = goog.iter.dropWhile(iter, function(val, index, iter3) {
277 assertEquals(iter, iter3);
278 assertEquals('index should be undefined', 'undefined', typeof index);
279 return val != 5;
280 });
281
282 assertEquals('56789', goog.iter.join(iter2, ''));
283 }
284
285
286 function testTakeWhile() {
287 var iter = goog.iter.range(10);
288 var iter2 = goog.iter.takeWhile(iter, function(val, index, iter3) {
289 assertEquals(iter, iter3);
290 assertEquals('index should be undefined', 'undefined', typeof index);
291 return val < 5;
292 });
293
294 assertEquals('01234', goog.iter.join(iter2, ''));
295
296 // next() should not have been called on iter after the first failure and
297 // therefore it should contain some elements. 5 failed so we should have
298 // the rest
299 assertEquals('6789', goog.iter.join(iter, ''));
300 }
301
302 function testTakeWhile2() {
303 var iter = goog.iter.range(10);
304 var iter2 = goog.iter.takeWhile(iter, function(val, index, iter3) {
305 assertEquals(iter, iter3);
306 assertEquals('index should be undefined', 'undefined', typeof index);
307 return val != 5;
308 });
309
310 assertEquals('01234', goog.iter.join(iter2, ''));
311
312 // next() should not have been called on iter after the first failure and
313 // therefore it should contain some elements. 5 failed so we should have
314 // the rest
315 assertEquals('6789', goog.iter.join(iter, ''));
316 }
317
318 function testToArray() {
319 var iter = goog.iter.range(5);
320 var array = goog.iter.toArray(iter);
321 assertEquals('01234', array.join(''));
322
323 // Empty
324 iter = new goog.iter.Iterator;
325 array = goog.iter.toArray(iter);
326 assertEquals('Empty iterator to array', '', array.join(''));
327 }
328
329 function testToArray2() {
330 var iterable = [0, 1, 2, 3, 4];
331 var array = goog.iter.toArray(iterable);
332 assertEquals('01234', array.join(''));
333
334 // Empty
335 iterable = [];
336 array = goog.iter.toArray(iterable);
337 assertEquals('Empty iterator to array', '', array.join(''));
338 }
339
340 function testEquals() {
341 var iter = goog.iter.range(5);
342 var iter2 = goog.iter.range(5);
343 assertTrue('Equal iterators', goog.iter.equals(iter, iter2));
344
345 iter = goog.iter.range(4);
346 iter2 = goog.iter.range(5);
347 assertFalse('Second one is longer', goog.iter.equals(iter, iter2));
348
349 iter = goog.iter.range(5);
350 iter2 = goog.iter.range(4);
351 assertFalse('First one is longer', goog.iter.equals(iter, iter2));
352
353 // 2 empty iterators
354 iter = new goog.iter.Iterator;
355 iter2 = new goog.iter.Iterator;
356 assertTrue('Two empty iterators are equal', goog.iter.equals(iter, iter2));
357
358 iter = goog.iter.range(4);
359 assertFalse('Same iterator', goog.iter.equals(iter, iter));
360
361 // equality function
362 iter = goog.iter.toIterator(['A', 'B', 'C']);
363 iter2 = goog.iter.toIterator(['a', 'b', 'c']);
364 var equalsFn = function(a, b) { return a.toLowerCase() == b.toLowerCase(); };
365 assertTrue('Case-insensitive equal', goog.iter.equals(iter, iter2, equalsFn));
366 }
367
368
369 function testToIterator() {
370 var iter = new goog.iter.range(5);
371 var iter2 = goog.iter.toIterator(iter);
372 assertEquals(
373 'toIterator on an iterator should return the same obejct', iter, iter2);
374
375 var iterLikeObject = {next: function() { throw goog.iter.StopIteration; }};
376 var obj = {
377 __iterator__: function(opt_keys) {
378 assertFalse(
379 '__iterator__ should always be called with false in toIterator',
380 opt_keys);
381 return iterLikeObject;
382 }
383 };
384
385 assertEquals(
386 'Should return the return value of __iterator_(false)', iterLikeObject,
387 goog.iter.toIterator(obj));
388
389 // Array
390 var array = [0, 1, 2, 3, 4];
391 iter = goog.iter.toIterator(array);
392 assertEquals('01234', goog.iter.join(iter, ''));
393
394 // Array like
395 var arrayLike = {'0': 0, '1': 1, '2': 2, length: 3};
396 iter = goog.iter.toIterator(arrayLike);
397 assertEquals('012', goog.iter.join(iter, ''));
398
399 // DOM
400 var dom = document.getElementById('t1').childNodes;
401 iter = goog.iter.toIterator(dom);
402 iter2 = goog.iter.map(iter, function(el) { return el.innerHTML; });
403 assertEquals('012', goog.iter.join(iter2, ''));
404 }
405
406
407 function testNextOrValue() {
408 var iter = goog.iter.toIterator([1]);
409
410 assertEquals(
411 'Should return value when iterator is non-empty', 1,
412 goog.iter.nextOrValue(iter, null));
413 assertNull(
414 'Should return given default when iterator is empty',
415 goog.iter.nextOrValue(iter, null));
416 assertEquals(
417 'Should return given default when iterator is (still) empty', -1,
418 goog.iter.nextOrValue(iter, -1));
419 }
420
421 // Return the product of several arrays as an array
422 function productAsArray(var_args) {
423 var iter = goog.iter.product.apply(null, arguments);
424 return goog.iter.toArray(iter);
425 }
426
427 function testProduct() {
428 assertArrayEquals(
429 [[1, 3], [1, 4], [2, 3], [2, 4]], productAsArray([1, 2], [3, 4]));
430
431 assertArrayEquals(
432 [
433 [1, 3, 5], [1, 3, 6], [1, 4, 5], [1, 4, 6], [2, 3, 5], [2, 3, 6],
434 [2, 4, 5], [2, 4, 6]
435 ],
436 productAsArray([1, 2], [3, 4], [5, 6]));
437
438 assertArrayEquals([[1]], productAsArray([1]));
439 assertArrayEquals([], productAsArray([1], []));
440 assertArrayEquals([], productAsArray());
441
442 var expectedResult = [];
443 var a = [1, 2, 3];
444 var b = [4, 5, 6];
445 var c = [7, 8, 9];
446 for (var i = 0; i < a.length; i++) {
447 for (var j = 0; j < b.length; j++) {
448 for (var k = 0; k < c.length; k++) {
449 expectedResult.push([a[i], b[j], c[k]]);
450 }
451 }
452 }
453
454 assertArrayEquals(expectedResult, productAsArray(a, b, c));
455 }
456
457 function testProductIteration() {
458 var iter = goog.iter.product([1, 2], [3, 4]);
459
460 assertArrayEquals([1, 3], iter.next());
461 assertArrayEquals([1, 4], iter.next());
462 assertArrayEquals([2, 3], iter.next());
463 assertArrayEquals([2, 4], iter.next());
464
465 var ex = assertThrows(function() { iter.next() });
466 assertEquals(goog.iter.StopIteration, ex);
467
468 // Ensure the iterator forever throws StopIteration.
469 for (var i = 0; i < 5; i++) {
470 var ex = assertThrows(function() { iter.next() });
471 assertEquals(goog.iter.StopIteration, ex);
472 }
473
474 iter = goog.iter.product();
475 var ex = assertThrows(function() { iter.next() });
476 assertEquals(goog.iter.StopIteration, ex);
477
478 iter = goog.iter.product([]);
479 var ex = assertThrows(function() { iter.next() });
480 assertEquals(goog.iter.StopIteration, ex);
481 }
482
483 function testCycle() {
484 var regularArray = [1, 2, 3];
485 var iter = goog.iter.cycle(regularArray);
486
487 // Test 3 cycles to ensure proper cache behavior
488 var values = [];
489 for (var i = 0; i < 9; i++) {
490 values.push(iter.next());
491 }
492
493 assertArrayEquals([1, 2, 3, 1, 2, 3, 1, 2, 3], values);
494 }
495
496 function testCycleSingleItemIterable() {
497 var singleItemArray = [1];
498
499 var iter = goog.iter.cycle(singleItemArray);
500 var values = [];
501
502 for (var i = 0; i < 5; i++) {
503 values.push(iter.next());
504 }
505
506 assertArrayEquals([1, 1, 1, 1, 1], values);
507 }
508
509 function testCycleEmptyIterable() {
510 var emptyArray = [];
511
512 var iter = goog.iter.cycle(emptyArray);
513 var ex = assertThrows(function() { iter.next(); });
514 assertEquals(goog.iter.StopIteration, ex);
515 }
516
517 function testCountNoArgs() {
518 var iter = goog.iter.count();
519 var values = goog.iter.limit(iter, 5);
520 assertArrayEquals([0, 1, 2, 3, 4], goog.iter.toArray(values));
521 }
522
523 function testCountStart() {
524 var iter = goog.iter.count(10);
525 var values = goog.iter.limit(iter, 5);
526 assertArrayEquals([10, 11, 12, 13, 14], goog.iter.toArray(values));
527 }
528
529 function testCountStep() {
530 var iter = goog.iter.count(10, 2);
531 var values = goog.iter.limit(iter, 5);
532 assertArrayEquals([10, 12, 14, 16, 18], goog.iter.toArray(values));
533 }
534
535 function testCountNegativeStep() {
536 var iter = goog.iter.count(10, -2);
537 var values = goog.iter.limit(iter, 5);
538 assertArrayEquals([10, 8, 6, 4, 2], goog.iter.toArray(values));
539 }
540
541 function testCountZeroStep() {
542 var iter = goog.iter.count(42, 0);
543 assertEquals(42, iter.next());
544 assertEquals(42, iter.next());
545 assertEquals(42, iter.next());
546 }
547
548 function testCountFloat() {
549 var iter = goog.iter.count(1.5, 0.5);
550 var values = goog.iter.limit(iter, 5);
551 assertArrayEquals([1.5, 2.0, 2.5, 3.0, 3.5], goog.iter.toArray(values));
552 }
553
554 function testRepeat() {
555 var obj = {foo: 'bar'};
556 var iter = goog.iter.repeat(obj);
557 assertEquals(obj, iter.next());
558 assertEquals(obj, iter.next());
559 assertEquals(obj, iter.next());
560 }
561
562 function testAccumulateArray() {
563 var iter = goog.iter.accumulate([1, 2, 3, 4, 5]);
564 assertArrayEquals([1, 3, 6, 10, 15], goog.iter.toArray(iter));
565 }
566
567 function testAccumulateIterator() {
568 var iter = goog.iter.accumulate(goog.iter.range(1, 6));
569 assertArrayEquals([1, 3, 6, 10, 15], goog.iter.toArray(iter));
570 }
571
572 function testAccumulateFloat() {
573 var iter = goog.iter.accumulate([1.0, 2.5, 0.5, 1.5, 0.5]);
574 assertArrayEquals([1.0, 3.5, 4.0, 5.5, 6.0], goog.iter.toArray(iter));
575 }
576
577 function testZipArrays() {
578 var iter = goog.iter.zip([1, 2, 3], [4, 5, 6], [7, 8, 9]);
579 assertArrayEquals([1, 4, 7], iter.next());
580 assertArrayEquals([2, 5, 8], iter.next());
581 assertArrayEquals([3, 6, 9], iter.next());
582 var ex = assertThrows(function() { iter.next() });
583 assertEquals(goog.iter.StopIteration, ex);
584 }
585
586 function testZipSingleArg() {
587 var iter = goog.iter.zip([1, 2, 3]);
588 assertArrayEquals([1], iter.next());
589 assertArrayEquals([2], iter.next());
590 assertArrayEquals([3], iter.next());
591 var ex = assertThrows(function() { iter.next() });
592 assertEquals(goog.iter.StopIteration, ex);
593 }
594
595 function testZipUnevenArgs() {
596 var iter = goog.iter.zip([1, 2, 3], [4, 5], [7]);
597 assertArrayEquals([1, 4, 7], iter.next());
598 var ex = assertThrows(function() { iter.next() });
599 assertEquals(goog.iter.StopIteration, ex);
600 }
601
602 function testZipNoArgs() {
603 var iter = goog.iter.zip();
604 var ex = assertThrows(function() { iter.next() });
605 assertEquals(goog.iter.StopIteration, ex);
606 }
607
608 function testZipIterators() {
609 var iter = goog.iter.zip(goog.iter.count(), goog.iter.repeat('foo'));
610 assertArrayEquals([0, 'foo'], iter.next());
611 assertArrayEquals([1, 'foo'], iter.next());
612 assertArrayEquals([2, 'foo'], iter.next());
613 assertArrayEquals([3, 'foo'], iter.next());
614 }
615
616 function testZipLongestArrays() {
617 var iter = goog.iter.zipLongest('-', 'ABCD'.split(''), 'xy'.split(''));
618 assertArrayEquals(['A', 'x'], iter.next());
619 assertArrayEquals(['B', 'y'], iter.next());
620 assertArrayEquals(['C', '-'], iter.next());
621 assertArrayEquals(['D', '-'], iter.next());
622 var ex = assertThrows(function() { iter.next() });
623 assertEquals(goog.iter.StopIteration, ex);
624 }
625
626 function testZipLongestSingleArg() {
627 var iter = goog.iter.zipLongest('-', 'ABCD'.split(''));
628 assertArrayEquals(['A'], iter.next());
629 assertArrayEquals(['B'], iter.next());
630 assertArrayEquals(['C'], iter.next());
631 assertArrayEquals(['D'], iter.next());
632 var ex = assertThrows(function() { iter.next() });
633 assertEquals(goog.iter.StopIteration, ex);
634 }
635
636 function testZipLongestNoArgs() {
637 var iter = goog.iter.zipLongest();
638 assertArrayEquals([], goog.iter.toArray(iter));
639 var iter = goog.iter.zipLongest('fill');
640 assertArrayEquals([], goog.iter.toArray(iter));
641 }
642
643 function testZipLongestIterators() {
644 var iter = goog.iter.zipLongest(null, goog.iter.range(3), goog.iter.range(5));
645 assertArrayEquals([0, 0], iter.next());
646 assertArrayEquals([1, 1], iter.next());
647 assertArrayEquals([2, 2], iter.next());
648 assertArrayEquals([null, 3], iter.next());
649 assertArrayEquals([null, 4], iter.next());
650 var ex = assertThrows(function() { iter.next() });
651 assertEquals(goog.iter.StopIteration, ex);
652 }
653
654 function testCompressArray() {
655 var iter = goog.iter.compress('ABCDEF'.split(''), [1, 0, 1, 0, 1, 1]);
656 assertEquals('ACEF', goog.iter.join(iter, ''));
657 }
658
659 function testCompressUnevenArgs() {
660 var iter = goog.iter.compress('ABCDEF'.split(''), [false, true, true]);
661 assertEquals('BC', goog.iter.join(iter, ''));
662 }
663
664 function testCompressIterators() {
665 var iter = goog.iter.compress(goog.iter.range(10), goog.iter.cycle([0, 1]));
666 assertArrayEquals([1, 3, 5, 7, 9], goog.iter.toArray(iter));
667 }
668
669 function testGroupByNoKeyFunc() {
670 var iter = goog.iter.groupBy('AAABBBBCDD'.split(''));
671 assertArrayEquals(['A', ['A', 'A', 'A']], iter.next());
672 assertArrayEquals(['B', ['B', 'B', 'B', 'B']], iter.next());
673 assertArrayEquals(['C', ['C']], iter.next());
674 assertArrayEquals(['D', ['D', 'D']], iter.next());
675 var ex = assertThrows(function() { iter.next() });
676 assertEquals(goog.iter.StopIteration, ex);
677 }
678
679 function testGroupByKeyFunc() {
680 var keyFunc = function(x) { return x.toLowerCase(); };
681 var iter = goog.iter.groupBy('AaAABBbbBCccddDD'.split(''), keyFunc);
682 assertArrayEquals(['a', ['A', 'a', 'A', 'A']], iter.next());
683 assertArrayEquals(['b', ['B', 'B', 'b', 'b', 'B']], iter.next());
684 assertArrayEquals(['c', ['C', 'c', 'c']], iter.next());
685 assertArrayEquals(['d', ['d', 'd', 'D', 'D']], iter.next());
686 var ex = assertThrows(function() { iter.next() });
687 assertEquals(goog.iter.StopIteration, ex);
688 }
689
690 function testStarMap() {
691 var iter = goog.iter.starMap([[2, 5], [3, 2], [10, 3]], Math.pow);
692 assertEquals(32, iter.next());
693 assertEquals(9, iter.next());
694 assertEquals(1000, iter.next());
695 var ex = assertThrows(function() { iter.next() });
696 assertEquals(goog.iter.StopIteration, ex);
697 }
698
699 function testStarMapExtraArgs() {
700 var func = function(string, radix, undef, iterator) {
701 assertEquals('undef should be undefined', 'undefined', typeof undef);
702 assertTrue(iterator instanceof goog.iter.Iterator);
703 return parseInt(string, radix);
704 };
705 var iter = goog.iter.starMap([['42', 10], ['0xFF', 16], ['101', 2]], func);
706 assertEquals(42, iter.next());
707 assertEquals(255, iter.next());
708 assertEquals(5, iter.next());
709 var ex = assertThrows(function() { iter.next() });
710 assertEquals(goog.iter.StopIteration, ex);
711 }
712
713 function testTeeArray() {
714 var iters = goog.iter.tee('ABC'.split(''));
715 assertEquals(2, iters.length);
716 var it0 = iters[0], it1 = iters[1];
717 assertEquals('A', it0.next());
718 assertEquals('A', it1.next());
719 assertEquals('B', it0.next());
720 assertEquals('B', it1.next());
721 assertEquals('C', it0.next());
722 assertEquals('C', it1.next());
723 var ex = assertThrows(function() { it0.next() });
724 assertEquals(goog.iter.StopIteration, ex);
725 ex = assertThrows(function() { it1.next() });
726 assertEquals(goog.iter.StopIteration, ex);
727 }
728
729 function testTeeIterator() {
730 var iters = goog.iter.tee(goog.iter.count(), 3);
731 assertEquals(3, iters.length);
732 var it0 = iters[0], it1 = iters[1], it2 = iters[2];
733 assertEquals(0, it0.next());
734 assertEquals(1, it0.next());
735 assertEquals(0, it1.next());
736 assertEquals(1, it1.next());
737 assertEquals(2, it1.next());
738 assertEquals(2, it0.next());
739 assertEquals(0, it2.next());
740 assertEquals(1, it2.next());
741 assertEquals(2, it2.next());
742 assertEquals(3, it0.next());
743 assertEquals(3, it1.next());
744 assertEquals(3, it2.next());
745 }
746
747 function testEnumerateNoStart() {
748 var iter = goog.iter.enumerate('ABC'.split(''));
749 assertArrayEquals([0, 'A'], iter.next());
750 assertArrayEquals([1, 'B'], iter.next());
751 assertArrayEquals([2, 'C'], iter.next());
752 var ex = assertThrows(function() { iter.next() });
753 assertEquals(goog.iter.StopIteration, ex);
754 }
755
756 function testEnumerateStart() {
757 var iter = goog.iter.enumerate('DEF'.split(''), 3);
758 assertArrayEquals([3, 'D'], iter.next());
759 assertArrayEquals([4, 'E'], iter.next());
760 assertArrayEquals([5, 'F'], iter.next());
761 var ex = assertThrows(function() { iter.next() });
762 assertEquals(goog.iter.StopIteration, ex);
763 }
764
765 function testLimitLess() {
766 var iter = goog.iter.limit('ABCDEFG'.split(''), 3);
767 assertEquals('ABC', goog.iter.join(iter, ''));
768 }
769
770 function testLimitGreater() {
771 var iter = goog.iter.limit('ABCDEFG'.split(''), 10);
772 assertEquals('ABCDEFG', goog.iter.join(iter, ''));
773 }
774
775 function testConsumeLess() {
776 var iter = goog.iter.consume('ABCDEFG'.split(''), 3);
777 assertEquals('DEFG', goog.iter.join(iter, ''));
778 }
779
780 function testConsumeGreater() {
781 var iter = goog.iter.consume('ABCDEFG'.split(''), 10);
782 var ex = assertThrows(function() { iter.next() });
783 assertEquals(goog.iter.StopIteration, ex);
784 }
785
786 function testSliceStart() {
787 var iter = goog.iter.slice('ABCDEFG'.split(''), 2);
788 assertEquals('CDEFG', goog.iter.join(iter, ''));
789 }
790
791 function testSliceStop() {
792 var iter = goog.iter.slice('ABCDEFG'.split(''), 2, 4);
793 assertEquals('CD', goog.iter.join(iter, ''));
794 }
795
796 function testSliceStartStopEqual() {
797 var iter = goog.iter.slice('ABCDEFG'.split(''), 1, 1);
798 var ex = assertThrows(function() { iter.next() });
799 assertEquals(goog.iter.StopIteration, ex);
800 }
801
802 function testSliceIterator() {
803 var iter = goog.iter.slice(goog.iter.count(20), 0, 5);
804 assertArrayEquals([20, 21, 22, 23, 24], goog.iter.toArray(iter));
805 }
806
807 function testSliceStartGreater() {
808 var iter = goog.iter.slice('ABCDEFG'.split(''), 10);
809 var ex = assertThrows(function() { iter.next() });
810 assertEquals(goog.iter.StopIteration, ex);
811 }
812
813 function testPermutationsNoLength() {
814 var iter = goog.iter.permutations(goog.iter.range(3));
815 assertArrayEquals([0, 1, 2], iter.next());
816 assertArrayEquals([0, 2, 1], iter.next());
817 assertArrayEquals([1, 0, 2], iter.next());
818 assertArrayEquals([1, 2, 0], iter.next());
819 assertArrayEquals([2, 0, 1], iter.next());
820 assertArrayEquals([2, 1, 0], iter.next());
821 var ex = assertThrows(function() { iter.next() });
822 assertEquals(goog.iter.StopIteration, ex);
823 }
824
825 function testPermutationsLength() {
826 var iter = goog.iter.permutations('ABC'.split(''), 2);
827 assertArrayEquals(['A', 'B'], iter.next());
828 assertArrayEquals(['A', 'C'], iter.next());
829 assertArrayEquals(['B', 'A'], iter.next());
830 assertArrayEquals(['B', 'C'], iter.next());
831 assertArrayEquals(['C', 'A'], iter.next());
832 assertArrayEquals(['C', 'B'], iter.next());
833 }
834
835 function testCombinations() {
836 var iter = goog.iter.combinations(goog.iter.range(4), 3);
837 assertArrayEquals([0, 1, 2], iter.next());
838 assertArrayEquals([0, 1, 3], iter.next());
839 assertArrayEquals([0, 2, 3], iter.next());
840 assertArrayEquals([1, 2, 3], iter.next());
841 var ex = assertThrows(function() { iter.next() });
842 assertEquals(goog.iter.StopIteration, ex);
843 }
844
845 function testCombinationsWithReplacement() {
846 var iter = goog.iter.combinationsWithReplacement('ABC'.split(''), 2);
847 assertArrayEquals(['A', 'A'], iter.next());
848 assertArrayEquals(['A', 'B'], iter.next());
849 assertArrayEquals(['A', 'C'], iter.next());
850 assertArrayEquals(['B', 'B'], iter.next());
851 assertArrayEquals(['B', 'C'], iter.next());
852 assertArrayEquals(['C', 'C'], iter.next());
853 var ex = assertThrows(function() { iter.next() });
854 assertEquals(goog.iter.StopIteration, ex);
855 }