UNPKG

list-runner

Version:

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

271 lines (250 loc) 8.86 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.initializeTestData = exports.objectify = exports.addProperties = exports.loopFunction = exports.findFunction = void 0; var _CellSingly = require("./CellSingly"); var _sidekick = require("../sidekick"); var _constants = require("../constants"); /* eslint-env jest */ var stem1; var cells = []; var newCell1; var targetCell1; var parameters = { idCounter: 0 }; var findFunction = function findFunction(id) { return function (cell) { return cell.id === id; }; }; exports.findFunction = findFunction; var loopFunction = function loopFunction(id) { return function (cell) { return cell.id === id; }; }; exports.loopFunction = loopFunction; var generateCell = function generateCell() { return new _CellSingly.CellSingly(); }; var addProperties = function addProperties(cell, params) { cell.id = params.idCounter++; }; exports.addProperties = addProperties; var objectify = function objectify(createCell, params) { var cell = createCell(); addProperties(cell, params); return cell; }; exports.objectify = objectify; var initializeTestData = function initializeTestData(createCell, params, stemCells, structureType) { for (var looper = 0; looper < 5; looper++) { stemCells[looper] = objectify(createCell, params); } var transientStem = (0, _sidekick.initializeStem)(stemCells, structureType); return transientStem; }; exports.initializeTestData = initializeTestData; var initTestData = function initTestData() { return initializeTestData(generateCell, parameters, cells, _constants.SINGLY); }; describe('singly: basic navigation', function () { beforeEach(function () { parameters.idCounter = 0; stem1 = initTestData(); }); afterEach(function () { console.log('singly: 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("the tail cell's getNext() should return SENTINEL", function () { targetCell1 = _constants.SENTINEL; expect(cells[4].getNext().type).toBe(targetCell1); }); }); describe('singly: inserts', function () { beforeEach(function () { parameters.idCounter = 0; stem1 = initTestData(); newCell1 = new _CellSingly.CellSingly(); newCell1.id = parameters.idCounter++; }); afterEach(function () { console.log('singly: 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); }); it('inserting a cell after the tail should interlink properly', function () { targetCell1 = newCell1; stem1.insert(newCell1, cells[4]); expect(cells[4].getNext()).toEqual(targetCell1); expect(cells[4].getNext().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('singly: extracts', function () { beforeEach(function () { parameters.idCounter = 0; stem1 = initTestData(); }); afterEach(function () { console.log('singly: 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]); }); it('extracting the tail cell should interlink properly', function () { stem1.extract(cells[3]); expect(cells[3].getNext().type).toBe(_constants.SENTINEL); }); it('extracting sentinelTail should return false', function () { var result = stem1.extract(cells[4]); expect(result).toBe(false); }); it('extracting a cell after sentinelTail should return false', function () { targetCell1 = newCell1; var result = stem1.extract(cells[4].getNext()); expect(result).toBe(false); }); }); describe('singly: replaces', function () { beforeEach(function () { parameters.idCounter = 0; stem1 = initTestData(); newCell1 = new _CellSingly.CellSingly(); newCell1.id = parameters.idCounter++; }); afterEach(function () { console.log('singly: 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]); }); it('replacing the tail should interlink properly', function () { targetCell1 = newCell1; stem1.replace(newCell1, cells[3]); expect(cells[3].getNext()).toEqual(targetCell1); expect(cells[3].getNext().getNext().type).toBe(_constants.SENTINEL); }); it('replacing sentinelTail should return false', function () { targetCell1 = newCell1; var result = stem1.replace(newCell1, cells[4]); expect(result).toBe(false); }); }); describe('singly: unshifting', function () { beforeEach(function () { parameters.idCounter = 0; stem1 = initTestData(); newCell1 = new _CellSingly.CellSingly(); newCell1.id = parameters.idCounter++; }); afterEach(function () { console.log('singly: 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]); }); }); describe('singly: shifting', function () { beforeEach(function () { parameters.idCounter = 0; stem1 = initTestData(); }); afterEach(function () { console.log('singly: 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]); }); }); describe('singly: deleting', function () { beforeEach(function () { parameters.idCounter = 0; stem1 = initTestData(); }); afterEach(function () { console.log('singly: deleting => test complete'); // eslint-disable-line no-console }); it('deleting a cell from the stem should interlink properly', function () { stem1["delete"](cells[3]); expect(cells[3].getNext().type).toBe(_constants.SENTINEL); }); }); describe('singly: findForward', function () { beforeEach(function () { parameters.idCounter = 0; stem1 = initTestData(); }); afterEach(function () { console.log('singly: findForward => test complete'); // eslint-disable-line no-console }); it('finds a cell with id === 3', function () { var match = (0, _sidekick.findForward)(stem1.getHead(), findFunction(3)); expect(cells[3]).toEqual(match); }); }); describe('singly: runForward', function () { beforeEach(function () { parameters.idCounter = 0; stem1 = initTestData(); }); afterEach(function () { console.log('singly: runForward => test complete'); // eslint-disable-line no-console }); it('finds a cell with id === 3', function () { var cell = (0, _sidekick.runForward)(stem1.getHead(), loopFunction(3)); // the last cell instance upon loop termination expect(cells[4]).toEqual(cell); }); }); describe('singly: runForward off the edge of the stem', function () { beforeEach(function () { parameters.idCounter = 0; stem1 = initTestData(); }); afterEach(function () { console.log('singly: runForward off the edge of the stem => test complete'); // eslint-disable-line no-console }); it('does not find an id', function () { var cell = (0, _sidekick.runForward)(stem1.getHead(), loopFunction(9)); // the last cell instance upon loop termination expect(cell.type).toBe(_constants.SENTINEL); }); }); describe('singly: countForward', function () { beforeEach(function () { parameters.idCounter = 0; stem1 = initTestData(); }); afterEach(function () { console.log('singly: countForward => test complete'); // eslint-disable-line no-console }); it('counts a total of 5 cells', function () { var count = (0, _sidekick.countForward)(cells[0]); expect(count).toBe(5); }); });