UNPKG

dist-javascript-algorithms-and-data-structures

Version:

Algorithms and data-structures implemented on JavaScript

38 lines (32 loc) 1.68 kB
"use strict"; var _GraphVertex = _interopRequireDefault(require("../../../../data-structures/graph/GraphVertex")); var _GraphEdge = _interopRequireDefault(require("../../../../data-structures/graph/GraphEdge")); var _Graph = _interopRequireDefault(require("../../../../data-structures/graph/Graph")); var _detectDirectedCycle = _interopRequireDefault(require("../detectDirectedCycle")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } describe('detectDirectedCycle', () => { it('should detect directed cycle', () => { const vertexA = new _GraphVertex.default('A'); const vertexB = new _GraphVertex.default('B'); const vertexC = new _GraphVertex.default('C'); const vertexD = new _GraphVertex.default('D'); const vertexE = new _GraphVertex.default('E'); const vertexF = new _GraphVertex.default('F'); const edgeAB = new _GraphEdge.default(vertexA, vertexB); const edgeBC = new _GraphEdge.default(vertexB, vertexC); const edgeAC = new _GraphEdge.default(vertexA, vertexC); const edgeDA = new _GraphEdge.default(vertexD, vertexA); const edgeDE = new _GraphEdge.default(vertexD, vertexE); const edgeEF = new _GraphEdge.default(vertexE, vertexF); const edgeFD = new _GraphEdge.default(vertexF, vertexD); const graph = new _Graph.default(true); graph.addEdge(edgeAB).addEdge(edgeBC).addEdge(edgeAC).addEdge(edgeDA).addEdge(edgeDE).addEdge(edgeEF); expect((0, _detectDirectedCycle.default)(graph)).toBeNull(); graph.addEdge(edgeFD); expect((0, _detectDirectedCycle.default)(graph)).toEqual({ D: vertexF, F: vertexE, E: vertexD }); }); });