1 | dojo.provide("dojox.lang.tests.array"); |
---|
2 | |
---|
3 | dojo.require("dojox.lang.functional"); |
---|
4 | dojo.require("dojox.lang.functional.fold"); |
---|
5 | dojo.require("dojox.lang.functional.reversed"); |
---|
6 | |
---|
7 | (function(){ |
---|
8 | var df = dojox.lang.functional, v, isOdd = "%2", x = {a: 1, b: 2, c: 3}; |
---|
9 | |
---|
10 | var revArrayIter = function(array){ |
---|
11 | this.array = array; |
---|
12 | this.position = array.length - 1; |
---|
13 | }; |
---|
14 | dojo.extend(revArrayIter, { |
---|
15 | hasNext: df.lambda("this.position >= 0"), |
---|
16 | next: df.lambda("this.array[this.position--]") |
---|
17 | }); |
---|
18 | |
---|
19 | tests.register("dojox.lang.tests.array", [ |
---|
20 | function testFilter1(t){ t.assertEqual(df.filter([1, 2, 3], isOdd), [1, 3]); }, |
---|
21 | function testFilter2(t){ t.assertEqual(df.filter([1, 2, 3], "%2==0"), [2]); }, |
---|
22 | function testFilterIter(t){ |
---|
23 | var iter = new revArrayIter([1, 2, 3]); |
---|
24 | t.assertEqual(df.filter(iter, isOdd), [3, 1]); |
---|
25 | }, |
---|
26 | function testFilterRev(t){ |
---|
27 | var iter = new revArrayIter([1, 2, 3]); |
---|
28 | t.assertEqual(df.filter(iter, isOdd), df.filterRev([1, 2, 3], isOdd)); |
---|
29 | }, |
---|
30 | |
---|
31 | function testForEach(t){ |
---|
32 | t.assertEqual((v = [], df.forEach([1, 2, 3], function(x){ v.push(x); }), v), [1, 2, 3]); |
---|
33 | }, |
---|
34 | function testForEachIter(t){ |
---|
35 | var iter = new revArrayIter([1, 2, 3]); |
---|
36 | t.assertEqual((v = [], df.forEach(iter, function(x){ v.push(x); }), v), [3, 2, 1]); |
---|
37 | }, |
---|
38 | function testForEachRev(t){ |
---|
39 | t.assertEqual((v = [], df.forEachRev([1, 2, 3], function(x){ v.push(x); }), v), [3, 2, 1]); |
---|
40 | }, |
---|
41 | |
---|
42 | function testMap(t){ t.assertEqual(df.map([1, 2, 3], "+3"), [4, 5, 6]); }, |
---|
43 | function testMapIter(t){ |
---|
44 | var iter = new revArrayIter([1, 2, 3]); |
---|
45 | t.assertEqual(df.map(iter, "+3"), [6, 5, 4]); |
---|
46 | }, |
---|
47 | function testMapRev(t){ |
---|
48 | var iter = new revArrayIter([1, 2, 3]); |
---|
49 | t.assertEqual(df.map(iter, "+3"), df.mapRev([1, 2, 3], "+3")); |
---|
50 | }, |
---|
51 | |
---|
52 | function testEvery1(t){ t.assertFalse(df.every([1, 2, 3], isOdd)); }, |
---|
53 | function testEvery2(t){ t.assertTrue(df.every([1, 3, 5], isOdd)); }, |
---|
54 | function testEveryIter(t){ |
---|
55 | var iter = new revArrayIter([1, 3, 5]); |
---|
56 | t.assertTrue(df.every(iter, isOdd)); |
---|
57 | }, |
---|
58 | function testEveryObj(t){ t.assertFalse(df.every(x, "%2")); }, |
---|
59 | function testEveryRev1(t){ t.assertFalse(df.everyRev([1, 2, 3], isOdd)); }, |
---|
60 | function testEveryRev2(t){ t.assertTrue(df.everyRev([1, 3, 5], isOdd)); }, |
---|
61 | |
---|
62 | function testSome1(t){ t.assertFalse(df.some([2, 4, 6], isOdd)); }, |
---|
63 | function testSome2(t){ t.assertTrue(df.some([1, 2, 3], isOdd)); }, |
---|
64 | function testSomeIter(t){ |
---|
65 | var iter = new revArrayIter([1, 2, 3]); |
---|
66 | t.assertTrue(df.some(iter, isOdd)); |
---|
67 | }, |
---|
68 | function testSomeObj(t){ t.assertTrue(df.some(x, "%2")); }, |
---|
69 | function testSomeRev1(t){ t.assertFalse(df.someRev([2, 4, 6], isOdd)); }, |
---|
70 | function testSomeRev2(t){ t.assertTrue(df.someRev([1, 2, 3], isOdd)); }, |
---|
71 | |
---|
72 | function testReduce1(t){ t.assertEqual(df.reduce([4, 2, 1], "x-y"), 1); }, |
---|
73 | function testReduce2(t){ t.assertEqual(df.reduce([4, 2, 1], "x-y", 8), 1); }, |
---|
74 | function testReduceIter(t){ |
---|
75 | var iter = new revArrayIter([1, 2, 4]); |
---|
76 | t.assertEqual(df.reduce(iter, "x-y"), 1); |
---|
77 | }, |
---|
78 | |
---|
79 | function testReduceRight1(t){ t.assertEqual(df.reduceRight([4, 2, 1], "x-y"), -5); }, |
---|
80 | function testReduceRight2(t){ t.assertEqual(df.reduceRight([4, 2, 1], "x-y", 8), 1); } |
---|
81 | ]); |
---|
82 | })(); |
---|