|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var mod = require('../../src/data-structures/linked-list.js'); |
| 4 | +var Node = mod.Node; |
| 5 | +var LinkedList = mod.LinkedList; |
| 6 | + |
| 7 | +describe('Node', function () { |
| 8 | + it('should be a constructor function', function () { |
| 9 | + expect(typeof Node).toBe('function'); |
| 10 | + }); |
| 11 | + it('should construct properly', function () { |
| 12 | + var node = new Node("data"); |
| 13 | + expect(node.data).toBe("data"); |
| 14 | + expect(node.next).toBe(null); |
| 15 | + expect(node.prev).toBe(null); |
| 16 | + }); |
| 17 | +}); |
| 18 | + |
| 19 | +describe('Linked List', function () { |
| 20 | + it('should be a constructor function', function () { |
| 21 | + expect(typeof LinkedList).toBe('function'); |
| 22 | + }); |
| 23 | + it('should push properly', function () { |
| 24 | + var linkedList = new LinkedList(); |
| 25 | + linkedList.push(1); |
| 26 | + linkedList.push(2); |
| 27 | + linkedList.push(3); |
| 28 | + linkedList.push(4); |
| 29 | + linkedList.push(5); |
| 30 | + expect(linkedList.first.data).toBe(1); |
| 31 | + expect(linkedList.first.next.data).toBe(2); |
| 32 | + expect(linkedList.first.next.next.data).toBe(3); |
| 33 | + expect(linkedList.first.next.next.next.data).toBe(4); |
| 34 | + expect(linkedList.first.next.next.next.next.data).toBe(5); |
| 35 | + expect(linkedList.last.data).toBe(5); |
| 36 | + }); |
| 37 | + it('should pop properly', function () { |
| 38 | + var linkedList = new LinkedList(); |
| 39 | + linkedList.push(1); |
| 40 | + linkedList.push(2); |
| 41 | + linkedList.push(3); |
| 42 | + linkedList.push(4); |
| 43 | + linkedList.push(5); |
| 44 | + expect(linkedList.pop().data).toBe(5); |
| 45 | + expect(linkedList.pop().data).toBe(4); |
| 46 | + expect(linkedList.pop().data).toBe(3); |
| 47 | + expect(linkedList.pop().data).toBe(2); |
| 48 | + expect(linkedList.pop().data).toBe(1); |
| 49 | + }); |
| 50 | + it('should shift properly', function () { |
| 51 | + var linkedList = new LinkedList(); |
| 52 | + linkedList.push(1); |
| 53 | + linkedList.push(2); |
| 54 | + linkedList.push(3); |
| 55 | + linkedList.push(4); |
| 56 | + linkedList.push(5); |
| 57 | + expect(linkedList.shift().data).toBe(1); |
| 58 | + expect(linkedList.shift().data).toBe(2); |
| 59 | + expect(linkedList.shift().data).toBe(3); |
| 60 | + expect(linkedList.shift().data).toBe(4); |
| 61 | + expect(linkedList.shift().data).toBe(5); |
| 62 | + }); |
| 63 | + it('should reverse properly', function () { |
| 64 | + var linkedList = new LinkedList(); |
| 65 | + linkedList.push(1); |
| 66 | + linkedList.push(2); |
| 67 | + linkedList.push(3); |
| 68 | + linkedList.push(4); |
| 69 | + linkedList.push(5); |
| 70 | + linkedList.reverse(); |
| 71 | + expect(linkedList.shift().data).toBe(5); |
| 72 | + expect(linkedList.shift().data).toBe(4); |
| 73 | + expect(linkedList.shift().data).toBe(3); |
| 74 | + expect(linkedList.shift().data).toBe(2); |
| 75 | + expect(linkedList.shift().data).toBe(1); |
| 76 | + }); |
| 77 | + it('should recursive reverse properly', function () { |
| 78 | + var linkedList = new LinkedList(); |
| 79 | + linkedList.push(1); |
| 80 | + linkedList.push(2); |
| 81 | + linkedList.push(3); |
| 82 | + linkedList.push(4); |
| 83 | + linkedList.push(5); |
| 84 | + linkedList.recursiveReverse(); |
| 85 | + expect(linkedList.shift().data).toBe(5); |
| 86 | + expect(linkedList.shift().data).toBe(4); |
| 87 | + expect(linkedList.shift().data).toBe(3); |
| 88 | + expect(linkedList.shift().data).toBe(2); |
| 89 | + expect(linkedList.shift().data).toBe(1); |
| 90 | + }); |
| 91 | + it('should unshift properly', function () { |
| 92 | + var linkedList = new LinkedList(); |
| 93 | + linkedList.push(1); |
| 94 | + linkedList.push(2); |
| 95 | + linkedList.push(3); |
| 96 | + linkedList.push(4); |
| 97 | + linkedList.push(5); |
| 98 | + linkedList.unshift(3); |
| 99 | + expect(linkedList.shift().data).toBe(3); |
| 100 | + expect(linkedList.shift().data).toBe(1); |
| 101 | + expect(linkedList.shift().data).toBe(2); |
| 102 | + expect(linkedList.shift().data).toBe(3); |
| 103 | + expect(linkedList.shift().data).toBe(4); |
| 104 | + expect(linkedList.shift().data).toBe(5); |
| 105 | + }); |
| 106 | + it('should properly check for existing cycle', function () { |
| 107 | + var linkedList = new LinkedList(); |
| 108 | + var last = new Node(2); |
| 109 | + var first = new Node(1); |
| 110 | + last.next = first; |
| 111 | + last.prev = first; |
| 112 | + first.next = last; |
| 113 | + first.prev = last; |
| 114 | + linkedList.first = first; |
| 115 | + linkedList.last = last; |
| 116 | + expect(linkedList.hasCycle()).toBe(true); |
| 117 | + }); |
| 118 | + it('should properly check for non existing cycle', function () { |
| 119 | + var linkedList = new LinkedList(); |
| 120 | + linkedList.push(1); |
| 121 | + linkedList.push(2); |
| 122 | + linkedList.push(3); |
| 123 | + linkedList.push(4); |
| 124 | + linkedList.push(5); |
| 125 | + expect(linkedList.hasCycle()).toBe(false); |
| 126 | + }); |
| 127 | + it('should inorder properly', function () { |
| 128 | + var linkedList = new LinkedList(); |
| 129 | + linkedList.push(1); |
| 130 | + linkedList.push(2); |
| 131 | + linkedList.push(3); |
| 132 | + linkedList.push(4); |
| 133 | + linkedList.push(5); |
| 134 | + var pushedValue = 1; |
| 135 | + function callback(node){ |
| 136 | + expect(node.data).toBe(pushedValue++); |
| 137 | + } |
| 138 | + linkedList.inorder(callback); |
| 139 | + }); |
| 140 | +}); |
0 commit comments