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 AbstractSparseMultigraphTest 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 }
45
46 public void testGetVertices() {
47 assertEquals(smallGraph.getVertexCount(), 3);
48 }
49
50 public void testAddVertex() {
51 int count = graph.getVertexCount();
52 graph.addVertex(count);
53 assertEquals(graph.getVertexCount(), count+1);
54 }
55
56 public void testRemoveEndVertex() {
57 int vertexCount = graph.getVertexCount();
58 int edgeCount = graph.getEdgeCount();
59 Collection<Number> incident = graph.getIncidentEdges(vertexCount-1);
60 graph.removeVertex(vertexCount-1);
61 assertEquals(vertexCount-1, graph.getVertexCount());
62 assertEquals(edgeCount - incident.size(), graph.getEdgeCount());
63 }
64
65 public void testRemoveMiddleVertex() {
66 int vertexCount = graph.getVertexCount();
67 int edgeCount = graph.getEdgeCount();
68 Collection<Number> incident = graph.getIncidentEdges(vertexCount/2);
69 graph.removeVertex(vertexCount/2);
70 assertEquals(vertexCount-1, graph.getVertexCount());
71 assertEquals(edgeCount - incident.size(), graph.getEdgeCount());
72 }
73
74 public void testAddEdge() {
75 int edgeCount = graph.getEdgeCount();
76 graph.addEdge(edgeFactory.get(), 0, 1);
77 assertEquals(graph.getEdgeCount(), edgeCount+1);
78 }
79
80 public void testNullEndpoint() {
81 try {
82 graph.addEdge(edgeFactory.get(), new Pair<Number>(1,null));
83 fail("should not be able to add an edge with a null endpoint");
84 } catch(IllegalArgumentException e) {
85
86 }
87 }
88
89
90 public void testRemoveEdge() {
91 List<Number> edgeList = new ArrayList<Number>(graph.getEdges());
92 int edgeCount = graph.getEdgeCount();
93 graph.removeEdge(edgeList.get(edgeList.size()/2));
94 assertEquals(graph.getEdgeCount(), edgeCount-1);
95 }
96
97 public void testGetInOutEdges() {
98 for(Number v : graph.getVertices()) {
99 Collection<Number> incident = graph.getIncidentEdges(v);
100 Collection<Number> in = graph.getInEdges(v);
101 Collection<Number> out = graph.getOutEdges(v);
102 assertTrue(incident.containsAll(in));
103 assertTrue(incident.containsAll(out));
104 for(Number e : in) {
105 if(out.contains(e)) {
106 assertTrue(graph.getEdgeType(e) == EdgeType.UNDIRECTED);
107 }
108 }
109 for(Number e : out) {
110 if(in.contains(e)) {
111 assertTrue(graph.getEdgeType(e) == EdgeType.UNDIRECTED);
112 }
113 }
114 }
115
116 assertEquals(smallGraph.getInEdges(v1).size(), 4);
117 assertEquals(smallGraph.getOutEdges(v1).size(), 3);
118 assertEquals(smallGraph.getOutEdges(v0).size(), 2);
119 }
120
121 public void testGetPredecessors() {
122 assertTrue(smallGraph.getPredecessors(v0).containsAll(Collections.singleton(v1)));
123 }
124
125 public void testGetSuccessors() {
126 assertTrue(smallGraph.getPredecessors(v1).contains(v0));
127 assertTrue(smallGraph.getPredecessors(v1).contains(v2));
128 }
129
130 public void testGetNeighbors() {
131 Collection<Integer> neighbors = smallGraph.getNeighbors(v1);
132 assertTrue(neighbors.contains(v0));
133 assertTrue(neighbors.contains(v2));
134 }
135
136 public void testGetIncidentEdges() {
137 assertEquals(smallGraph.getIncidentEdges(v0).size(), 2);
138 }
139
140 public void testFindEdge() {
141 Number edge = smallGraph.findEdge(v1, v2);
142 assertTrue(edge == e12 || edge == e21);
143 }
144
145 public void testGetEndpoints() {
146 Pair<Integer> endpoints = smallGraph.getEndpoints(e01);
147 assertTrue((endpoints.getFirst() == v0 && endpoints.getSecond() == v1) ||
148 endpoints.getFirst() == v1 && endpoints.getSecond() == v0);
149 }
150
151 public void testIsDirected() {
152 for(Number edge : smallGraph.getEdges()) {
153 if(edge == e21) {
154 assertEquals(smallGraph.getEdgeType(edge), EdgeType.DIRECTED);
155 } else {
156 assertEquals(smallGraph.getEdgeType(edge), EdgeType.UNDIRECTED);
157 }
158 }
159 }
160 }