Skip to content

Commit e75fa6c

Browse files
committed
Added queue functionality and unit test cases
1 parent 1e98f42 commit e75fa6c

File tree

6 files changed

+167
-17
lines changed

6 files changed

+167
-17
lines changed

karma.conf.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ module.exports = function (config) {
3434
// preprocess matching files before serving them to the browser
3535
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
3636
preprocessors: {
37-
'src/test-index.spec.js': ['webpack'],
3837
'src/**/*.js': ['webpack', 'coverage']
3938
},
4039

lib/data-structures.js

Lines changed: 50 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/data-structures.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/queue/queue.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const _queue = Symbol('queue');
2+
3+
export class Queue {
4+
constructor() {
5+
this[_queue] = [];
6+
}
7+
get queue() {
8+
return this[_queue];
9+
}
10+
enqueue(o) {
11+
this[_queue].unshift(o);
12+
}
13+
dequeue() {
14+
if (this.isEmpty()) {
15+
return new Error('The Queue is Empty');
16+
} else {
17+
this[_queue].pop();
18+
}
19+
}
20+
21+
size() {
22+
return this[_queue].length;
23+
}
24+
25+
isEmpty() {
26+
return this.size() === 0;
27+
}
28+
29+
top() {
30+
return !this.isEmpty() ? this[_queue][this.size() - 1] : new Error('The Queue is Empty');
31+
}
32+
33+
clear() {
34+
this[_queue].length = 0;
35+
}
36+
}

src/queue/queue.spec.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { Queue } from './queue';
2+
3+
describe('Queue', () => {
4+
let q;
5+
beforeEach(() => {
6+
q = new Queue;
7+
});
8+
9+
it('should create a new queue', () => {
10+
expect(q).toBeDefined();
11+
expect(q.size()).toBe(0);
12+
expect(q.isEmpty()).toBeTruthy();
13+
expect(q.queue).toEqual([]);
14+
});
15+
16+
it('should insert values in the queue', () => {
17+
q.enqueue('a');
18+
expect(q.size()).toBe(1);
19+
expect(q.isEmpty()).toBeFalsy();
20+
expect(q.queue).toEqual(['a']);
21+
22+
q.enqueue('b');
23+
expect(q.size()).toBe(2);
24+
expect(q.isEmpty()).toBeFalsy();
25+
expect(q.queue).toEqual(['b', 'a']);
26+
});
27+
28+
describe('Dequeue, Top and Clear functionality', () => {
29+
beforeEach(() => {
30+
q.enqueue('a');
31+
q.enqueue('b');
32+
q.enqueue('c');
33+
});
34+
35+
it('should dequeue value', () => {
36+
expect(q.size()).toBe(3);
37+
expect(q.queue).toEqual(['c', 'b', 'a']);
38+
39+
q.dequeue();
40+
expect(q.size()).toBe(2);
41+
expect(q.queue).toEqual(['c', 'b']);
42+
43+
q.dequeue();
44+
expect(q.size()).toBe(1);
45+
expect(q.queue).toEqual(['c']);
46+
47+
q.dequeue();
48+
expect(q.size()).toBe(0);
49+
expect(q.queue).toEqual([]);
50+
51+
expect(q.dequeue().message).toBe('The Queue is Empty')
52+
});
53+
54+
it('should remove all elements from the queue', () => {
55+
expect(q.size()).toBe(3);
56+
expect(q.queue).toEqual(['c', 'b', 'a']);
57+
58+
q.clear();
59+
expect(q.size()).toBe(0);
60+
expect(q.queue).toEqual([]);
61+
});
62+
63+
it('should return top element from the queue', () => {
64+
expect(q.size()).toBe(3);
65+
expect(q.queue).toEqual(['c', 'b', 'a']);
66+
expect(q.top()).toBe('a');
67+
});
68+
69+
it('should return error message The Queue is Empty when top is fetched from an empty queue', () => {
70+
expect(q.size()).toBe(3);
71+
72+
q.clear();
73+
expect(q.size()).toBe(0);
74+
75+
expect(q.top().message).toBe('The Queue is Empty');
76+
});
77+
});
78+
});

src/test-index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
require('./binary-search-tree/binary-search-tree.spec');
22
require('./graph/graph.spec');
3-
require('./graph/adj-list.spec');
3+
require('./queue/queue.spec');
4+
require('./graph/adj-list.spec');

0 commit comments

Comments
 (0)