-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphTest.java
143 lines (117 loc) · 3.12 KB
/
GraphTest.java
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
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.*;
/**
* The test class GraphTest.
*
* @author (your name)
* @version (a version number or a date)
*/
public class GraphTest
{
/**
* Default constructor for test class GraphTest
*/
public GraphTest()
{
}
/**
* Sets up the test fixture.
*
* Called before every test case method.
*/
@Before
public void setUp()
{
}
/**
* Tears down the test fixture.
*
* Called after every test case method.
*/
@After
public void tearDown()
{
}
class Djk {
int node;
int prev;
Integer dist;
}
@Test
public void isConnected()
{
Graph graph = new Graph(10,6);
graph . generateGraph(123);
ArrayList<Edge> edges = graph.getEdges();
ArrayDeque<Djk> q = new ArrayDeque<Djk>();
int visited[] = new int[10];
for(int i=0;i<visited.length;i++)
{
visited[i]=0;
}
Djk djk = new Djk();
djk.node = 0;
visited[djk.node] = 1;
q . addLast(djk);
int ans = 1;
while(q.size()>0)
{
Djk d = q.pollFirst();
ArrayList<Integer> l = graph.getNeighbors(d.node);
for(int i=0;i<l.size();i++)
{
if(visited[l.get(i)]==1) continue;
Djk dj = new Djk();
dj.node = l.get(i);
visited[l.get(i)] = 1;
ans ++;
q.addLast(dj);
}
}
assertEquals(10,ans);
}
@Test
public void GetNeighbors()
{
Graph graph1 = new Graph(10, 6);
graph1 . generateGraph(123);
ArrayList<Integer> list = graph1.getNeighbors(1);
int x = list.get(0);
assertEquals(x,0);
}
@Test
public void dijkstraTest1()
{
Graph graph1 = new Graph(10, 40);
graph1 . generateGraph(123);
int[] list = graph1.listDijkstra(0);
assertEquals(list[9], graph1.timeDijkstra(0, 9));
}
@Test
public void graph1()
{
Graph graph1 = new Graph(10,40);
graph1.generateGraph(123);
Edge edge = graph1.getEdge(0,0);
assertEquals(null,edge);
}
@Test
public void graph2()
{
Graph graph1 = new Graph(10,40);
graph1.generateGraph(123);
assertEquals(1,graph1.addCar(0,3,1));
assertEquals(0,graph1.removeCar(0,3,1));
}
@Test
public void graph3()
{
Graph graph1 = new Graph(10,40);
graph1.generateGraph(123);
ArrayList<Integer> nei = graph1.getNeighbors(0);
assertEquals(nei.contains(graph1.generateNeighborForFreeMove(0)),true);
}
}