Last active
October 17, 2019 20:34
-
-
Save barraponto/52a7f35a49b37dcc28c749354ef83526 to your computer and use it in GitHub Desktop.
Linked List Find By Position Tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-env node, mocha */ | |
const { expect } = require('chai') | |
const LinkedList = require('../src/linked-list'); | |
describe('Linked List', () => { | |
it('Creates empty Linked Lists', () => { | |
const list = new LinkedList(); | |
expect(list).to.be.instanceOf(LinkedList); | |
expect(list.head).to.be.null; | |
}); | |
it('Pushes the first item properly', () => { | |
const list = new LinkedList(); | |
list.push('A'); | |
expect(list.head.value).to.equal('A'); | |
}); | |
it('Gets the last item if it has one node', () => { | |
const list = new LinkedList(); | |
list.push('A'); | |
expect(list.tail().value).to.equal('A'); | |
}); | |
it('Gets the last item even if empty', () => { | |
const list = new LinkedList(); | |
expect(list.tail()).to.be.null; | |
}); | |
it('Pushes the second item properly', () => { | |
const list = new LinkedList(); | |
list.push('A'); | |
list.push('B'); | |
expect(list.head.next.value).to.equal('B'); | |
}); | |
it('Gets the last item if it has more than one node', () => { | |
const list = new LinkedList(); | |
list.push('A'); | |
list.push('B'); | |
expect(list.tail().value).to.equal('B'); | |
}); | |
}); | |
describe('Linked List positions', () => { | |
it('Finds existing elements by position', () => { | |
const list = new LinkedList(); | |
list.push('A'); | |
list.push('B'); | |
list.push('C'); | |
expect(list.findByPosition(0).value).to.equal('A'); | |
expect(list.findByPosition(1).value).to.equal('B'); | |
expect(list.findByPosition(2).value).to.equal('C'); | |
}); | |
it('Throws error if position is smaller than zero', () => { | |
const list = new LinkedList(); | |
expect(() => list.findByPosition(-1)).to.throw("Position must be greater than 0."); | |
}); | |
it('Throws error if LL is shorter than position', () => { | |
const list = new LinkedList(); | |
list.push('A'); | |
list.push('B'); | |
list.push('C'); | |
expect(() => list.findByPosition(3)).to.throw("Position 3 does not exist."); | |
}); | |
}); | |
describe('Linked List positions', () => { | |
it('Swaps elements by their positions', () => { | |
const list = new LinkedList(); | |
list.push('A'); | |
list.push('B'); | |
list.push('C'); | |
list.swapPositions(0, 2); | |
expect(list.findByPosition(0).value).to.equal('C'); | |
expect(list.findByPosition(1).value).to.equal('B'); | |
expect(list.findByPosition(2).value).to.equal('A'); | |
}); | |
it('Swaps elements by their positions', () => { | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment