Skip to content

Commit f8a84c2

Browse files
committed
Add test for graph reverse with cycle of length two
1 parent 2c67b48 commit f8a84c2

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

src/data-structures/graph/__test__/Graph.test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,35 @@ describe('Graph', () => {
305305
expect(graph.getNeighbors(vertexD)[0].getKey()).toBe(vertexC.getKey());
306306
});
307307

308+
it('should be possible to reverse directed graph with cycle of lenght two', () => {
309+
const vertexA = new GraphVertex('A');
310+
const vertexB = new GraphVertex('B');
311+
312+
const edgeAB = new GraphEdge(vertexA, vertexB);
313+
const edgeBA = new GraphEdge(vertexB, vertexA);
314+
315+
const graph = new Graph(true);
316+
graph
317+
.addEdge(edgeAB)
318+
.addEdge(edgeBA);
319+
320+
expect(graph.toString()).toBe('A,B');
321+
expect(graph.getAllEdges().length).toBe(2);
322+
expect(graph.getNeighbors(vertexA).length).toBe(1);
323+
expect(graph.getNeighbors(vertexA)[0].getKey()).toBe(vertexB.getKey());
324+
expect(graph.getNeighbors(vertexB).length).toBe(1);
325+
expect(graph.getNeighbors(vertexB)[0].getKey()).toBe(vertexA.getKey());
326+
327+
graph.reverse();
328+
329+
expect(graph.toString()).toBe('A,B');
330+
expect(graph.getAllEdges().length).toBe(2);
331+
expect(graph.getNeighbors(vertexA).length).toBe(1);
332+
expect(graph.getNeighbors(vertexA)[0].getKey()).toBe(vertexB.getKey());
333+
expect(graph.getNeighbors(vertexB).length).toBe(1);
334+
expect(graph.getNeighbors(vertexB)[0].getKey()).toBe(vertexA.getKey());
335+
});
336+
308337
it('should return vertices indices', () => {
309338
const vertexA = new GraphVertex('A');
310339
const vertexB = new GraphVertex('B');

0 commit comments

Comments
 (0)