Skip to content

Commit a47787d

Browse files
authored
Merge pull request knaxus#159 from knaxus/codeCoverage
Code coverage
2 parents 5d3d94b + edb379a commit a47787d

File tree

13 files changed

+84
-39
lines changed

13 files changed

+84
-39
lines changed

src/_DataStructures_/Heaps/MaxHeap/MaxHeap.test.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,11 @@ describe('MaxHeap', () => {
3838
});
3939

4040
it('Should return `null` when heap is empty', () => {
41-
[1, 34].forEach(el => mh.add(el));
42-
expect(mh.getMax()).toEqual(34);
41+
[1, 34, 43, 54, 123].forEach(el => mh.add(el));
42+
mh.remove();
43+
mh.remove();
44+
mh.remove();
45+
mh.remove();
4346
mh.remove();
4447
mh.remove();
4548
expect(mh.getMax()).toEqual(null);

src/_DataStructures_/Heaps/MinHeap/MinHeap.test.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,14 @@ describe('MinHeap', () => {
3838
});
3939

4040
it('Should return `null` when heap is empty', () => {
41-
[1, 34].forEach(el => mh.add(el));
41+
[1, 34, 43, 54, 123].forEach(el => mh.add(el));
4242
expect(mh.getMin()).toEqual(1);
4343
mh.remove();
4444
mh.remove();
45+
mh.remove();
46+
mh.remove();
47+
mh.remove();
48+
mh.remove();
4549
expect(mh.getMin()).toEqual(null);
4650
});
4751

src/_DataStructures_/LinkedList/LinkedList.test.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,11 @@ describe('Data Structures: Linked Lists', () => {
224224
});
225225

226226
it('Should remove and return the element at given index value', () => {
227-
expect(list.removeAt(3).data).toEqual('Welcome');
228-
expect(list.removeAt(2).data).toEqual('There!');
227+
list.delete();
228+
[1, 2, 3, 4, 5, 6, 7, 8, 9].forEach(el => list.addAtBeginning(el));
229+
expect(list.removeAt(10).data).toEqual(1);
230+
expect(list.removeAt(0).data).toEqual(9);
231+
expect(list.removeAt(5).data).toEqual(3);
229232
});
230233
});
231234
});

src/_DataStructures_/Trees/BinaryTree/bottom-view-binary-tree/BottomViewBinaryTree.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
const BinaryTree = require("../index");
1+
const BinaryTree = require('../index');
22
const bottomView = require('.');
33

