| 1 | if (typeof(dojo) != 'undefined') { dojo.require('MochiKit.Iter'); } |
| 2 | if (typeof(JSAN) != 'undefined') { JSAN.use('MochiKit.Iter'); } |
| 3 | if (typeof(tests) == 'undefined') { tests = {}; } |
| 4 | |
| 5 | tests.test_Iter = function (t) { |
| 6 | t.is( sum([1, 2, 3, 4, 5]), 15, "sum works on Arrays" ); |
| 7 | t.is( compare(list([1, 2, 3]), [1, 2, 3]), 0, "list([x]) == [x]" ); |
| 8 | t.is( compare(list(range(6, 0, -1)), [6, 5, 4, 3, 2, 1]), 0, "list(range(6, 0, -1)"); |
| 9 | t.is( compare(list(range(6)), [0, 1, 2, 3, 4, 5]), 0, "list(range(6))" ); |
| 10 | var moreThanTwo = partial(operator.lt, 2); |
| 11 | t.is( sum(ifilter(moreThanTwo, range(6))), 12, "sum(ifilter(, range()))" ); |
| 12 | t.is( sum(ifilterfalse(moreThanTwo, range(6))), 3, "sum(ifilterfalse(, range()))" ); |
| 13 | |
| 14 | var c = count(10); |
| 15 | t.is( compare([c.next(), c.next(), c.next()], [10, 11, 12]), 0, "count()" ); |
| 16 | c = cycle([1, 2]); |
| 17 | t.is( compare([c.next(), c.next(), c.next()], [1, 2, 1]), 0, "cycle()" ); |
| 18 | c = repeat("foo", 3); |
| 19 | t.is( compare(list(c), ["foo", "foo", "foo"]), 0, "repeat()" ); |
| 20 | c = izip([1, 2], [3, 4, 5], repeat("foo")); |
| 21 | t.is( compare(list(c), [[1, 3, "foo"], [2, 4, "foo"]]), 0, "izip()" ); |
| 22 | |
| 23 | t.is( compare(list(range(5)), [0, 1, 2, 3, 4]), 0, "range(x)" ); |
| 24 | c = islice(range(10), 0, 10, 2); |
| 25 | t.is( compare(list(c), [0, 2, 4, 6, 8]), 0, "islice(x, y, z)" ); |
| 26 | |
| 27 | c = imap(operator.add, [1, 2, 3], [2, 4, 6]); |
| 28 | t.is( compare(list(c), [3, 6, 9]), 0, "imap(fn, p, q)" ); |
| 29 | |
| 30 | c = filter(partial(operator.lt, 1), iter([1, 2, 3])); |
| 31 | t.is( compare(c, [2, 3]), 0, "filter(fn, iterable)" ); |
| 32 | |
| 33 | c = map(partial(operator.add, -1), iter([1, 2, 3])); |
| 34 | t.is( compare(c, [0, 1, 2]), 0, "map(fn, iterable)" ); |
| 35 | |
| 36 | c = map(operator.add, iter([1, 2, 3]), [2, 4, 6]); |
| 37 | t.is( compare(c, [3, 6, 9]), 0, "map(fn, iterable, q)" ); |
| 38 | |
| 39 | c = map(operator.add, iter([1, 2, 3]), iter([2, 4, 6])); |
| 40 | t.is( compare(c, [3, 6, 9]), 0, "map(fn, iterable, iterable)" ); |
| 41 | |
| 42 | c = applymap(operator.add, [[1, 2], [2, 4], [3, 6]]); |
| 43 | t.is( compare(list(c), [3, 6, 9]), 0, "applymap()" ); |
| 44 | |
| 45 | c = applymap(function (a) { return [this, a]; }, [[1], [2]], 1); |
| 46 | t.is( compare(list(c), [[1, 1], [1, 2]]), 0, "applymap(self)" ); |
| 47 | |
| 48 | c = chain(range(2), range(3)); |
| 49 | t.is( compare(list(c), [0, 1, 0, 1, 2]), 0, "chain(p, q)" ); |
| 50 | |
| 51 | var lessThanFive = partial(operator.gt, 5); |
| 52 | c = takewhile(lessThanFive, count()); |
| 53 | t.is( compare(list(c), [0, 1, 2, 3, 4]), 0, "takewhile()" ); |
| 54 | |
| 55 | c = dropwhile(lessThanFive, range(10)); |
| 56 | t.is( compare(list(c), [5, 6, 7, 8, 9]), 0, "dropwhile()" ); |
| 57 | |
| 58 | c = tee(range(5), 3); |
| 59 | t.is( compare(list(c[0]), list(c[1])), 0, "tee(..., 3) p0 == p1" ); |
| 60 | t.is( compare(list(c[2]), [0, 1, 2, 3, 4]), 0, "tee(..., 3) p2 == fixed" ); |
| 61 | |
| 62 | t.is( compare(reduce(operator.add, range(10)), 45), 0, "reduce(op.add)" ); |
| 63 | |
| 64 | try { |
| 65 | reduce(operator.add, []); |
| 66 | t.ok( false, "reduce didn't raise anything with empty list and no start?!" ); |
| 67 | } catch (e) { |
| 68 | if (e instanceof TypeError) { |
| 69 | t.ok( true, "reduce raised TypeError correctly" ); |
| 70 | } else { |
| 71 | t.ok( false, "reduce raised the wrong exception?" ); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | t.is( reduce(operator.add, [], 10), 10, "range initial value OK empty" ); |
| 76 | t.is( reduce(operator.add, [1], 10), 11, "range initial value OK populated" ); |
| 77 | |
| 78 | t.is( compare(iextend([1], range(2)), [1, 0, 1]), 0, "iextend(...)" ); |
| 79 | |
| 80 | var x = []; |
| 81 | exhaust(imap(bind(x.push, x), range(5))); |
| 82 | t.is( compare(x, [0, 1, 2, 3, 4]), 0, "exhaust(...)" ); |
| 83 | |
| 84 | t.is( every([1, 2, 3, 4, 5, 4], lessThanFive), false, "every false" ); |
| 85 | t.is( every([1, 2, 3, 4, 4], lessThanFive), true, "every true" ); |
| 86 | t.is( some([1, 2, 3, 4, 4], lessThanFive), true, "some true" ); |
| 87 | t.is( some([5, 6, 7, 8, 9], lessThanFive), false, "some false" ); |
| 88 | t.is( some([5, 6, 7, 8, 4], lessThanFive), true, "some true" ); |
| 89 | |
| 90 | var rval = []; |
| 91 | forEach(range(2), rval.push, rval); |
| 92 | t.is( compare(rval, [0, 1]), 0, "forEach works bound" ); |
| 93 | |
| 94 | function foo(o) { |
| 95 | rval.push(o); |
| 96 | } |
| 97 | forEach(range(2), foo); |
| 98 | t.is( compare(rval, [0, 1, 0, 1]), 0, "forEach works unbound" ); |
| 99 | |
| 100 | t.is( compare(sorted([3, 2, 1]), [1, 2, 3]), 0, "sorted default" ); |
| 101 | rval = sorted(["aaa", "bb", "c"], keyComparator("length")); |
| 102 | t.is(compare(rval, ["c", "bb", "aaa"]), 0, "sorted custom"); |
| 103 | |
| 104 | t.is( compare(reversed(range(4)), [3, 2, 1, 0]), 0, "reversed iterator" ); |
| 105 | t.is( compare(reversed([5, 6, 7]), [7, 6, 5]), 0, "reversed list" ); |
| 106 | |
| 107 | var o = {lst: [1, 2, 3], iterateNext: function () { return this.lst.shift(); }}; |
| 108 | t.is( compare(list(o), [1, 2, 3]), 0, "iterateNext" ); |
| 109 | |
| 110 | |
| 111 | function except(exc, func) { |
| 112 | try { |
| 113 | func(); |
| 114 | t.ok(false, exc.name + " was not raised."); |
| 115 | } catch (e) { |
| 116 | if (e == exc) { |
| 117 | t.ok( true, "raised " + exc.name + " correctly" ); |
| 118 | } else { |
| 119 | t.ok( false, "raised the wrong exception?" ); |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | odd = partial(operator.and, 1) |
| 125 | |
| 126 | // empty |
| 127 | grouped = groupby([]); |
| 128 | except(StopIteration, grouped.next); |
| 129 | |
| 130 | // exhaust sub-iterator |
| 131 | grouped = groupby([2,4,6,7], odd); |
| 132 | kv = grouped.next(); k = kv[0], subiter = kv[1]; |
| 133 | t.is(k, 0, "odd(2) = odd(4) = odd(6) == 0"); |
| 134 | t.is(subiter.next(), 2, "sub-iterator.next() == 2"); |
| 135 | t.is(subiter.next(), 4, "sub-iterator.next() == 4"); |
| 136 | t.is(subiter.next(), 6, "sub-iterator.next() == 6"); |
| 137 | except(StopIteration, subiter.next); |
| 138 | kv = grouped.next(); key = kv[0], subiter = kv[1]; |
| 139 | t.is(key, 1, "odd(7) == 1"); |
| 140 | t.is(subiter.next(), 7, "sub-iterator.next() == 7"); |
| 141 | except(StopIteration, subiter.next); |
| 142 | |
| 143 | // not consume sub-iterator |
| 144 | grouped = groupby([2,4,6,7], odd); |
| 145 | kv = grouped.next(); key = kv[0], subiter = kv[1]; |
| 146 | t.is(key, 0, "0 = odd(2) = odd(4) = odd(6)"); |
| 147 | kv = grouped.next(); key = kv[0], subiter = kv[1]; |
| 148 | t.is(key, 1, "1 = odd(7)"); |
| 149 | except(StopIteration, grouped.next); |
| 150 | |
| 151 | // consume sub-iterator partially |
| 152 | grouped = groupby([3,1,1,2], odd); |
| 153 | kv = grouped.next(); key = kv[0], subiter = kv[1]; |
| 154 | t.is(key, 1, "odd(1) == 1"); |
| 155 | t.is(subiter.next(), 3, "sub-iterator.next() == 3"); |
| 156 | kv = grouped.next(); key = kv[0], v = kv[1]; |
| 157 | t.is(key, 0, "skip (1,1), odd(2) == 0"); |
| 158 | except(StopIteration, grouped.next); |
| 159 | |
| 160 | // null |
| 161 | grouped = groupby([null,null]); |
| 162 | kv = grouped.next(); k = kv[0], v = kv[1]; |
| 163 | t.is(k, null, "null ok"); |
| 164 | |
| 165 | // groupby - array version |
| 166 | isEqual = (t.isDeeply || function (a, b, msg) { |
| 167 | return t.ok(compare(a, b) == 0, msg); |
| 168 | }); |
| 169 | isEqual(groupby_as_array([ ] ), [ ], "empty"); |
| 170 | isEqual(groupby_as_array([1,1,1]), [ [1,[1,1,1]] ], "[1,1,1]: [1,1,1]"); |
| 171 | isEqual(groupby_as_array([1,2,2]), [ [1,[1] ], [2,[2,2]] ], "[1,2,2]: [1], [2,2]"); |
| 172 | isEqual(groupby_as_array([1,1,2]), [ [1,[1,1] ], [2,[2 ]] ], "[1,1,2]: [1,1], [2]"); |
| 173 | isEqual(groupby_as_array([null,null] ), [ [null,[null,null]] ], "[null,null]: [null,null]"); |
| 174 | grouped = groupby_as_array([1,1,3,2,4,6,8], odd); |
| 175 | isEqual(grouped, [[1, [1,1,3]], [0,[2,4,6,8]]], "[1,1,3,2,4,6,7] odd: [1,1,3], [2,4,6,8]"); |
| 176 | }; |