Skip to content

Commit ed10960

Browse files
committed
Merge branch 'tests'
2 parents f14d9b8 + db75928 commit ed10960

File tree

1 file changed

+198
-0
lines changed

1 file changed

+198
-0
lines changed

test/test.js

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
var itertools = require('../itertools');
2+
var _Iterator = itertools.Iterator;
3+
var _List = itertools.Array;
4+
var assert = require('assert');
5+
6+
var assertIteratorResult = function (list, iterator) {
7+
var _list = _List(iterator);
8+
return assert.deepEqual(list, _list, list + ' != ' + _list);
9+
}
10+
11+
describe("creation", function () {
12+
it('should work with Arrays', function () {
13+
assertIteratorResult([1,2,3,4,5,6,7,8,9], _Iterator([1,2,3,4,5,6,7,8,9]));
14+
});
15+
it('should work with empty Arrays', function () {
16+
assertIteratorResult([], _Iterator([]));
17+
});
18+
it('should work with Strings', function () {
19+
assertIteratorResult(['A','B','C','D','E','F','G'], _Iterator('ABCDEFG'));
20+
});
21+
it('should work with Objects', function () {
22+
assertIteratorResult([['one', 1], ['two', 2], ['three', 3]], _Iterator({one: 1, two: 2, three: 3}));
23+
});
24+
it('should work with Iterators', function () {
25+
var iterator = _Iterator([1,2,3,4,5,6,7,8,9]);
26+
assert.ok(iterator === _Iterator(iterator));
27+
});
28+
});
29+
30+
describe("count", function () {
31+
it('should work with negative to positive', function () {
32+
assertIteratorResult([-10,-8,-6,-4,-2,0,2,4,6,8,10], itertools.slice(itertools.count(-10, 2), 11));
33+
});
34+
it('should work with positive to negative', function () {
35+
assertIteratorResult([10,8,6,4,2,0,-2,-4,-6,-8,-10], itertools.slice(itertools.count(10, -2), 11));
36+
});
37+
it('should work with positive to zero', function () {
38+
assertIteratorResult([10,10,10,10,10,10,10,10,10,10], itertools.slice(itertools.count(10, 0), 10));
39+
});
40+
});
41+
42+
describe("cycle", function () {
43+
it('should work with Arrays', function () {
44+
assertIteratorResult([1,2,3,4,5,1,2,3,4,5,1,2,3,4,5], itertools.slice(itertools.cycle(itertools.slice(itertools.count(1, 1), 5)), 15));
45+
});
46+
it('should work with empty Arrays', function () {
47+
assertIteratorResult([], itertools.slice(itertools.cycle([]), 10));
48+
});
49+
});
50+
51+
describe("repeat", function () {
52+
it('should work', function () {
53+
assertIteratorResult([10,10,10,10,10], itertools.repeat(10, 5));
54+
assertIteratorResult([10,10,10,10,10,10,10,10,10,10], itertools.slice(itertools.repeat(10), 10));
55+
assertIteratorResult([10,10,10,10,10,10,10,10,10,10], itertools.slice(itertools.repeat(10, 'NONSENSE'), 10));
56+
});
57+
});
58+
59+
describe("chain", function () {
60+
it('should work', function () {
61+
assertIteratorResult(['A','B','C','D',1,2,3,4,'RED','GREEN','BLUE'], itertools.chain(['A','B','C','D'],[1,2,3,4],['RED','GREEN','BLUE']));
62+
});
63+
it('should work with empty', function () {
64+
assertIteratorResult([], itertools.chain());
65+
});
66+
});
67+
68+
describe("dropWhile", function () {
69+
it('should work with default test', function () {
70+
assertIteratorResult([0,1,2,3,4,5,0,1,2,3,4,5], itertools.dropWhile(null, itertools.chain(itertools.repeat(1, 5), [0,1,2,3,4,5,0,1,2,3,4,5])));
71+
});
72+
it('should work with custom test', function () {
73+
assertIteratorResult([5,0,1,2,3,4,5], itertools.dropWhile(function (x) {return x < 5}, itertools.chain(itertools.repeat(1, 5), [0,1,2,3,4,5,0,1,2,3,4,5])));
74+
});
75+
});
76+
77+
describe("takeWhile", function () {
78+
it('should work with default test', function () {
79+
assertIteratorResult([1,1,1,1,1,1,2,3,4,5], itertools.takeWhile(null, itertools.chain(itertools.repeat(1, 5), [1,2,3,4,5,0,1,2,3,4,5,0])));
80+
});
81+
it('should work with custom test', function () {
82+
assertIteratorResult([1,1,1,1,1,0,1,2,3], itertools.takeWhile(function (x) {return x < 4}, itertools.chain(itertools.repeat(1, 5), [0,1,2,3,4,5,0,1,2,3,4,5])));
83+
});
84+
});
85+
86+
describe("filter", function () {
87+
it('should work with default test', function () {
88+
assertIteratorResult([1,2,3,4,5], itertools.filter(null, [0,1,0,2,0,3,0,4,0,5]));
89+
});
90+
it('should work with custom test', function () {
91+
assertIteratorResult([2,4,2,4,2], itertools.slice(itertools.filter(function (x) {return x == 2 || x == 4;}, itertools.cycle([1,2,3,4,5])), 5));
92+
});
93+
});
94+
95+
describe("slice", function () {
96+
it('should work with start index', function () {
97+
assertIteratorResult([0,1,2,3,4], itertools.slice([0,1,2,3,4,5,6,7,8,9], 5));
98+
});
99+
it('should work with zero start index', function () {
100+
assertIteratorResult([], itertools.slice([0,1,2,3,4,5,6,7,8,9], 0));
101+
});
102+
it('should work with zero range', function () {
103+
assertIteratorResult([], itertools.slice([0,1,2,3,4,5,6,7,8,9], 5, 5));
104+
});
105+
it('should work with positive range', function () {
106+
assertIteratorResult([5], itertools.slice([0,1,2,3,4,5,6,7,8,9], 5, 6));
107+
});
108+
it('should work with negative range', function () {
109+
assertIteratorResult([], itertools.slice([0,1,2,3,4,5,6,7,8,9], 5, 4));
110+
});
111+
it('should work with over overflowing range', function () {
112+
assertIteratorResult([5,6,7,8,9], itertools.slice([0,1,2,3,4,5,6,7,8,9], 5, 15));
113+
});
114+
it('should work with step', function () {
115+
assertIteratorResult([0,2,4], itertools.slice([0,1,2,3,4,5,6,7,8,9], 0, 5, 2));
116+
});
117+
it('should work with overflowing step', function () {
118+
assertIteratorResult([5], itertools.slice([0,1,2,3,4,5,6,7,8,9], 5, 6, 100));
119+
});
120+
it('should require positive step', function () {
121+
assert.throws(function () {
122+
itertools.slice([0,1,2,3,4,5,6,7,8,9], 5, 6, -1);
123+
}, Error);
124+
});
125+
it('should require positive range', function () {
126+
assert.throws(function () {
127+
itertools.slice([0,1,2,3,4,5,6,7,8,9], 5, -1, 5);
128+
}, Error);
129+
});
130+
});
131+
132+
describe("filter", function () {
133+
it('should work', function () {
134+
assertIteratorResult([[0,1,2,3,4,5],[0,1,2,3,4,5],[0,1,2,3,4,5]], itertools.map(function (x) {return _List(x)}, itertools.tee([0,1,2,3,4,5], 3)));
135+
});
136+
it('should work with empty', function () {
137+
assertIteratorResult([[],[],[]], itertools.map(function (x) {return _List(x)}, itertools.tee([], 3)));
138+
});
139+
it('should work with unspecified number', function () {
140+
assertIteratorResult([[0,1,2,3,4,5],[0,1,2,3,4,5]], itertools.map(function (x) {return _List(x)}, itertools.tee([0,1,2,3,4,5])));
141+
});
142+
});
143+
144+
describe("zip", function () {
145+
it('should work', function () {
146+
assertIteratorResult([[0,1,2],[1,2,3],[2,3,4],[3,4,5],[4,5,6]], itertools.zip([0,1,2,3,4],[1,2,3,4,5],[2,3,4,5,6]));
147+
});
148+
it('should work with empty', function () {
149+
assertIteratorResult([], itertools.zip([],[1,2,3,4,5],[]));
150+
});
151+
});
152+
153+
describe("zipLongest", function () {
154+
it('should work', function () {
155+
assertIteratorResult([[0,1,2],[1,2,3],[2,3,4],[3,4,5],[4,5,6]], itertools.zipLongest([0,1,2,3,4],[1,2,3,4,5],[2,3,4,5,6]));
156+
});
157+
it('should work with empty', function () {
158+
assertIteratorResult([[undefined,1,undefined],[undefined,2,undefined],[undefined,3,undefined],[undefined,4,undefined],[undefined,5,undefined]], itertools.zipLongest([],[1,2,3,4,5],[]));
159+
});
160+
});
161+
162+
describe("roundrobin", function () {
163+
it('should work', function () {
164+
assertIteratorResult([0,1,2,3,4,5,6,7,8,9], itertools.roundrobin([0,3,5,7],[1,4,6,8,9],[2]));
165+
});
166+
});
167+
168+
describe("all", function () {
169+
it('should work', function () {
170+
assert.ok(false === itertools.all([0,1,0,0]));
171+
assert.ok(true === itertools.all([1,1,1,1]));
172+
});
173+
});
174+
175+
describe("some", function () {
176+
it('should work', function () {
177+
assert.ok(false === itertools.some([0,1,0,0], 2));
178+
assert.ok(true === itertools.some([0,1,1,0], 2));
179+
assert.ok(true === itertools.some([1,1,1,1], 2));
180+
181+
});
182+
});
183+
184+
describe("any", function () {
185+
it('should work', function () {
186+
assert.ok(true === itertools.any([0,1,0,0]));
187+
assert.ok(false === itertools.any([0,0,0,0]));
188+
});
189+
});
190+
191+
describe("reduce", function () {
192+
it('should use provided initial', function () {
193+
assert.ok('(((((0+1)+2)+3)+4)+5)' === itertools.reduce(function (x,y) {return '('+x+'+'+y+')'}, [1,2,3,4,5], 0));
194+
});
195+
it('should use first valueu for initial', function () {
196+
assert.ok('((((1+2)+3)+4)+5)' === itertools.reduce(function (x,y) {return '('+x+'+'+y+')'}, [1,2,3,4,5]));
197+
});
198+
});

0 commit comments

Comments
 (0)