44
describe('Bottom View Binary Tree', () => {
5-
let btree
5+
let btree;
66

77
beforeEach(() => {
88
btree = new BinaryTree([1, 2, 3, 4, 5, 6]);

src/_DataStructures_/Trees/BinaryTree/btree-traversals.test.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
1-
const BinaryTree = require("./index");
1+
const BinaryTree = require('./index');
22

3-
describe("Binary Tree Preorder Traversal", () => {
3+
describe('Binary Tree Preorder Traversal', () => {
44
let btree;
55
let preOrderTraversal;
66

7-
describe("Creates BTree", () => {
7+
describe('Creates BTree', () => {
8+
it('Should throw error if argument is not array', () => {
9+
expect(() => {
10+
btree = new BinaryTree('Hello tree');
11+
}).toThrow('Invalid argument to create a Binary Tree');
12+
});
813
btree = new BinaryTree([1, 2, 3, 4, 5, 6]);
914
});
1015

11-
describe("BTree Traversals", () => {
12-
it("should compute the Preorder traversal for the above created binary tree", () => {
16+
describe('BTree Traversals', () => {
17+
it('Should compute the Preorder traversal for the above created binary tree', () => {
1318
preOrderTraversal = btree.preOrder();
1419
expect(preOrderTraversal).toEqual([1, 2, 4, 5, 3, 6]);
1520
});

src/_DataStructures_/Trees/Trie/Node.js

-4
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ class TrieNode {
1313
this.isEndOfWord = true;
1414
}
1515

16-
unmarkAsLeaf() {
17-
this.isEndOfWord = false;
18-
}
19-
2016
increaseCount() {
2117
this.wordCount += 1;
2218
}

src/_DataStructures_/Trees/Trie/all-words-in-trie/all-words-in-trie.test.js

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ const allWordsInTrie = require('./index');
22
const Trie = require('../index');
33

44
describe('Data Structure : Trie : All Words In Tree', () => {
5+
it('Should return empty array', () => {
6+
const trie = new Trie();
7+
const result = allWordsInTrie(trie.root);
8+
expect(result.length).toEqual(0);
9+
});
10+
511
it('Should return all words sorted alphabetically', () => {
612
const words = ['bed', 'ball', 'apple', 'java', 'javascript', 'bed'];
713
const trie = new Trie();

src/_DataStructures_/Trees/Trie/all-words-in-trie/index.js

-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ const TrieNode = require('../Node');
55
function getAllWords(root, level, word) {
66
let result = [];
77

8-
if (!root) return result;
9-
108
if (root.isEndOfWord) {
119
let temp = '';
1210
for (let i = 0; i < level; i += 1) {

src/_DataStructures_/Trees/Trie/get-unique-words/index.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ const TrieNode = require('../Node');
44
function getAllUniqueWords(root, level, word) {
55
let result = [];
66

7-
if (!root) return result;
8-
97
if (root.isEndOfWord) {
108
let temp = '';
119
for (let i = 0; i < level; i += 1) {
@@ -25,7 +23,7 @@ function getAllUniqueWords(root, level, word) {
2523
}
2624

2725
function allUniqueWordsFromTrie(root) {
28-
if (!(root instanceof TrieNode)) {
26+
if (!(root instanceof TrieNode)) {
2927
throw new Error('Invalid argument: Root of Trie is required');
3028
}
3129
const word = []; // char arr to store a word
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const Trie = require('./index');
2+
3+
describe('Data Structure : Trie', () => {
4+
describe('Trie Instance', () => {
5+
it('Should be a class', () => {
6+
expect(typeof Trie.prototype.constructor).toEqual('function');
7+
});
8+
});
9+
10+
describe('Trie API', () => {
11+
const words = ['bed', 'ball', 'apple', 'java', 'javascript'];
12+
let trie;
13+
it('Should insert string', () => {
14+
trie = new Trie();
15+
words.forEach(word => trie.insert(word));
16+
});
17+
18+
it('Should return `True` if string present', () => {
19+
expect(trie.search(words[0])).toEqual(true);
20+
});
21+
22+
it('Should return `False` if string present', () => {
23+
expect(trie.search('Ashu')).toEqual(false);
24+
expect(trie.search('be')).toEqual(false);
25+
});
26+
27+
it('Should return `False` if argument is not pass', () => {
28+
expect(trie.search()).toEqual(false);
29+
});
30+
});
31+
});

src/_Problems_/binary-tree-to-binary-search-tree/binary-tree-to-binary-search-tree.test.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@ const BinaryTree = require('../../_DataStructures_/Trees/BinaryTree');
44
describe('Binary tree to binary search tree', () => {
55
let tree;
66

7-
describe('Create Binary Tree', () => {
8-
tree = new BinaryTree([10, 30, 15, 20, null, null, 5]);
7+
it('Should return `null` if root is null', () => {
8+
tree = new BinaryTree([1]);
9+
tree.root = null;
10+
expect(binaryTreeToBST(tree)).toEqual(null);
911
});
1012

1113
it('Should converted binary tree to binary search tree', () => {
14+
tree = new BinaryTree([10, 30, 15, 20, null, null, 5]);
1215
const bTree = binaryTreeToBST(tree);
1316
expect(storeInorder(bTree)).toEqual([5, 10, 15, 20, 30]);
1417
});

src/_Problems_/next-greater-element/index.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ function nextGreaterElement(arr) {
2020
for (let i = arr.length - 1; i >= 0; i -= 1) {
2121
if (s1.peek()) {
2222
let top = s1.peek();
23-
while (top <= arr[i]) {
24-
// if the stack is empty, break the while loop
25-
if (!s1.peek()) break;
23+
while (top && top <= arr[i]) {
2624
// pop the elements
2725
s1.pop();
2826
// get the new top
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
11
const { nextGreaterElement } = require('.');
22

33
describe('Next greater element', () => {
4-
it('returns next greater elements collection', () => {
5-
const input = [4, 6, 3, 2, 8, 1]
6-
const greaterElements = [6, 8, 8, 8, -1, -1]
4+
it('Should returns next greater elements collection', () => {
5+
const input = [4, 11, 6, 3, 2, 8, 1];
6+
const greaterElements = [11, -1, 8, 8, 8, -1, -1];
77

88
expect(nextGreaterElement(input)).toEqual(greaterElements);
99
});
1010

11-
it('returns and empty collection for an empty array', () => {
11+
it('Should returns and empty collection for an empty array', () => {
1212
expect(nextGreaterElement([])).toEqual([]);
1313
});
1414

15-
it('returns an array with -1 if the input has only one element', () => {
15+
it('Should returns an array with -1 if the input has only one element', () => {
1616
expect(nextGreaterElement([0])).toEqual([-1]);
1717
});
1818

19-
it('returns a collection of -1 if there is no greater element', () => {
20-
const input = [90, 40, 15, 7, -1, -10]
21-
const greaterElements = [-1, -1, -1, -1, -1, -1]
19+
it('Should returns a collection of -1 if there is no greater element', () => {
20+
const input = [90, 40, 15, 7, -1, -10];
21+
const greaterElements = [-1, -1, -1, -1, -1, -1];
2222

2323
expect(nextGreaterElement(input)).toEqual(greaterElements);
2424
});
2525

26-
it('uses -1 if the numbers are the same', () => {
27-
const input = [90, 90]
28-
const greaterElements = [-1, -1]
26+
it('Should uses -1 if the numbers are the same', () => {
27+
const input = [90, 90];
28+
const greaterElements = [-1, -1];
2929

3030
expect(nextGreaterElement(input)).toEqual(greaterElements);
3131
});
3232

33-
it('throws an error if the input is not an array', () => {
33+
it('Should throws an error if the input is not an array', () => {
3434
expect(() => {
35-
nextGreaterElement('xunda')
35+
nextGreaterElement('xunda');
3636
}).toThrowError('Invalid Argument');
3737
});
3838
});

0 commit comments

Comments
 (0)