|
| 1 | +import Graph from '../Graph'; |
| 2 | +import GraphVertex from '../GraphVertex'; |
| 3 | +import GraphEdge from '../GraphEdge'; |
| 4 | + |
| 5 | +describe('Graph', () => { |
| 6 | + it('should add vertices to graph', () => { |
| 7 | + const graph = new Graph(); |
| 8 | + |
| 9 | + const vertexA = new GraphVertex('A'); |
| 10 | + const vertexB = new GraphVertex('B'); |
| 11 | + |
| 12 | + graph |
| 13 | + .addVertex(vertexA) |
| 14 | + .addVertex(vertexB); |
| 15 | + |
| 16 | + expect(graph.toString()).toBe('A,B'); |
| 17 | + expect(graph.getVertexByKey(vertexA.getKey())).toEqual(vertexA); |
| 18 | + expect(graph.getVertexByKey(vertexB.getKey())).toEqual(vertexB); |
| 19 | + }); |
| 20 | + |
| 21 | + it('should add edges to undirected graph', () => { |
| 22 | + const graph = new Graph(); |
| 23 | + |
| 24 | + const vertexA = new GraphVertex('A'); |
| 25 | + const vertexB = new GraphVertex('B'); |
| 26 | + |
| 27 | + const edgeAB = new GraphEdge(vertexA, vertexB); |
| 28 | + |
| 29 | + graph.addEdge(edgeAB); |
| 30 | + |
| 31 | + const graphVertexA = graph.findVertexByKey(vertexA.getKey()); |
| 32 | + const graphVertexB = graph.findVertexByKey(vertexB.getKey()); |
| 33 | + |
| 34 | + expect(graph.toString()).toBe('A,B'); |
| 35 | + expect(graphVertexA).toBeDefined(); |
| 36 | + expect(graphVertexB).toBeDefined(); |
| 37 | + |
| 38 | + expect(graph.findVertexByKey('not existing')).toBeNull(); |
| 39 | + |
| 40 | + expect(graphVertexA.getNeighbors().length).toBe(1); |
| 41 | + expect(graphVertexA.getNeighbors()[0]).toEqual(vertexB); |
| 42 | + expect(graphVertexA.getNeighbors()[0]).toEqual(graphVertexB); |
| 43 | + |
| 44 | + expect(graphVertexB.getNeighbors().length).toBe(1); |
| 45 | + expect(graphVertexB.getNeighbors()[0]).toEqual(vertexA); |
| 46 | + expect(graphVertexB.getNeighbors()[0]).toEqual(graphVertexA); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should add edges to directed graph', () => { |
| 50 | + const graph = new Graph(true); |
| 51 | + |
| 52 | + const vertexA = new GraphVertex('A'); |
| 53 | + const vertexB = new GraphVertex('B'); |
| 54 | + |
| 55 | + const edgeAB = new GraphEdge(vertexA, vertexB); |
| 56 | + |
| 57 | + graph.addEdge(edgeAB); |
| 58 | + |
| 59 | + const graphVertexA = graph.findVertexByKey(vertexA.getKey()); |
| 60 | + const graphVertexB = graph.findVertexByKey(vertexB.getKey()); |
| 61 | + |
| 62 | + expect(graph.toString()).toBe('A,B'); |
| 63 | + expect(graphVertexA).toBeDefined(); |
| 64 | + expect(graphVertexB).toBeDefined(); |
| 65 | + |
| 66 | + expect(graphVertexA.getNeighbors().length).toBe(1); |
| 67 | + expect(graphVertexA.getNeighbors()[0]).toEqual(vertexB); |
| 68 | + expect(graphVertexA.getNeighbors()[0]).toEqual(graphVertexB); |
| 69 | + |
| 70 | + expect(graphVertexB.getNeighbors().length).toBe(0); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should find edge by vertices in undirected graph', () => { |
| 74 | + const graph = new Graph(); |
| 75 | + |
| 76 | + const vertexA = new GraphVertex('A'); |
| 77 | + const vertexB = new GraphVertex('B'); |
| 78 | + const vertexC = new GraphVertex('C'); |
| 79 | + |
| 80 | + const edgeAB = new GraphEdge(vertexA, vertexB, 10); |
| 81 | + |
| 82 | + graph.addEdge(edgeAB); |
| 83 | + |
| 84 | + const graphEdgeAB = graph.findEdge(vertexA, vertexB); |
| 85 | + const graphEdgeBA = graph.findEdge(vertexB, vertexA); |
| 86 | + const graphEdgeAC = graph.findEdge(vertexB, vertexC); |
| 87 | + |
| 88 | + expect(graphEdgeAC).toBeNull(); |
| 89 | + expect(graphEdgeAB).toEqual(edgeAB); |
| 90 | + expect(graphEdgeBA).toEqual(edgeAB); |
| 91 | + expect(graphEdgeAB.weight).toBe(10); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should find edge by vertices in directed graph', () => { |
| 95 | + const graph = new Graph(true); |
| 96 | + |
| 97 | + const vertexA = new GraphVertex('A'); |
| 98 | + const vertexB = new GraphVertex('B'); |
| 99 | + const vertexC = new GraphVertex('C'); |
| 100 | + |
| 101 | + const edgeAB = new GraphEdge(vertexA, vertexB, 10); |
| 102 | + |
| 103 | + graph.addEdge(edgeAB); |
| 104 | + |
| 105 | + const graphEdgeAB = graph.findEdge(vertexA, vertexB); |
| 106 | + const graphEdgeBA = graph.findEdge(vertexB, vertexA); |
| 107 | + const graphEdgeAC = graph.findEdge(vertexB, vertexC); |
| 108 | + |
| 109 | + expect(graphEdgeAC).toBeNull(); |
| 110 | + expect(graphEdgeBA).toBeNull(); |
| 111 | + expect(graphEdgeAB).toEqual(edgeAB); |
| 112 | + expect(graphEdgeAB.weight).toBe(10); |
| 113 | + }); |
| 114 | +}); |
0 commit comments