-
Notifications
You must be signed in to change notification settings - Fork 0
/
BackendDeveloperTests.java
161 lines (138 loc) · 4.81 KB
/
BackendDeveloperTests.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeEach;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Arrays;
import java.util.LinkedList;
@SuppressWarnings({"rawtypes", "unchecked"})
public class BackendDeveloperTests {
private CS400Graph<String, Integer> graph;
/**
* Instantiate graph from last week's shortest path activity.
*/
@BeforeEach
public void createGraph() {
//directed graph
graph = new CS400Graph<>();
// insert vertices A-F
graph.insertVertex("A");
graph.insertVertex("B");
graph.insertVertex("C");
graph.insertVertex("D");
graph.insertVertex("E");
graph.insertVertex("F");
// insert edges
graph.insertEdge("A", "B", 6);
graph.insertEdge("A", "C", 2);
graph.insertEdge("A", "D", 5);
graph.insertEdge("B", "E", 1);
graph.insertEdge("B", "C", 2);
graph.insertEdge("C", "B", 3);
graph.insertEdge("C", "F", 1);
graph.insertEdge("D", "E", 3);
graph.insertEdge("E", "A", 4);
graph.insertEdge("F", "A", 1);
graph.insertEdge("F", "D", 1);
}
/**
* Tests if paths are constucted properly
*/
@Test
public void testPathCreation() {
Path pathA = new Path(graph.vertices.get("A"));
LinkedList expected = new LinkedList<>();
expected.add("A");
if (pathA.getCost() == 0 && pathA.getPath().equals(expected)) {
assertTrue(true);
} else {
assertTrue(false);
}
}
/**
* Tests if paths are extended properly
*/
@Test
public void testPathExtension() {
Path pathA = new Path(graph.vertices.get("A"));
Path pathAB = new Path(pathA, graph.vertices.get("A").edgesLeaving.get(0));
LinkedList expected = new LinkedList<>();
expected.add("A");
expected.add("B");
if (pathAB.getCost() == 6 && pathAB.getPath().equals(expected)) {
assertTrue(true);
} else {
assertTrue(false);
}
}
/**
* Tests if paths are sorted properly
*/
@Test
public void testSort() {
Path pathA = new Path(graph.vertices.get("A")); // A, 0
Path pathAB = new Path(pathA, graph.vertices.get("A").edgesLeaving.get(0)); // AB, 6
Path pathAC = new Path(pathA, graph.vertices.get("A").edgesLeaving.get(1)); // AC, 2
Path pathACB = new Path(pathAC, graph.vertices.get("C").edgesLeaving.get(0)); // ACB, 5
Path pathUnsort[] = new Path[4];
pathUnsort[0] = pathAC;
pathUnsort[1] = pathA;
pathUnsort[2] = pathAB;
pathUnsort[3] = pathACB;
Path pathSort[] = new Path[4];
pathSort[0] = pathA;
pathSort[1] = pathAC;
pathSort[2] = pathACB;
pathSort[3] = pathAB;
SortPaths sPaths = new SortPaths();
Path pathDone[] = sPaths.sort(pathUnsort);
if (Arrays.equals(pathDone, pathSort)) {
assertTrue(true);
} else {
assertTrue(false);
}
}
/**
* Tests if the shortest path is returned
*/
@Test
public void testShortestPath() {
Path pathA = new Path(graph.vertices.get("A")); // A, 0
Path pathAB = new Path(pathA, graph.vertices.get("A").edgesLeaving.get(0)); // AB, 6
Path pathAC = new Path(pathA, graph.vertices.get("A").edgesLeaving.get(1)); // AC, 2
Path pathACB = new Path(pathAC, graph.vertices.get("C").edgesLeaving.get(0)); // ACB, 5
Path pathUnsort[] = new Path[4];
pathUnsort[0] = pathAC;
pathUnsort[1] = pathA;
pathUnsort[2] = pathAB;
pathUnsort[3] = pathACB;
Path pathSort = pathA;
SortPaths sPaths = new SortPaths();
Path pathDone = sPaths.shortestPath(pathUnsort);
if (pathDone.equals(pathSort)) {
assertTrue(true);
} else {
assertTrue(false);
}
}
/**
* Tests if the path with the fewest nodes is returned
*/
@Test
public void testFewestDoors() {
Path pathA = new Path(graph.vertices.get("A")); // A, 0
Path pathAB = new Path(pathA, graph.vertices.get("A").edgesLeaving.get(0)); // AB, 6
Path pathAC = new Path(pathA, graph.vertices.get("A").edgesLeaving.get(1)); // AC, 2
Path pathACB = new Path(pathAC, graph.vertices.get("C").edgesLeaving.get(0)); // ACB, 5
Path pathUnsort[] = new Path[3];
pathUnsort[0] = pathACB;
pathUnsort[1] = pathAB;
pathUnsort[2] = pathAC;
Path pathSort = pathAC;
SortPaths sPaths = new SortPaths();
Path pathDone = sPaths.fewestDoors(pathUnsort);
if (pathDone.equals(pathSort)) {
assertTrue(true);
} else {
assertTrue(false);
}
}
}