From 3dc9f8f1e120f0c95f67221ac0d1a9bac7b88cd4 Mon Sep 17 00:00:00 2001 From: Alexander Early Date: Thu, 6 Apr 2017 22:17:49 -0700 Subject: added tests for DLL, filter(), non-leaking empty() --- lib/internal/DoublyLinkedList.js | 28 +++++++++++++- mocha_test/linked_list.js | 83 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 mocha_test/linked_list.js diff --git a/lib/internal/DoublyLinkedList.js b/lib/internal/DoublyLinkedList.js index 79c3bb5..0feea1b 100644 --- a/lib/internal/DoublyLinkedList.js +++ b/lib/internal/DoublyLinkedList.js @@ -23,7 +23,10 @@ DLL.prototype.removeLink = function(node) { return node; } -DLL.prototype.empty = DLL; +DLL.prototype.empty = function () { + while(this.head) this.shift(); + return this; +}; DLL.prototype.insertAfter = function(node, newNode) { newNode.prev = node; @@ -60,3 +63,26 @@ DLL.prototype.shift = function() { DLL.prototype.pop = function() { return this.tail && this.removeLink(this.tail); }; + +DLL.prototype.toArray = function () { + var arr = Array(this.length); + var idx = 0; + var curr = this.head; + for(idx = 0; idx < this.length; idx++) { + arr[idx] = curr.data; + curr = curr.next; + } + return arr; +} + +DLL.prototype.filter = function (testFn) { + var curr = this.head; + while(!!curr) { + var next = curr.next; + if (!testFn(curr)) { + this.removeLink(curr); + } + curr = next; + } + return this; +} diff --git a/mocha_test/linked_list.js b/mocha_test/linked_list.js new file mode 100644 index 0000000..19cb155 --- /dev/null +++ b/mocha_test/linked_list.js @@ -0,0 +1,83 @@ +var DLL = require('../lib/internal/DoublyLinkedList').default; +var expect = require('chai').expect; + +describe('DoublyLinkedList', function () { + it('toArray', function() { + var list = new DLL(); + expect(list.toArray()).to.eql([]); + + for (var i = 0; i < 5; i++) { + list.push({data: i}); + } + expect(list.toArray()).to.eql([0, 1, 2, 3, 4]); + }); + + it('filter', function() { + var list = new DLL(); + + for (var i = 0; i < 5; i++) { + list.push({data: i}); + } + + list.filter(function (node) { + return node.data !== 3; + }) + + expect(list.toArray()).to.eql([0, 1, 2, 4]); + }); + + it('filter (head)', function() { + var list = new DLL(); + + for (var i = 0; i < 5; i++) { + list.push({data: i}); + } + + list.filter(function (node) { + return node.data !== 0; + }) + + expect(list.toArray()).to.eql([1, 2, 3, 4]); + }); + + it('filter (tail)', function() { + var list = new DLL(); + + for (var i = 0; i < 5; i++) { + list.push({data: i}); + } + + list.filter(function (node) { + return node.data !== 4; + }) + + expect(list.toArray()).to.eql([0, 1, 2, 3]); + }); + + it('filter (all)', function() { + var list = new DLL(); + + for (var i = 0; i < 5; i++) { + list.push({data: i}); + } + + list.filter(function (node) { + return node.data > 5; + }) + + expect(list.toArray()).to.eql([]); + }); + + it('empty', function() { + var list = new DLL(); + + for (var i = 0; i < 5; i++) { + list.push({data: i}); + } + + var empty = list.empty(); + + expect(list).to.equal(empty); + expect(list.toArray()).to.eql([]); + }); +}); -- cgit v1.2.1