Skip to content

Commit 705c3be

Browse files
itsamirhntrekhleb
authored andcommitted
Add test for graph reverse with cycle of length two
1 parent 7563da8 commit 705c3be

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
@@ -318,6 +318,35 @@ describe('Graph', () => {
318318
expect(graph.getNeighbors(vertexD)[0].getKey()).toBe(vertexC.getKey());
319319
});
320320

321+
it('should be possible to reverse directed graph with cycle of lenght two', () => {
322+
const vertexA = new GraphVertex('A');
323+
const vertexB = new GraphVertex('B');
324+
325+
const edgeAB = new GraphEdge(vertexA, vertexB);
326+
const edgeBA = new GraphEdge(vertexB, vertexA);
327+
328+
const graph = new Graph(true);
329+
graph
330+
.addEdge(edgeAB)
331+
.addEdge(edgeBA);
332+
333+
expect(graph.toString()).toBe('A,B');
334+
expect(graph.getAllEdges().length).toBe(2);
335+
expect(graph.getNeighbors(vertexA).length).toBe(1);
336+
expect(graph.getNeighbors(vertexA)[0].getKey()).toBe(vertexB.getKey());
337+
expect(graph.getNeighbors(vertexB).length).toBe(1);
338+
expect(graph.getNeighbors(vertexB)[0].getKey()).toBe(vertexA.getKey());
339+
340+
graph.reverse();
341+
342+
expect(graph.toString()).toBe('A,B');
343+
expect(graph.getAllEdges().length).toBe(2);
344+
expect(graph.getNeighbors(vertexA).length).toBe(1);
345+
expect(graph.getNeighbors(vertexA)[0].getKey()).toBe(vertexB.getKey());
346+
expect(graph.getNeighbors(vertexB).length).toBe(1);
347+
expect(graph.getNeighbors(vertexB)[0].getKey()).toBe(vertexA.getKey());
348+
});
349+
321350
it('should return vertices indices', () => {
322351
const vertexA = new GraphVertex('A');
323352
const vertexB = new GraphVertex('B');

0 commit comments

Comments
 (0)