View Javadoc
1   package edu.uci.ics.jung.graph;
2   
3   import java.util.Collection;
4   import java.util.Collections;
5   
6   import junit.framework.TestCase;
7   import edu.uci.ics.jung.graph.util.EdgeType;
8   import edu.uci.ics.jung.graph.util.Pair;
9   
10  public abstract class AbstractUndirectedSparseMultigraphTest extends TestCase {
11  
12      protected Integer v0 = new Integer(0);
13      protected Integer v1 = new Integer(1);
14      protected Integer v2 = new Integer(2);
15      
16      protected Float e01 = new Float(.1f);
17      protected Float e10 = new Float(.2f);
18      protected Float e12 = new Float(.3f);
19      protected Float e21 = new Float(.4f);
20      
21      protected Graph<Integer,Number> graph;
22  
23      public void testGetEdges() {
24          assertEquals(graph.getEdgeCount(), 4);
25      }
26  
27      public void testGetVertices() {
28          assertEquals(graph.getVertexCount(), 3);
29      }
30  
31      public void testAddVertex() {
32          int count = graph.getVertexCount();
33          graph.addVertex(new Integer(3));
34          assertEquals(graph.getVertexCount(), count+1);
35      }
36  
37      public void testRemoveEndVertex() {
38          int vertexCount = graph.getVertexCount();
39          graph.removeVertex(v0);
40          assertEquals(vertexCount-1, graph.getVertexCount());
41          assertEquals(2, graph.getEdgeCount());
42      }
43  
44      public void testRemoveMiddleVertex() {
45          int vertexCount = graph.getVertexCount();
46          graph.removeVertex(v1);
47          assertEquals(vertexCount-1, graph.getVertexCount());
48          assertEquals(0, graph.getEdgeCount());
49      }
50  
51      public void testAddEdge() {
52          int edgeCount = graph.getEdgeCount();
53          graph.addEdge(new Double(.5), v0, v1);
54          assertEquals(graph.getEdgeCount(), edgeCount+1);
55      }
56  
57      public void testRemoveEdge() {
58          int edgeCount = graph.getEdgeCount();
59          graph.removeEdge(e12);
60          assertEquals(graph.getEdgeCount(), edgeCount-1);
61      }
62  
63      public void testGetInEdges() {
64          assertEquals(graph.getInEdges(v1).size(), 4);
65      }
66  
67      public void testGetOutEdges() {
68          assertEquals(graph.getOutEdges(v1).size(), 4);
69      }
70  
71      public void testGetPredecessors() {
72          assertTrue(graph.getPredecessors(v0).containsAll(Collections.singleton(v1)));
73      }
74  
75      public void testGetSuccessors() {
76          assertTrue(graph.getPredecessors(v1).contains(v0));
77          assertTrue(graph.getPredecessors(v1).contains(v2));
78      }
79  
80      public void testGetNeighbors() {
81          Collection<Integer> neighbors = graph.getNeighbors(v1);
82          assertTrue(neighbors.contains(v0));
83          assertTrue(neighbors.contains(v2));
84      }
85  
86      public void testGetIncidentEdges() {
87          assertEquals(graph.getIncidentEdges(v0).size(), 2);
88      }
89  
90      public void testFindEdge() {
91          Number edge = graph.findEdge(v1, v2);
92          assertTrue(edge == e12 || edge == e21);
93      }
94  
95      public void testNullEndpoint() {
96      	try {
97      		graph.addEdge(.99, new Pair<Integer>(1,null));
98      		fail("should not be able to add an edge with a null endpoint");
99      	} catch(IllegalArgumentException e) {
100     		// all is well
101     	}
102     }
103 
104     public void testGetEndpoints() {
105         Pair<Integer> endpoints = graph.getEndpoints(e01);
106         assertTrue((endpoints.getFirst() == v0 && endpoints.getSecond() == v1) ||
107                 endpoints.getFirst() == v1 && endpoints.getSecond() == v0);
108     }
109 
110     public void testIsDirected() {
111         for(Number edge : graph.getEdges()) {
112             assertEquals(graph.getEdgeType(edge), EdgeType.UNDIRECTED);
113         }
114     }
115 
116     public void testAddDirectedEdge() {
117         try {
118             graph.addEdge(new Float(.9), v1, v2, EdgeType.DIRECTED);
119             fail("Cannot add a directed edge to this graph");
120         } catch(IllegalArgumentException uoe) {
121             // all is well
122         }
123     }
124 }