/
Stream.spec.ts
305 lines (303 loc) · 8.99 KB
/
Stream.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import * as assert from 'assert';
import { Stream, stream } from '../../../src/stream/Stream';
describe('Stream', () => {
describe('constructor', () => {
it('Should have the same values as its underlying', () => {
function* g() {
yield* [1, 42, 350, 0];
}
let s = stream(g());
assert.deepEqual([...s], [1, 42, 350, 0]);
});
it('Should not be able to iterate twice', () => {
function* g() {
yield* [1, 42, 350, 0];
}
let s = stream(g());
assert.deepEqual([...s], [1, 42, 350, 0]);
assert.deepEqual([...s], []);
});
it('Should handle infinite generators', () => {
function* g() {
let idx = 0;
while (true) {
yield ++idx;
}
}
let s = stream(g());
let data = [];
for (let idx = 0; idx !== 5; ++idx) {
data.push(s.next().value);
}
assert.deepEqual(data, [1, 2, 3, 4, 5]);
});
});
describe('nil', () => {
it('Should instantiate an empty stream', () => {
let s: Stream<number> = Stream.nil<number>();
assert.deepEqual([...s], []);
});
});
describe('map', () => {
it('Should apply on each element', () => {
function* g() {
yield* [1, 2, 3, 5];
}
let s = stream(g()).map(v => v * v);
assert.deepEqual([...s], [1, 4, 9, 25]);
});
it('Should be able to perform conversions', () => {
function* g() {
yield* [1, 2, 3, 5];
}
let s: Stream<string> = stream(g()).map(v => String(v));
assert.deepEqual([...s], ['1', '2', '3', '5']);
});
});
describe('flatMap', () => {
it('Should apply on each element', () => {
function* g() {
yield* [1, 2, 3, 5];
}
function* expand(n: number) {
for (let idx = 0; idx !== n; ++idx) {
yield n;
}
}
let s = stream(g()).flatMap(expand);
assert.deepEqual([...s], [1, 2, 2, 3, 3, 3, 5, 5, 5, 5, 5]);
});
it('Should handle correctly empty iterables', () => {
function* g() {
yield* [1, 2, 3, 0, 5];
}
function* noexpand(n: number) {
if (n >= 3) {
yield n;
}
}
let s = stream(g()).flatMap(noexpand);
assert.deepEqual([...s], [3, 5]);
});
});
describe('drop', () => {
it('Should drop the right number of elements', () => {
function* g() {
yield* [1, 2, 3, 4, 5, 6];
}
let s = stream(g()).drop(2);
assert.deepEqual([...s], [3, 4, 5, 6]);
});
});
describe('dropWhile', () => {
it('Should drop while predicate stays valid', () => {
function* g() {
yield* [-4, -2, -3, 1, -8, 7];
}
let s = stream(g()).dropWhile(v => v < 0);
assert.deepEqual([...s], [1, -8, 7]);
});
it('Should drop everything', () => {
function* g() {
yield* [-4, -2, -3, 1, -8, 7];
}
let s = stream(g()).dropWhile(v => true);
assert.deepEqual([...s], []);
});
it('Should drop nothing', () => {
function* g() {
yield* [-4, -2, -3, 1, -8, 7];
}
let s = stream(g()).dropWhile(v => false);
assert.deepEqual([...s], [-4, -2, -3, 1, -8, 7]);
});
});
describe('take', () => {
it('Should take the right number of elements', () => {
function* g() {
yield* [1, 2, 3, 4, 5, 6];
}
let s = stream(g()).take(4);
assert.deepEqual([...s], [1, 2, 3, 4]);
});
});
describe('takeWhile', () => {
it('Should take while predicate stays valid', () => {
function* g() {
yield* [-4, -2, -3, 1, -8, 7];
}
let s = stream(g()).takeWhile(v => v < 0);
assert.deepEqual([...s], [-4, -2, -3]);
});
it('Should take everything', () => {
function* g() {
yield* [-4, -2, -3, 1, -8, 7];
}
let s = stream(g()).takeWhile(v => true);
assert.deepEqual([...s], [-4, -2, -3, 1, -8, 7]);
});
it('Should take nothing', () => {
function* g() {
yield* [-4, -2, -3, 1, -8, 7];
}
let s = stream(g()).takeWhile(v => false);
assert.deepEqual([...s], []);
});
});
describe('filter', () => {
it('Should remove undesirable values', () => {
function* g() {
yield* [1, 3, 4, 7, 8, 10, 1, 1, 3, 4, 4];
}
let s = stream(g()).filter(v => v % 2 === 0);
assert.deepEqual([...s], [4, 8, 10, 4, 4]);
});
});
describe('every', () => {
it('Should be true if all values are ok', () => {
function* g() {
yield* [1, 3, 4, 7, 8, 10, 1, 1, 3, 4, 4];
}
assert.ok(stream(g()).every(v => v > 0));
});
it('Should be true for empty streams', () => {
function* g() {
yield* [];
}
assert.ok(stream(g()).every(v => v > 0));
});
it('Should be false if it starts by a failing value', () => {
function* g() {
yield* [0, 1, 3, 4, 7, 8, 10, 1, 1, 3, 4, 4];
}
assert.equal(stream(g()).every(v => v > 0), false);
});
it('Should be false if it ends by a failing value', () => {
function* g() {
yield* [1, 3, 4, 7, 8, 10, 1, 1, 3, 4, 4, 0];
}
assert.equal(stream(g()).every(v => v > 0), false);
});
it('Should be false if it contains a failing value', () => {
function* g() {
yield* [1, 3, 4, 7, 8, 10, 0, 1, 1, 3, 4, 4];
}
assert.equal(stream(g()).every(v => v > 0), false);
});
});
describe('has', () => {
it('Should be true if one of the values is ok', () => {
function* g() {
yield* [1, 3, 4, 7, 8, 10, 1, 1, 3, 4, 4];
}
assert.deepStrictEqual(stream(g()).has(v => v > 9), [true, 10]);
});
it('Should be true if multiple values are ok', () => {
function* g() {
yield* [1, 3, 4, 7, 8, 10, 1, 1, 3, 4, 4];
}
assert.deepStrictEqual(stream(g()).has(v => v > 4), [true, 7]);
});
it('Should be false for empty streams', () => {
function* g() {
yield* [];
}
assert.deepStrictEqual(stream(g()).has(v => v > 0), [false, null]);
});
it('Should be false if no value is ok', () => {
function* g() {
yield* [-2, -4, 0];
}
assert.deepStrictEqual(stream(g()).has(v => v > 0), [false, null]);
});
});
describe('join', () => {
it('Should be able to join nothing', () => {
function* g() {
yield* [1, 2, 3, 4, 5];
}
let s = stream(g()).join();
assert.deepEqual([...s], [1, 2, 3, 4, 5]);
});
it('Should be able to join another iterable', () => {
function* g1() {
yield* [1, 2, 3, 4, 5];
}
function* g2() {
yield* [8, 9];
}
let s = stream(g1()).join(g2());
assert.deepEqual([...s], [1, 2, 3, 4, 5, 8, 9]);
});
it('Should be able to join multiple other streams', () => {
function* g1() {
yield* [1, 2, 3, 4, 5];
}
function* g2() {
yield* [8, 9];
}
let s = stream(g1()).join(g2(), g1());
assert.deepEqual([...s], [1, 2, 3, 4, 5, 8, 9, 1, 2, 3, 4, 5]);
});
it('Should be able to join multiple other streams while mapping the initial stream', () => {
function* g1() {
yield* [1, 2, 3, 4, 5];
}
function* g2() {
yield* [8, 9];
}
let s = stream(g1())
.map(v => 10 * v)
.join(g2(), g1());
assert.deepEqual([...s], [10, 20, 30, 40, 50, 8, 9, 1, 2, 3, 4, 5]);
});
it('Should be able to join infinite streams', () => {
function* g1() {
while (true) yield 1;
}
function* g2() {
while (true) yield 2;
}
let s = stream(g1())
.map(v => 10 * v)
.join(g2())
.take(5);
assert.deepEqual([...s], [10, 10, 10, 10, 10]);
});
it('Should be able to join on nil', () => {
function* g1() {
yield* [1, 2, 3, 4, 5];
}
let s = Stream.nil().join(g1());
assert.deepEqual([...s], [1, 2, 3, 4, 5]);
});
});
describe('getNthOrLast', () => {
it('Should return the nth value of the stream', () => {
function* g() {
yield* [42, 5, 43, 8, 19];
}
let v = stream(g()).getNthOrLast(2);
assert.deepEqual(v, 43);
});
it('Should return the last value if the stream is too small', () => {
function* g() {
yield* [42, 5, 43, 8, 19];
}
let v = stream(g()).getNthOrLast(20);
assert.deepEqual(v, 19);
});
it('Should be null for empty streams', () => {
let v = Stream.nil<number>().getNthOrLast(10);
assert.deepEqual(v, null);
});
it('Should be able to run on infinite streams', () => {
function* g() {
let idx = 0;
while (true) yield idx++;
}
let v = stream(g()).getNthOrLast(10);
assert.deepEqual(v, 10);
});
});
});