-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphTest.java
More file actions
640 lines (564 loc) · 24.2 KB
/
GraphTest.java
File metadata and controls
640 lines (564 loc) · 24.2 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
package graph;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static support.graph.Constants.MAX_VERTICES;
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.Iterator;
import java.util.List;
import java.util.Random;
import heap.IntegerComparator;
import heap.MyHeap;
import net.datastructures.InvalidKeyException;
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.*;
/**
* This class tests the functionality of a Graph based on 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 program 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'
* via the methods under the 'DO NOT MODIFY ANYTHING BELOW THIS LINE' line.
* Of course, please do not modify anything below that, or the above
* assumptions may be broken.
*/
@RunWith(Parameterized.class)
public class GraphTest {
// Undirected Graph instance variable
private Graph<String> _graph;
// Directed Graph instance variable
private Graph<String> _dirGraph;
private String _graphClassName;
/**
* A simple test for insertVertex, that adds 3 vertices and then checks to
* make sure they were added by accessing them through the vertices
* iterator.
*/
@Test(timeout = 10000)
public void testInsertVertex() {
// insert vertices
CS16Vertex<String> A = _graph.insertVertex("A");
CS16Vertex<String> B = _graph.insertVertex("B");
CS16Vertex<String> C = _graph.insertVertex("C");
// use the vertex iterator to get a list of the vertices in the actual
// graph
List<CS16Vertex<String>> actualVertices = new ArrayList<CS16Vertex<String>>();
Iterator<CS16Vertex<String>> it = _graph.vertices();
while (it.hasNext()) {
actualVertices.add(it.next());
}
// assert that the graph state is consistent with what you expect
assertThat(actualVertices.size(), is(3));
assertThat(actualVertices.contains(A), is(true));
assertThat(actualVertices.contains(B), is(true));
assertThat(actualVertices.contains(C), is(true));
}
/**
* Tests the insertVertex method on a directed graph
*/
@Test(timeout = 10000)
public void testInsertVertexDirected() {
// insert vertices
CS16Vertex<String> A = _dirGraph.insertVertex("A");
CS16Vertex<String> B = _dirGraph.insertVertex("B");
CS16Vertex<String> C = _dirGraph.insertVertex("C");
// use the vertex iterator to get a list of the vertices in the actual
// graph
List<CS16Vertex<String>> actualVertices = new ArrayList<CS16Vertex<String>>();
Iterator<CS16Vertex<String>> it = _dirGraph.vertices();
while (it.hasNext()) {
actualVertices.add(it.next());
}
// assert that the graph state is consistent with what you expect
assertThat(actualVertices.size(), is(3));
assertThat(actualVertices.contains(A), is(true));
assertThat(actualVertices.contains(B), is(true));
assertThat(actualVertices.contains(C), is(true));
}
/**
* A simple test for insertEdges that adds 3 vertices, adds two edges to the
* graph and then asserts that both edges were in fact added using the edge
* iterator as well as checks to make sure their from and to vertices were
* set correctly.
*/
@Test(timeout = 10000)
public void testInsertEdges() {
CS16Vertex<String> A = _graph.insertVertex("A");
CS16Vertex<String> B = _graph.insertVertex("B");
CS16Vertex<String> C = _graph.insertVertex("C");
// use the edge iterator to get a list of the edges in the actual graph.
CS16Edge<String> ab = _graph.insertEdge(A, B, 1);
CS16Edge<String> bc = _graph.insertEdge(B, C, 2);
// use the edge iterator to get a list of the edges in the actual graph.
List<CS16Edge<String>> actualEdges = new ArrayList<CS16Edge<String>>();
Iterator<CS16Edge<String>> it = _graph.edges();
while (it.hasNext()) {
actualEdges.add(it.next());
}
// assert that the graph state is consistent with what you expect.
assertThat(actualEdges.size(), is(2));
assertThat(actualEdges.contains(ab), is(true));
assertThat(actualEdges.contains(bc), is(true));
}
/**
* Tests the insertEdges method on a directed graph
*/
@Test(timeout = 10000)
public void testInsertEdgesDirected() {
CS16Vertex<String> A = _dirGraph.insertVertex("A");
CS16Vertex<String> B = _dirGraph.insertVertex("B");
CS16Vertex<String> C = _dirGraph.insertVertex("C");
// use the edge iterator to get a list of the edges in the actual graph.
CS16Edge<String> ab = _dirGraph.insertEdge(A, B, 1);
CS16Edge<String> bc = _dirGraph.insertEdge(B, C, 2);
// use the edge iterator to get a list of the edges in the actual graph.
List<CS16Edge<String>> actualEdges = new ArrayList<CS16Edge<String>>();
Iterator<CS16Edge<String>> it = _dirGraph.edges();
while (it.hasNext()) {
actualEdges.add(it.next());
}
// assert that the graph state is consistent with what you expect.
assertThat(actualEdges.size(), is(2));
assertThat(actualEdges.contains(ab), is(true));
assertThat(actualEdges.contains(bc), is(true));
}
/**
* Tests whether an InvalidVertexException error is thrown on an improper method call for insertEdge
*/
@Test(expected = InvalidVertexException.class)
public void insertEdgeExceptionTest(){
CS16Vertex<String> A = _graph.insertVertex("A");
CS16Vertex<String> B = _graph.insertVertex("B");
CS16Vertex<String> C = _graph.insertVertex("C");
CS16Edge<String> nullB = _graph.insertEdge(null, B, 1);
}
/**
* Tests whether an InvalidVertexException error is thrown on an improper method call for removeVertex
*/
@Test(expected = InvalidVertexException.class)
public void removeVertexExceptionTest(){
CS16Vertex<String> A = _graph.insertVertex("A");
CS16Vertex<String> B = _graph.insertVertex("B");
CS16Vertex<String> C = _graph.insertVertex("C");
_graph.removeVertex(null);
}
/**
* Tests whether an InvalidEdgeException error is thrown on an improper method call for removeEdge
*/
@Test(expected = InvalidEdgeException.class)
public void removeEdgeExceptionTest(){
CS16Vertex<String> A = _graph.insertVertex("A");
CS16Vertex<String> B = _graph.insertVertex("B");
CS16Vertex<String> C = _graph.insertVertex("C");
_graph.removeEdge(null);
}
/**
* Tests whether an InvalidVertexException error is thrown on an improper method call for connectingEdge
*/
@Test(expected = InvalidVertexException.class)
public void connectingEdgeVertexExceptionTest(){
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);
_graph.connectingEdge(A, null);
}
/**
* Tests whether a NoSuchEdgeException error is thrown on an improper method call for connectingEdge
*/
@Test(expected = NoSuchEdgeException.class)
public void connectingEdgeEdgeExceptionTest(){
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);
_graph.connectingEdge(B, C);
}
/**
* Tests whether an InvalidVertexException error is thrown on an improper method call for incomingEdges
*/
@Test(expected = InvalidVertexException.class)
public void incomingEdgesExceptionTest(){
_graph.incomingEdges(null);
}
/**
* Tests whether an InvalidVertexException error is thrown on an improper method call for outgoingEdges
*/
@Test(expected = InvalidVertexException.class)
public void outgoingEdgesExceptionTest(){
_graph.outgoingEdges(null);
}
/**
* Tests whether an InvalidVertexException error is thrown on an improper method call for numOutgoingEdgesException
*/
@Test(expected = InvalidVertexException.class)
public void numOutgoingEdgesExceptionTest(){
_graph.numOutgoingEdges(null);
}
/**
* Tests whether a DirectionException error is thrown on an improper method call for numOutgoingEdges
*/
@Test(expected = DirectionException.class)
public void numOutgoingEdgesDirectionExceptionTest(){
CS16Vertex<String> A = _graph.insertVertex("A");
CS16Vertex<String> B = _graph.insertVertex("B");
CS16Edge<String> ab = _graph.insertEdge(A, B, 1);
_graph.numOutgoingEdges(A);
}
/**
* Tests whether an InvalidVertexException error is thrown on an improper method call for opposite
*/
@Test(expected = InvalidVertexException.class)
public void oppositeVertException(){
CS16Vertex<String> A = _graph.insertVertex("A");
CS16Vertex<String> B = _graph.insertVertex("B");
CS16Edge<String> ab = _graph.insertEdge(A, B, 1);
_graph.opposite(null, ab);
}
/**
* Tests whether an InvalidEdgeException error is thrown on an improper method call for opposite
*/
@Test(expected = InvalidEdgeException.class)
public void oppositeEdgeException(){
CS16Vertex<String> A = _graph.insertVertex("A");
CS16Vertex<String> B = _graph.insertVertex("B");
CS16Edge<String> ab = _graph.insertEdge(A, B, 1);
_graph.opposite(A, null);
}
/**
* Tests whether a NoSuchVertexException error is thrown on an improper method call for opposite
*/
@Test(expected = NoSuchVertexException.class)
public void oppositeNoVertexException(){
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);
_graph.opposite(C, ab);
}
/**
* Tests whether an InvalidEdgeException error is thrown on an improper method call for endVertices
*/
@Test(expected = InvalidEdgeException.class)
public void endVerticesException(){
_graph.endVertices(null);
}
/**
* Tests whether an InvalidVertexException error is thrown on an improper method call for areAdjacent
*/
@Test(expected = InvalidVertexException.class)
public void areAdjacentException(){
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);
_graph.areAdjacent(null, B);
}
/**
* Tests the removeVertex method
*/
@Test(timeout = 10000)
public void testRemoveVertex() {
// insert vertices
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);
_graph.removeVertex(C);
// use the vertex iterator to get a list of the vertices in the actual
// graph
List<CS16Vertex<String>> actualVertices = new ArrayList<CS16Vertex<String>>();
Iterator<CS16Vertex<String>> it = _graph.vertices();
while (it.hasNext()) {
actualVertices.add(it.next());
}
List<CS16Edge<String>> edges = new ArrayList<CS16Edge<String>>();
Iterator<CS16Edge<String>> edgeIterator = _graph.edges();
while(edgeIterator.hasNext()){
edges.add(edgeIterator.next());
}
// assert that the graph state is consistent with what you expect
assertThat(actualVertices.size(), is(2));
assertThat(edges.size(), is(1));
assertThat(actualVertices.contains(A), is(true));
assertThat(edges.contains(ab), is(true));
assertThat(actualVertices.contains(B), is(true));
assertThat(actualVertices.contains(C), is(false));
assertThat(edges.contains(bc), is(false));
}
/**
* Tests the areAdjacent method on an undirected graph
*/
@Test(timeout = 10000)
public void testAreAdjacent() {
// insert vertices
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);
assertThat(_graph.areAdjacent(A, B), is(true));
assertThat(_graph.areAdjacent(B, C), is(true));
assertThat(_graph.areAdjacent(C, B), is(true));
assertThat(_graph.areAdjacent(B, A), is(true));
assertThat(_graph.areAdjacent(A, C), is(false));
assertThat(_graph.areAdjacent(C, A), is(false));
}
/**
* Tests the areAdjacent method on a directed graph
*/
@Test(timeout = 10000)
public void testAreAdjacentDirected() {
// insert vertices
CS16Vertex<String> A = _dirGraph.insertVertex("A");
CS16Vertex<String> B = _dirGraph.insertVertex("B");
CS16Vertex<String> C = _dirGraph.insertVertex("C");
CS16Edge<String> ab = _dirGraph.insertEdge(A, B, 1);
CS16Edge<String> bc = _dirGraph.insertEdge(B, C, 1);
assertThat(_dirGraph.areAdjacent(A, B), is(true));
assertThat(_dirGraph.areAdjacent(B, C), is(true));
assertThat(_dirGraph.areAdjacent(C, B), is(false));
assertThat(_dirGraph.areAdjacent(B, A), is(false));
assertThat(_dirGraph.areAdjacent(A, C), is(false));
assertThat(_dirGraph.areAdjacent(C, A), is(false));
}
/**
* Tests the connectingEdge method
*/
@Test(timeout = 10000)
public void testConnectingEdge() {
// insert vertices
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);
assertThat(_graph.connectingEdge(A, B), is(ab));
assertThat(_graph.connectingEdge(B, C), is(bc));
}
/**
* Tests the incomingEdges method on an undirected graph
*/
@Test(timeout = 10000)
public void incomingEdges() {
// insert vertices
CS16Vertex<String> A = _graph.insertVertex("A");
CS16Vertex<String> B = _graph.insertVertex("B");
CS16Vertex<String> C = _graph.insertVertex("C");
CS16Vertex<String> D = _graph.insertVertex("D");
CS16Vertex<String> E = _graph.insertVertex("E");
CS16Edge<String> ab = _graph.insertEdge(A, B, 1);
CS16Edge<String> cb = _graph.insertEdge(C, B, 1);
CS16Edge<String> db = _graph.insertEdge(D, B, 1);
CS16Edge<String> eb = _graph.insertEdge(E, B, 1);
CS16Edge<String> ac = _graph.insertEdge(A, C, 1);
CS16Edge<String> de = _graph.insertEdge(D, E, 1);
List<CS16Edge<String>> edges = new ArrayList<CS16Edge<String>>();
Iterator<CS16Edge<String>> edgeIterator = _graph.incomingEdges(B);
while(edgeIterator.hasNext()){
edges.add(edgeIterator.next());
}
assertThat(edges.contains(ab), is(true));
assertThat(edges.contains(cb), is(true));
assertThat(edges.contains(db), is(true));
assertThat(edges.contains(eb), is(true));
assertThat(edges.contains(ac), is(false));
assertThat(edges.contains(de), is(false));
}
/**
* Tests the incomingEdges method on a directed graph
*/
@Test(timeout = 10000)
public void incomingEdgesDirected() {
// insert vertices
CS16Vertex<String> A = _dirGraph.insertVertex("A");
CS16Vertex<String> B = _dirGraph.insertVertex("B");
CS16Vertex<String> C = _dirGraph.insertVertex("C");
CS16Vertex<String> D = _dirGraph.insertVertex("D");
CS16Vertex<String> E = _dirGraph.insertVertex("E");
CS16Edge<String> ab = _dirGraph.insertEdge(A, B, 1);
CS16Edge<String> cb = _dirGraph.insertEdge(C, B, 1);
CS16Edge<String> bd = _dirGraph.insertEdge(B, D, 1);
CS16Edge<String> be = _dirGraph.insertEdge(B, E, 1);
CS16Edge<String> ac = _dirGraph.insertEdge(A, C, 1);
CS16Edge<String> de = _dirGraph.insertEdge(D, E, 1);
List<CS16Edge<String>> edges = new ArrayList<CS16Edge<String>>();
Iterator<CS16Edge<String>> edgeIterator = _dirGraph.incomingEdges(B);
while(edgeIterator.hasNext()){
edges.add(edgeIterator.next());
}
assertThat(edges.contains(ab), is(true));
assertThat(edges.contains(cb), is(true));
assertThat(edges.contains(bd), is(false));
assertThat(edges.contains(be), is(false));
assertThat(edges.contains(ac), is(false));
assertThat(edges.contains(de), is(false));
}
/**
* Tests the outgoingEdges method on an undirected graph
*/
@Test(timeout = 10000)
public void outgoingEdges() {
// insert vertices
CS16Vertex<String> A = _graph.insertVertex("A");
CS16Vertex<String> B = _graph.insertVertex("B");
CS16Vertex<String> C = _graph.insertVertex("C");
CS16Vertex<String> D = _graph.insertVertex("D");
CS16Vertex<String> E = _graph.insertVertex("E");
CS16Edge<String> ab = _graph.insertEdge(A, B, 1);
CS16Edge<String> cb = _graph.insertEdge(C, B, 1);
CS16Edge<String> bd = _graph.insertEdge(B, D, 1);
CS16Edge<String> be = _graph.insertEdge(B, E, 1);
CS16Edge<String> ac = _graph.insertEdge(A, C, 1);
CS16Edge<String> de = _graph.insertEdge(D, E, 1);
List<CS16Edge<String>> edges = new ArrayList<CS16Edge<String>>();
Iterator<CS16Edge<String>> edgeIterator = _graph.outgoingEdges(B);
while(edgeIterator.hasNext()){
edges.add(edgeIterator.next());
}
assertThat(edges.contains(ab), is(true));
assertThat(edges.contains(cb), is(true));
assertThat(edges.contains(bd), is(true));
assertThat(edges.contains(be), is(true));
assertThat(edges.contains(ac), is(false));
assertThat(edges.contains(de), is(false));
}
/**
* Tests the outgoingEdges method on a Directed graph
*/
@Test(timeout = 10000)
public void outgoingEdgesDirected() {
// insert vertices
CS16Vertex<String> A = _dirGraph.insertVertex("A");
CS16Vertex<String> B = _dirGraph.insertVertex("B");
CS16Vertex<String> C = _dirGraph.insertVertex("C");
CS16Vertex<String> D = _dirGraph.insertVertex("D");
CS16Vertex<String> E = _dirGraph.insertVertex("E");
CS16Edge<String> ab = _dirGraph.insertEdge(A, B, 1);
CS16Edge<String> cb = _dirGraph.insertEdge(C, B, 1);
CS16Edge<String> bd = _dirGraph.insertEdge(B, D, 1);
CS16Edge<String> be = _dirGraph.insertEdge(B, E, 1);
CS16Edge<String> ac = _dirGraph.insertEdge(A, C, 1);
CS16Edge<String> de = _dirGraph.insertEdge(D, E, 1);
List<CS16Edge<String>> edges = new ArrayList<CS16Edge<String>>();
Iterator<CS16Edge<String>> edgeIterator = _dirGraph.outgoingEdges(B);
while(edgeIterator.hasNext()){
edges.add(edgeIterator.next());
}
assertThat(edges.contains(ab), is(false));
assertThat(edges.contains(cb), is(false));
assertThat(edges.contains(bd), is(true));
assertThat(edges.contains(be), is(true));
assertThat(edges.contains(ac), is(false));
assertThat(edges.contains(de), is(false));
assertThat(_dirGraph.numOutgoingEdges(B), is(2));
}
/**
* Tests the opposite method
*/
@Test(timeout = 10000)
public void testOpposite() {
// insert vertices
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);
assertThat(_graph.opposite(A, ab), is(B));
assertThat(_graph.opposite(B, ab), is(A));
assertThat(_graph.opposite(B, bc), is(C));
assertThat(_graph.opposite(C, bc), is(B));
}
@Test(timeout = 10000)
public void testEndVertices() {
// insert vertices
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);
assertThat(_graph.endVertices(ab).contains(A), is(true));
assertThat(_graph.endVertices(ab).contains(B), is(true));
assertThat(_graph.endVertices(ab).contains(C), is(false));
}
/**
* Tests the clear method
*/
@Test(timeout = 10000)
public void testClear() {
// insert vertices
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);
_graph.clear();
List<CS16Edge<String>> edges = new ArrayList<CS16Edge<String>>();
Iterator<CS16Edge<String>> edgeIterator = _graph.edges();
while(edgeIterator.hasNext()){
edges.add(edgeIterator.next());
}
assertThat(_graph.getNumVertices() == 0, is(true));
assertThat(edges.size() == 0, is(true));
}
/**
* Tests the getNumVertices method
*/
@Test(timeout = 10000)
public void numVerticesTest() {
// insert vertices
CS16Vertex<String> A = _graph.insertVertex("A");
CS16Vertex<String> B = _graph.insertVertex("B");
CS16Vertex<String> C = _graph.insertVertex("C");
CS16Vertex<String> D = _graph.insertVertex("D");
CS16Vertex<String> E = _graph.insertVertex("E");
assertThat(_graph.getNumVertices() == 5, is(true));
}
/*
* List of graphs for testing!
*/
@Parameters(name = "with graph: {0}")
public static Collection<String> graphs() {
List<String> names = new ArrayList<>();
names.add("graph.AdjacencyMatrixGraph");
return names;
}
/*
* ####################################################
*
* DO NOT MODIFY ANYTHING BELOW THIS LINE
*
* ####################################################
*/
@SuppressWarnings("unchecked")
@Before
public void makeGraph() {
Class<?> graphClass = null;
try {
graphClass = Class.forName(_graphClassName);
Constructor<?> constructor = graphClass.getConstructors()[0];
_graph = (Graph<String>) constructor.newInstance(false);
_dirGraph = (Graph<String>) constructor.newInstance(true);
} catch (ClassNotFoundException | InvocationTargetException | InstantiationException | IllegalAccessException | IllegalArgumentException e) {
System.err.println("Exception while instantiating Graph class in GraphTest.");
e.printStackTrace();
}
}
public GraphTest(String graphClassName) {
this._graphClassName = graphClassName;
}
}