UNPKG

list-runner

Version:

a lightweight linked-list implementation that offers both Singly (next) and Doubly data structures (next and previous)

284 lines (273 loc) 10.3 kB
"use strict"; var _CellDoubly = require("./CellDoubly"); var _sidekick = require("../sidekick"); var _constants = require("../constants"); var _StemSingly = require("./StemSingly.test"); /* eslint-env jest */ var stem1; var cells = []; var newCell1; var targetCell1; var parameters = { idCounter: 0 }; var generateCell = function generateCell() { return new _CellDoubly.CellDoubly(); }; var initTestData = function initTestData() { return (0, _StemSingly.initializeTestData)(generateCell, parameters, cells, _constants.DOUBLY); }; describe('doubly: basic navigation', function () { beforeEach(function () { parameters.idCounter = 0; stem1 = initTestData(); }); afterEach(function () { console.log('doubly: basic navigation => test complete'); // eslint-disable-line no-console }); it('getHead should return the head cell (cells[0])', function () { targetCell1 = cells[0]; expect(stem1.getHead()).toEqual(targetCell1); }); it("calling the head cell's getNext function should return cells[1]", function () { targetCell1 = cells[1]; expect(stem1.getHead().getNext()).toEqual(targetCell1); }); it('calling cells[4].getPrev() should return cells[3]', function () { targetCell1 = cells[3]; expect(cells[4].getPrev()).toEqual(cells[3]); }); it('getTail should return the tail cell (cells[4])', function () { targetCell1 = cells[4]; expect(stem1.getTail()).toEqual(targetCell1); }); it("the head cell's getPrev() should return SENTINEL", function () { targetCell1 = _constants.SENTINEL; expect(stem1.getHead().getPrev().type).toBe(targetCell1); }); it("the tail cell's getNext() should return SENTINEL", function () { targetCell1 = _constants.SENTINEL; expect(stem1.getTail().getNext().type).toBe(targetCell1); }); }); describe('doubly: inserts', function () { beforeEach(function () { parameters.idCounter = 0; stem1 = initTestData(); newCell1 = new _CellDoubly.CellDoubly(); newCell1.id = parameters.idCounter++; }); afterEach(function () { console.log('doubly: inserts => test complete'); // eslint-disable-line no-console }); it('inserting a cell after sentinelHead should interlink properly', function () { targetCell1 = newCell1; stem1.insert(newCell1, stem1.sentinelHead); expect(stem1.getHead()).toEqual(targetCell1); expect(stem1.getHead().getNext().id).toBe(0); expect(stem1.getHead().getPrev().type).toBe(_constants.SENTINEL); }); it('inserting a cell after the tail should interlink properly', function () { targetCell1 = newCell1; stem1.insert(newCell1, stem1.getTail()); expect(stem1.getTail()).toEqual(targetCell1); expect(stem1.getTail().getPrev().id).toBe(4); expect(stem1.getTail().getNext().type).toBe(_constants.SENTINEL); }); it('inserting a cell after sentinelTail should return false', function () { targetCell1 = newCell1; var result = stem1.insert(newCell1, stem1.sentinelTail); expect(result).toBe(false); }); }); describe('doubly: extracts', function () { beforeEach(function () { parameters.idCounter = 0; stem1 = initTestData(); }); afterEach(function () { console.log('doubly: extracts => test complete'); // eslint-disable-line no-console }); it('extracting the head cell should interlink properly', function () { stem1.extract(stem1.sentinelHead); expect(stem1.getHead()).toEqual(cells[1]); expect(stem1.getHead().getNext()).toEqual(cells[2]); expect(stem1.getHead().getPrev().type).toBe(_constants.SENTINEL); }); it('extracting the tail cell should interlink properly', function () { stem1.extract(cells[3]); expect(stem1.getTail()).toEqual(cells[3]); expect(stem1.getTail().getPrev().id).toBe(2); expect(stem1.getTail().getNext().type).toBe(_constants.SENTINEL); }); it('extracting sentinelTail should return false', function () { var result = stem1.extract(stem1.getTail()); expect(result).toBe(false); }); it('extracting a cell after sentinelTail should return false', function () { targetCell1 = newCell1; var result = stem1.extract(stem1.sentinelTail); expect(result).toBe(false); }); }); describe('doubly: replaces', function () { beforeEach(function () { parameters.idCounter = 0; stem1 = initTestData(); newCell1 = new _CellDoubly.CellDoubly(); newCell1.id = parameters.idCounter++; }); afterEach(function () { console.log('doubly: replaces => test complete'); // eslint-disable-line no-console }); it('replacing the cell after sentinelHead should interlink properly', function () { stem1.replace(newCell1, stem1.sentinelHead); expect(stem1.getHead()).toEqual(newCell1); expect(stem1.getHead().getNext()).toEqual(cells[1]); expect(stem1.getHead().getPrev().type).toBe(_constants.SENTINEL); }); it('replacing the tail should interlink properly', function () { targetCell1 = newCell1; stem1.replace(newCell1, cells[3]); expect(stem1.getTail()).toEqual(targetCell1); expect(stem1.getTail().getPrev().id).toBe(3); expect(stem1.getTail().getNext().type).toBe(_constants.SENTINEL); }); it('replacing sentinelTail should return false', function () { targetCell1 = newCell1; var result = stem1.replace(newCell1, stem1.getTail()); expect(result).toBe(false); }); }); describe('doubly: unshifting', function () { beforeEach(function () { parameters.idCounter = 0; stem1 = initTestData(); newCell1 = new _CellDoubly.CellDoubly(); newCell1.id = parameters.idCounter++; }); afterEach(function () { console.log('doubly: unshifting => test complete'); // eslint-disable-line no-console }); it('unshifting a cell onto the stem should interlink properly', function () { stem1.unshift(newCell1); expect(stem1.getHead()).toEqual(newCell1); expect(stem1.getHead().getNext()).toEqual(cells[0]); expect(stem1.getHead().getPrev().type).toBe(_constants.SENTINEL); }); }); describe('doubly: shifting', function () { beforeEach(function () { parameters.idCounter = 0; stem1 = initTestData(); }); afterEach(function () { console.log('doubly: shifting => test complete'); // eslint-disable-line no-console }); it('shifting a a cell off the stem should interlink properly', function () { stem1.shift(); expect(stem1.getHead()).toEqual(cells[1]); expect(stem1.getHead().getNext()).toEqual(cells[2]); expect(stem1.getHead().getPrev().type).toBe(_constants.SENTINEL); }); }); describe('doubly: pushing', function () { beforeEach(function () { parameters.idCounter = 0; stem1 = initTestData(); newCell1 = new _CellDoubly.CellDoubly(); newCell1.id = parameters.idCounter++; }); afterEach(function () { console.log('doubly: pushing => test complete'); // eslint-disable-line no-console }); it('pushing a cell onto the stem should interlink properly', function () { stem1.push(newCell1); expect(stem1.getTail()).toEqual(newCell1); expect(stem1.getTail().getPrev()).toEqual(cells[4]); expect(stem1.getTail().getNext().type).toBe(_constants.SENTINEL); }); }); describe('doubly: popping', function () { beforeEach(function () { parameters.idCounter = 0; stem1 = initTestData(); }); afterEach(function () { console.log('doubly: popping => test complete'); // eslint-disable-line no-console }); it('popping a cell off the stem should interlink properly', function () { stem1.pop(); expect(stem1.getTail()).toEqual(cells[3]); expect(stem1.getTail().getPrev()).toEqual(cells[2]); expect(stem1.getTail().getNext().type).toBe(_constants.SENTINEL); }); }); describe('doubly: deleting', function () { beforeEach(function () { parameters.idCounter = 0; stem1 = initTestData(); }); afterEach(function () { console.log('doubly: deleting => test complete'); // eslint-disable-line no-console }); it('deleting a cell from the stem should interlink properly', function () { stem1["delete"](cells[3]); expect(stem1.getTail()).toEqual(cells[3]); expect(stem1.getTail().getPrev()).toEqual(cells[2]); expect(stem1.getTail().getNext().type).toBe(_constants.SENTINEL); // confirm that SentinelTail's getPrev() points to the tail cell expect(stem1.sentinelTail.getPrev()).toEqual(cells[3]); }); }); describe('doubly: findBackward', function () { beforeEach(function () { parameters.idCounter = 0; stem1 = initTestData(); }); afterEach(function () { console.log('doubly: findBackward => test complete'); // eslint-disable-line no-console }); it('finds a cell with id === 1', function () { var match = (0, _sidekick.findBackward)(stem1.getTail(), (0, _StemSingly.findFunction)(1)); expect(cells[1]).toEqual(match); }); }); describe('doubly: runBackward', function () { beforeEach(function () { parameters.idCounter = 0; stem1 = initTestData(); }); afterEach(function () { console.log('doubly: runBackward => test complete'); // eslint-disable-line no-console }); it('finds a cell with id === 2', function () { var cell = (0, _sidekick.runBackward)(stem1.getTail(), (0, _StemSingly.loopFunction)(2)); // the last cell instance upon loop termination expect(cells[1]).toEqual(cell); }); }); describe('doubly: runBackward off the edge of the stem', function () { beforeEach(function () { parameters.idCounter = 0; stem1 = initTestData(); }); afterEach(function () { console.log('doubly: runBackward off the edge fo the stem => test complete'); // eslint-disable-line no-console }); it('does not find an id', function () { var cell = (0, _sidekick.runBackward)(stem1.getTail(), (0, _StemSingly.loopFunction)(9)); // the last cell instance upon loop termination expect(cell.type).toBe(_constants.SENTINEL); }); }); describe('doubly: countBackward', function () { beforeEach(function () { parameters.idCounter = 0; stem1 = initTestData(); }); afterEach(function () { console.log('doubly: countBackward => test complete'); // eslint-disable-line no-console }); it('counts a total of 5 cells', function () { var count = (0, _sidekick.countBackward)(cells[4]); expect(count).toBe(5); }); });