This repository was archived by the owner on Apr 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathqueue.test.ts
89 lines (85 loc) · 2.28 KB
/
queue.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
import { Queue } from "./queue";
describe("constructor", () => {
it("creates a queue of length 10", () => {
const q = new Queue(10);
expect(q.ids.length).toBe(0);
expect(q.maxSize).toBe(10);
});
it("creates a queue of unlimited length", () => {
const q = new Queue(-1);
expect(q.ids.length).toBe(0);
expect(q.maxSize).toBe(-1);
});
});
describe("getNextID", () => {
it("gets first ID", () => {
const q = new Queue(10);
const id = q.getNextID();
expect(id).toBe(0);
});
it("gets first and second ID", () => {
const q = new Queue(10);
const id1 = q.getNextID();
q.push();
const id2 = q.getNextID();
expect(id1).toBe(0);
expect(id2).toBe(1);
});
it("gets ID 1 in the middle", () => {
const q = new Queue(10);
q.push();
q.push();
q.push();
q.shift();
q.shift();
q.push();
const id = q.getNextID();
expect(id).toBe(1);
});
});
describe("push", () => {
it("once", () => {
const q = new Queue(10);
q.push();
expect(q.ids.length).toBe(1);
});
it("multiple times", () => {
const q = new Queue(10);
q.push();
q.push();
expect(q.ids.length).toBe(2);
});
it("too many times", () => {
const q = new Queue(2);
q.push();
q.push();
const f = () => q.push();
expect(f).toThrowError("queue reached its maximum size")
expect(q.ids.length).toBe(2);
});
});
describe("shift", () => {
it("once for queue of size 2", () => {
const q = new Queue(10);
q.push();
q.push();
q.shift();
expect(q.ids.length).toBe(1);
});
it("all in queue", () => {
const q = new Queue(10);
q.push();
q.push();
q.shift();
q.shift();
expect(q.ids.length).toBe(0);
});
it("too many times", () => {
const q = new Queue(10);
q.push();
q.shift();
const f = () => q.shift();
expect(f).toThrowError("cannot shift because queue is empty")
expect(q.ids.length).toBe(0);
});
});