-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCovidTest.java
More file actions
304 lines (223 loc) · 6.6 KB
/
Copy pathCovidTest.java
File metadata and controls
304 lines (223 loc) · 6.6 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
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.NoSuchElementException;
import java.util.Scanner;
/**
* Tests the Covid Application
*
* @author mkornely
*
*/
public class CovidTest {
public static void main(String[] args) {
System.out.println(testStudent());
System.out.println(testStudentHash());
System.out.println(testCovidTracker());
}
/**
* Tests the Student class
*
* @returns boolean if it passes the tests
*
*/
public static boolean testStudent() {
Student test1;
Student test2;
Student test3;
try {
test1 = new Student();
test2 = new Student(123, "Witte", "Positive");
test3 = new Student(456, "Smith", "Negative");
} catch (Exception ex) {
return false;
}
if (test1.getID() == 0)
if (test1.getLocation() == "NA")
if (test1.getStatus() == "NA")
test1.setID(909);
test1.setLocation("Sellery");
test1.setStatus("Negative");
if (test1.getID() == 909)
if (test1.getLocation().equals("Sellery"))
if (test1.getStatus().equals("Negative"))
if (test2.getID() == 123)
if (test2.getLocation() == "Witte")
if (test2.getStatus() == "Positive")
if (test3.getID() == 456)
if (test3.getLocation() == "Smith")
if (test3.getStatus() == "Negative")
return true;
return false;
}
/**
* Tests the Student class in the hashmap
*
* @returns boolean if it passes the tests
*
*/
public static boolean testStudentHash() {
if (testStudent()) {
Student test1;
Student test2;
Student test3;
Student test4;
Student test5;
test1 = new Student();
test2 = new Student(123, "Witte", "Positive");
test3 = new Student(456, "Smith", "Negative");
test4 = new Student(789, "Sellery", "Positive");
test5 = new Student(101, "Off Campus", "Negative");
HashTableMap<Integer, Student> m = new HashTableMap<>();
if (m.put(test1.getID(), test1))
if (m.put(test2.getID(), test2))
if (m.put(test3.getID(), test3))
if (m.put(test4.getID(), test4))
if (m.put(test5.getID(), test5))
try {
if (m.get(test1.getID()) != test1)
return false;
if (m.get(test2.getID()) != test2)
return false;
if (m.get(test3.getID()) != test3)
return false;
if (m.get(test4.getID()) != test4)
return false;
if (m.get(test5.getID()) != test5)
return false;
} catch (NoSuchElementException e) {
return false;
}
try {
m.get(5);
} catch (NoSuchElementException e) {
return true;
}
}
return false;
}
/**
* Tests functionality of FileReader()
*
* @return true if data from provided .txt file is correctly transformed into Student objects and
* properly added to Hash Table; false otherwise
*/
public static boolean testFileReader() {
HashTableMap<Integer, Student> table = new HashTableMap<>();
File inputs = new File("FileReaderTestInputs.txt");
String[] arr = {"NA", "A", "B", "C", "X", "Y", "Z"};
try {
FileReader reader = new FileReader(table, inputs);
} catch (FileNotFoundException e) {
return false;
}
if (table.size() != arr.length)
return false;
for (int i = 0; i < arr.length; i++) {
try {
if (!table.remove(i).getLocation().contentEquals(arr[i]))
return false;
} catch (NoSuchElementException e) {
return false;
}
}
if (table.size() != 0)
return false;
return true;
}
/**
* Tests the CovidTracker class
*
* @returns boolean if it passes the tests
*
*/
public static boolean testCovidTracker() {
ByteArrayOutputStream driverOutput = new ByteArrayOutputStream();
PrintStream p = new PrintStream(driverOutput);
PrintStream old = System.out;
File inputs = new File("DriverTestInputs.txt");
Scanner sc;
try {
sc = new Scanner(inputs);
} catch (FileNotFoundException e) {
System.out.println("Test file not found");
return false;
}
HashTableMap<Integer, Student> t = new HashTableMap<>();
{ // add students 101-104
System.setOut(p); // prevents driver from flooding console
CovidTracker.run(sc, t);
System.setOut(old);
System.out.flush();
if (t.size() != 4)
return false;
if (!t.get(101).getLocation().contentEquals("A")
|| !t.get(102).getLocation().contentEquals("B")
|| !t.get(103).getLocation().contentEquals("C")
|| !t.get(104).getLocation().contentEquals("D"))
return false;
}
{ // remove students 102 and 104
driverOutput = new ByteArrayOutputStream();
p = new PrintStream(driverOutput);
System.setOut(p);
CovidTracker.run(sc, t);
System.setOut(old);
System.out.flush();
if (t.size() != 2)
return false;
try {
t.get(102);
t.get(104);
return false;
} catch (NoSuchElementException e) {
}
}
{ // clear table
driverOutput = new ByteArrayOutputStream();
p = new PrintStream(driverOutput);
System.setOut(p);
CovidTracker.run(sc, t);
System.setOut(old);
System.out.flush();
if (t.size() != 0)
return false;
}
{ // add 9 students from file
driverOutput = new ByteArrayOutputStream();
p = new PrintStream(driverOutput);
System.setOut(p);
CovidTracker.run(sc, t);
System.setOut(old);
System.out.flush();
String[] arr = {"NA", "A", "B", "C", "X", "Y", "Z"};
if (t.size() != arr.length)
return false;
for (int i = 0; i < arr.length; i++) {
try {
if (!t.remove(i).getLocation().contentEquals(arr[i]))
return false;
} catch (NoSuchElementException e) {
return false;
}
}
if (t.size() != 0)
return false;
}
{ // erroneous input
driverOutput = new ByteArrayOutputStream();
p = new PrintStream(driverOutput);
System.setOut(p);
try {
CovidTracker.run(sc, t);
} catch (Exception e) {
return false;
}
System.setOut(old);
System.out.flush();
}
sc.close();
return true;
}
}