Debian Codegraph @

closure-library/closure/goog/array/array_test.js

raw
1 // Copyright 2006 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.arrayTest');
16 goog.setTestOnly('goog.arrayTest');
17
18 goog.require('goog.array');
19 goog.require('goog.dom');
20 goog.require('goog.dom.TagName');
21 goog.require('goog.testing.PropertyReplacer');
22 goog.require('goog.testing.jsunit');
23 goog.require('goog.testing.recordFunction');
24
25 function testArrayLast() {
26 assertEquals(goog.array.last([1, 2, 3]), 3);
27 assertEquals(goog.array.last([1]), 1);
28 assertUndefined(goog.array.last([]));
29 }
30
31 function testArrayLastWhenDeleted() {
32 var a = [1, 2, 3];
33 delete a[2];
34 assertUndefined(goog.array.last(a));
35 }
36
37 function testArrayIndexOf() {
38 assertEquals(goog.array.indexOf([0, 1, 2, 3], 1), 1);
39 assertEquals(goog.array.indexOf([0, 1, 1, 1], 1), 1);
40 assertEquals(goog.array.indexOf([0, 1, 2, 3], 4), -1);
41 assertEquals(goog.array.indexOf([0, 1, 2, 3], 1, 1), 1);
42 assertEquals(goog.array.indexOf([0, 1, 2, 3], 1, 2), -1);
43 assertEquals(goog.array.indexOf([0, 1, 2, 3], 1, -3), 1);
44 assertEquals(goog.array.indexOf([0, 1, 2, 3], 1, -2), -1);
45 }
46
47 function testArrayIndexOfOmitsDeleted() {
48 var a = [0, 1, 2, 3];
49 delete a[1];
50 delete a[3];
51 assertEquals(goog.array.indexOf(a, undefined), -1);
52 }
53
54 function testArrayIndexOfString() {
55 assertEquals(goog.array.indexOf('abcd', 'd'), 3);
56 assertEquals(goog.array.indexOf('abbb', 'b', 2), 2);
57 assertEquals(goog.array.indexOf('abcd', 'e'), -1);
58 assertEquals(goog.array.indexOf('abcd', 'cd'), -1);
59 assertEquals(goog.array.indexOf('0123', 1), -1);
60 }
61
62 function testArrayLastIndexOf() {
63 assertEquals(goog.array.lastIndexOf([0, 1, 2, 3], 1), 1);
64 assertEquals(goog.array.lastIndexOf([0, 1, 1, 1], 1), 3);
65 assertEquals(goog.array.lastIndexOf([0, 1, 1, 1], 1, 2), 2);
66 }
67
68 function testArrayLastIndexOfOmitsDeleted() {
69 var a = [0, 1, 2, 3];
70 delete a[1];
71 delete a[3];
72 assertEquals(goog.array.lastIndexOf(a, undefined), -1);
73 }
74
75 function testArrayLastIndexOfString() {
76 assertEquals(goog.array.lastIndexOf('abcd', 'b'), 1);
77 assertEquals(goog.array.lastIndexOf('abbb', 'b'), 3);
78 assertEquals(goog.array.lastIndexOf('abbb', 'b', 2), 2);
79 assertEquals(goog.array.lastIndexOf('abcd', 'cd'), -1);
80 assertEquals(goog.array.lastIndexOf('0123', 1), -1);
81 }
82
83 function testArrayForEachBasic() {
84 var s = '';
85 var a = ['a', 'b', 'c', 'd'];
86 goog.array.forEach(a, function(val, index, a2) {
87 assertEquals(a, a2);
88 assertEquals('Index is not a number', 'number', typeof index);
89 s += val + index;
90 });
91 assertEquals('a0b1c2d3', s);
92 }
93
94 function testArrayForEachWithEmptyArray() {
95 var a = new Array(100);
96 goog.array.forEach(a, function(val, index, a2) {
97 fail('The function should not be called since no values were assigned.');
98 });
99 }
100
101 function testArrayForEachWithOnlySomeValuesAsigned() {
102 var count = 0;
103 var a = new Array(1000);
104 a[100] = undefined;
105 goog.array.forEach(a, function(val, index, a2) {
106 assertEquals(100, index);
107 count++;
108 });
109 assertEquals(
110 'Should only call function when a value of array was assigned.', 1,
111 count);
112 }
113
114 function testArrayForEachWithArrayLikeObject() {
115 var counter = goog.testing.recordFunction();
116 var a = {'length': 1, '0': 0, '100': 100, '101': 102};
117 goog.array.forEach(a, counter);
118 assertEquals(
119 'Number of calls should not exceed the value of its length', 1,
120 counter.getCallCount());
121 }
122
123 function testArrayForEachOmitsDeleted() {
124 var s = '';
125 var a = ['a', 'b', 'c', 'd'];
126 delete a[1];
127 delete a[3];
128 goog.array.forEach(a, function(val, index, a2) {
129 assertEquals(a, a2);
130 assertEquals('number', typeof index);
131 s += val + index;
132 });
133 assertEquals('a0c2', s);
134 }
135
136 function testArrayForEachScope() {
137 var scope = {};
138 var a = ['a', 'b', 'c', 'd'];
139 goog.array.forEach(a, function(val, index, a2) {
140 assertEquals(a, a2);
141 assertEquals('number', typeof index);
142 assertEquals(this, scope);
143 }, scope);
144 }
145
146 function testArrayForEachRight() {
147 var s = '';
148 var a = ['a', 'b', 'c', 'd'];
149 goog.array.forEachRight(a, function(val, index, a2) {
150 assertEquals(a, a2);
151 assertEquals('number', typeof index);
152 s += val + String(index);
153 });
154 assertEquals('d3c2b1a0', s);
155 }
156
157 function testArrayForEachRightOmitsDeleted() {
158 var s = '';
159 var a = ['a', 'b', 'c', 'd'];
160 delete a[1];
161 delete a[3];
162 goog.array.forEachRight(a, function(val, index, a2) {
163 assertEquals(a, a2);
164 assertEquals('number', typeof index);
165 assertEquals('string', typeof val);
166 s += val + String(index);
167 });
168 assertEquals('c2a0', s);
169 }
170
171 function testArrayFilter() {
172 var a = [0, 1, 2, 3];
173 a = goog.array.filter(a, function(val, index, a2) {
174 assertEquals(a, a2);
175 assertEquals('index is not a number', 'number', typeof index);
176 return val > 1;
177 });
178 assertArrayEquals([2, 3], a);
179 }
180
181 function testArrayFilterOmitsDeleted() {
182 var a = [0, 1, 2, 3];
183 delete a[1];
184 delete a[3];
185 a = goog.array.filter(a, function(val, index, a2) {
186 assertEquals(a, a2);
187 assertEquals('number', typeof val);
188 assertEquals('index is not a number', 'number', typeof index);
189 return val > 1;
190 });
191 assertArrayEquals([2], a);
192 }
193
194 function testArrayFilterPreservesValues() {
195 var a = [0, 1, 2, 3];
196 a = goog.array.filter(a, function(val, index, a2) {
197 assertEquals(a, a2);
198 // sometimes functions might be evil and do something like this, but we
199 // should still use the original values when returning the filtered array
200 a2[index] = a2[index] - 1;
201 return a2[index] >= 1;
202 });
203 assertArrayEquals([2, 3], a);
204 }
205
206 function testArrayMap() {
207 var a = [0, 1, 2, 3];
208 var result = goog.array.map(a, function(val, index, a2) {
209 assertEquals(a, a2);
210 assertEquals('index is not a number', 'number', typeof index);
211 return val * val;
212 });
213 assertArrayEquals([0, 1, 4, 9], result);
214 }
215
216 function testArrayMapOmitsDeleted() {
217 var a = [0, 1, 2, 3];
218 delete a[1];
219 delete a[3];
220 var result = goog.array.map(a, function(val, index, a2) {
221 assertEquals(a, a2);
222 assertEquals('number', typeof val);
223 assertEquals('index is not a number', 'number', typeof index);
224 return val * val;
225 });
226 var expected = [0, 1, 4, 9];
227 delete expected[1];
228 delete expected[3];
229
230 assertArrayEquals(expected, result);
231 assertFalse('1' in result);
232 assertFalse('3' in result);
233 }
234
235 function testArrayReduce() {
236 var a = [0, 1, 2, 3];
237 assertEquals(6, goog.array.reduce(a, function(rval, val, i, arr) {
238 assertEquals('number', typeof i);
239 assertEquals(a, arr);
240 return rval + val;
241 }, 0));
242
243 var scope = {
244 last: 0,
245 testFn: function(r, v, i, arr) {
246 assertEquals('number', typeof i);
247 assertEquals(a, arr);
248 var l = this.last;
249 this.last = r + v;
250 return this.last + l;
251 }
252 };
253
254 assertEquals(10, goog.array.reduce(a, scope.testFn, 0, scope));
255 }
256
257 function testArrayReduceOmitDeleted() {
258 var a = [0, 1, 2, 3];
259 delete a[1];
260 delete a[3];
261 assertEquals(2, goog.array.reduce(a, function(rval, val, i, arr) {
262 assertEquals('number', typeof i);
263 assertEquals(a, arr);
264 return rval + val;
265 }, 0));
266
267 var scope = {
268 last: 0,
269 testFn: function(r, v, i, arr) {
270 assertEquals('number', typeof i);
271 assertEquals(a, arr);
272 var l = this.last;
273 this.last = r + v;
274 return this.last + l;
275 }
276 };
277
278 assertEquals(2, goog.array.reduce(a, scope.testFn, 0, scope));
279 }
280
281 function testArrayReduceRight() {
282 var a = [0, 1, 2, 3, 4];
283 assertEquals('43210', goog.array.reduceRight(a, function(rval, val, i, arr) {
284 assertEquals('number', typeof i);
285 assertEquals(a, arr);
286 return rval + val;
287 }, ''));
288
289 var scope = {
290 last: '',
291 testFn: function(r, v, i, arr) {
292 assertEquals('number', typeof i);
293 assertEquals(a, arr);
294 var l = this.last;
295 this.last = v;
296 return r + v + l;
297 }
298 };
299
300 a = ['a', 'b', 'c'];
301 assertEquals('_cbcab', goog.array.reduceRight(a, scope.testFn, '_', scope));
302 }
303
304 function testArrayReduceRightOmitsDeleted() {
305 var a = [0, 1, 2, 3, 4];
306 delete a[1];
307 delete a[4];
308 assertEquals('320', goog.array.reduceRight(a, function(rval, val, i, arr) {
309 assertEquals('number', typeof i);
310 assertEquals(a, arr);
311 return rval + val;
312 }, ''));
313
314 scope = {
315 last: '',
316 testFn: function(r, v, i, arr) {
317 assertEquals('number', typeof i);
318 assertEquals(a, arr);
319 var l = this.last;
320 this.last = v;
321 return r + v + l;
322 }
323 };
324
325 a = ['a', 'b', 'c', 'd'];
326 delete a[1];
327 delete a[3];
328 assertEquals('_cac', goog.array.reduceRight(a, scope.testFn, '_', scope));
329 }
330
331 function testArrayFind() {
332 var a = [0, 1, 2, 3];
333 var b = goog.array.find(a, function(val, index, a2) {
334 assertEquals(a, a2);
335 assertEquals('index is not a number', 'number', typeof index);
336 return val > 1;
337 });
338 assertEquals(2, b);
339
340 b = goog.array.find(a, function(val, index, a2) {
341 assertEquals(a, a2);
342 assertEquals('index is not a number', 'number', typeof index);
343 return val > 100;
344 });
345 assertNull(b);
346
347 a = 'abCD';
348 b = goog.array.find(a, function(val, index, a2) {
349 assertEquals(a, a2);
350 assertEquals('index is not a number', 'number', typeof index);
351 return val >= 'A' && val <= 'Z';
352 });
353 assertEquals('C', b);
354
355 a = 'abcd';
356 b = goog.array.find(a, function(val, index, a2) {
357 assertEquals(a, a2);
358 assertEquals('index is not a number', 'number', typeof index);
359 return val >= 'A' && val <= 'Z';
360 });
361 assertNull(b);
362 }
363
364 function testArrayFindOmitsDeleted() {
365 var a = [0, 1, 2, 3];
366 delete a[1];
367 delete a[3];
368 var b = goog.array.find(a, function(val, index, a2) {
369 assertEquals(a, a2);
370 assertEquals('index is not a number', 'number', typeof index);
371 return val > 1;
372 });
373
374 assertEquals(2, b);
375 b = goog.array.find(a, function(val, index, a2) {
376 assertEquals(a, a2);
377 assertEquals('index is not a number', 'number', typeof index);
378 return val > 100;
379 });
380 assertNull(b);
381 }
382
383 function testArrayFindIndex() {
384 var a = [0, 1, 2, 3];
385 var b = goog.array.findIndex(a, function(val, index, a2) {
386 assertEquals(a, a2);
387 assertEquals('index is not a number', 'number', typeof index);
388 return val > 1;
389 });
390 assertEquals(2, b);
391
392 b = goog.array.findIndex(a, function(val, index, a2) {
393 assertEquals(a, a2);
394 assertEquals('index is not a number', 'number', typeof index);
395 return val > 100;
396 });
397 assertEquals(-1, b);
398
399 a = 'abCD';
400 b = goog.array.findIndex(a, function(val, index, a2) {
401 assertEquals(a, a2);
402 assertEquals('index is not a number', 'number', typeof index);
403 return val >= 'A' && val <= 'Z';
404 });
405 assertEquals(2, b);
406
407 a = 'abcd';
408 b = goog.array.findIndex(a, function(val, index, a2) {
409 assertEquals(a, a2);
410 assertEquals('index is not a number', 'number', typeof index);
411 return val >= 'A' && val <= 'Z';
412 });
413 assertEquals(-1, b);
414 }
415
416 function testArrayFindIndexOmitsDeleted() {
417 var a = [0, 1, 2, 3];
418 delete a[1];
419 delete a[3];
420 var b = goog.array.findIndex(a, function(val, index, a2) {
421 assertEquals(a, a2);
422 assertEquals('index is not a number', 'number', typeof index);
423 return val > 1;
424 });
425 assertEquals(2, b);
426
427 b = goog.array.findIndex(a, function(val, index, a2) {
428 assertEquals(a, a2);
429 assertEquals('index is not a number', 'number', typeof index);
430 return val > 100;
431 });
432 assertEquals(-1, b);
433 }
434
435 function testArrayFindRight() {
436 var a = [0, 1, 2, 3];
437 var b = goog.array.findRight(a, function(val, index, a2) {
438 assertEquals(a, a2);
439 assertEquals('index is not a number', 'number', typeof index);
440 return val < 3;
441 });
442 assertEquals(2, b);
443 b = goog.array.findRight(a, function(val, index, a2) {
444 assertEquals(a, a2);
445 assertEquals('index is not a number', 'number', typeof index);
446 return val > 100;
447 });
448 assertNull(b);
449 }
450
451 function testArrayFindRightOmitsDeleted() {
452 var a = [0, 1, 2, 3];
453 delete a[1];
454 delete a[3];
455 var b = goog.array.findRight(a, function(val, index, a2) {
456 assertEquals(a, a2);
457 assertEquals('index is not a number', 'number', typeof index);
458 return val < 3;
459 });
460 assertEquals(2, b);
461 b = goog.array.findRight(a, function(val, index, a2) {
462 assertEquals(a, a2);
463 assertEquals('index is not a number', 'number', typeof index);
464 return val > 100;
465 });
466 assertNull(b);
467 }
468
469 function testArrayFindIndexRight() {
470 var a = [0, 1, 2, 3];
471 var b = goog.array.findIndexRight(a, function(val, index, a2) {
472 assertEquals(a, a2);
473 assertEquals('index is not a number', 'number', typeof index);
474 return val < 3;
475 });
476 assertEquals(2, b);
477
478 b = goog.array.findIndexRight(a, function(val, index, a2) {
479 assertEquals(a, a2);
480 assertEquals('index is not a number', 'number', typeof index);
481 return val > 100;
482 });
483 assertEquals(-1, b);
484
485 a = 'abCD';
486 b = goog.array.findIndexRight(a, function(val, index, a2) {
487 assertEquals(a, a2);
488 assertEquals('index is not a number', 'number', typeof index);
489 return val >= 'a' && val <= 'z';
490 });
491 assertEquals(1, b);
492
493 a = 'abcd';
494 b = goog.array.findIndexRight(a, function(val, index, a2) {
495 assertEquals(a, a2);
496 assertEquals('index is not a number', 'number', typeof index);
497 return val >= 'A' && val <= 'Z';
498 });
499 assertEquals(-1, b);
500 }
501
502 function testArrayFindIndexRightOmitsDeleted() {
503 var a = [0, 1, 2, 3];
504 delete a[1];
505 delete a[3];
506 var b = goog.array.findIndexRight(a, function(val, index, a2) {
507 assertEquals(a, a2);
508 assertEquals('index is not a number', 'number', typeof index);
509 return val < 3;
510 });
511 assertEquals(2, b);
512 b = goog.array.findIndexRight(a, function(val, index, a2) {
513 assertEquals(a, a2);
514 assertEquals('index is not a number', 'number', typeof index);
515 return val > 100;
516 });
517 assertEquals(-1, b);
518 }
519
520 function testArraySome() {
521 var a = [0, 1, 2, 3];
522 var b = goog.array.some(a, function(val, index, a2) {
523 assertEquals(a, a2);
524 assertEquals('index is not a number', 'number', typeof index);
525 return val > 1;
526 });
527 assertTrue(b);
528 b = goog.array.some(a, function(val, index, a2) {
529 assertEquals(a, a2);
530 assertEquals('index is not a number', 'number', typeof index);
531 return val > 100;
532 });
533 assertFalse(b);
534 }
535
536 function testArraySomeOmitsDeleted() {
537 var a = [0, 1, 2, 3];
538 delete a[1];
539 delete a[3];
540 var b = goog.array.some(a, function(val, index, a2) {
541 assertEquals(a, a2);
542 assertEquals('number', typeof val);
543 assertEquals('index is not a number', 'number', typeof index);
544 return val > 1;
545 });
546 assertTrue(b);
547 b = goog.array.some(a, function(val, index, a2) {
548 assertEquals(a, a2);
549 assertEquals('number', typeof val);
550 assertEquals('index is not a number', 'number', typeof index);
551 return val > 100;
552 });
553 assertFalse(b);
554 }
555
556 function testArrayEvery() {
557 var a = [0, 1, 2, 3];
558 var b = goog.array.every(a, function(val, index, a2) {
559 assertEquals(a, a2);
560 assertEquals('index is not a number', 'number', typeof index);
561 return val >= 0;
562 });
563 assertTrue(b);
564 b = goog.array.every(a, function(val, index, a2) {
565 assertEquals(a, a2);
566 assertEquals('index is not a number', 'number', typeof index);
567 return val > 1;
568 });
569 assertFalse(b);
570 }
571
572 function testArrayEveryOmitsDeleted() {
573 var a = [0, 1, 2, 3];
574 delete a[1];
575 delete a[3];
576 var b = goog.array.every(a, function(val, index, a2) {
577 assertEquals(a, a2);
578 assertEquals('number', typeof val);
579 assertEquals('index is not a number', 'number', typeof index);
580 return val >= 0;
581 });
582 assertTrue(b);
583 b = goog.array.every(a, function(val, index, a2) {
584 assertEquals(a, a2);
585 assertEquals('number', typeof val);
586 assertEquals('index is not a number', 'number', typeof index);
587 return val > 1;
588 });
589 assertFalse(b);
590 }
591
592 function testArrayCount() {
593 var a = [0, 1, 2, 3, 4];
594 var context = {};
595 assertEquals(3, goog.array.count(a, function(element, index, array) {
596 assertTrue(goog.isNumber(index));
597 assertEquals(a, array);
598 assertEquals(context, this);
599 return element % 2 == 0;
600 }, context));
601
602 delete a[2];
603 assertEquals('deleted element is ignored', 4, goog.array.count(a, function() {
604 return true;
605 }));
606 }
607
608 function testArrayContains() {
609 var a = [0, 1, 2, 3];
610 assertTrue('contain, Should contain 3', goog.array.contains(a, 3));
611 assertFalse('contain, Should not contain 4', goog.array.contains(a, 4));
612
613 var s = 'abcd';
614 assertTrue('contain, Should contain d', goog.array.contains(s, 'd'));
615 assertFalse('contain, Should not contain e', goog.array.contains(s, 'e'));
616 }
617
618 function testArrayContainsOmitsDeleted() {
619 var a = [0, 1, 2, 3];
620 delete a[1];
621 delete a[3];
622 assertFalse(
623 'should not contain undefined', goog.array.contains(a, undefined));
624 }
625
626 function testArrayInsert() {
627 var a = [0, 1, 2, 3];
628
629 goog.array.insert(a, 4);
630 assertEquals('insert, Should append 4', a[4], 4);
631 goog.array.insert(a, 3);
632 assertEquals('insert, Should not append 3', a.length, 5);
633 assertNotEquals('insert, Should not append 3', a[a.length - 1], 3);
634 }
635
636 function testArrayInsertAt() {
637 var a = [0, 1, 2, 3];
638
639 goog.array.insertAt(a, 4, 2);
640 assertArrayEquals('insertAt, insert in middle', [0, 1, 4, 2, 3], a);
641 goog.array.insertAt(a, 5, 10);
642 assertArrayEquals(
643 'insertAt, too large value should append', [0, 1, 4, 2, 3, 5], a);
644 goog.array.insertAt(a, 6);
645 assertArrayEquals(
646 'insertAt, null/undefined value should insert at 0',
647 [6, 0, 1, 4, 2, 3, 5], a);
648 goog.array.insertAt(a, 7, -2);
649 assertArrayEquals(
650 'insertAt, negative values start from end', [6, 0, 1, 4, 2, 7, 3, 5], a);
651 }
652
653 function testArrayInsertArrayAt() {
654 var a = [2, 5];
655 goog.array.insertArrayAt(a, [3, 4], 1);
656 assertArrayEquals('insertArrayAt, insert in middle', [2, 3, 4, 5], a);
657 goog.array.insertArrayAt(a, [0, 1], 0);
658 assertArrayEquals(
659 'insertArrayAt, insert at beginning', [0, 1, 2, 3, 4, 5], a);
660 goog.array.insertArrayAt(a, [6, 7], 6);
661 assertArrayEquals(
662 'insertArrayAt, insert at end', [0, 1, 2, 3, 4, 5, 6, 7], a);
663 goog.array.insertArrayAt(a, ['x'], 4);
664 assertArrayEquals(
665 'insertArrayAt, insert one element', [0, 1, 2, 3, 'x', 4, 5, 6, 7], a);
666 goog.array.insertArrayAt(a, [], 4);
667 assertArrayEquals(
668 'insertArrayAt, insert 0 elements', [0, 1, 2, 3, 'x', 4, 5, 6, 7], a);
669 goog.array.insertArrayAt(a, ['y', 'z']);
670 assertArrayEquals(
671 'insertArrayAt, undefined value should insert at 0',
672 ['y', 'z', 0, 1, 2, 3, 'x', 4, 5, 6, 7], a);
673 goog.array.insertArrayAt(a, ['a'], null);
674 assertArrayEquals(
675 'insertArrayAt, null value should insert at 0',
676 ['a', 'y', 'z', 0, 1, 2, 3, 'x', 4, 5, 6, 7], a);
677 goog.array.insertArrayAt(a, ['b'], 100);
678 assertArrayEquals(
679 'insertArrayAt, too large value should append',
680 ['a', 'y', 'z', 0, 1, 2, 3, 'x', 4, 5, 6, 7, 'b'], a);
681 goog.array.insertArrayAt(a, ['c', 'd'], -2);
682 assertArrayEquals(
683 'insertArrayAt, negative values start from end',
684 ['a', 'y', 'z', 0, 1, 2, 3, 'x', 4, 5, 6, 'c', 'd', 7, 'b'], a);
685 }
686
687 function testArrayInsertBefore() {
688 var a = ['a', 'b', 'c', 'd'];
689 goog.array.insertBefore(a, 'e', 'b');
690 assertArrayEquals(
691 'insertBefore, with existing element', ['a', 'e', 'b', 'c', 'd'], a);
692 goog.array.insertBefore(a, 'f', 'x');
693 assertArrayEquals(
694 'insertBefore, with non existing element', ['a', 'e', 'b', 'c', 'd', 'f'],
695 a);
696 }
697
698 function testArrayRemove() {
699 var a = ['a', 'b', 'c', 'd'];
700 goog.array.remove(a, 'c');
701 assertArrayEquals('remove, remove existing element', ['a', 'b', 'd'], a);
702 goog.array.remove(a, 'x');
703 assertArrayEquals('remove, remove non existing element', ['a', 'b', 'd'], a);
704 }
705
706 function testArrayRemoveLast() {
707 var a = ['c', 'a', 'b', 'c', 'd', 'a'];
708 goog.array.removeLast(a, 'c');
709 var temp = ['c', 'a', 'b', 'd', 'a'];
710 assertArrayEquals('remove, remove existing element', temp, a);
711 goog.array.removeLast(a, 'a');
712 temp = ['c', 'a', 'b', 'd'];
713 assertArrayEquals('remove, remove existing element', temp, a);
714 goog.array.removeLast(a, 'y');
715 temp = ['c', 'a', 'b', 'd'];
716 assertArrayEquals('remove, remove non existing element', temp, a);
717 }
718
719 function testArrayRemoveAt() {
720 var a = [0, 1, 2, 3];
721 goog.array.removeAt(a, 2);
722 assertArrayEquals('removeAt, remove existing index', [0, 1, 3], a);
723 a = [0, 1, 2, 3];
724 goog.array.removeAt(a, 10);
725 assertArrayEquals('removeAt, remove non existing index', [0, 1, 2, 3], a);
726 a = [0, 1, 2, 3];
727 goog.array.removeAt(a, -2);
728 assertArrayEquals('removeAt, remove with negative index', [0, 1, 3], a);
729 }
730
731 function testArrayRemoveIf() {
732 var a = [0, 1, 2, 3];
733 var b = goog.array.removeIf(a, function(val, index, a2) {
734 assertEquals(a, a2);
735 assertEquals('index is not a number', 'number', typeof index);
736 return val > 1;
737 });
738 assertArrayEquals('removeIf, remove existing element', [0, 1, 3], a);
739
740 a = [0, 1, 2, 3];
741 var b = goog.array.removeIf(a, function(val, index, a2) {
742 assertEquals(a, a2);
743 assertEquals('index is not a number', 'number', typeof index);
744 return val > 100;
745 });
746 assertArrayEquals('removeIf, remove non-existing element', [0, 1, 2, 3], a);
747 }
748
749 function testArrayClone() {
750 var a = [0, 1, 2, 3];
751 var a2 = goog.array.clone(a);
752 assertArrayEquals('clone, should be equal', a, a2);
753
754 var b = {0: 0, 1: 1, 2: 2, 3: 3, length: 4};
755 var b2 = goog.array.clone(b);
756 for (var i = 0; i < b.length; i++) {
757 assertEquals('clone, should be equal', b[i], b2[i]);
758 }
759 }
760
761 function testToArray() {
762 var a = [0, 1, 2, 3];
763 var a2 = goog.array.toArray(a);
764 assertArrayEquals('toArray, should be equal', a, a2);
765
766 var b = {0: 0, 1: 1, 2: 2, 3: 3, length: 4};
767 var b2 = goog.array.toArray(b);
768 for (var i = 0; i < b.length; i++) {
769 assertEquals('toArray, should be equal', b[i], b2[i]);
770 }
771 }
772
773 function testToArrayOnNonArrayLike() {
774 var nonArrayLike = {};
775 assertArrayEquals(
776 'toArray on non ArrayLike should return an empty array', [],
777 goog.array.toArray(nonArrayLike));
778
779 var nonArrayLike2 = {length: 'hello world'};
780 assertArrayEquals(
781 'toArray on non ArrayLike should return an empty array', [],
782 goog.array.toArray(nonArrayLike2));
783 }
784
785 function testExtend() {
786 var a = [0, 1];
787 goog.array.extend(a, [2, 3]);
788 var a2 = [0, 1, 2, 3];
789 assertArrayEquals('extend, should be equal', a, a2);
790
791 var b = [0, 1];
792 goog.array.extend(b, 2);
793 var b2 = [0, 1, 2];
794 assertArrayEquals('extend, should be equal', b, b2);
795
796 a = [0, 1];
797 goog.array.extend(a, [2, 3], [4, 5]);
798 a2 = [0, 1, 2, 3, 4, 5];
799 assertArrayEquals('extend, should be equal', a, a2);
800
801 b = [0, 1];
802 goog.array.extend(b, 2, 3);
803 b2 = [0, 1, 2, 3];
804 assertArrayEquals('extend, should be equal', b, b2);
805
806 var c = [0, 1];
807 goog.array.extend(c, 2, [3, 4], 5, [6]);
808 var c2 = [0, 1, 2, 3, 4, 5, 6];
809 assertArrayEquals('extend, should be equal', c, c2);
810
811 var d = [0, 1];
812 var arrayLikeObject = {0: 2, 1: 3, length: 2};
813 goog.array.extend(d, arrayLikeObject);
814 var d2 = [0, 1, 2, 3];
815 assertArrayEquals('extend, should be equal', d, d2);
816
817 var e = [0, 1];
818 var emptyArrayLikeObject = {length: 0};
819 goog.array.extend(e, emptyArrayLikeObject);
820 assertArrayEquals('extend, should be equal', e, e);
821
822 var f = [0, 1];
823 var length3ArrayLikeObject = {0: 2, 1: 4, 2: 8, length: 3};
824 goog.array.extend(f, length3ArrayLikeObject, length3ArrayLikeObject);
825 var f2 = [0, 1, 2, 4, 8, 2, 4, 8];
826 assertArrayEquals('extend, should be equal', f2, f);
827
828 var result = [];
829 var i = 1000000;
830 var bigArray = Array(i);
831 while (i--) {
832 bigArray[i] = i;
833 }
834 goog.array.extend(result, bigArray);
835 assertArrayEquals(bigArray, result);
836 }
837
838 function testExtendWithArguments() {
839 function f() { return arguments; }
840 var a = [0];
841 var a2 = [0, 1, 2, 3, 4, 5];
842 goog.array.extend(a, f(1, 2, 3), f(4, 5));
843 assertArrayEquals('extend, should be equal', a, a2);
844 }
845
846 function testExtendWithQuerySelector() {
847 var a = [0];
848 var d = goog.dom.getElementsByTagNameAndClass(goog.dom.TagName.DIV, 'foo');
849 goog.array.extend(a, d);
850 assertEquals(2, a.length);
851 }
852
853 function testArraySplice() {
854 var a = [0, 1, 2, 3];
855 goog.array.splice(a, 1, 0, 4);
856 assertArrayEquals([0, 4, 1, 2, 3], a);
857 goog.array.splice(a, 1, 1, 5);
858 assertArrayEquals([0, 5, 1, 2, 3], a);
859 goog.array.splice(a, 1, 1);
860 assertArrayEquals([0, 1, 2, 3], a);
861 // var args
862 goog.array.splice(a, 1, 1, 4, 5, 6);
863 assertArrayEquals([0, 4, 5, 6, 2, 3], a);
864 }
865
866 function testArraySlice() {
867 var a = [0, 1, 2, 3];
868 a = goog.array.slice(a, 1, 3);
869 assertArrayEquals([1, 2], a);
870 a = [0, 1, 2, 3];
871 a = goog.array.slice(a, 1, 6);
872 assertArrayEquals('slice, with too large end', [1, 2, 3], a);
873 a = [0, 1, 2, 3];
874 a = goog.array.slice(a, 1, -1);
875 assertArrayEquals('slice, with negative end', [1, 2], a);
876 a = [0, 1, 2, 3];
877 a = goog.array.slice(a, -2, 3);
878 assertArrayEquals('slice, with negative start', [2], a);
879 }
880
881 function assertRemovedDuplicates(expected, original) {
882 var tempArr = goog.array.clone(original);
883 goog.array.removeDuplicates(tempArr);
884 assertArrayEquals(expected, tempArr);
885 }
886
887 function testRemoveDuplicates() {
888 assertRemovedDuplicates([1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6]);
889 assertRemovedDuplicates(
890 [9, 4, 2, 1, 3, 6, 0, -9], [9, 4, 2, 4, 4, 2, 9, 1, 3, 6, 0, -9]);
891 assertRemovedDuplicates(
892 ['four', 'one', 'two', 'three', 'THREE'],
893 ['four', 'one', 'two', 'one', 'three', 'THREE', 'four', 'two']);
894 assertRemovedDuplicates([], []);
895 assertRemovedDuplicates(
896 ['abc', 'hasOwnProperty', 'toString'],
897 ['abc', 'hasOwnProperty', 'toString', 'abc']);
898
899 var o1 = {}, o2 = {}, o3 = {}, o4 = {};
900 assertRemovedDuplicates([o1, o2, o3, o4], [o1, o1, o2, o3, o2, o4]);
901
902 // Mixed object types.
903 assertRemovedDuplicates([1, '1', 2, '2'], [1, '1', 2, '2']);
904 assertRemovedDuplicates(
905 [true, 'true', false, 'false'], [true, 'true', false, 'false']);
906 assertRemovedDuplicates(['foo'], [String('foo'), 'foo']);
907 assertRemovedDuplicates([12], [Number(12), 12]);
908
909 var obj = {};
910 var uid = goog.getUid(obj);
911 assertRemovedDuplicates([obj, uid], [obj, uid]);
912 }
913
914 function testRemoveDuplicates_customHashFn() {
915 var object1 = {key: 'foo'};
916 var object2 = {key: 'bar'};
917 var dupeObject = {key: 'foo'};
918 var array = [object1, object2, dupeObject, 'bar'];
919 var hashFn = function(object) {
920 return goog.isObject(object) ? object.key :
921 (typeof object).charAt(0) + object;
922 };
923 goog.array.removeDuplicates(array, /* opt_rv */ undefined, hashFn);
924 assertArrayEquals([object1, object2, 'bar'], array);
925 }
926
927 function testBinaryInsertRemove() {
928 var makeChecker = function(array, fn, opt_compareFn) {
929 return function(value, expectResult, expectArray) {
930 var result = fn(array, value, opt_compareFn);
931 assertEquals(expectResult, result);
932 assertArrayEquals(expectArray, array);
933 }
934 };
935
936 var a = [];
937 var check = makeChecker(a, goog.array.binaryInsert);
938 check(3, true, [3]);
939 check(3, false, [3]);
940 check(1, true, [1, 3]);
941 check(5, true, [1, 3, 5]);
942 check(2, true, [1, 2, 3, 5]);
943 check(2, false, [1, 2, 3, 5]);
944
945 check = makeChecker(a, goog.array.binaryRemove);
946 check(0, false, [1, 2, 3, 5]);
947 check(3, true, [1, 2, 5]);
948 check(1, true, [2, 5]);
949 check(5, true, [2]);
950 check(2, true, []);
951 check(2, false, []);
952
953 // test with custom comparison function, which reverse orders numbers
954 var revNumCompare = function(a, b) { return b - a; };
955
956 check = makeChecker(a, goog.array.binaryInsert, revNumCompare);
957 check(3, true, [3]);
958 check(3, false, [3]);
959 check(1, true, [3, 1]);
960 check(5, true, [5, 3, 1]);
961 check(2, true, [5, 3, 2, 1]);
962 check(2, false, [5, 3, 2, 1]);
963
964 check = makeChecker(a, goog.array.binaryRemove, revNumCompare);
965 check(0, false, [5, 3, 2, 1]);
966 check(3, true, [5, 2, 1]);
967 check(1, true, [5, 2]);
968 check(5, true, [2]);
969 check(2, true, []);
970 check(2, false, []);
971 }
972
973 function testBinarySearch() {
974 var insertionPoint = function(position) { return -(position + 1) };
975 var pos;
976
977 // test default comparison on array of String(s)
978 var a = [
979 '1000', '9', 'AB', 'ABC', 'ABCABC', 'ABD', 'ABDA', 'B',
980 'B', 'B', 'C', 'CA', 'CC', 'ZZZ', 'ab', 'abc',
981 'abcabc', 'abd', 'abda', 'b', 'c', 'ca', 'cc', 'zzz'
982 ];
983
984 assertEquals(
985 '\'1000\' should be found at index 0', 0,
986 goog.array.binarySearch(a, '1000'));
987 assertEquals(
988 '\'zzz\' should be found at index ' + (a.length - 1), a.length - 1,
989 goog.array.binarySearch(a, 'zzz'));
990 assertEquals(
991 '\'C\' should be found at index 10', 10, goog.array.binarySearch(a, 'C'));
992 pos = goog.array.binarySearch(a, 'B');
993 assertTrue(
994 '\'B\' should be found at index 7 || 8 || 9',
995 pos == 7 || pos == 8 || pos == 9);
996 pos = goog.array.binarySearch(a, '100');
997 assertTrue('\'100\' should not be found', pos < 0);
998 assertEquals(
999 '\'100\' should have an insertion point of 0', 0, insertionPoint(pos));
1000 pos = goog.array.binarySearch(a, 'zzz0');
1001 assertTrue('\'zzz0\' should not be found', pos < 0);
1002 assertEquals(
1003 '\'zzz0\' should have an insertion point of ' + (a.length), a.length,
1004 insertionPoint(pos));
1005 pos = goog.array.binarySearch(a, 'BA');
1006 assertTrue('\'BA\' should not be found', pos < 0);
1007 assertEquals(
1008 '\'BA\' should have an insertion point of 10', 10, insertionPoint(pos));
1009
1010 // test 0 length array with default comparison
1011 var b = [];
1012
1013 pos = goog.array.binarySearch(b, 'a');
1014 assertTrue('\'a\' should not be found', pos < 0);
1015 assertEquals(
1016 '\'a\' should have an insertion point of 0', 0, insertionPoint(pos));
1017
1018 // test single element array with default lexiographical comparison
1019 var c = ['only item'];
1020
1021 assertEquals(
1022 '\'only item\' should be found at index 0', 0,
1023 goog.array.binarySearch(c, 'only item'));
1024 pos = goog.array.binarySearch(c, 'a');
1025 assertTrue('\'a\' should not be found', pos < 0);
1026 assertEquals(
1027 '\'a\' should have an insertion point of 0', 0, insertionPoint(pos));
1028 pos = goog.array.binarySearch(c, 'z');
1029 assertTrue('\'z\' should not be found', pos < 0);
1030 assertEquals(
1031 '\'z\' should have an insertion point of 1', 1, insertionPoint(pos));
1032
1033 // test default comparison on array of Number(s)
1034 var d = [
1035 -897123.9, -321434.58758, -1321.3124, -324, -9, -3, 0, 0, 0, 0.31255, 5,
1036 142.88888708, 334, 342, 453, 54254
1037 ];
1038
1039 assertEquals(
1040 '-897123.9 should be found at index 0', 0,
1041 goog.array.binarySearch(d, -897123.9));
1042 assertEquals(
1043 '54254 should be found at index ' + (a.length - 1), d.length - 1,
1044 goog.array.binarySearch(d, 54254));
1045 assertEquals(
1046 '-3 should be found at index 5', 5, goog.array.binarySearch(d, -3));
1047 pos = goog.array.binarySearch(d, 0);
1048 assertTrue(
1049 '0 should be found at index 6 || 7 || 8',
1050 pos == 6 || pos == 7 || pos == 8);
1051 pos = goog.array.binarySearch(d, -900000);
1052 assertTrue('-900000 should not be found', pos < 0);
1053 assertEquals(
1054 '-900000 should have an insertion point of 0', 0, insertionPoint(pos));
1055 pos = goog.array.binarySearch(d, 54255);
1056 assertTrue('54255 should not be found', pos < 0);
1057 assertEquals(
1058 '54255 should have an insertion point of ' + (d.length), d.length,
1059 insertionPoint(pos));
1060 pos = goog.array.binarySearch(d, 1.1);
1061 assertTrue('1.1 should not be found', pos < 0);
1062 assertEquals(
1063 '1.1 should have an insertion point of 10', 10, insertionPoint(pos));
1064
1065 // test with custom comparison function, which reverse orders numbers
1066 var revNumCompare = function(a, b) { return b - a; };
1067
1068 var e = [
1069 54254, 453, 342, 334, 142.88888708, 5, 0.31255, 0, 0, 0, -3, -9, -324,
1070 -1321.3124, -321434.58758, -897123.9
1071 ];
1072
1073 assertEquals(
1074 '54254 should be found at index 0', 0,
1075 goog.array.binarySearch(e, 54254, revNumCompare));
1076 assertEquals(
1077 '-897123.9 should be found at index ' + (e.length - 1), e.length - 1,
1078 goog.array.binarySearch(e, -897123.9, revNumCompare));
1079 assertEquals(
1080 '-3 should be found at index 10', 10,
1081 goog.array.binarySearch(e, -3, revNumCompare));
1082 pos = goog.array.binarySearch(e, 0, revNumCompare);
1083 assertTrue(
1084 '0 should be found at index 7 || 8 || 9',
1085 pos == 7 || pos == 8 || pos == 9);
1086 pos = goog.array.binarySearch(e, 54254.1, revNumCompare);
1087 assertTrue('54254.1 should not be found', pos < 0);
1088 assertEquals(
1089 '54254.1 should have an insertion point of 0', 0, insertionPoint(pos));
1090 pos = goog.array.binarySearch(e, -897124, revNumCompare);
1091 assertTrue('-897124 should not be found', pos < 0);
1092 assertEquals(
1093 '-897124 should have an insertion point of ' + (e.length), e.length,
1094 insertionPoint(pos));
1095 pos = goog.array.binarySearch(e, 1.1, revNumCompare);
1096 assertTrue('1.1 should not be found', pos < 0);
1097 assertEquals(
1098 '1.1 should have an insertion point of 6', 6, insertionPoint(pos));
1099
1100 // test 0 length array with custom comparison function
1101 var f = [];
1102
1103 pos = goog.array.binarySearch(f, 0, revNumCompare);
1104 assertTrue('0 should not be found', pos < 0);
1105 assertEquals('0 should have an insertion point of 0', 0, insertionPoint(pos));
1106
1107 // test single element array with custom comparison function
1108 var g = [1];
1109
1110 assertEquals(
1111 '1 should be found at index 0', 0,
1112 goog.array.binarySearch(g, 1, revNumCompare));
1113 pos = goog.array.binarySearch(g, 2, revNumCompare);
1114 assertTrue('2 should not be found', pos < 0);
1115 assertEquals('2 should have an insertion point of 0', 0, insertionPoint(pos));
1116 pos = goog.array.binarySearch(g, 0, revNumCompare);
1117 assertTrue('0 should not be found', pos < 0);
1118 assertEquals('0 should have an insertion point of 1', 1, insertionPoint(pos));
1119
1120 assertEquals(
1121 'binarySearch should find the index of the first 0', 0,
1122 goog.array.binarySearch([0, 0, 1], 0));
1123 assertEquals(
1124 'binarySearch should find the index of the first 1', 1,
1125 goog.array.binarySearch([0, 1, 1], 1));
1126 }
1127
1128
1129 function testBinarySearchPerformance() {
1130 // Ensure that Array#slice, Function#apply and Function#call are not called
1131 // from within binarySearch, since they have performance implications in IE.
1132
1133 var propertyReplacer = new goog.testing.PropertyReplacer();
1134 propertyReplacer.replace(Array.prototype, 'slice', function() {
1135 fail('Should not call Array#slice from binary search.');
1136 });
1137 propertyReplacer.replace(Function.prototype, 'apply', function() {
1138 fail('Should not call Function#apply from binary search.');
1139 });
1140 propertyReplacer.replace(Function.prototype, 'call', function() {
1141 fail('Should not call Function#call from binary search.');
1142 });
1143
1144 try {
1145 var array = [1, 5, 7, 11, 13, 16, 19, 24, 28, 31, 33, 36, 40, 50, 52, 55];
1146 // Test with the default comparison function.
1147 goog.array.binarySearch(array, 48);
1148 // Test with a custom comparison function.
1149 goog.array.binarySearch(
1150 array, 13, function(a, b) { return a > b ? 1 : a < b ? -1 : 0; });
1151 } finally {
1152 // The test runner uses Function.prototype.apply to call tearDown in the
1153 // global context so it has to be reset here.
1154 propertyReplacer.reset();
1155 }
1156 }
1157
1158
1159 function testBinarySelect() {
1160 var insertionPoint = function(position) { return -(position + 1) };
1161 var numbers = [
1162 -897123.9, -321434.58758, -1321.3124, -324, -9, -3, 0, 0, 0, 0.31255, 5,
1163 142.88888708, 334, 342, 453, 54254
1164 ];
1165 var objects = goog.array.map(numbers, function(n) { return {n: n}; });
1166 function makeEvaluator(target) {
1167 return function(obj, i, arr) {
1168 assertEquals(objects, arr);
1169 assertEquals(obj, arr[i]);
1170 return target - obj.n;
1171 };
1172 }
1173 assertEquals(
1174 '{n:-897123.9} should be found at index 0', 0,
1175 goog.array.binarySelect(objects, makeEvaluator(-897123.9)));
1176 assertEquals(
1177 '{n:54254} should be found at index ' + (objects.length - 1),
1178 objects.length - 1,
1179 goog.array.binarySelect(objects, makeEvaluator(54254)));
1180 assertEquals(
1181 '{n:-3} should be found at index 5', 5,
1182 goog.array.binarySelect(objects, makeEvaluator(-3)));
1183 pos = goog.array.binarySelect(objects, makeEvaluator(0));
1184 assertTrue(
1185 '{n:0} should be found at index 6 || 7 || 8',
1186 pos == 6 || pos == 7 || pos == 8);
1187 pos = goog.array.binarySelect(objects, makeEvaluator(-900000));
1188 assertTrue('{n:-900000} should not be found', pos < 0);
1189 assertEquals(
1190 '{n:-900000} should have an insertion point of 0', 0,
1191 insertionPoint(pos));
1192 pos = goog.array.binarySelect(objects, makeEvaluator('54255'));
1193 assertTrue('{n:54255} should not be found', pos < 0);
1194 assertEquals(
1195 '{n:54255} should have an insertion point of ' + (objects.length),
1196 objects.length, insertionPoint(pos));
1197 pos = goog.array.binarySelect(objects, makeEvaluator(1.1));
1198 assertTrue('{n:1.1} should not be found', pos < 0);
1199 assertEquals(
1200 '{n:1.1} should have an insertion point of 10', 10, insertionPoint(pos));
1201 }
1202
1203
1204 function testArrayEquals() {
1205 // Test argument types.
1206 assertFalse('array == not array', goog.array.equals([], null));
1207 assertFalse('not array == array', goog.array.equals(null, []));
1208 assertFalse('not array == not array', goog.array.equals(null, null));
1209
1210 // Test with default comparison function.
1211 assertTrue('[] == []', goog.array.equals([], []));
1212 assertTrue('[1] == [1]', goog.array.equals([1], [1]));
1213 assertTrue('["1"] == ["1"]', goog.array.equals(['1'], ['1']));
1214 assertFalse('[1] == ["1"]', goog.array.equals([1], ['1']));
1215 assertTrue('[null] == [null]', goog.array.equals([null], [null]));
1216 assertFalse('[null] == [undefined]', goog.array.equals([null], [undefined]));
1217 assertTrue('[1, 2] == [1, 2]', goog.array.equals([1, 2], [1, 2]));
1218 assertFalse('[1, 2] == [2, 1]', goog.array.equals([1, 2], [2, 1]));
1219 assertFalse('[1, 2] == [1]', goog.array.equals([1, 2], [1]));
1220 assertFalse('[1] == [1, 2]', goog.array.equals([1], [1, 2]));
1221 assertFalse('[{}] == [{}]', goog.array.equals([{}], [{}]));
1222
1223 // Test with custom comparison function.
1224 var cmp = function(a, b) { return typeof a == typeof b; };
1225 assertTrue('[] cmp []', goog.array.equals([], [], cmp));
1226 assertTrue('[1] cmp [1]', goog.array.equals([1], [1], cmp));
1227 assertTrue('[1] cmp [2]', goog.array.equals([1], [2], cmp));
1228 assertTrue('["1"] cmp ["1"]', goog.array.equals(['1'], ['1'], cmp));
1229 assertTrue('["1"] cmp ["2"]', goog.array.equals(['1'], ['2'], cmp));
1230 assertFalse('[1] cmp ["1"]', goog.array.equals([1], ['1'], cmp));
1231 assertTrue('[1, 2] cmp [3, 4]', goog.array.equals([1, 2], [3, 4], cmp));
1232 assertFalse('[1] cmp [2, 3]', goog.array.equals([1], [2, 3], cmp));
1233 assertTrue('[{}] cmp [{}]', goog.array.equals([{}], [{}], cmp));
1234 assertTrue('[{}] cmp [{a: 1}]', goog.array.equals([{}], [{a: 1}], cmp));
1235
1236 // Test with array-like objects.
1237 assertTrue('[5] == obj [5]', goog.array.equals([5], {0: 5, length: 1}));
1238 assertTrue('obj [5] == [5]', goog.array.equals({0: 5, length: 1}, [5]));
1239 assertTrue(
1240 '["x"] == obj ["x"]', goog.array.equals(['x'], {0: 'x', length: 1}));
1241 assertTrue(
1242 'obj ["x"] == ["x"]', goog.array.equals({0: 'x', length: 1}, ['x']));
1243 assertTrue(
1244 '[5] == {0: 5, 1: 6, length: 1}',
1245 goog.array.equals([5], {0: 5, 1: 6, length: 1}));
1246 assertTrue(
1247 '{0: 5, 1: 6, length: 1} == [5]',
1248 goog.array.equals({0: 5, 1: 6, length: 1}, [5]));
1249 assertFalse(
1250 '[5, 6] == {0: 5, 1: 6, length: 1}',
1251 goog.array.equals([5, 6], {0: 5, 1: 6, length: 1}));
1252 assertFalse(
1253 '{0: 5, 1: 6, length: 1}, [5, 6]',
1254 goog.array.equals({0: 5, 1: 6, length: 1}, [5, 6]));
1255 assertTrue(
1256 '[5, 6] == obj [5, 6]',
1257 goog.array.equals([5, 6], {0: 5, 1: 6, length: 2}));
1258 assertTrue(
1259 'obj [5, 6] == [5, 6]',
1260 goog.array.equals({0: 5, 1: 6, length: 2}, [5, 6]));
1261 assertFalse(
1262 '{0: 5, 1: 6} == [5, 6]', goog.array.equals({0: 5, 1: 6}, [5, 6]));
1263 }
1264
1265
1266 function testArrayCompare3Basic() {
1267 assertEquals(0, goog.array.compare3([], []));
1268 assertEquals(0, goog.array.compare3(['111', '222'], ['111', '222']));
1269 assertEquals(-1, goog.array.compare3(['111', '222'], ['1111', '']));
1270 assertEquals(1, goog.array.compare3(['111', '222'], ['111']));
1271 assertEquals(1, goog.array.compare3(['11', '222', '333'], []));
1272 assertEquals(-1, goog.array.compare3([], ['11', '222', '333']));
1273 }
1274
1275
1276 function testArrayCompare3ComparatorFn() {
1277 function cmp(a, b) { return a - b; };
1278 assertEquals(0, goog.array.compare3([], [], cmp));
1279 assertEquals(0, goog.array.compare3([8, 4], [8, 4], cmp));
1280 assertEquals(-1, goog.array.compare3([4, 3], [5, 0]));
1281 assertEquals(1, goog.array.compare3([6, 2], [6]));
1282 assertEquals(1, goog.array.compare3([1, 2, 3], []));
1283 assertEquals(-1, goog.array.compare3([], [1, 2, 3]));
1284 }
1285
1286
1287 function testSort() {
1288 // Test sorting empty array
1289 var a = [];
1290 goog.array.sort(a);
1291 assertEquals(
1292 'Sorted empty array is still an empty array (length 0)', 0, a.length);
1293
1294 // Test sorting homogenous array of String(s) of length > 1
1295 var b = [
1296 'JUST', '1', 'test', 'Array', 'to', 'test', 'array', 'Sort', 'about', 'NOW',
1297 '!!'
1298 ];
1299 var bSorted = [
1300 '!!', '1', 'Array', 'JUST', 'NOW', 'Sort', 'about', 'array', 'test', 'test',
1301 'to'
1302 ];
1303 goog.array.sort(b);
1304 assertArrayEquals(bSorted, b);
1305
1306 // Test sorting already sorted array of String(s) of length > 1
1307 goog.array.sort(b);
1308 assertArrayEquals(bSorted, b);
1309
1310 // Test sorting homogenous array of integer Number(s) of length > 1
1311 var c = [
1312 100, 1, 2000, -1, 0, 1000023, 12312512, -12331, 123, 54325, -38104783,
1313 93708, 908, -213, -4, 5423, 0
1314 ];
1315 var cSorted = [
1316 -38104783, -12331, -213, -4, -1, 0, 0, 1, 100, 123, 908, 2000, 5423, 54325,
1317 93708, 1000023, 12312512
1318 ];
1319 goog.array.sort(c);
1320 assertArrayEquals(cSorted, c);
1321
1322 // Test sorting already sorted array of integer Number(s) of length > 1
1323 goog.array.sort(c);
1324 assertArrayEquals(cSorted, c);
1325
1326 // Test sorting homogenous array of Number(s) of length > 1
1327 var e = [
1328 -1321.3124, 0.31255, 54254, 0, 142.88888708, -321434.58758, -324, 453, 334,
1329 -3, 5, -9, 342, -897123.9
1330 ];
1331 var eSorted = [
1332 -897123.9, -321434.58758, -1321.3124, -324, -9, -3, 0, 0.31255, 5,
1333 142.88888708, 334, 342, 453, 54254
1334 ];
1335 goog.array.sort(e);
1336 assertArrayEquals(eSorted, e);
1337
1338 // Test sorting already sorted array of Number(s) of length > 1
1339 goog.array.sort(e);
1340 assertArrayEquals(eSorted, e);
1341
1342 // Test sorting array of Number(s) of length > 1,
1343 // using custom comparison function which does reverse ordering
1344 var f = [
1345 -1321.3124, 0.31255, 54254, 0, 142.88888708, -321434.58758, -324, 453, 334,
1346 -3, 5, -9, 342, -897123.9
1347 ];
1348 var fSorted = [
1349 54254, 453, 342, 334, 142.88888708, 5, 0.31255, 0, -3, -9, -324, -1321.3124,
1350 -321434.58758, -897123.9
1351 ];
1352 goog.array.sort(f, function(a, b) { return b - a; });
1353 assertArrayEquals(fSorted, f);
1354
1355 // Test sorting already sorted array of Number(s) of length > 1
1356 // using custom comparison function which does reverse ordering
1357 goog.array.sort(f, function(a, b) { return b - a; });
1358 assertArrayEquals(fSorted, f);
1359
1360 // Test sorting array of custom Object(s) of length > 1 that have
1361 // an overriden toString
1362 function ComparedObject(value) { this.value = value; };
1363
1364 ComparedObject.prototype.toString = function() { return this.value; };
1365
1366 var co1 = new ComparedObject('a');
1367 var co2 = new ComparedObject('b');
1368 var co3 = new ComparedObject('c');
1369 var co4 = new ComparedObject('d');
1370
1371 var g = [co3, co4, co2, co1];
1372 var gSorted = [co1, co2, co3, co4];
1373 goog.array.sort(g);
1374 assertArrayEquals(gSorted, g);
1375
1376 // Test sorting already sorted array of custom Object(s) of length > 1
1377 // that have an overriden toString
1378 goog.array.sort(g);
1379 assertArrayEquals(gSorted, g);
1380
1381 // Test sorting an array of custom Object(s) of length > 1 using
1382 // a custom comparison function
1383 var h = [co4, co2, co1, co3];
1384 var hSorted = [co1, co2, co3, co4];
1385 goog.array.sort(h, function(a, b) {
1386 return a.value > b.value ? 1 : a.value < b.value ? -1 : 0;
1387 });
1388 assertArrayEquals(hSorted, h);
1389
1390 // Test sorting already sorted array of custom Object(s) of length > 1
1391 // using a custom comparison function
1392 goog.array.sort(h);
1393 assertArrayEquals(hSorted, h);
1394
1395 // Test sorting arrays of length 1
1396 var i = ['one'];
1397 var iSorted = ['one'];
1398 goog.array.sort(i);
1399 assertArrayEquals(iSorted, i);
1400
1401 var j = [1];
1402 var jSorted = [1];
1403 goog.array.sort(j);
1404 assertArrayEquals(jSorted, j);
1405
1406 var k = [1.1];
1407 var kSorted = [1.1];
1408 goog.array.sort(k);
1409 assertArrayEquals(kSorted, k);
1410
1411 var l = [co3];
1412 var lSorted = [co3];
1413 goog.array.sort(l);
1414 assertArrayEquals(lSorted, l);
1415
1416 var m = [co2];
1417 var mSorted = [co2];
1418 goog.array.sort(m, function(a, b) {
1419 return a.value > b.value ? 1 : a.value < b.value ? -1 : 0;
1420 });
1421 assertArrayEquals(mSorted, m);
1422 }
1423
1424 function testStableSort() {
1425 // Test array with custom comparison function
1426 var arr = [
1427 {key: 3, val: 'a'}, {key: 2, val: 'b'}, {key: 3, val: 'c'},
1428 {key: 4, val: 'd'}, {key: 3, val: 'e'}
1429 ];
1430 var arrClone = goog.array.clone(arr);
1431
1432 function comparisonFn(obj1, obj2) { return obj1.key - obj2.key; }
1433 goog.array.stableSort(arr, comparisonFn);
1434 var sortedValues = [];
1435 for (var i = 0; i < arr.length; i++) {
1436 sortedValues.push(arr[i].val);
1437 }
1438 var wantedSortedValues = ['b', 'a', 'c', 'e', 'd'];
1439 assertArrayEquals(wantedSortedValues, sortedValues);
1440
1441 // Test array without custom comparison function
1442 var arr2 = [];
1443 for (var i = 0; i < arrClone.length; i++) {
1444 arr2.push({
1445 val: arrClone[i].val,
1446 toString: goog.partial(function(index) { return arrClone[index].key; }, i)
1447 });
1448 }
1449 goog.array.stableSort(arr2);
1450 var sortedValues2 = [];
1451 for (var i = 0; i < arr2.length; i++) {
1452 sortedValues2.push(arr2[i].val);
1453 }
1454 assertArrayEquals(wantedSortedValues, sortedValues2);
1455 }
1456
1457 function testSortByKey() {
1458 function Item(value) {
1459 this.getValue = function() { return value; };
1460 }
1461 var keyFn = function(item) { return item.getValue(); };
1462
1463 // Test without custom key comparison function
1464 var arr1 = [new Item(3), new Item(2), new Item(1), new Item(5), new Item(4)];
1465 goog.array.sortByKey(arr1, keyFn);
1466 var wantedSortedValues1 = [1, 2, 3, 4, 5];
1467 for (var i = 0; i < arr1.length; i++) {
1468 assertEquals(wantedSortedValues1[i], arr1[i].getValue());
1469 }
1470
1471 // Test with custom key comparison function
1472 var arr2 = [new Item(3), new Item(2), new Item(1), new Item(5), new Item(4)];
1473 function comparisonFn(key1, key2) { return -(key1 - key2); }
1474 goog.array.sortByKey(arr2, keyFn, comparisonFn);
1475 var wantedSortedValues2 = [5, 4, 3, 2, 1];
1476 for (var i = 0; i < arr2.length; i++) {
1477 assertEquals(wantedSortedValues2[i], arr2[i].getValue());
1478 }
1479 }
1480
1481 function testArrayBucketModulus() {
1482 // bucket things by modulus
1483 var a = {};
1484 var b = [];
1485
1486 function modFive(num) { return num % 5; }
1487
1488 for (var i = 0; i < 20; i++) {
1489 var mod = modFive(i);
1490 a[mod] = a[mod] || [];
1491 a[mod].push(i);
1492 b.push(i);
1493 }
1494
1495 var buckets = goog.array.bucket(b, modFive);
1496
1497 for (var i = 0; i < 5; i++) {
1498 // The order isn't defined, but they should be the same sorted.
1499 goog.array.sort(a[i]);
1500 goog.array.sort(buckets[i]);
1501 assertArrayEquals(a[i], buckets[i]);
1502 }
1503 }
1504
1505 function testArrayBucketEvenOdd() {
1506 var a = [1, 2, 3, 4, 5, 6, 7, 8, 9];
1507
1508 // test even/odd
1509 function isEven(value, index, array) {
1510 assertEquals(value, array[index]);
1511 assertEquals('number', typeof index);
1512 assertEquals(a, array);
1513 return value % 2 == 0;
1514 }
1515
1516 var b = goog.array.bucket(a, isEven);
1517
1518 assertArrayEquals(b[true], [2, 4, 6, 8]);
1519 assertArrayEquals(b[false], [1, 3, 5, 7, 9]);
1520 }
1521
1522 function testArrayBucketUsingThisObject() {
1523 var a = [1, 2, 3, 4, 5];
1524
1525 var obj = {specialValue: 2};
1526
1527 function isSpecialValue(value, index, array) {
1528 return value == this.specialValue ? 1 : 0;
1529 }
1530
1531 var b = goog.array.bucket(a, isSpecialValue, obj);
1532 assertArrayEquals(b[0], [1, 3, 4, 5]);
1533 assertArrayEquals(b[1], [2]);
1534 }
1535
1536 function testArrayToObject() {
1537 var a = [{name: 'a'}, {name: 'b'}, {name: 'c'}, {name: 'd'}];
1538
1539 function getName(value, index, array) {
1540 assertEquals(value, array[index]);
1541 assertEquals('number', typeof index);
1542 assertEquals(a, array);
1543 return value.name;
1544 }
1545
1546 var b = goog.array.toObject(a, getName);
1547
1548 for (var i = 0; i < a.length; i++) {
1549 assertEquals(a[i], b[a[i].name]);
1550 }
1551 }
1552
1553 function testRange() {
1554 assertArrayEquals([], goog.array.range(0));
1555 assertArrayEquals([], goog.array.range(5, 5, 5));
1556 assertArrayEquals([], goog.array.range(-3, -3));
1557 assertArrayEquals([], goog.array.range(10, undefined, -1));
1558 assertArrayEquals([], goog.array.range(8, 0));
1559 assertArrayEquals([], goog.array.range(-5, -10, 3));
1560
1561 assertArrayEquals([0], goog.array.range(1));
1562 assertArrayEquals([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], goog.array.range(10));
1563
1564 assertArrayEquals([1], goog.array.range(1, 2));
1565 assertArrayEquals([-3, -2, -1, 0, 1, 2], goog.array.range(-3, 3));
1566
1567 assertArrayEquals([4], goog.array.range(4, 40, 400));
1568 assertArrayEquals([5, 8, 11, 14], goog.array.range(5, 15, 3));
1569 assertArrayEquals([1, -1, -3], goog.array.range(1, -5, -2));
1570 assertElementsRoughlyEqual([.2, .3, .4], goog.array.range(.2, .5, .1), 0.001);
1571
1572 assertArrayEquals([0], goog.array.range(7, undefined, 9));
1573 assertArrayEquals([0, 2, 4, 6], goog.array.range(8, undefined, 2));
1574 }
1575
1576 function testArrayRepeat() {
1577 assertArrayEquals([], goog.array.repeat(3, 0));
1578 assertArrayEquals([], goog.array.repeat(3, -1));
1579 assertArrayEquals([3], goog.array.repeat(3, 1));
1580 assertArrayEquals([3, 3, 3], goog.array.repeat(3, 3));
1581 assertArrayEquals([null, null], goog.array.repeat(null, 2));
1582 }
1583
1584 function testArrayFlatten() {
1585 assertArrayEquals([1, 2, 3, 4, 5], goog.array.flatten(1, 2, 3, 4, 5));
1586 assertArrayEquals([1, 2, 3, 4, 5], goog.array.flatten(1, [2, [3, [4, 5]]]));
1587 assertArrayEquals([1, 2, 3, 4], goog.array.flatten(1, [2, [3, [4]]]));
1588 assertArrayEquals([1, 2, 3, 4], goog.array.flatten([[[1], 2], 3], 4));
1589 assertArrayEquals([1], goog.array.flatten([[1]]));
1590 assertArrayEquals([], goog.array.flatten());
1591 assertArrayEquals([], goog.array.flatten([]));
1592 assertArrayEquals(
1593 goog.array.repeat(3, 180002),
1594 goog.array.flatten(3, goog.array.repeat(3, 180000), 3));
1595 assertArrayEquals(
1596 goog.array.repeat(3, 180000),
1597 goog.array.flatten([goog.array.repeat(3, 180000)]));
1598 }
1599
1600 function testSortObjectsByKey() {
1601 var sortedArray = buildSortedObjectArray(4);
1602 var objects =
1603 [sortedArray[1], sortedArray[2], sortedArray[0], sortedArray[3]];
1604
1605 goog.array.sortObjectsByKey(objects, 'name');
1606 validateObjectArray(sortedArray, objects);
1607 }
1608
1609 function testSortObjectsByKeyWithCompareFunction() {
1610 var sortedArray = buildSortedObjectArray(4);
1611 var objects =
1612 [sortedArray[1], sortedArray[2], sortedArray[0], sortedArray[3]];
1613 var descSortedArray =
1614 [sortedArray[3], sortedArray[2], sortedArray[1], sortedArray[0]];
1615
1616 function descCompare(a, b) { return a < b ? 1 : a > b ? -1 : 0; };
1617
1618 goog.array.sortObjectsByKey(objects, 'name', descCompare);
1619 validateObjectArray(descSortedArray, objects);
1620 }
1621
1622 function buildSortedObjectArray(size) {
1623 var objectArray = [];
1624 for (var i = 0; i < size; i++) {
1625 objectArray.push({'name': 'name_' + i, 'id': 'id_' + (size - i)});
1626 }
1627
1628 return objectArray;
1629 }
1630
1631 function validateObjectArray(expected, actual) {
1632 assertEquals(expected.length, actual.length);
1633 for (var i = 0; i < expected.length; i++) {
1634 assertEquals(expected[i].name, actual[i].name);
1635 assertEquals(expected[i].id, actual[i].id);
1636 }
1637 }
1638
1639 function testIsSorted() {
1640 assertTrue(goog.array.isSorted([1, 2, 3]));
1641 assertTrue(goog.array.isSorted([1, 2, 2]));
1642 assertFalse(goog.array.isSorted([1, 2, 1]));
1643
1644 assertTrue(goog.array.isSorted([1, 2, 3], null, true));
1645 assertFalse(goog.array.isSorted([1, 2, 2], null, true));
1646 assertFalse(goog.array.isSorted([1, 2, 1], null, true));
1647
1648 function compare(a, b) { return b - a; }
1649
1650 assertFalse(goog.array.isSorted([1, 2, 3], compare));
1651 assertTrue(goog.array.isSorted([3, 2, 2], compare));
1652 }
1653
1654 function assertRotated(expect, array, rotate) {
1655 assertArrayEquals(expect, goog.array.rotate(array, rotate));
1656 }
1657
1658 function testRotate() {
1659 assertRotated([], [], 3);
1660 assertRotated([1], [1], 3);
1661 assertRotated([1, 2, 3, 4, 0], [0, 1, 2, 3, 4], -6);
1662 assertRotated([0, 1, 2, 3, 4], [0, 1, 2, 3, 4], -5);
1663 assertRotated([4, 0, 1, 2, 3], [0, 1, 2, 3, 4], -4);
1664 assertRotated([3, 4, 0, 1, 2], [0, 1, 2, 3, 4], -3);
1665 assertRotated([2, 3, 4, 0, 1], [0, 1, 2, 3, 4], -2);
1666 assertRotated([1, 2, 3, 4, 0], [0, 1, 2, 3, 4], -1);
1667 assertRotated([0, 1, 2, 3, 4], [0, 1, 2, 3, 4], 0);
1668 assertRotated([4, 0, 1, 2, 3], [0, 1, 2, 3, 4], 1);
1669 assertRotated([3, 4, 0, 1, 2], [0, 1, 2, 3, 4], 2);
1670 assertRotated([2, 3, 4, 0, 1], [0, 1, 2, 3, 4], 3);
1671 assertRotated([1, 2, 3, 4, 0], [0, 1, 2, 3, 4], 4);
1672 assertRotated([0, 1, 2, 3, 4], [0, 1, 2, 3, 4], 5);
1673 assertRotated([4, 0, 1, 2, 3], [0, 1, 2, 3, 4], 6);
1674 }
1675
1676 function testMoveItemWithArray() {
1677 var arr = [0, 1, 2, 3];
1678 goog.array.moveItem(arr, 1, 3); // toIndex > fromIndex
1679 assertArrayEquals([0, 2, 3, 1], arr);
1680 goog.array.moveItem(arr, 2, 0); // toIndex < fromIndex
1681 assertArrayEquals([3, 0, 2, 1], arr);
1682 goog.array.moveItem(arr, 1, 1); // toIndex == fromIndex
1683 assertArrayEquals([3, 0, 2, 1], arr);
1684 // Out-of-bounds indexes throw assertion errors.
1685 assertThrows(function() { goog.array.moveItem(arr, -1, 1); });
1686 assertThrows(function() { goog.array.moveItem(arr, 4, 1); });
1687 assertThrows(function() { goog.array.moveItem(arr, 1, -1); });
1688 assertThrows(function() { goog.array.moveItem(arr, 1, 4); });
1689 // The array should not be modified by the out-of-bound calls.
1690 assertArrayEquals([3, 0, 2, 1], arr);
1691 }
1692
1693 function testMoveItemWithArgumentsObject() {
1694 var f = function() {
1695 goog.array.moveItem(arguments, 0, 1);
1696 return arguments;
1697 };
1698 assertArrayEquals([1, 0], goog.array.toArray(f(0, 1)));
1699 }
1700
1701 function testConcat() {
1702 var a1 = [1, 2, 3];
1703 var a2 = [4, 5, 6];
1704 var a3 = goog.array.concat(a1, a2);
1705 a1.push(1);
1706 a2.push(5);
1707 assertArrayEquals([1, 2, 3, 4, 5, 6], a3);
1708 }
1709
1710 function testConcatWithNoSecondArg() {
1711 var a1 = [1, 2, 3, 4];
1712 var a2 = goog.array.concat(a1);
1713 a1.push(5);
1714 assertArrayEquals([1, 2, 3, 4], a2);
1715 }
1716
1717 function testConcatWithNonArrayArgs() {
1718 var a1 = [1, 2, 3, 4];
1719 var o = {0: 'a', 1: 'b', length: 2};
1720 var a2 = goog.array.concat(a1, 5, '10', o);
1721 assertArrayEquals([1, 2, 3, 4, 5, '10', o], a2);
1722 }
1723
1724 function testConcatWithNull() {
1725 var a1 = goog.array.concat(null, [1, 2, 3]);
1726 var a2 = goog.array.concat([1, 2, 3], null);
1727 assertArrayEquals([null, 1, 2, 3], a1);
1728 assertArrayEquals([1, 2, 3, null], a2);
1729 }
1730
1731 function testZip() {
1732 var a1 = goog.array.zip([1, 2, 3], [3, 2, 1]);
1733 var a2 = goog.array.zip([1, 2], [3, 2, 1]);
1734 var a3 = goog.array.zip();
1735 assertArrayEquals([[1, 3], [2, 2], [3, 1]], a1);
1736 assertArrayEquals([[1, 3], [2, 2]], a2);
1737 assertArrayEquals([], a3);
1738 }
1739
1740 function testShuffle() {
1741 // Test array. This array should have unique values for the purposes of this
1742 // test case.
1743 var testArray = [1, 2, 3, 4, 5];
1744 var testArrayCopy = goog.array.clone(testArray);
1745
1746 // Custom random function, which always returns a value approaching 1,
1747 // resulting in a "shuffle" that preserves the order of original array
1748 // (for array sizes that we work with here).
1749 var noChangeShuffleFunction = function() { return .999999; };
1750 goog.array.shuffle(testArray, noChangeShuffleFunction);
1751 assertArrayEquals(testArrayCopy, testArray);
1752
1753 // Custom random function, which always returns 0, resulting in a
1754 // deterministic "shuffle" that is predictable but differs from the
1755 // original order of the array.
1756 var testShuffleFunction = function() { return 0; };
1757 goog.array.shuffle(testArray, testShuffleFunction);
1758 assertArrayEquals([2, 3, 4, 5, 1], testArray);
1759
1760 // Test the use of a real random function(no optional RNG is specified).
1761 goog.array.shuffle(testArray);
1762
1763 // Ensure the shuffled array comprises the same elements (without regard to
1764 // order).
1765 assertSameElements(testArrayCopy, testArray);
1766 }
1767
1768 function testRemoveAllIf() {
1769 var testArray = [9, 1, 9, 2, 9, 3, 4, 9, 9, 9, 5];
1770 var expectedArray = [1, 2, 3, 4, 5];
1771
1772 var actualOutput =
1773 goog.array.removeAllIf(testArray, function(el) { return el == 9; });
1774
1775 assertEquals(6, actualOutput);
1776 assertArrayEquals(expectedArray, testArray);
1777 }
1778
1779 function testRemoveAllIf_noMatches() {
1780 var testArray = [1];
1781 var expectedArray = [1];
1782
1783 var actualOutput =
1784 goog.array.removeAllIf(testArray, function(el) { return false; });
1785
1786 assertEquals(0, actualOutput);
1787 assertArrayEquals(expectedArray, testArray);
1788 }
1789
1790 function testCopyByIndex() {
1791 var testArray = [1, 2, 'a', 'b', 'c', 'd'];
1792 var copyIndexes = [1, 3, 0, 0, 2];
1793 var expectedArray = [2, 'b', 1, 1, 'a'];
1794
1795 var actualOutput = goog.array.copyByIndex(testArray, copyIndexes);
1796
1797 assertArrayEquals(expectedArray, actualOutput);
1798 }
1799
1800 function testComparators() {
1801 var greater = 42;
1802 var smaller = 13;
1803
1804 assertTrue(goog.array.defaultCompare(smaller, greater) < 0);
1805 assertEquals(0, goog.array.defaultCompare(smaller, smaller));
1806 assertTrue(goog.array.defaultCompare(greater, smaller) > 0);
1807
1808 assertTrue(goog.array.inverseDefaultCompare(greater, smaller) < 0);
1809 assertEquals(0, goog.array.inverseDefaultCompare(greater, greater));
1810 assertTrue(goog.array.inverseDefaultCompare(smaller, greater) > 0);
1811 }
1812
1813 function testConcatMap() {
1814 var a = [0, 1, 2, 0];
1815 var context = {};
1816 var arraysToReturn = [['x', 'y', 'z'], [], ['a', 'b']];
1817 var timesCalled = 0;
1818 var result = goog.array.concatMap(a, function(val, index, a2) {
1819 assertEquals(a, a2);
1820 assertEquals(context, this);
1821 assertEquals(timesCalled++, index);
1822 assertEquals(a[index], val);
1823 return arraysToReturn[val];
1824 }, context);
1825 assertArrayEquals(['x', 'y', 'z', 'a', 'b', 'x', 'y', 'z'], result);
1826 }