View Javadoc
1   /*
2   * Copyright (c) 2003, The JUNG Authors 
3   *
4   * All rights reserved.
5   *
6   * This software is open-source under the BSD license; see either
7   * "license.txt" or
8   * https://github.com/jrtom/jung/blob/master/LICENSE for a description.
9   */
10  package edu.uci.ics.jung.algorithms.shortestpath;
11  
12  import junit.framework.Assert;
13  import junit.framework.Test;
14  import junit.framework.TestCase;
15  import junit.framework.TestSuite;
16  import edu.uci.ics.jung.algorithms.shortestpath.BFSDistanceLabeler;
17  import edu.uci.ics.jung.graph.Graph;
18  import edu.uci.ics.jung.graph.UndirectedSparseMultigraph;
19  
20  /**
21   * @author Scott White, adapted to jung2 by Tom Nelson
22   */
23  public class TestBFSDistanceLabeler extends TestCase {
24  	public static Test suite() {
25  		return new TestSuite(TestBFSDistanceLabeler.class);
26  	}
27  
28  	@Override
29    protected void setUp() {
30  
31  	}
32  
33  	public void test() {
34          Graph<Number,Number> graph = new UndirectedSparseMultigraph<Number,Number>();
35          for(int i=0; i<6; i++) {
36          	graph.addVertex(i);
37          }
38          int j = 0;
39          graph.addEdge(j++,0,1);
40          graph.addEdge(j++,0,5);
41          graph.addEdge(j++,0,3);
42          graph.addEdge(j++,0,4);
43          graph.addEdge(j++,1,5);
44          graph.addEdge(j++,3,4);
45          graph.addEdge(j++,3,2);
46          graph.addEdge(j++,5,2);
47          Number root = 0;
48  
49  		BFSDistanceLabeler<Number,Number> labeler = new BFSDistanceLabeler<Number,Number>();
50  		labeler.labelDistances(graph,root);
51  
52  		Assert.assertEquals(labeler.getPredecessors(root).size(),0);
53          Assert.assertEquals(labeler.getPredecessors(1).size(),1);
54          Assert.assertEquals(labeler.getPredecessors(2).size(),2);
55          Assert.assertEquals(labeler.getPredecessors(3).size(),1);
56          Assert.assertEquals(labeler.getPredecessors(4).size(),1);
57          Assert.assertEquals(labeler.getPredecessors(5).size(),1);
58  
59          Assert.assertEquals(labeler.getDistance(graph,0),0);
60          Assert.assertEquals(labeler.getDistance(graph,1),1);
61          Assert.assertEquals(labeler.getDistance(graph,2),2);
62          Assert.assertEquals(labeler.getDistance(graph,3),1);
63          Assert.assertEquals(labeler.getDistance(graph,4),1);
64          Assert.assertEquals(labeler.getDistance(graph,5),1);
65  
66  	}
67  }