-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiter_utils_test.ts
141 lines (120 loc) · 2.85 KB
/
iter_utils_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
// Copyright 2023-latest Tomoki Miyauchi. All rights reserved. MIT license.
import {
count,
enumerate,
isPositiveInteger,
iter,
iterIter,
map,
take,
} from "./iter_utils.ts";
import {
assert,
assertEquals,
assertFalse,
assertSpyCalls,
assertThrows,
describe,
it,
spy,
} from "./_dev_deps.ts";
describe("isPositiveInteger", () => {
it("should return true", () => {
const table: number[] = [
1,
Number.MAX_SAFE_INTEGER,
];
table.forEach((input) => {
assert(isPositiveInteger(input));
});
});
it("should return false", () => {
const table: number[] = [
NaN,
0,
-0,
Infinity,
-Infinity,
-1,
1.1,
Number.MIN_SAFE_INTEGER,
];
table.forEach((input) => {
assertFalse(isPositiveInteger(input));
});
});
});
describe("take", () => {
it("should yield all value by default", () => {
function* gen() {
yield* [1, 2, 3];
}
assertEquals([...take(gen())], [1, 2, 3]);
});
it("should take max number of limit", () => {
function* gen() {
yield* [1, 2, 3];
}
assertEquals([...take(gen(), 1)], [1]);
assertEquals([...take(gen(), 2)], [1, 2]);
assertEquals([...take(gen(), 10)], [1, 2, 3]);
});
it("should throw error if the limit is not positive number", () => {
assertThrows(() => [...take([], -1)]);
assertThrows(() => [...take([], 0)]);
assertThrows(() => [...take([], 1.1)]);
assertThrows(() => [...take([], NaN)]);
});
});
describe("map", () => {
it("should return mapped iterable", () => {
assertEquals([...map([1, 2, 3], (number) => number ** number)], [1, 4, 27]);
});
it("should not call map until yield", () => {
const mapper = spy(() => 1);
const iterable = map([1, 2, 3], mapper);
assertSpyCalls(mapper, 0);
[...iterable];
assertSpyCalls(mapper, 3);
});
});
describe("iter", () => {
it("should return iterator", () => {
function* gen() {
yield 1;
}
const iterable = gen();
assertEquals(iter(iterable), iterable[Symbol.iterator]());
});
});
describe("iterIter", () => {
it("should return iterable iterator", () => {
function* gen() {
yield 1;
yield 2;
yield 3;
yield 4;
}
const iterable = iterIter(gen());
assertEquals(iterable.next().value, 1);
assertEquals(iterable.next().value, 2);
assertEquals([...iterable], [3, 4]);
});
});
describe("count", () => {
it("should return number of yielded", () => {
assertEquals(count([1, 2, 3]), 3);
function* gen() {
yield 1;
yield 2;
yield 3;
}
assertEquals(count(gen()), 3);
});
});
describe("enumerate", () => {
it("should return entries", () => {
assertEquals([...enumerate([1, 2, 3])], [[0, 1], [1, 2], [2, 3]]);
assertEquals([...enumerate([1, 2, 3], 4)], [[4, 1], [5, 2], [6, 3]]);
});
});