1 | dojo.provide("dojox.lang.tests.fold"); |
---|
2 | |
---|
3 | dojo.require("dojox.lang.functional.fold"); |
---|
4 | dojo.require("dojox.lang.functional.scan"); |
---|
5 | dojo.require("dojox.lang.functional.curry"); |
---|
6 | dojo.require("dojox.lang.functional.sequence"); |
---|
7 | dojo.require("dojox.lang.functional.listcomp"); |
---|
8 | dojo.require("dojox.lang.functional.object"); |
---|
9 | |
---|
10 | (function(){ |
---|
11 | var df = dojox.lang.functional, a = df.arg, x = {a: 1, b: 2, c: 3}; |
---|
12 | |
---|
13 | var revArrayIter = function(array){ |
---|
14 | this.array = array; |
---|
15 | this.position = array.length - 1; |
---|
16 | }; |
---|
17 | dojo.extend(revArrayIter, { |
---|
18 | hasNext: df.lambda("this.position >= 0"), |
---|
19 | next: df.lambda("this.array[this.position--]") |
---|
20 | }); |
---|
21 | |
---|
22 | tests.register("dojox.lang.tests.fold", [ |
---|
23 | function testFoldl1(t){ t.assertEqual(df.foldl([1, 2, 3], "+", 0), 6); }, |
---|
24 | function testFoldl2(t){ t.assertEqual(df.foldl1([1, 2, 3], "*"), 6); }, |
---|
25 | function testFoldl3(t){ t.assertEqual(df.foldl1([1, 2, 3], "/"), 1/6); }, |
---|
26 | function testFoldl4(t){ t.assertEqual(df.foldl1([1, 2, 3], df.partial(Math.max, a, a)), 3); }, |
---|
27 | function testFoldl5(t){ t.assertEqual(df.foldl1([1, 2, 3], df.partial(Math.min, a, a)), 1); }, |
---|
28 | |
---|
29 | function testFoldlIter(t){ |
---|
30 | var iter = new revArrayIter([1, 2, 3]); |
---|
31 | t.assertEqual(df.foldl(iter, "+", 0), 6); |
---|
32 | }, |
---|
33 | function testFoldl1Iter(t){ |
---|
34 | var iter = new revArrayIter([1, 2, 3]); |
---|
35 | t.assertEqual(df.foldl1(iter, "/"), 3/2); |
---|
36 | }, |
---|
37 | |
---|
38 | function testFoldlObj(t){ t.assertEqual(df.foldl(x, "*", 2), 12); }, |
---|
39 | function testFoldl1Obj(t){ t.assertEqual(df.foldl1(x, "+"), 6); }, |
---|
40 | |
---|
41 | function testFoldr1(t){ t.assertEqual(df.foldr([1, 2, 3], "+", 0), 6); }, |
---|
42 | function testFoldr2(t){ t.assertEqual(df.foldr1([1, 2, 3], "*"), 6); }, |
---|
43 | function testFoldr3(t){ t.assertEqual(df.foldr1([1, 2, 3], "/"), 3/2); }, |
---|
44 | function testFoldr4(t){ t.assertEqual(df.foldr1([1, 2, 3], df.partial(Math.max, a, a)), 3); }, |
---|
45 | function testFoldr5(t){ t.assertEqual(df.foldr1([1, 2, 3], df.partial(Math.min, a, a)), 1); }, |
---|
46 | |
---|
47 | function testUnfold1(t){ |
---|
48 | // simulate df.repeat() |
---|
49 | t.assertEqual( |
---|
50 | df.repeat(10, "2*", 1), |
---|
51 | df.unfold("x[0] >= 10", "x[1]", "[x[0] + 1, 2 * x[1]]", [0, 1]) |
---|
52 | ); |
---|
53 | }, |
---|
54 | function testUnfold2(t){ |
---|
55 | // simulate df.until() |
---|
56 | t.assertEqual( |
---|
57 | df.until(">1024", "2*", 1), |
---|
58 | df.unfold(">1024", "x", "2*", 1) |
---|
59 | ); |
---|
60 | }, |
---|
61 | |
---|
62 | function testScanl1(t){ t.assertEqual(df.scanl([1, 2, 3], "+", 0), [0, 1, 3, 6]); }, |
---|
63 | function testScanl2(t){ t.assertEqual(df.scanl1([1, 2, 3], "*"), [1, 2, 6]); }, |
---|
64 | function testScanl3(t){ t.assertEqual(df.scanl1([1, 2, 3], df.partial(Math.max, a, a)), [1, 2, 3]); }, |
---|
65 | function testScanl4(t){ t.assertEqual(df.scanl1([1, 2, 3], df.partial(Math.min, a, a)), [1, 1, 1]); }, |
---|
66 | |
---|
67 | function testScanlIter(t){ |
---|
68 | var iter = new revArrayIter([1, 2, 3]); |
---|
69 | t.assertEqual(df.scanl(iter, "+", 0), [0, 3, 5, 6]); |
---|
70 | }, |
---|
71 | function testScanl1Iter(t){ |
---|
72 | var iter = new revArrayIter([1, 2, 3]); |
---|
73 | t.assertEqual(df.scanl1(iter, "*"), [3, 6, 6]); |
---|
74 | }, |
---|
75 | |
---|
76 | function testScanlObj(t){ t.assertEqual(df.scanl(x, "+", 0), df.scanl(df.values(x), "+", 0)); }, |
---|
77 | function testScanl1Obj(t){ t.assertEqual(df.scanl1(x, "*"), df.scanl1(df.values(x), "*")); }, |
---|
78 | |
---|
79 | function testScanr1(t){ t.assertEqual(df.scanr([1, 2, 3], "+", 0), [6, 5, 3, 0]); }, |
---|
80 | function testScanr2(t){ t.assertEqual(df.scanr1([1, 2, 3], "*"), [6, 6, 3]); }, |
---|
81 | function testScanr3(t){ t.assertEqual(df.scanr1([1, 2, 3], df.partial(Math.max, a, a)), [3, 3, 3]); }, |
---|
82 | function testScanr4(t){ t.assertEqual(df.scanr1([1, 2, 3], df.partial(Math.min, a, a)), [1, 2, 3]); } |
---|
83 | ]); |
---|
84 | })(); |
---|