Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code coverage #159

Merged
merged 4 commits into from
Nov 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/_DataStructures_/Heaps/MaxHeap/MaxHeap.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@ describe('MaxHeap', () => {
});

it('Should return `null` when heap is empty', () => {
[1, 34].forEach(el => mh.add(el));
expect(mh.getMax()).toEqual(34);
[1, 34, 43, 54, 123].forEach(el => mh.add(el));
mh.remove();
mh.remove();
mh.remove();
mh.remove();
mh.remove();
mh.remove();
expect(mh.getMax()).toEqual(null);
Expand Down
6 changes: 5 additions & 1 deletion src/_DataStructures_/Heaps/MinHeap/MinHeap.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,14 @@ describe('MinHeap', () => {
});

it('Should return `null` when heap is empty', () => {
[1, 34].forEach(el => mh.add(el));
[1, 34, 43, 54, 123].forEach(el => mh.add(el));
expect(mh.getMin()).toEqual(1);
mh.remove();
mh.remove();
mh.remove();
mh.remove();
mh.remove();
mh.remove();
expect(mh.getMin()).toEqual(null);
});

Expand Down
7 changes: 5 additions & 2 deletions src/_DataStructures_/LinkedList/LinkedList.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,11 @@ describe('Data Structures: Linked Lists', () => {
});

it('Should remove and return the element at given index value', () => {
expect(list.removeAt(3).data).toEqual('Welcome');
expect(list.removeAt(2).data).toEqual('There!');
list.delete();
[1, 2, 3, 4, 5, 6, 7, 8, 9].forEach(el => list.addAtBeginning(el));
expect(list.removeAt(10).data).toEqual(1);
expect(list.removeAt(0).data).toEqual(9);
expect(list.removeAt(5).data).toEqual(3);
});
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const BinaryTree = require("../index");
const BinaryTree = require('../index');
const bottomView = require('.');

describe('Bottom View Binary Tree', () => {
let btree
let btree;

beforeEach(() => {
btree = new BinaryTree([1, 2, 3, 4, 5, 6]);
Expand Down
15 changes: 10 additions & 5 deletions src/_DataStructures_/Trees/BinaryTree/btree-traversals.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
const BinaryTree = require("./index");
const BinaryTree = require('./index');

describe("Binary Tree Preorder Traversal", () => {
describe('Binary Tree Preorder Traversal', () => {
let btree;
let preOrderTraversal;

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

describe("BTree Traversals", () => {
it("should compute the Preorder traversal for the above created binary tree", () => {
describe('BTree Traversals', () => {
it('Should compute the Preorder traversal for the above created binary tree', () => {
preOrderTraversal = btree.preOrder();
expect(preOrderTraversal).toEqual([1, 2, 4, 5, 3, 6]);
});
Expand Down
4 changes: 0 additions & 4 deletions src/_DataStructures_/Trees/Trie/Node.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ class TrieNode {
this.isEndOfWord = true;
}

unmarkAsLeaf() {
this.isEndOfWord = false;
}

increaseCount() {
this.wordCount += 1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ const allWordsInTrie = require('./index');
const Trie = require('../index');

describe('Data Structure : Trie : All Words In Tree', () => {
it('Should return empty array', () => {
const trie = new Trie();
const result = allWordsInTrie(trie.root);
expect(result.length).toEqual(0);
});

it('Should return all words sorted alphabetically', () => {
const words = ['bed', 'ball', 'apple', 'java', 'javascript', 'bed'];
const trie = new Trie();
Expand Down
2 changes: 0 additions & 2 deletions src/_DataStructures_/Trees/Trie/all-words-in-trie/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ const TrieNode = require('../Node');
function getAllWords(root, level, word) {
let result = [];

if (!root) return result;

if (root.isEndOfWord) {
let temp = '';
for (let i = 0; i < level; i += 1) {
Expand Down
4 changes: 1 addition & 3 deletions src/_DataStructures_/Trees/Trie/get-unique-words/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ const TrieNode = require('../Node');
function getAllUniqueWords(root, level, word) {
let result = [];

if (!root) return result;

if (root.isEndOfWord) {
let temp = '';
for (let i = 0; i < level; i += 1) {
Expand All @@ -25,7 +23,7 @@ function getAllUniqueWords(root, level, word) {
}

function allUniqueWordsFromTrie(root) {
if (!(root instanceof TrieNode)) {
if (!(root instanceof TrieNode)) {
throw new Error('Invalid argument: Root of Trie is required');
}
const word = []; // char arr to store a word
Expand Down
31 changes: 31 additions & 0 deletions src/_DataStructures_/Trees/Trie/search.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const Trie = require('./index');

describe('Data Structure : Trie', () => {
describe('Trie Instance', () => {
it('Should be a class', () => {
expect(typeof Trie.prototype.constructor).toEqual('function');
});
});

describe('Trie API', () => {
const words = ['bed', 'ball', 'apple', 'java', 'javascript'];
let trie;
it('Should insert string', () => {
trie = new Trie();
words.forEach(word => trie.insert(word));
});

it('Should return `True` if string present', () => {
expect(trie.search(words[0])).toEqual(true);
});

it('Should return `False` if string present', () => {
expect(trie.search('Ashu')).toEqual(false);
expect(trie.search('be')).toEqual(false);
});

it('Should return `False` if argument is not pass', () => {
expect(trie.search()).toEqual(false);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ const BinaryTree = require('../../_DataStructures_/Trees/BinaryTree');
describe('Binary tree to binary search tree', () => {
let tree;

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

it('Should converted binary tree to binary search tree', () => {
tree = new BinaryTree([10, 30, 15, 20, null, null, 5]);
const bTree = binaryTreeToBST(tree);
expect(storeInorder(bTree)).toEqual([5, 10, 15, 20, 30]);
});
Expand Down
4 changes: 1 addition & 3 deletions src/_Problems_/next-greater-element/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ function nextGreaterElement(arr) {
for (let i = arr.length - 1; i >= 0; i -= 1) {
if (s1.peek()) {
let top = s1.peek();
while (top <= arr[i]) {
// if the stack is empty, break the while loop
if (!s1.peek()) break;
while (top && top <= arr[i]) {
// pop the elements
s1.pop();
// get the new top
Expand Down
26 changes: 13 additions & 13 deletions src/_Problems_/next-greater-element/next-greater-element.test.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
const { nextGreaterElement } = require('.');

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

expect(nextGreaterElement(input)).toEqual(greaterElements);
});

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

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

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

expect(nextGreaterElement(input)).toEqual(greaterElements);
});

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

expect(nextGreaterElement(input)).toEqual(greaterElements);
});

it('throws an error if the input is not an array', () => {
it('Should throws an error if the input is not an array', () => {
expect(() => {
nextGreaterElement('xunda')
nextGreaterElement('xunda');
}).toThrowError('Invalid Argument');
});
});