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 AbstractDirectedSparseMultigraphTest 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 testNullEndpoint() {
64      	try {
65      		graph.addEdge(.99, new Pair<Integer>(1,null));
66      		fail("should not be able to add an edge with a null endpoint");
67      	} catch(IllegalArgumentException e) {
68      		// all is well
69      	}
70      }
71  
72      public void testGetInEdges() {
73          assertEquals(graph.getInEdges(v1).size(), 2);
74      }
75  
76      public void testGetOutEdges() {
77          assertEquals(graph.getOutEdges(v1).size(), 2);
78      }
79  
80      public void testGetPredecessors() {
81          assertTrue(graph.getPredecessors(v0).containsAll(Collections.singleton(v1)));
82      }
83  
84      public void testGetSuccessors() {
85          assertTrue(graph.getPredecessors(v1).contains(v0));
86          assertTrue(graph.getPredecessors(v1).contains(v2));
87      }
88  
89      public void testGetNeighbors() {
90          Collection<Integer> neighbors = graph.getNeighbors(v1);
91          assertTrue(neighbors.contains(v0));
92          assertTrue(neighbors.contains(v2));
93      }
94  
95      public void testGetIncidentEdges() {
96          assertEquals(graph.getIncidentEdges(v0).size(), 2);
97      }
98  
99      public void testFindEdge() {
100         Number edge = graph.findEdge(v1, v2);
101         assertTrue(edge == e12 || edge == e21);
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.DIRECTED);
113         }
114     }
115 
116     public void testAddDirectedEdge() {
117         Float edge = new Float(.9);
118         graph.addEdge(edge, v1, v2, EdgeType.DIRECTED);
119         assertEquals(graph.getEdgeType(edge), EdgeType.DIRECTED);
120     }
121     
122     public void testAddUndirectedEdge() {
123         try {
124             graph.addEdge(new Float(.9), v1, v2, EdgeType.UNDIRECTED);
125             fail("Cannot add an undirected edge to this graph");
126         } catch(IllegalArgumentException uoe) {
127             // all is well
128         }
129     }
130 
131 }