-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
builtins.test.ts
369 lines (316 loc) · 9.86 KB
/
builtins.test.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
import { assertThrows } from "@std/assert";
import { describe, it } from "@std/testing/bdd";
import { expect } from "@std/expect";
import {
all,
any,
contains,
enumerate,
filter,
iter,
map,
max,
min,
range,
reduce,
sorted,
sum,
zip,
zip3,
} from "./builtins.ts";
import { first } from "./custom.ts";
const isEven = (n: number) => n % 2 === 0;
describe("all", () => {
it("all of empty list is true", () => {
expect(all([])).toBe(true);
});
it("all is true if all elements are truthy", () => {
expect(all([1])).toBe(true);
expect(all([1, 2, 3])).toBe(true);
});
it("all is false if any elements are not truthy", () => {
expect(all([0, 1])).toBe(false);
expect(all([1, 2, undefined, 3, 4])).toBe(false);
});
});
describe("any", () => {
it("any of empty list is false", () => {
expect(any([])).toBe(false);
});
it("any is true if any elements are truthy", () => {
expect(any([1, 2, 3])).toBe(true);
expect(any([0, 1])).toBe(true);
expect(any([1, 0])).toBe(true);
expect(any([0, undefined, NaN, 1])).toBe(true);
});
it("any is false if no elements are truthy", () => {
expect(any([0, null, NaN, undefined])).toBe(false);
});
});
describe("contains", () => {
it("contains of empty list is false", () => {
expect(contains([], 0)).toBe(false);
expect(contains([], 1)).toBe(false);
expect(contains([], null)).toBe(false);
expect(contains([], undefined)).toBe(false);
});
it("contains is true iff iterable contains the given exact value", () => {
expect(contains([1], 1)).toBe(true);
expect(contains([1], 2)).toBe(false);
expect(contains([1, 2, 3], 1)).toBe(true);
expect(contains([1, 2, 3], 2)).toBe(true);
expect(contains([1, 2, 3], 3)).toBe(true);
expect(contains([1, 2, 3], 4)).toBe(false);
});
it("contains does not work for elements with identity equality", () => {
expect(contains([{}], {})).toBe(false);
expect(contains([{ x: 123 }], { x: 123 })).toBe(false);
expect(contains([[1, 2, 3]], [1, 2, 3])).toBe(false);
});
});
describe("enumerate", () => {
it("enumerate empty list", () => {
expect(Array.from(enumerate([]))).toEqual([]);
});
it("enumerate attaches indexes", () => {
// We'll have to wrap it in a take() call to avoid infinite-length arrays :)
expect(Array.from(enumerate(["x"]))).toEqual([[0, "x"]]);
expect(Array.from(enumerate(["even", "odd"]))).toEqual([
[0, "even"],
[1, "odd"],
]);
});
it("enumerate from 3 up", () => {
expect(Array.from(enumerate("abc", 3))).toEqual([
[3, "a"],
[4, "b"],
[5, "c"],
]);
});
});
describe("filter", () => {
it("filters empty list", () => {
expect(filter([], isEven)).toEqual([]);
});
it("ifilter works like Array.filter, but lazy", () => {
expect(filter([0, 1, 2, 3], isEven)).toEqual([0, 2]);
});
});
describe("iter", () => {
it("iter makes any iterable a one-time iterable", () => {
expect(
Array.from(
iter(
new Map([
[1, "x"],
[2, "y"],
[3, "z"],
]),
),
),
).toEqual([
[1, "x"],
[2, "y"],
[3, "z"],
]);
expect(Array.from(iter([1, 2, 3]))).toEqual([1, 2, 3]);
expect(Array.from(iter(new Set([1, 2, 3])))).toEqual([1, 2, 3]);
});
it("iter results can be consumed only once", () => {
const it = iter([1, 2, 3]);
expect(it.next()).toEqual({ value: 1, done: false });
expect(it.next()).toEqual({ value: 2, done: false });
expect(it.next()).toEqual({ value: 3, done: false });
expect(it.next()).toEqual({ value: undefined, done: true });
// Keeps returning "done" when exhausted...
expect(it.next()).toEqual({ value: undefined, done: true });
expect(it.next()).toEqual({ value: undefined, done: true });
// ...
});
it("iter results can be consumed in pieces", () => {
const it = iter([1, 2, 3, 4, 5]);
expect(first(it)).toBe(1);
expect(first(it)).toBe(2);
expect(first(it)).toBe(3);
expect(Array.from(it)).toEqual([4, 5]);
// Keeps returning "[]" when exhausted...
expect(Array.from(it)).toEqual([]);
expect(Array.from(it)).toEqual([]);
// ...
});
it("wrapping iter()s has no effect on the iterator's state", () => {
const originalIter = iter([1, 2, 3, 4, 5]);
expect(first(originalIter)).toBe(1);
const wrappedIter = iter(iter(iter(originalIter)));
expect(first(wrappedIter)).toBe(2);
expect(first(iter(wrappedIter))).toBe(3);
expect(Array.from(iter(originalIter))).toEqual([4, 5]);
// Keeps returning "[]" when exhausted...
expect(Array.from(originalIter)).toEqual([]);
expect(Array.from(wrappedIter)).toEqual([]);
// ...
});
});
describe("map", () => {
it("map on empty iterable", () => {
expect(map([], (x) => x)).toEqual([]);
});
it("imap works like Array.map, but lazy", () => {
expect(map([1, 2, 3], (x) => x)).toEqual([1, 2, 3]);
expect(map([1, 2, 3], (x) => 2 * x)).toEqual([2, 4, 6]);
expect(map([1, 2, 3], (x) => x.toString())).toEqual(["1", "2", "3"]);
});
});
describe("max", () => {
it("can't take max of empty list", () => {
expect(max([])).toBeUndefined();
});
it("max of single-item array", () => {
expect(max([1])).toEqual(1);
expect(max([2])).toEqual(2);
expect(max([5])).toEqual(5);
});
it("max of multi-item array", () => {
expect(max([1, 2, 3])).toEqual(3);
expect(max([2, 2, 2, 2])).toEqual(2);
expect(max([5, 4, 3, 2, 1])).toEqual(5);
expect(max([-3, -2, -1])).toEqual(-1);
});
it("max of multi-item array with key function", () => {
expect(max([{ n: 2 }, { n: 3 }, { n: 1 }], (o) => o.n)).toEqual({ n: 3 });
});
});
describe("min", () => {
it("can't take min of empty list", () => {
expect(min([])).toBeUndefined();
});
it("min of single-item array", () => {
expect(min([1])).toEqual(1);
expect(min([2])).toEqual(2);
expect(min([5])).toEqual(5);
});
it("min of multi-item array", () => {
expect(min([1, 2, 3])).toEqual(1);
expect(min([2, 2, 2, 2])).toEqual(2);
expect(min([5, 4, 3, 2, 1])).toEqual(1);
expect(min([-3, -2, -1])).toEqual(-3);
});
it("min of multi-item array with key function", () => {
expect(min([{ n: 2 }, { n: 3 }, { n: 1 }], (o) => o.n)).toEqual({ n: 1 });
});
});
describe("range", () => {
it("range with end", () => {
expect(Array.from(range(0))).toEqual([]);
expect(Array.from(range(1))).toEqual([0]);
expect(Array.from(range(2))).toEqual([0, 1]);
expect(Array.from(range(5))).toEqual([0, 1, 2, 3, 4]);
expect(Array.from(range(-1))).toEqual([]);
});
it("range with start and end", () => {
expect(Array.from(range(3, 5))).toEqual([3, 4]);
expect(Array.from(range(4, 7))).toEqual([4, 5, 6]);
// If end < start, then range is empty
expect(Array.from(range(5, 1))).toEqual([]);
});
it("range with start, end, and step", () => {
expect(Array.from(range(3, 9, 3))).toEqual([3, 6]);
expect(Array.from(range(3, 10, 3))).toEqual([3, 6, 9]);
expect(Array.from(range(5, 1, -1))).toEqual([5, 4, 3, 2]);
expect(Array.from(range(5, -3, -2))).toEqual([5, 3, 1, -1]);
});
it("step cannot be 0", () => {
assertThrows(
() => {
range(0, 1, 0);
},
Error,
"range() arg 3 must not be zero",
);
});
});
describe("reduce", () => {
const adder = (x: number, y: number) => x + y;
it("reduce on empty list returns start value", () => {
expect(reduce([], adder, 0)).toEqual(0);
expect(reduce([], adder, 13)).toEqual(13);
});
it("reduce on list with only one item", () => {
expect(reduce([5], adder, 0)).toEqual(5);
expect(reduce([5], adder, 13)).toEqual(18);
});
it("reduce on list with multiple items", () => {
expect(reduce([1, 2, 3, 4], adder, 0)).toEqual(10);
expect(reduce([1, 2, 3, 4, 5], adder, 13)).toEqual(28);
});
});
describe("sorted", () => {
it("sorted w/ empty list", () => {
expect(sorted([])).toEqual([]);
});
it("sorted values", () => {
expect(sorted([1, 2, 3, 4, 5])).toEqual([1, 2, 3, 4, 5]);
expect(sorted([2, 1, 3, 4, 5])).toEqual([1, 2, 3, 4, 5]);
expect(sorted([5, 4, 3, 2, 1])).toEqual([1, 2, 3, 4, 5]);
// Explicitly test numeral ordering... in plain JS the following is true:
// [4, 44, 100, 80, 9].sort() ~~> [100, 4, 44, 80, 9]
expect(sorted([4, 44, 100, 80, 9])).toEqual([4, 9, 44, 80, 100]);
expect(sorted(["4", "44", "100", "80", "44", "9"])).toEqual([
"100",
"4",
"44",
"44",
"80",
"9",
]);
expect(sorted([false, true, true, false])).toEqual([
false,
false,
true,
true,
]);
});
it("sorted does not modify input", () => {
const values = [4, 0, -3, 7, 1];
expect(sorted(values)).toEqual([-3, 0, 1, 4, 7]);
expect(values).toEqual([4, 0, -3, 7, 1]);
});
it("sorted in reverse", () => {
expect(sorted([2, 1, 3, 4, 5], undefined, true)).toEqual([5, 4, 3, 2, 1]);
});
});
describe("sum", () => {
it("sum w/ empty iterable", () => {
expect(sum([])).toEqual(0);
});
it("sum works", () => {
expect(sum([1, 2, 3, 4, 5, 6])).toEqual(21);
expect(sum([-3, -2, -1, 0, 1, 2, 3])).toEqual(0);
// expect(sum([0.1, 0.2, 0.3])).toBeCloseTo(0.6);
});
});
describe("zip", () => {
it("zip with empty iterable", () => {
expect(zip([], [])).toEqual([]);
expect(zip("abc", [])).toEqual([]);
});
it("izip with two iterables", () => {
expect(zip("abc", "ABC")).toEqual([
["a", "A"],
["b", "B"],
["c", "C"],
]);
});
it("izip with three iterables", () => {
expect(zip3("abc", "ABC", [5, 4, 3])).toEqual([
["a", "A", 5],
["b", "B", 4],
["c", "C", 3],
]);
});
it("izip different input lengths", () => {
// Shortest lengthed input determines result length
expect(zip("a", "ABC")).toEqual([["a", "A"]]);
expect(zip3("", "ABCD", "PQR")).toEqual([]);
});
});