@@ -261,4 +261,43 @@ describe('Graph', () => {
261261
262262 expect ( deleteNotExistingEdge ) . toThrowError ( ) ;
263263 } ) ;
264+
265+ it ( 'should be possible to reverse graph' , ( ) => {
266+ const vertexA = new GraphVertex ( 'A' ) ;
267+ const vertexB = new GraphVertex ( 'B' ) ;
268+ const vertexC = new GraphVertex ( 'C' ) ;
269+ const vertexD = new GraphVertex ( 'D' ) ;
270+
271+ const edgeAB = new GraphEdge ( vertexA , vertexB ) ;
272+ const edgeAC = new GraphEdge ( vertexA , vertexC ) ;
273+ const edgeCD = new GraphEdge ( vertexC , vertexD ) ;
274+
275+ const graph = new Graph ( true ) ;
276+ graph
277+ . addEdge ( edgeAB )
278+ . addEdge ( edgeAC )
279+ . addEdge ( edgeCD ) ;
280+
281+ expect ( graph . toString ( ) ) . toBe ( 'A,B,C,D' ) ;
282+ expect ( graph . getAllEdges ( ) . length ) . toBe ( 3 ) ;
283+ expect ( graph . getNeighbors ( vertexA ) . length ) . toBe ( 2 ) ;
284+ expect ( graph . getNeighbors ( vertexA ) [ 0 ] . getKey ( ) ) . toBe ( vertexB . getKey ( ) ) ;
285+ expect ( graph . getNeighbors ( vertexA ) [ 1 ] . getKey ( ) ) . toBe ( vertexC . getKey ( ) ) ;
286+ expect ( graph . getNeighbors ( vertexB ) . length ) . toBe ( 0 ) ;
287+ expect ( graph . getNeighbors ( vertexC ) . length ) . toBe ( 1 ) ;
288+ expect ( graph . getNeighbors ( vertexC ) [ 0 ] . getKey ( ) ) . toBe ( vertexD . getKey ( ) ) ;
289+ expect ( graph . getNeighbors ( vertexD ) . length ) . toBe ( 0 ) ;
290+
291+ graph . reverse ( ) ;
292+
293+ expect ( graph . toString ( ) ) . toBe ( 'A,B,C,D' ) ;
294+ expect ( graph . getAllEdges ( ) . length ) . toBe ( 3 ) ;
295+ expect ( graph . getNeighbors ( vertexA ) . length ) . toBe ( 0 ) ;
296+ expect ( graph . getNeighbors ( vertexB ) . length ) . toBe ( 1 ) ;
297+ expect ( graph . getNeighbors ( vertexB ) [ 0 ] . getKey ( ) ) . toBe ( vertexA . getKey ( ) ) ;
298+ expect ( graph . getNeighbors ( vertexC ) . length ) . toBe ( 1 ) ;
299+ expect ( graph . getNeighbors ( vertexC ) [ 0 ] . getKey ( ) ) . toBe ( vertexA . getKey ( ) ) ;
300+ expect ( graph . getNeighbors ( vertexD ) . length ) . toBe ( 1 ) ;
301+ expect ( graph . getNeighbors ( vertexD ) [ 0 ] . getKey ( ) ) . toBe ( vertexC . getKey ( ) ) ;
302+ } ) ;
264303} ) ;
0 commit comments