Skip to content

Commit a37062e

Browse files
authored
Merge pull request knaxus#142 from knaxus/codeCoverage
Increase code coverage
2 parents 1ecf0de + 980dcf9 commit a37062e

File tree

27 files changed

+474
-204
lines changed

27 files changed

+474
-204
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Node {
2+
constructor(data, previous, next) {
3+
this.data = data;
4+
this.previous = previous;
5+
this.next = next;
6+
}
7+
}
8+
module.exports = Node;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const DLL = require('.');
2+
3+
describe('Doubly Linked List', () => {
4+
it('Doubly linked list should be class', () => {
5+
expect(typeof DLL.prototype.constructor).toEqual('function');
6+
});
7+
8+
const doublyLinkedList = new DLL();
9+
10+
it('It should create a DLL', () => {
11+
expect(doublyLinkedList.head.next).toEqual(doublyLinkedList.tail);
12+
expect(doublyLinkedList.tail.previous).toEqual(doublyLinkedList.head);
13+
expect(doublyLinkedList.length()).toEqual(0);
14+
});
15+
16+
it('It should add at beginning (addAtBeginning)', () => {
17+
doublyLinkedList.addAtBeginning(1);
18+
doublyLinkedList.addAtBeginning(2);
19+
doublyLinkedList.addAtBeginning(3);
20+
expect(doublyLinkedList.traverse()).toEqual([3, 2, 1]);
21+
});
22+
23+
it('It should add at end (addAtEnd)', () => {
24+
doublyLinkedList.addAtEnd(1);
25+
doublyLinkedList.addAtEnd(2);
26+
doublyLinkedList.addAtEnd(3);
27+
expect(doublyLinkedList.traverse()).toEqual([3, 2, 1, 1, 2, 3]);
28+
});
29+
30+
it('It should remove at beginning (removeAtBeginning)', () => {
31+
doublyLinkedList.removeAtBeginning();
32+
doublyLinkedList.removeAtBeginning();
33+
doublyLinkedList.removeAtBeginning();
34+
expect(doublyLinkedList.traverse()).toEqual([1, 2, 3]);
35+
});
36+
37+
it('It should remove at end (removeAtEnd)', () => {
38+
doublyLinkedList.removeAtEnd();
39+
doublyLinkedList.removeAtEnd();
40+
doublyLinkedList.removeAtEnd();
41+
42+
expect(doublyLinkedList.traverse()).toEqual([]);
43+
});
44+
});

src/_DataStructures_/DoublyLinkedList/index.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
/* eslint-disable class-methods-use-this */
2-
class Node {
3-
constructor(data, previous, next) {
4-
this.data = data;
5-
this.previous = previous;
6-
this.next = next;
7-
}
8-
}
2+
const Node = require('./Node');
93

104
class DoublyLinkedList {
115
constructor() {

src/_DataStructures_/LinkedList/LinkedList.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ describe('Data Structures: Linked Lists', () => {
6868
list.addAtEnd(10);
6969

7070
expect(list.length()).toEqual(4);
71+
expect(list.traverseList()).toEqual([15, 23, 33, 10]);
7172
});
7273
});
7374

src/_DataStructures_/Queue/Queue.test.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const Queue = require('.');
22

33
describe('Data Structure : Queue', () => {
4-
it('Should be class', () => {
4+
it('Queue should be class', () => {
55
expect(typeof Queue.prototype.constructor).toEqual('function');
66
});
77

@@ -53,6 +53,30 @@ describe('Data Structure : Queue', () => {
5353
expect(queue.dequeue()).toEqual(1);
5454
expect(queue.dequeue()).toEqual(4);
5555
expect(queue.dequeue()).toEqual(3);
56+
expect(queue.dequeue()).toEqual(null);
57+
});
58+
59+
it('Shoud return size of Queue', () => {
60+
const queue2 = new Queue();
61+
queue2.enqueue(2);
62+
queue2.enqueue(1);
63+
queue2.enqueue(4);
64+
queue2.enqueue(3);
65+
expect(queue2.length()).toEqual(4);
66+
});
67+
68+
it('Should Destroy Queue', () => {
69+
queue.destroy();
70+
expect(queue.length()).toEqual(0);
71+
});
72+
73+
it('Override and throw error for other LL methods', () => {
74+
expect(() => { queue.addAtBeginning(); }).toThrowError('Not Allowed');
75+
expect(() => { queue.addAt(); }).toThrowError('Not Allowed');
76+
expect(() => { queue.removeFromEnd(); }).toThrowError('Not Allowed');
77+
expect(() => { queue.getLast(); }).toThrowError('Not Allowed');
78+
expect(() => { queue.getAt(); }).toThrowError('Not Allowed');
79+
expect(() => { queue.removeAt(); }).toThrowError('Not Allowed');
5680
});
5781
});
5882
});

src/_DataStructures_/Stack/postfix-expression-evaluation/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function evaluatePostfixExpression(expression) {
4343
s.push(val2 / val1);
4444
break;
4545
default:
46-
break;
46+
throw new Error('Operation is not valid');
4747
}
4848
}
4949
}

src/_DataStructures_/Stack/postfix-expression-evaluation/postfix-expression-evaluation.test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,8 @@ describe('Postfix expression evaluation', () => {
5858
test.each(invalidExpressions)('running for %p', (expression) => {
5959
expect(() => evaluatePostfixExpression(expression)).toThrow(ERROR_STRING);
6060
});
61+
62+
expect(() => evaluatePostfixExpression('1&2')).toThrow('Operation is not valid');
63+
6164
});
6265
});

src/_DataStructures_/Trees/BinarySearchTree/BinarySearchTree.test.js

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const BinarySearchTree = require('./index');
2+
3+
describe('Binary Search Tree', () => {
4+
describe('Is Empty', () => {
5+
const bst = new BinarySearchTree(6);
6+
const keys = [4, 9, 2, 5, 8, 12];
7+
keys.forEach(el => bst.add(el));
8+
it('should return False when BST is not empty', () => {
9+
expect(bst.isEmpty()).toEqual(false);
10+
});
11+
12+
it('should return True when BST is empty', () => {
13+
keys.push(6);
14+
keys.forEach(el => bst.remove(el));
15+
expect(bst.isEmpty()).toEqual(true);
16+
});
17+
});
18+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const BinarySearchTree = require('./index');
2+
3+
describe('Binary Search Tree', () => {
4+
describe('Find maximum value in BST', () => {
5+
const bst = new BinarySearchTree(6);
6+
const keys = [4, 9, 2, 5, 8, 12];
7+
keys.forEach(el => bst.add(el));
8+
9+
it('It should expect maximum key', () => {
10+
expect(bst.getMaximum()).toEqual(12);
11+
});
12+
13+
it('It should expect new maximum key added in BST', () => {
14+
bst.add(20);
15+
expect(bst.getMaximum()).toEqual(20);
16+
});
17+
});
18+
});

0 commit comments

Comments
 (0)