|
| 1 | +import Graph from '../Graph'; |
| 2 | + |
| 3 | +describe('Graphs', () => { |
| 4 | + test('create an empty graph', () => { |
| 5 | + const graph = new Graph(); |
| 6 | + |
| 7 | + expect(graph).toBeDefined(); |
| 8 | + }); |
| 9 | + |
| 10 | + test('add a new vertex to the graph', () => { |
| 11 | + const graph = new Graph(); |
| 12 | + |
| 13 | + graph.addVertex('0'); |
| 14 | + graph.addVertex('1'); |
| 15 | + graph.addVertex('2'); |
| 16 | + graph.addVertex('3'); |
| 17 | + graph.addVertex('4'); |
| 18 | + graph.addVertex('5'); |
| 19 | + graph.addVertex('6'); |
| 20 | + |
| 21 | + expect(graph.numberOfNodes).toBe(7); |
| 22 | + expect(graph.adjacentList[0]).toEqual([]); |
| 23 | + expect(graph.adjacentList[1]).toEqual([]); |
| 24 | + expect(graph.adjacentList[2]).toEqual([]); |
| 25 | + expect(graph.adjacentList[3]).toEqual([]); |
| 26 | + expect(graph.adjacentList[4]).toEqual([]); |
| 27 | + expect(graph.adjacentList[5]).toEqual([]); |
| 28 | + expect(graph.adjacentList[6]).toEqual([]); |
| 29 | + }); |
| 30 | + |
| 31 | + test('throws error if vertex already exists', () => { |
| 32 | + try { |
| 33 | + const graph = new Graph(); |
| 34 | + |
| 35 | + graph.addVertex('0'); |
| 36 | + graph.addVertex('1'); |
| 37 | + graph.addVertex('2'); |
| 38 | + |
| 39 | + expect(graph.addVertex('2')).toThrowError(); |
| 40 | + } catch (error) { |
| 41 | + expect(error).toBeInstanceOf(Error); |
| 42 | + expect(error).toHaveProperty('message', 'The node already exists'); |
| 43 | + } |
| 44 | + }); |
| 45 | + |
| 46 | + test('add edges to graph', () => { |
| 47 | + const graph = new Graph(); |
| 48 | + |
| 49 | + graph.addVertex('0'); |
| 50 | + graph.addVertex('1'); |
| 51 | + graph.addVertex('2'); |
| 52 | + graph.addVertex('3'); |
| 53 | + graph.addVertex('4'); |
| 54 | + graph.addVertex('5'); |
| 55 | + graph.addVertex('6'); |
| 56 | + |
| 57 | + graph.addEdge('3', '1'); |
| 58 | + graph.addEdge('3', '4'); |
| 59 | + graph.addEdge('4', '2'); |
| 60 | + graph.addEdge('4', '5'); |
| 61 | + graph.addEdge('1', '2'); |
| 62 | + graph.addEdge('1', '0'); |
| 63 | + graph.addEdge('0', '2'); |
| 64 | + graph.addEdge('6', '5'); |
| 65 | + |
| 66 | + expect(graph.hasEdge('3', '4')).toBeTruthy(); |
| 67 | + expect(graph.hasEdge('3', '1')).toBeTruthy(); |
| 68 | + expect(graph.hasEdge('4', '5')).toBeTruthy(); |
| 69 | + expect(graph.hasEdge('1', '2')).toBeTruthy(); |
| 70 | + }); |
| 71 | +}); |
0 commit comments