-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestClass1.java
More file actions
352 lines (262 loc) · 8.16 KB
/
Copy pathTestClass1.java
File metadata and controls
352 lines (262 loc) · 8.16 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
// --== CS400 File Header Information ==--
// Name: Ian Koh
// Email: iskoh@wisc.edu
// Team: BA
// Role: Test Engineer 2
// TA: Bri Cochran
// Lecturer: Florian Heiml
// Notes to Grader: <optional extra notes>
import java.util.InputMismatchException;
import java.util.NoSuchElementException;
import java.util.Scanner;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
public class TestClass1 {
public static void main(String[] args) {
System.out.println("\nStudent object:");
System.out.println("\ttestStudent:" + testStudent());
System.out.println("\ttestStudentHash:" + testStudentHash());
System.out.println("\nFile reader:");
System.out.println("\ttestFileReader:" + testFileReader());
System.out.println("\nDriver interface:");
System.out.println("\ttestCovidDriver:" + testCovidDriver());
}
/**
* Tests functionality of constructors for Student class, as well as getters, setters, and toString().
*
* @return true if Student objects are created with provided/default field values with no errors;
* false otherwise
*/
public static boolean testStudent() {
Student a;
Student b;
Student c;
Student d;
Student e;
try {
a = new Student();
b = new Student(908, "Sellery", "N");
c = new Student(907, "Witte", "P");
d = new Student(906, "Off-campus", "N");
e = new Student(910, "Chadborne", "N");
//System.out.println("A");
} catch (Exception ex) { return false; }
if (a.getID() != 0
|| !a.getLocation().equals("NA")
|| !a.getStatus().contentEquals("NA"))
return false;
//System.out.println("B");
if (b.getID() != 908
|| !b.getLocation().equals("Sellery")
|| !b.getStatus().contentEquals("N"))
return false;
//System.out.println("C");
if (c.getID() != 907
|| !c.getLocation().equals("Witte")
|| !c.getStatus().contentEquals("P"))
return false;
//System.out.println("D");
if (d.getID() != 906
|| !d.getLocation().equals("Off-campus")
|| !d.getStatus().contentEquals("N"))
return false;
//System.out.println("E");
if (e.getID() != 910
|| !e.getLocation().equals("Chadborne")
|| !e.getStatus().contentEquals("N"))
return false;
//System.out.println("F");
a.setID(203);
b.setLocation("Dejope");
c.setStatus("N");
//System.out.println("G");
if (a.getID() != 203
|| !b.getLocation().contentEquals("Dejope")
|| !c.getStatus().equals("N"))
return false;
//System.out.println("H");
return true;
}
/**
* Tests if Student.java integrates correctly with HashTableMap implementation.
*
* @return true if Student objects are correctly stored; false otherwise
*/
public static boolean testStudentHash() {
Student a = new Student();
Student b = new Student(908, "Sellery", "N");;
Student c = new Student(907, "Witte", "P");;
Student d = new Student(906, "Off-campus", "N");;
Student e = new Student(910, "Chadborne", "N");;
HashTableMap<Integer, Student> table = new HashTableMap<>();
try {
if (!table.put(a.getID(), a)
|| !table.put(b.getID(), b)
|| !table.put(c.getID(), c)
|| !table.put(d.getID(), d)
|| !table.put(e.getID(), e))
return false;
} catch (Exception ex) {
return false;
}
try {
if (table.get(0) != a)
return false;
if (table.get(908) != b)
return false;
if (table.get(907) != c)
return false;
if (table.get(d.getID()) != d)
return false;
if (table.get(e.getID()) != e)
return false;
} catch (NoSuchElementException ex) {
return false;
}
return true;
}
/**
* 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;
}
public static boolean testCovidDriver() {
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
CovidDriver.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;
//System.out.println("1: " + driverOutput.toString());
}
{ // remove students 102 and 104
driverOutput = new ByteArrayOutputStream();
p = new PrintStream(driverOutput);
System.setOut(p);
CovidDriver.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) { }
//System.out.println("2: " + driverOutput.toString());
}
{ // clear table
driverOutput = new ByteArrayOutputStream();
p = new PrintStream(driverOutput);
System.setOut(p);
CovidDriver.run(sc, t);
System.setOut(old);
System.out.flush();
if (t.size() != 0) return false;
//System.out.println("3: " + driverOutput.toString());
}
{ // add 9 students from file
driverOutput = new ByteArrayOutputStream();
p = new PrintStream(driverOutput);
System.setOut(p);
CovidDriver.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;
//System.out.println("4: " + driverOutput.toString());
}
{ // erroneous input
driverOutput = new ByteArrayOutputStream();
p = new PrintStream(driverOutput);
System.setOut(p);
try {
CovidDriver.run(sc, t);
return false;
} catch (InputMismatchException e) { sc.next(); }
//System.out.println("1");
try {
CovidDriver.run(sc, t);
} catch (Exception e) { return false; }
//System.out.println("2");
try {
CovidDriver.run(sc, t);
return false;
} catch (InputMismatchException e) { sc.next(); }
//System.out.println("3");
try {
CovidDriver.run(sc, t);
} catch (Exception e) { return false; }
//System.out.println("4");
try {
CovidDriver.run(sc, t);
return false;
} catch (InputMismatchException e) { sc.next(); }
//System.out.println("5");
try {
CovidDriver.run(sc, t);
} catch (Exception e) { return false; }
//System.out.println("6");
System.setOut(old);
System.out.flush();
//System.out.println("5: " + driverOutput.toString());
}
sc.close();
return true;
}
}