|
| 1 | +import { LinkedList, Node } from '../LinkedList'; |
| 2 | + |
| 3 | +describe('Singly Linked List', () => { |
| 4 | + test('create empty list node', () => { |
| 5 | + const node = new Node(); |
| 6 | + |
| 7 | + expect(node.data).toBeUndefined(); |
| 8 | + expect(node.next).toBeNull(); |
| 9 | + }); |
| 10 | + |
| 11 | + test('create list node with value', () => { |
| 12 | + const node = new Node(10); |
| 13 | + |
| 14 | + expect(node.data).toBe(10); |
| 15 | + expect(node.next).toBeNull(); |
| 16 | + }); |
| 17 | + |
| 18 | + test('create list node with object as a value', () => { |
| 19 | + const nodeValue = { key: 1, value: 'test' }; |
| 20 | + const node = new Node(nodeValue); |
| 21 | + |
| 22 | + expect(node.data.key).toBe(1); |
| 23 | + expect(node.data.value).toBe('test'); |
| 24 | + expect(node.next).toBeNull(); |
| 25 | + }); |
| 26 | + |
| 27 | + test('append node to linked list', () => { |
| 28 | + const linkedList = new LinkedList(); |
| 29 | + |
| 30 | + linkedList.append(5); |
| 31 | + |
| 32 | + expect(linkedList.head.data).toBeUndefined(); |
| 33 | + expect(linkedList.head.next.data).toBe(5); |
| 34 | + expect(linkedList.head.next.next).toBeNull(); |
| 35 | + }); |
| 36 | + |
| 37 | + test('prepend node to linked list', () => { |
| 38 | + const linkedList = new LinkedList(); |
| 39 | + |
| 40 | + linkedList.prepend(5); |
| 41 | + |
| 42 | + expect(linkedList.head.data).toBe(5); |
| 43 | + expect(linkedList.tail.next).toBeNull(); |
| 44 | + }); |
| 45 | + |
| 46 | + test('insert node list in a given index', () => { |
| 47 | + const linkedList = new LinkedList(); |
| 48 | + |
| 49 | + const nodeValue1 = { value: 1, key: 'key1' }; |
| 50 | + const nodeValue2 = { value: 2, key: 'key2' }; |
| 51 | + const nodeValue3 = { value: 3, key: 'key3' }; |
| 52 | + |
| 53 | + linkedList.append(nodeValue1).prepend(nodeValue3).insert(1, nodeValue2); |
| 54 | + |
| 55 | + expect(linkedList.head.next.data).toEqual({ value: 2, key: 'key2' }); |
| 56 | + }); |
| 57 | +}); |
0 commit comments