1
2
3
4
5
6
7
8
9
10
11
12 package edu.uci.ics.jung.graph;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16
17 import junit.framework.TestCase;
18
19 import com.google.common.base.Supplier;
20
21 import edu.uci.ics.jung.graph.util.Pair;
22
23
24 public abstract class AbstractHypergraphTest extends TestCase
25 {
26 protected Supplier<? extends Hypergraph<Integer,Character>> factory;
27 protected Hypergraph<Integer,Character> h;
28
29 public AbstractHypergraphTest(Supplier<? extends Hypergraph<Integer,Character>> factory)
30 {
31 this.factory = factory;
32 }
33
34 @Override
35 public void runTest() throws Exception {
36 setUp();
37 testAddVertex();
38 testAddEdge();
39 testEdgeEndpoints();
40 tearDown();
41 }
42
43
44
45
46
47
48
49
50
51
52 public void testAddVertex()
53 {
54 int count = h.getVertexCount();
55 assertTrue(h.addVertex(new Integer(1)));
56 assertEquals(count+1, h.getVertexCount());
57 assertTrue(h.containsVertex(1));
58 boolean success = false;
59 try
60 {
61 success = h.addVertex(null);
62 fail("Implementation should disallow null vertices");
63 }
64 catch (IllegalArgumentException iae) {}
65 catch (NullPointerException npe)
66 {
67 fail("Implementation should actively prevent null vertices");
68 }
69 assertFalse(success);
70 assertFalse(h.addVertex(1));
71 assertEquals(count+1, h.getVertexCount());
72 assertFalse(h.containsVertex(2));
73 }
74
75
76
77
78
79
80
81
82
83
84
85 public void testAddEdge()
86 {
87 int edge_count = h.getEdgeCount();
88 int vertex_count = h.getVertexCount();
89 Pair<Integer> p = new Pair<Integer>(2, 3);
90 assertTrue(h.addEdge('a', p));
91 assertEquals(edge_count+1, h.getEdgeCount());
92 assertEquals(vertex_count+2, h.getVertexCount());
93 assertTrue(h.containsEdge('a'));
94 boolean success = false;
95 try
96 {
97 success = h.addEdge('b', null);
98 fail("Implementation should disallow null pairs/collections");
99 success = h.addEdge(null, p);
100 fail("Implementation should disallow null edges");
101 }
102 catch (IllegalArgumentException iae) {}
103 catch (NullPointerException npe)
104 {
105 fail("Implementation should actively prevent null edges, pairs, and collections");
106 }
107 assertFalse(success);
108
109 assertFalse(h.addEdge('a', new Pair<Integer>(2,3)));
110
111 assertFalse(h.addEdge('a', p));
112 try
113 {
114 success = h.addEdge('a', new Pair<Integer>(3,4));
115 fail("Implementation should disallow existing edge objects from connecting new pairs/collections");
116 }
117 catch (IllegalArgumentException iae) {}
118 assertEquals(edge_count+1, h.getEdgeCount());
119 assertFalse(h.containsEdge('b'));
120 }
121
122
123
124
125
126
127
128
129
130
131 public void testEdgeEndpoints()
132 {
133 Collection<Integer> c = new ArrayList<Integer>();
134 for (int i = 0; i < 10; i++)
135 {
136 try
137 {
138 h.addEdge((char)i, c);
139 c.add(i);
140 }
141 catch (IllegalArgumentException iae)
142 {
143 if (h instanceof Graph)
144 {
145 if (c.size() == 2)
146 fail("improperly rejected incident vertex collection " + c);
147 }
148 else
149 fail("hypergraph implementations should accept any positive number of incident vertices");
150 }
151 }
152 }
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168 public void testFindEdge()
169 {
170
171 }
172 }