Skip to content

Add unit test to loop-in-list #46

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

Merged
merged 1 commit into from
Oct 8, 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
6 changes: 5 additions & 1 deletion src/_DataStructures_/LinkedList/loop-in-list/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Floyd’s Cycle-Finding Algorithm

function detechLoop(linkedList) {
function detectLoop(linkedList) {
let slow = linkedList.getFirst();
let fast = linkedList.getFirst();

Expand All @@ -14,3 +14,7 @@ function detechLoop(linkedList) {
}
return false;
}

module.exports = {
detectLoop,
};
40 changes: 40 additions & 0 deletions src/_DataStructures_/LinkedList/loop-in-list/loop-in-list.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const { LinkedList } = require('../index');
const { detectLoop } = require('.');

describe('Loop a LinkedList', () => {
let loopList = null;
let last = null;
beforeEach(() => {
loopList = new LinkedList();
loopList.addAtBeginning('1');
loopList.addAtEnd('2');
loopList.addAtEnd('3');
loopList.addAtEnd('4');
loopList.addAtEnd('5');
// Create loop in list
last = loopList.getLast();
last.next = loopList.getFirst();
});

it('Should break for empty list', () => {
loopList.delete();
expect(() => detectLoop(loopList)).toThrow(TypeError);
});

it('Should return `true` when looping list', () => {
expect(detectLoop(loopList)).toEqual(true);
});

it('Should return `false` for non loop list', () => {
last.next = null; // remove loop in list
expect(detectLoop(loopList)).toEqual(false);
});

it('Should return `false` for non loop list', () => {
const list = new LinkedList();
list.addAtBeginning('1');
list.addAtEnd('1');
list.addAtEnd('1');
expect(detectLoop(list)).toEqual(false);
});
});