summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Early <alexander.early@gmail.com>2017-04-06 22:17:49 -0700
committerAlexander Early <alexander.early@gmail.com>2017-04-06 22:19:23 -0700
commit3dc9f8f1e120f0c95f67221ac0d1a9bac7b88cd4 (patch)
tree18407f9b503e0b01a29536e02cbb1bcd9e541737
parent1af382d62b535f255a699fa03a4043faa5d3514e (diff)
downloadasync-3dc9f8f1e120f0c95f67221ac0d1a9bac7b88cd4.tar.gz
added tests for DLL, filter(), non-leaking empty()
-rw-r--r--lib/internal/DoublyLinkedList.js28
-rw-r--r--mocha_test/linked_list.js83
2 files changed, 110 insertions, 1 deletions
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([]);
+ });
+});