-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathround-linked-queue.test.js
181 lines (139 loc) · 5.87 KB
/
round-linked-queue.test.js
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
const chai = require("chai");
const expect = chai.expect;
const RoundLinkedQueue = require("./round-linked-queue");
describe("Round-Queue", () => {
describe("When creating an instance", () => {
it("Should properly set the maxLength property", () => {
const queueLength = 3;
const queue = new RoundLinkedQueue(queueLength);
expect(queue.maxLength).to.equal(queueLength);
});
it("Should initially set the length to zero", () => {
const queueLength = 3;
const queue = new RoundLinkedQueue(queueLength);
expect(queue.length).to.equal(0);
});
});
describe("When adding elements", () => {
it("Should add an element to an empty queue", () => {
const queue = new RoundLinkedQueue(3);
const originalLength = queue.length;
const elementToAdd = 1;
queue.add(elementToAdd);
// Element should've been added to the end of the queue
expect(queue.last).to.equal(elementToAdd);
// But since it is now the only element, it should also be the at beginning as well
expect(queue.first).to.equal(elementToAdd);
// Length should've been increased by 1
expect(queue.length).to.equal(originalLength + 1);
});
it("Should add an element to the end of a non-empty queue", () => {
const queue = new RoundLinkedQueue(3);
const previousElement = 1;
const elementToAdd = 2;
// Make the queue non-empty
queue.add(previousElement);
queue.add(elementToAdd);
// Element should've been added to the end of the queue
expect(queue.last).to.equal(elementToAdd, "last not properly set");
// But the first pointer must remain the first element added
expect(queue.first).to.equal(previousElement, "first not properly set");
// Length should've been increased by 2
expect(queue.length).to.equal(2, "length not properly set");
});
it("Should remove the first element and add the new element to the end of a full queue", () => {
const queue = new RoundLinkedQueue(3);
queue.add(1);
queue.add(2);
queue.add(3);
queue.add(4);
// Element should've been added to the end of the queue
expect(queue.last).to.equal(4, "last not properly set");
// The second element should've been shifted to the first position
expect(queue.first).to.equal(2, "first not properly set");
// Length should still be the same
expect(queue.length).to.equal(3, "length not properly set");
});
it("Should return the removed element from a full queue", () => {
const queue = new RoundLinkedQueue(3);
queue.add(1);
queue.add(2);
queue.add(3);
const result = queue.add(4);
expect(result).to.equal(1, "removed wrong element");
});
it("Should return undefined when the queue is not full", () => {
const queue = new RoundLinkedQueue(3);
const result = queue.add(1);
expect(result).to.equal(undefined, "should not return an element");
});
});
describe("When removing elements", () => {
it("Should remove the first element of a non-empty queue", () => {
const queue = new RoundLinkedQueue(3);
queue.add(1);
queue.add(2);
queue.add(3);
const lengthBefore = queue.length;
const result = queue.remove();
const lengthAfter = queue.length;
expect(lengthAfter).to.equal(lengthBefore - 1, "length should decrease by 1");
expect(result).to.equal(1, "first element should the one being removed");
expect(queue.first).to.equal(2, "should shift the second element to the head of the queue");
expect(queue.last).to.equal(3, "should not change the last element");
});
it("Should throw an error when the queue is empty", () => {
const queue = new RoundLinkedQueue(3);
expect(() => queue.remove()).to.throw("Cannot remove element from an empty queue");
});
});
describe("When accessing elements", () => {
it("Should throw a proper error when acessing the first element of an empty queue", () => {
const queue = new RoundLinkedQueue(3);
expect(() => queue.first).to.throw("Cannot access the first element of an empty queue");
});
it("Should throw a proper error when acessing the last element of an empty queue", () => {
const queue = new RoundLinkedQueue(3);
expect(() => queue.last).to.throw("Cannot access the last element of an empty queue");
});
});
describe("When interoperating with native Array", () => {
describe("When transforming into an Array", () => {
it("Can be converted to an array using the spread operator", () => {
const queue = new RoundLinkedQueue(3);
queue.add(1);
queue.add(2);
queue.add(3);
const result = [...queue];
expect(result).to.deep.equal([1, 2, 3]);
});
it("Can be converted to an array using the toArray method", () => {
const queue = new RoundLinkedQueue(3);
queue.add(1);
queue.add(2);
queue.add(3);
const result = queue.toArray();
expect(result).to.deep.equal([1, 2, 3]);
});
});
describe("When created from an Array", () => {
it("Should create an instance whose maxLength is the same as the length of the input array", () => {
const inputArray = [1, 2, 3];
const queue = RoundLinkedQueue.fromArray(inputArray);
expect(queue.maxLength).to.equal(inputArray.length);
// inputArray.forEach(el => {
// const removed = queue.remove();
// expect(removed).to.equal(el);
// });
});
it("Should preserve the order of the elements in the array when adding them to the queue", () => {
const inputArray = [1, 2, 3];
const queue = RoundLinkedQueue.fromArray(inputArray);
inputArray.forEach(el => {
const removed = queue.remove();
expect(removed).to.equal(el);
});
});
});
});
});