View Javadoc
1   package edu.uci.ics.jung.graph;
2   
3   import java.util.ArrayList;
4   import java.util.Collection;
5   import java.util.Collections;
6   import java.util.List;
7   
8   import junit.framework.TestCase;
9   
10  import com.google.common.base.Supplier;
11  
12  import edu.uci.ics.jung.graph.util.EdgeType;
13  import edu.uci.ics.jung.graph.util.Pair;
14  
15  public abstract class AbstractOrderedSparseMultigraphTest extends TestCase {
16  
17      protected Integer v0 = 0;
18      protected Integer v1 = 1;
19      protected Integer v2 = 2;
20      protected Number e01 = .1;
21      protected Number e10 = .2;
22      protected Number e12 = .3;
23      protected Number e21 = .4;
24  
25      protected Supplier<Number> vertexFactory = new Supplier<Number>() {
26      	int v=0;
27  		public Number get() {
28  			return v++;
29  		}
30      };
31      protected Supplier<Number> edgeFactory = new Supplier<Number>() {
32      	int e=0;
33  		public Number get() {
34  			return e++;
35  		}
36      };
37      
38      protected Graph<Number,Number> graph;
39      protected int vertexCount = 50;
40      protected Graph<Integer,Number> smallGraph;
41  
42      public void testGetEdges() {
43          assertEquals(smallGraph.getEdgeCount(), 4);
44  //        System.err.println("getEdges()="+graph.getEdges());
45      }
46  
47      public void testGetVertices() {
48          assertEquals(smallGraph.getVertexCount(), 3);
49  //        System.err.println("getVertices()="+graph.getVertices());
50      }
51  
52      public void testAddVertex() {
53          int count = graph.getVertexCount();
54          graph.addVertex(count);
55          assertEquals(graph.getVertexCount(), count+1);
56      }
57  
58      public void testRemoveEndVertex() {
59          int vertexCount = graph.getVertexCount();
60          int edgeCount = graph.getEdgeCount();
61          Collection<Number> incident = graph.getIncidentEdges(vertexCount-1);
62          graph.removeVertex(vertexCount-1);
63          assertEquals(vertexCount-1, graph.getVertexCount());
64          assertEquals(edgeCount - incident.size(), graph.getEdgeCount());
65      }
66  
67      public void testRemoveMiddleVertex() {
68          int vertexCount = graph.getVertexCount();
69          int edgeCount = graph.getEdgeCount();
70          Collection<Number> incident = graph.getIncidentEdges(vertexCount/2);
71          graph.removeVertex(vertexCount/2);
72          assertEquals(vertexCount-1, graph.getVertexCount());
73          assertEquals(edgeCount - incident.size(), graph.getEdgeCount());
74      }
75  
76      public void testAddEdge() {
77          int edgeCount = graph.getEdgeCount();
78          graph.addEdge(edgeFactory.get(), 0, 1);
79          assertEquals(graph.getEdgeCount(), edgeCount+1);
80      }
81      
82      public void testNullEndpoint() {
83      	try {
84      		graph.addEdge(edgeFactory.get(), new Pair<Number>(1,null));
85      		fail("should not be able to add an edge with a null endpoint");
86      	} catch(IllegalArgumentException e) {
87      		// all is well
88      	}
89      }
90  
91  
92      public void testRemoveEdge() {
93      	List<Number> edgeList = new ArrayList<Number>(graph.getEdges());
94          int edgeCount = graph.getEdgeCount();
95          graph.removeEdge(edgeList.get(edgeList.size()/2));
96          assertEquals(graph.getEdgeCount(), edgeCount-1);
97      }
98  
99      public void testGetInOutEdges() {
100     	for(Number v : graph.getVertices()) {
101     		Collection<Number> incident = graph.getIncidentEdges(v);
102     		Collection<Number> in = graph.getInEdges(v);
103     		Collection<Number> out = graph.getOutEdges(v);
104     		assertTrue(incident.containsAll(in));
105     		assertTrue(incident.containsAll(out));
106     		for(Number e : in) {
107     			if(out.contains(e)) {
108     				assertTrue(graph.getEdgeType(e) == EdgeType.UNDIRECTED);
109     			}
110     		}
111     		for(Number e : out) {
112     			if(in.contains(e)) {
113     				assertTrue(graph.getEdgeType(e) == EdgeType.UNDIRECTED);
114     			}
115     		}
116     	}
117     	
118         assertEquals(smallGraph.getInEdges(v1).size(), 4);
119         assertEquals(smallGraph.getOutEdges(v1).size(), 3);
120         assertEquals(smallGraph.getOutEdges(v0).size(), 2);
121     }
122 
123     public void testGetPredecessors() {
124         assertTrue(smallGraph.getPredecessors(v0).containsAll(Collections.singleton(v1)));
125     }
126 
127     public void testGetSuccessors() {
128         assertTrue(smallGraph.getPredecessors(v1).contains(v0));
129         assertTrue(smallGraph.getPredecessors(v1).contains(v2));
130     }
131 
132     public void testGetNeighbors() {
133         Collection<Integer> neighbors = smallGraph.getNeighbors(v1);
134         assertTrue(neighbors.contains(v0));
135         assertTrue(neighbors.contains(v2));
136     }
137 
138     public void testGetIncidentEdges() {
139         assertEquals(smallGraph.getIncidentEdges(v0).size(), 2);
140     }
141 
142     public void testFindEdge() {
143         Number edge = smallGraph.findEdge(v1, v2);
144         assertTrue(edge == e12 || edge == e21);
145     }
146 
147     public void testGetEndpoints() {
148         Pair<Integer> endpoints = smallGraph.getEndpoints(e01);
149         assertTrue((endpoints.getFirst() == v0 && endpoints.getSecond() == v1) ||
150                 endpoints.getFirst() == v1 && endpoints.getSecond() == v0);
151     }
152 
153     public void testIsDirected() {
154         for(Number edge : smallGraph.getEdges()) {
155         	if(edge == e21) {
156         		assertEquals(smallGraph.getEdgeType(edge), EdgeType.DIRECTED);
157         	} else {
158         		assertEquals(smallGraph.getEdgeType(edge), EdgeType.UNDIRECTED);
159         	}
160         }
161     }
162 }