-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMsfTest.java
More file actions
194 lines (165 loc) · 6.85 KB
/
MsfTest.java
File metadata and controls
194 lines (165 loc) · 6.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package graph;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import support.graph.CS16Edge;
import support.graph.CS16Vertex;
import support.graph.Graph;
import support.graph.MinSpanForest;
/**
* This class tests the functionality of your MSF algorithms on an AdjacencyMatrixGraph
* with a 'String' type parameter for the vertices. Edge elements are Integers.
* The general framework of a test case is: - Name the test method descriptively,
* mentioning what is being tested (it is ok to have slightly verbose method names here)
* Set-up the program state (ex: instantiate a heap and insert K,V pairs into it) - Use
* assertions to validate that the progam is in the state you expect it to be
* See header comments over tests for what each test does
*
* Before each test is run, you can assume that the '_graph' variable is reset to
* a new instance of the a Graph<String> that you can simply use 'as is', as
* well as the '_msf' variable.
*
* Of course, please do not modify anything below the 'DO NOT MODIFY ANYTHING BELOW THIS LINE'
* line, or the above assumptions may be broken.
*/
@RunWith(Parameterized.class)
public class MsfTest {
private String _msfClassName;
private MinSpanForest<String> _msf;
private Graph<String> _graph;
/**
* Tests Prim-Jarnik's basic functionality on a standard simple graph
*/
@Test
public void simpleTest() {
CS16Vertex<String> A = _graph.insertVertex("A");
CS16Vertex<String> B = _graph.insertVertex("B");
CS16Vertex<String> C = _graph.insertVertex("C");
CS16Edge<String> ab = _graph.insertEdge(A, B, 1);
CS16Edge<String> bc = _graph.insertEdge(B, C, 1);
CS16Edge<String> ca = _graph.insertEdge(A, C, 10);
Collection<CS16Edge<String>> MSF = _msf.genMinSpanForest(_graph, null);
assertThat(MSF.size(), is(2));
assertThat(MSF.contains(ab), is(true));
assertThat(MSF.contains(bc), is(true));
assertThat(MSF.contains(ca), is(false));
}
/**
* Tests Prim-Jarnik on an empty graph
*/
@Test
public void emptyGraphTest() {
Collection<CS16Edge<String>> MSF = _msf.genMinSpanForest(_graph, null);
assertThat(MSF.size(), is(0));
}
/**
* Tests Prim-Jarnik on graph with just one vertex
*/
@Test
public void oneVertexTest() {
CS16Vertex<String> A = _graph.insertVertex("A");
Collection<CS16Edge<String>> MSF = _msf.genMinSpanForest(_graph, null);
assertThat(MSF.size(), is(0));
}
/**
* Tests Prim-Jarnik on a graph with just one edge
*/
@Test
public void oneEdgeTest() {
CS16Vertex<String> A = _graph.insertVertex("A");
CS16Vertex<String> B = _graph.insertVertex("B");
CS16Edge<String> ab = _graph.insertEdge(A, B, 1);
Collection<CS16Edge<String>> MSF = _msf.genMinSpanForest(_graph, null);
assertThat(MSF.size(), is(1));
}
/**
* Tests Prim-Jarnik on a graph with two disconnected trees
*/
@Test
public void twoSeparateTrees() {
CS16Vertex<String> A = _graph.insertVertex("A");
CS16Vertex<String> B = _graph.insertVertex("B");
CS16Vertex<String> C = _graph.insertVertex("C");
CS16Edge<String> ab = _graph.insertEdge(A, B, 21);
CS16Edge<String> bc = _graph.insertEdge(B, C, 22);
CS16Edge<String> ca = _graph.insertEdge(A, C, 23);
CS16Vertex<String> X = _graph.insertVertex("X");
CS16Vertex<String> Y = _graph.insertVertex("Y");
CS16Vertex<String> Z = _graph.insertVertex("Z");
CS16Edge<String> xy = _graph.insertEdge(X, Y, 10);
CS16Edge<String> yz = _graph.insertEdge(Y, Z, 11);
CS16Edge<String> xz = _graph.insertEdge(X, Z, 12);
Collection<CS16Edge<String>> MSF = _msf.genMinSpanForest(_graph, null);
assertThat(MSF.size(), is(4));
assertThat(MSF.contains(ab), is(true));
assertThat(MSF.contains(bc), is(true));
assertThat(MSF.contains(ca), is(false));
assertThat(MSF.contains(xy), is(true));
assertThat(MSF.contains(yz), is(true));
assertThat(MSF.contains(xz), is(false));
}
/**
* Tests Prim-Jarnik on a graph where there are multiple valid MSFs
*/
@Test
public void multipleValidPathsTest() {
CS16Vertex<String> A = _graph.insertVertex("A");
CS16Vertex<String> B = _graph.insertVertex("B");
CS16Vertex<String> C = _graph.insertVertex("C");
CS16Edge<String> ab = _graph.insertEdge(A, B, 2);
CS16Edge<String> bc = _graph.insertEdge(B, C, 2);
CS16Edge<String> ac = _graph.insertEdge(A, C, 2);
Collection<CS16Edge<String>> MSF = _msf.genMinSpanForest(_graph, null);
assertThat(MSF.size(), is(2));
assertThat((MSF.contains(ab) && MSF.contains(ac)) || (MSF.contains(ab) && MSF.contains(bc)) ||
(MSF.contains(ac) && MSF.contains(ab)), is(true));
}
/*
* This is the method that, using junit magic, provides the list of MSF algorithms
* that should be created and be tested via the methods above.
* By default, all of the above tests will be run on MyPrimJarnik algorithm implementations.
* If you're interested in testing the methods on just one of the
* algorithms, comment out the one you don't want in the method below!
*/
@Parameters(name = "with msf algo: {0}")
public static Collection<String> msts() {
List<String> algoNames = new ArrayList<>();
algoNames.add("graph.MyPrimJarnik");
return algoNames;
}
/*
* ####################################################
*
* DO NOT MODIFY ANYTHING BELOW THIS LINE
*
* ####################################################
*/
@SuppressWarnings("unchecked")
@Before
public void setup() {
Class<?> msfClass = null;
try {
msfClass = Class.forName(_msfClassName);
Constructor<?> constructor = msfClass.getConstructors()[0];
_msf = (MinSpanForest<String>) constructor.newInstance();
} catch (ClassNotFoundException | InvocationTargetException | InstantiationException | IllegalAccessException
| IllegalArgumentException e) {
System.err.println("Exception while instantiating msf class " + _msfClassName + " from test.");
e.printStackTrace();
}
_graph = new AdjacencyMatrixGraph<>(false);
}
public MsfTest(String msfClassName) {
_msfClassName = msfClassName;
}
}