UNPKG

couchpotato-linked-list

Version:

DoublyLinkedList implementation in javascript

124 lines (122 loc) 4.19 kB
"use strict"; var _linkedList = require("./linked-list"); describe('Item is the basic node for a linked list', function () { test('node can be created with a value, or with both value and next node', function () { var n = new _linkedList.Item(10); expect(n.getValue()).toBe(10); }); test('node can be created with a value, or with both value and next node', function () { var n = new _linkedList.Item(10); var nn = new _linkedList.Item(20, n); expect(nn.getValue()).toBe(20); expect(nn.getNext()).toBe(n); }); }); describe('LinkedList using Item', function () { test('Empty Linked list with head and tail pointing to null', function () { var ll = new _linkedList.LinkedList(); expect(ll.head()).toBe(null); expect(ll.tail()).toBe(null); expect(ll.length()).toBe(0); }); test('Linked list - prepend an element', function () { var ll = new _linkedList.LinkedList(); ll.prepend(1); expect(ll.head().getValue()).toBe(1); expect(ll.tail().getValue()).toBe(1); expect(ll.length()).toBe(1); ll.prepend(2); expect(ll.head().getValue()).toBe(2); expect(ll.tail().getValue()).toBe(1); expect(ll.length()).toBe(2); ll.prepend(3); var first = ll.head(); var second = first.getNext(); var third = second.getNext(); expect(first.getValue()).toBe(3); expect(second.getValue()).toBe(2); expect(third.getValue()).toBe(1); expect(second.getPrev().getValue()).toBe(3); }); test('Linked list - append an element', function () { var ll = new _linkedList.LinkedList(); ll.append(1); expect(ll.head().getValue()).toBe(1); expect(ll.tail().getValue()).toBe(1); expect(ll.length()).toBe(1); ll.append(2); expect(ll.head().getValue()).toBe(1); expect(ll.tail().getValue()).toBe(2); expect(ll.length()).toBe(2); var first = ll.head(); var second = ll.tail(); expect(first.getNext()).toBe(second); expect(second.getPrev()).toBe(first); expect(first.getPrev()).toBe(null); expect(second.getNext()).toBe(null); }); test('Linked list - remove from first', function () { var ll = new _linkedList.LinkedList(); expect(ll.removeFromFront()).toBe(undefined); ll.append(1); var fn = ll.removeFromFront(); expect(fn.getValue()).toBe(1); expect(fn.getNext()).toBe(null); expect(fn.getPrev()).toBe(null); expect(ll.head()).toBe(null); expect(ll.tail()).toBe(null); expect(ll.length()).toBe(0); ll.append(1); ll.append(2); ll.append(3); var fn1 = ll.removeFromFront(); expect(fn1.getValue()).toBe(1); expect(fn1.getNext()).toBe(null); expect(fn1.getPrev()).toBe(null); expect(ll.head().getValue()).toBe(2); expect(ll.length()).toBe(2); expect(ll.head().getPrev()).toBe(null); }); test('Linked list - remove from last', function () { var ll = new _linkedList.LinkedList(); expect(ll.removeFromEnd()).toBe(undefined); ll.append(1); var last = ll.removeFromEnd(); expect(last.getValue()).toBe(1); expect(last.getNext()).toBe(null); expect(last.getPrev()).toBe(null); expect(ll.head()).toBe(null); expect(ll.tail()).toBe(null); expect(ll.length()).toBe(0); ll.append(1); ll.append(2); ll.append(3); var last1 = ll.removeFromEnd(); expect(last1.getValue()).toBe(3); expect(last1.getNext()).toBe(null); expect(last1.getPrev()).toBe(null); expect(ll.head().getValue()).toBe(1); expect(ll.tail().getValue()).toBe(2); expect(ll.length()).toBe(2); expect(ll.tail().getNext()).toBe(null); }); test('Linked list - get a node from a given index', function () { var ll = new _linkedList.LinkedList(); ll.append(1); ll.append(2); ll.append(3); ll.append(4); ll.append(5); ll.append(6); expect(ll.get(0)).toBe(1); expect(ll.get(2)).toBe(3); expect(ll.get(ll.length() - 1)).toBe(6); expect(ll.get(ll.length())).toBe(undefined); expect(ll.get(-1)).toBe(undefined); }); test('Linked list - remove a given node', function () { var ll = new _linkedList.LinkedList(); ll.append(0); expect(ll.removeAt(0)).toBe(0); }); });