-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
count_test.ts
70 lines (60 loc) · 2.37 KB
/
count_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
import { test } from "@cross/test";
import { assertEquals, assertThrows } from "@std/assert";
import { assertType, type IsExact } from "@std/testing/types";
import { take } from "./take.ts";
import { count } from "./count.ts";
await test("count default", () => {
const result = count();
const expected = [0, 1, 2];
assertEquals(Array.from(take(result, 3)), expected);
assertType<IsExact<typeof result, Iterable<number>>>(true);
});
await test("count with positive start", () => {
const result = count(1);
const expected = [1, 2, 3];
assertEquals(Array.from(take(result, 3)), expected);
assertType<IsExact<typeof result, Iterable<number>>>(true);
});
await test("count with negative start", () => {
const result = count(-1);
const expected = [-1, 0, 1];
assertEquals(Array.from(take(result, 3)), expected);
assertType<IsExact<typeof result, Iterable<number>>>(true);
});
await test("count with float start", () => {
const result = count(1.1);
const expected = [1.1, 2.1, 3.1];
assertEquals(Array.from(take(result, 3)), expected);
assertType<IsExact<typeof result, Iterable<number>>>(true);
});
await test("count with start and positive step", () => {
const result = count(1, 2);
const expected = [1, 3, 5];
assertEquals(Array.from(take(result, 3)), expected);
assertType<IsExact<typeof result, Iterable<number>>>(true);
});
await test("count with start and negative step", () => {
const result = count(1, -1);
const expected = [1, 0, -1];
assertEquals(Array.from(take(result, 3)), expected);
assertType<IsExact<typeof result, Iterable<number>>>(true);
});
await test("count with start and float step", () => {
const result = count(1, 0.2);
const expected = [1.0, 1.2, 1.4];
assertEquals(Array.from(take(result, 3)), expected);
assertType<IsExact<typeof result, Iterable<number>>>(true);
});
await test("count throws RangeError if the start is not finite", () => {
assertThrows(() => count(NaN), RangeError);
assertThrows(() => count(Infinity), RangeError);
assertThrows(() => count(-Infinity), RangeError);
});
await test("count throws RangeError if the step is not finite", () => {
assertThrows(() => count(0, NaN), RangeError);
assertThrows(() => count(0, Infinity), RangeError);
assertThrows(() => count(0, -Infinity), RangeError);
});
await test("count throws RangeError if the step is 0", () => {
assertThrows(() => count(0, 0), RangeError);
});