1
2
3
4
5 package edu.uci.ics.jung.algorithms.shortestpath;
6
7 import junit.framework.Assert;
8 import junit.framework.Test;
9 import junit.framework.TestCase;
10 import junit.framework.TestSuite;
11
12 import com.google.common.base.Supplier;
13 import com.google.common.collect.BiMap;
14
15 import edu.uci.ics.jung.algorithms.util.Indexer;
16 import edu.uci.ics.jung.graph.DirectedGraph;
17 import edu.uci.ics.jung.graph.DirectedSparseMultigraph;
18 import edu.uci.ics.jung.graph.UndirectedGraph;
19 import edu.uci.ics.jung.graph.UndirectedSparseMultigraph;
20
21
22
23
24 public class TestUnweightedShortestPath extends TestCase
25 {
26 private Supplier<String> vertexFactory =
27 new Supplier<String>() {
28 int count = 0;
29 public String get() {
30 return "V"+count++;
31 }};
32
33 private Supplier<Integer> edgeFactory =
34 new Supplier<Integer>() {
35 int count = 0;
36 public Integer get() {
37 return count++;
38 }};
39 BiMap<String,Integer> id;
40
41 @Override
42 protected void setUp() {
43 }
44 public static Test suite()
45 {
46 return new TestSuite(TestUnweightedShortestPath.class);
47 }
48
49 public void testUndirected() {
50 UndirectedGraph<String,Integer> ug =
51 new UndirectedSparseMultigraph<String,Integer>();
52 for(int i=0; i<5; i++) {
53 ug.addVertex(vertexFactory.get());
54 }
55 id = Indexer.<String>create(ug.getVertices());
56
57
58
59 ug.addEdge(edgeFactory.get(), id.inverse().get(0), id.inverse().get(1));
60 ug.addEdge(edgeFactory.get(), id.inverse().get(1), id.inverse().get(2));
61 ug.addEdge(edgeFactory.get(), id.inverse().get(2), id.inverse().get(3));
62 ug.addEdge(edgeFactory.get(), id.inverse().get(0), id.inverse().get(4));
63 ug.addEdge(edgeFactory.get(), id.inverse().get(4), id.inverse().get(3));
64
65 UnweightedShortestPath<String,Integer> usp =
66 new UnweightedShortestPath<String,Integer>(ug);
67 Assert.assertEquals(usp.getDistance(id.inverse().get(0),id.inverse().get(3)).intValue(),2);
68 Assert.assertEquals((usp.getDistanceMap(id.inverse().get(0)).get(id.inverse().get(3))).intValue(),2);
69 Assert.assertNull(usp.getIncomingEdgeMap(id.inverse().get(0)).get(id.inverse().get(0)));
70 Assert.assertNotNull(usp.getIncomingEdgeMap(id.inverse().get(0)).get(id.inverse().get(3)));
71 }
72
73 public void testDirected() {
74 DirectedGraph<String,Integer> dg =
75 new DirectedSparseMultigraph<String,Integer>();
76 for(int i=0; i<5; i++) {
77 dg.addVertex(vertexFactory.get());
78 }
79 id = Indexer.<String>create(dg.getVertices());
80 dg.addEdge(edgeFactory.get(), id.inverse().get(0), id.inverse().get(1));
81 dg.addEdge(edgeFactory.get(), id.inverse().get(1), id.inverse().get(2));
82 dg.addEdge(edgeFactory.get(), id.inverse().get(2), id.inverse().get(3));
83 dg.addEdge(edgeFactory.get(), id.inverse().get(0), id.inverse().get(4));
84 dg.addEdge(edgeFactory.get(), id.inverse().get(4), id.inverse().get(3));
85 dg.addEdge(edgeFactory.get(), id.inverse().get(3), id.inverse().get(0));
86
87 UnweightedShortestPath<String,Integer> usp =
88 new UnweightedShortestPath<String,Integer>(dg);
89 Assert.assertEquals(usp.getDistance(id.inverse().get(0),id.inverse().get(3)).intValue(),2);
90 Assert.assertEquals((usp.getDistanceMap(id.inverse().get(0)).get(id.inverse().get(3))).intValue(),2);
91 Assert.assertNull(usp.getIncomingEdgeMap(id.inverse().get(0)).get(id.inverse().get(0)));
92 Assert.assertNotNull(usp.getIncomingEdgeMap(id.inverse().get(0)).get(id.inverse().get(3)));
93
94 }
95 }