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
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;
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

+1-7
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

+1
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

+25-1
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

+1-1
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

+3
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

-25
This file was deleted.
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+
});
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+
});
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('It should Find the minimum 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 minimum key', () => {
10+
expect(bst.getMinimum()).toEqual(2);
11+
});
12+
13+
it('It should expect new minimum key added to BST', () => {
14+
bst.add(1);
15+
expect(bst.getMinimum()).toEqual(1);
16+
});
17+
});
18+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const BST = require('.');
2+
3+
describe('Data Structure : Binary Search Tree', () => {
4+
it('Binary Search Tree should be a Class', () => {
5+
expect(typeof BST.prototype.constructor).toEqual('function');
6+
});
7+
8+
describe('Binary Search Tree API', () => {
9+
let bst = null;
10+
11+
beforeEach(() => {
12+
bst = new BST(5);
13+
});
14+
15+
it('Should delete() an element from Binary Search Tree', () => {
16+
bst.add(4);
17+
bst.add(9);
18+
bst.add(2);
19+
bst.delete(bst.root, 4);
20+
expect(bst.traverseInorder()).toEqual([2, 5, 9]);
21+
bst.delete(bst.root, 2);
22+
expect(bst.traverseInorder()).toEqual([5, 9]);
23+
});
24+
25+
it('Should return NULL if root is empty', () => {
26+
const bst2 = new BST(6);
27+
bst2.remove(6);
28+
bst2.remove(9);
29+
expect(bst2.root).toEqual(null);
30+
});
31+
});
32+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const BinarySearchTree = require('./index');
2+
3+
describe('Binary Search Tree', () => {
4+
describe('It should Find the key 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 return true for 8', () => {
10+
expect(bst.searchFor(8)).toEqual(true);
11+
});
12+
13+
it('It should return false for 100', () => {
14+
expect(bst.searchFor(100)).toEqual(false);
15+
});
16+
});
17+
});

src/_DataStructures_/Trees/BinarySearchTree/bst-deletion.test.js src/_DataStructures_/Trees/BinarySearchTree/height-of-bst/height-of-bst.test.js

+14-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const BinarySearchTree = require('./index');
2-
const heightOfBST = require('./height-of-bst/index');
1+
const BinarySearchTree = require('../index');
2+
const heightOfBST = require('./index');
33

44
describe('Binary search tree traversals', () => {
55
let bst;
@@ -8,12 +8,8 @@ describe('Binary search tree traversals', () => {
88
it('should create BST', () => {
99
// Creates BST
1010
bst = new BinarySearchTree(6);
11-
bst.add(4);
12-
bst.add(9);
13-
bst.add(2);
14-
bst.add(5);
15-
bst.add(8);
16-
bst.add(12);
11+
const keys = [4, 9, 2, 5, 8, 12];
12+
keys.forEach(el => bst.add(el));
1713
});
1814
});
1915

@@ -48,4 +44,14 @@ describe('Binary search tree traversals', () => {
4844
expect(heightOfBST(bst.root)).toEqual(2);
4945
});
5046
});
47+
48+
describe('When root left subtree height is greater than right', () => {
49+
const bst2 = new BinarySearchTree(10);
50+
const keys = [11, 20, 9, 8, 7, 6, 5, 4];
51+
keys.forEach(el => bst2.add(el));
52+
53+
it('should return height of BST ', () => {
54+
expect(heightOfBST(bst2.root)).toEqual(7);
55+
});
56+
});
5157
});

src/_DataStructures_/Trees/BinarySearchTree/index.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable consistent-return */
12
const Node = require('./Node');
23

34
class BinarySearchTree {
@@ -23,7 +24,6 @@ class BinarySearchTree {
2324
root.rightChild = this.insert(root.rightChild, value);
2425
return root;
2526
}
26-
return root;
2727
}
2828

2929
preorder(root) {
@@ -80,7 +80,6 @@ class BinarySearchTree {
8080
if (value > root.value) {
8181
return this.search(root.rightChild, value);
8282
}
83-
return false;
8483
}
8584

8685
delete(root, value) {
@@ -169,7 +168,7 @@ class BinarySearchTree {
169168
}
170169

171170
remove(value) {
172-
return this.delete(this.root, value);
171+
this.root = this.delete(this.root, value);
173172
}
174173
}
175174

src/_DataStructures_/Trees/BinarySearchTree/lowest-common-ancestor/index.js

+6-9
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,19 @@
33
*
44
* Given values of two values n1 and n2 in a Binary Search Tree, find the Lowest Common Ancestor (LCA). You may assume that both the values exist in the tree.
55
*/
6-
7-
function lca(node, n1, n2) {
8-
if (node == null)
9-
return null;
6+
7+
function lowestCommonAncestor(node, n1, n2) {
8+
if (node === null) return null;
109

1110
// If both n1 and n2 are smaller than root, then LCA lies in left
12-
if (node.value > n1 && node.value > n2)
13-
return lca(node.leftChild, n1, n2);
11+
if (node.value > n1 && node.value > n2) { return lowestCommonAncestor(node.leftChild, n1, n2); }
1412

1513
// If both n1 and n2 are greater than root, then LCA lies in right
16-
if (node.value < n1 && node.value < n2)
17-
return lca(node.rightChild, n1, n2);
14+
if (node.value < n1 && node.value < n2) { return lowestCommonAncestor(node.rightChild, n1, n2); }
1815

1916
return node;
2017
}
2118

2219
module.exports = {
23-
lca,
20+
lowestCommonAncestor,
2421
};

0 commit comments

Comments
 (0)