-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
reduce_test.ts
44 lines (41 loc) · 1.45 KB
/
reduce_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
import { test } from "@cross/test";
import { assertEquals } from "@std/assert";
import { assertType, type IsExact } from "@std/testing/types";
import { reduce } from "./reduce.ts";
await test("reduce with initial", () => {
const accumulators: number[] = [];
const values: number[] = [];
const indices: number[] = [];
const result = reduce([1, 2, 3, 4, 5], (acc, value, index) => {
accumulators.push(acc);
values.push(value);
indices.push(index);
return acc + value;
}, 5);
assertEquals(result, 20);
assertEquals(accumulators, [5, 6, 8, 11, 15]);
assertEquals(values, [1, 2, 3, 4, 5]);
assertEquals(indices, [0, 1, 2, 3, 4]);
assertType<IsExact<typeof result, number>>(true);
});
await test("reduce without initial", () => {
const accumulators: number[] = [];
const values: number[] = [];
const indices: number[] = [];
const result = reduce([1, 2, 3, 4, 5], (acc, value, index) => {
accumulators.push(acc);
values.push(value);
indices.push(index);
return acc + value;
});
assertEquals(result, 15);
assertEquals(accumulators, [1, 3, 6, 10]);
assertEquals(values, [2, 3, 4, 5]);
assertEquals(indices, [1, 2, 3, 4]);
assertType<IsExact<typeof result, number | undefined>>(true);
});
await test("reduce without initial / with empty iterable", () => {
const result = reduce([] as number[], (acc, v) => acc + v);
assertEquals(result, undefined);
assertType<IsExact<typeof result, number | undefined>>(true);
});