|
| 1 | +import DoublyLinkedList from '../DoublyLinkedList'; |
| 2 | + |
| 3 | +describe('DoublyLinkedList', () => { |
| 4 | + it('should create empty linked list', () => { |
| 5 | + const linkedList = new DoublyLinkedList(); |
| 6 | + expect(linkedList.toString()).toBe(''); |
| 7 | + }); |
| 8 | + |
| 9 | + it('should append node to linked list', () => { |
| 10 | + const linkedList = new DoublyLinkedList(); |
| 11 | + |
| 12 | + expect(linkedList.head).toBeNull(); |
| 13 | + expect(linkedList.tail).toBeNull(); |
| 14 | + |
| 15 | + linkedList.append(1); |
| 16 | + linkedList.append(2); |
| 17 | + |
| 18 | + expect(linkedList.head.next.value).toBe(2); |
| 19 | + expect(linkedList.tail.previous.value).toBe(1); |
| 20 | + expect(linkedList.toString()).toBe('1,2'); |
| 21 | + }); |
| 22 | + |
| 23 | + it('should prepend node to linked list', () => { |
| 24 | + const linkedList = new DoublyLinkedList(); |
| 25 | + |
| 26 | + linkedList.prepend(2); |
| 27 | + expect(linkedList.head.toString()).toBe('2'); |
| 28 | + expect(linkedList.tail.toString()).toBe('2'); |
| 29 | + |
| 30 | + linkedList.append(1); |
| 31 | + linkedList.prepend(3); |
| 32 | + |
| 33 | + expect(linkedList.head.next.next.previous).toBe(linkedList.head.next); |
| 34 | + expect(linkedList.tail.previous.next).toBe(linkedList.tail); |
| 35 | + expect(linkedList.tail.previous.value).toBe(2); |
| 36 | + expect(linkedList.toString()).toBe('3,2,1'); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should delete node by value from linked list', () => { |
| 40 | + const linkedList = new DoublyLinkedList(); |
| 41 | + |
| 42 | + expect(linkedList.delete(5)).toBeNull(); |
| 43 | + |
| 44 | + linkedList.append(1); |
| 45 | + linkedList.append(1); |
| 46 | + linkedList.append(2); |
| 47 | + linkedList.append(3); |
| 48 | + linkedList.append(3); |
| 49 | + linkedList.append(3); |
| 50 | + linkedList.append(4); |
| 51 | + linkedList.append(5); |
| 52 | + |
| 53 | + expect(linkedList.head.toString()).toBe('1'); |
| 54 | + expect(linkedList.tail.toString()).toBe('5'); |
| 55 | + |
| 56 | + const deletedNode = linkedList.delete(3); |
| 57 | + expect(deletedNode.value).toBe(3); |
| 58 | + expect(linkedList.tail.previous.previous.value).toBe(2); |
| 59 | + expect(linkedList.toString()).toBe('1,1,2,4,5'); |
| 60 | + |
| 61 | + linkedList.delete(3); |
| 62 | + expect(linkedList.toString()).toBe('1,1,2,4,5'); |
| 63 | + |
| 64 | + linkedList.delete(1); |
| 65 | + expect(linkedList.toString()).toBe('2,4,5'); |
| 66 | + |
| 67 | + expect(linkedList.head.toString()).toBe('2'); |
| 68 | + expect(linkedList.head.next.next).toBe(linkedList.tail); |
| 69 | + expect(linkedList.tail.previous.previous).toBe(linkedList.head); |
| 70 | + expect(linkedList.tail.toString()).toBe('5'); |
| 71 | + |
| 72 | + linkedList.delete(5); |
| 73 | + expect(linkedList.toString()).toBe('2,4'); |
| 74 | + |
| 75 | + expect(linkedList.head.toString()).toBe('2'); |
| 76 | + expect(linkedList.tail.toString()).toBe('4'); |
| 77 | + |
| 78 | + linkedList.delete(4); |
| 79 | + expect(linkedList.toString()).toBe('2'); |
| 80 | + |
| 81 | + expect(linkedList.head.toString()).toBe('2'); |
| 82 | + expect(linkedList.tail.toString()).toBe('2'); |
| 83 | + expect(linkedList.head).toBe(linkedList.tail); |
| 84 | + |
| 85 | + linkedList.delete(2); |
| 86 | + expect(linkedList.toString()).toBe(''); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should delete linked list tail', () => { |
| 90 | + const linkedList = new DoublyLinkedList(); |
| 91 | + |
| 92 | + expect(linkedList.deleteTail()).toBeNull(); |
| 93 | + |
| 94 | + linkedList.append(1); |
| 95 | + linkedList.append(2); |
| 96 | + linkedList.append(3); |
| 97 | + |
| 98 | + expect(linkedList.head.toString()).toBe('1'); |
| 99 | + expect(linkedList.tail.toString()).toBe('3'); |
| 100 | + |
| 101 | + const deletedNode1 = linkedList.deleteTail(); |
| 102 | + |
| 103 | + expect(deletedNode1.value).toBe(3); |
| 104 | + expect(linkedList.toString()).toBe('1,2'); |
| 105 | + expect(linkedList.head.toString()).toBe('1'); |
| 106 | + expect(linkedList.tail.toString()).toBe('2'); |
| 107 | + |
| 108 | + const deletedNode2 = linkedList.deleteTail(); |
| 109 | + |
| 110 | + expect(deletedNode2.value).toBe(2); |
| 111 | + expect(linkedList.toString()).toBe('1'); |
| 112 | + expect(linkedList.head.toString()).toBe('1'); |
| 113 | + expect(linkedList.tail.toString()).toBe('1'); |
| 114 | + |
| 115 | + const deletedNode3 = linkedList.deleteTail(); |
| 116 | + |
| 117 | + expect(deletedNode3.value).toBe(1); |
| 118 | + expect(linkedList.toString()).toBe(''); |
| 119 | + expect(linkedList.head).toBeNull(); |
| 120 | + expect(linkedList.tail).toBeNull(); |
| 121 | + }); |
| 122 | + |
| 123 | + it('should delete linked list head', () => { |
| 124 | + const linkedList = new DoublyLinkedList(); |
| 125 | + |
| 126 | + expect(linkedList.deleteHead()).toBeNull(); |
| 127 | + |
| 128 | + linkedList.append(1); |
| 129 | + linkedList.append(2); |
| 130 | + |
| 131 | + expect(linkedList.head.toString()).toBe('1'); |
| 132 | + expect(linkedList.tail.toString()).toBe('2'); |
| 133 | + |
| 134 | + const deletedNode1 = linkedList.deleteHead(); |
| 135 | + |
| 136 | + expect(deletedNode1.value).toBe(1); |
| 137 | + expect(linkedList.head.previous).toBeNull(); |
| 138 | + expect(linkedList.toString()).toBe('2'); |
| 139 | + expect(linkedList.head.toString()).toBe('2'); |
| 140 | + expect(linkedList.tail.toString()).toBe('2'); |
| 141 | + |
| 142 | + const deletedNode2 = linkedList.deleteHead(); |
| 143 | + |
| 144 | + expect(deletedNode2.value).toBe(2); |
| 145 | + expect(linkedList.toString()).toBe(''); |
| 146 | + expect(linkedList.head).toBeNull(); |
| 147 | + expect(linkedList.tail).toBeNull(); |
| 148 | + }); |
| 149 | + |
| 150 | + it('should be possible to store objects in the list and to print them out', () => { |
| 151 | + const linkedList = new DoublyLinkedList(); |
| 152 | + |
| 153 | + const nodeValue1 = { value: 1, key: 'key1' }; |
| 154 | + const nodeValue2 = { value: 2, key: 'key2' }; |
| 155 | + |
| 156 | + linkedList |
| 157 | + .append(nodeValue1) |
| 158 | + .prepend(nodeValue2); |
| 159 | + |
| 160 | + const nodeStringifier = value => `${value.key}:${value.value}`; |
| 161 | + |
| 162 | + expect(linkedList.toString(nodeStringifier)).toBe('key2:2,key1:1'); |
| 163 | + }); |
| 164 | + |
| 165 | + it('should find node by value', () => { |
| 166 | + const linkedList = new DoublyLinkedList(); |
| 167 | + |
| 168 | + expect(linkedList.find({ value: 5 })).toBeNull(); |
| 169 | + |
| 170 | + linkedList.append(1); |
| 171 | + expect(linkedList.find({ value: 1 })).toBeDefined(); |
| 172 | + |
| 173 | + linkedList |
| 174 | + .append(2) |
| 175 | + .append(3); |
| 176 | + |
| 177 | + const node = linkedList.find({ value: 2 }); |
| 178 | + |
| 179 | + expect(node.value).toBe(2); |
| 180 | + expect(linkedList.find({ value: 5 })).toBeNull(); |
| 181 | + }); |
| 182 | + |
| 183 | + it('should find node by callback', () => { |
| 184 | + const linkedList = new DoublyLinkedList(); |
| 185 | + |
| 186 | + linkedList |
| 187 | + .append({ value: 1, key: 'test1' }) |
| 188 | + .append({ value: 2, key: 'test2' }) |
| 189 | + .append({ value: 3, key: 'test3' }); |
| 190 | + |
| 191 | + const node = linkedList.find({ callback: value => value.key === 'test2' }); |
| 192 | + |
| 193 | + expect(node).toBeDefined(); |
| 194 | + expect(node.value.value).toBe(2); |
| 195 | + expect(node.value.key).toBe('test2'); |
| 196 | + expect(linkedList.find({ callback: value => value.key === 'test5' })).toBeNull(); |
| 197 | + }); |
| 198 | + |
| 199 | + it('should find node by means of custom compare function', () => { |
| 200 | + const comparatorFunction = (a, b) => { |
| 201 | + if (a.customValue === b.customValue) { |
| 202 | + return 0; |
| 203 | + } |
| 204 | + |
| 205 | + return a.customValue < b.customValue ? -1 : 1; |
| 206 | + }; |
| 207 | + |
| 208 | + const linkedList = new DoublyLinkedList(comparatorFunction); |
| 209 | + |
| 210 | + linkedList |
| 211 | + .append({ value: 1, customValue: 'test1' }) |
| 212 | + .append({ value: 2, customValue: 'test2' }) |
| 213 | + .append({ value: 3, customValue: 'test3' }); |
| 214 | + |
| 215 | + const node = linkedList.find({ |
| 216 | + value: { value: 2, customValue: 'test2' }, |
| 217 | + }); |
| 218 | + |
| 219 | + expect(node).toBeDefined(); |
| 220 | + expect(node.value.value).toBe(2); |
| 221 | + expect(node.value.customValue).toBe('test2'); |
| 222 | + expect(linkedList.find({ value: 2, customValue: 'test5' })).toBeNull(); |
| 223 | + }); |
| 224 | +}); |
0 commit comments