-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProjectADriver.java
More file actions
432 lines (364 loc) · 21 KB
/
ProjectADriver.java
File metadata and controls
432 lines (364 loc) · 21 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
import java.util.Arrays;
public class ProjectADriver {
private static boolean allTestsPassed = true;
public static void main(String[] args) {
FrontBackCappedListInterface<Integer> list = new ArrayFrontBackCappedList<Integer>(10);
// un-comment the lines below to test the extra credit
// list = new ListFrontBackCappedList<Integer>(10);
// System.out.println("-----------------------------DRIVER BEING RUN WITH EXTRA CREDIT CLASS-----------------------------");
System.out.println("-----------------------------TESTING WITH EMPTY LIST-----------------------------");
// parameter 1: the list
// parameter 2: the expected result of isEmpty
// parameter 3: the expected result of isFull
testIsEmptyFull(list, true, false);
// parameter 1: the list
// parameter 2: the expected size of the list
testSize(list, 0);
// parameter 1: the list
// parameter 2: the expected String returned from the toString method
testDisplayMatch(list, "size=0; capacity=10; []");
System.out.println("\n-----------------------------TESTING ADD TO BACK-----------------------------");
// parameter 1: the list
// parameter 2: indicates if we are adding to the front or back
// parameter 3: the values to add; values are added from the array beginning (index 0) to end;
// for example, {1, 2, 3} will first add 1 to the back, then 2 to the back, then 3 to the back
// parameter 4: expected return value (true if the element was added, false otherwise)
// parameter 5: a description of the test
testAdd(list, AddRemovePosition.BACK, new Integer[] {7}, true, "addBack to empty list");
testDisplayMatch(list, "size=1; capacity=10; [7]");
testAdd(list, AddRemovePosition.BACK, new Integer[] {9, 5}, true, "addBack to non-empty list");
testIsEmptyFull(list, false, false);
testSize(list, 3);
testDisplayMatch(list, "size=3; capacity=10; [7, 9, 5]");
testAdd(list, AddRemovePosition.BACK, new Integer[] {5, 3, 2, 1, 9, 8, 7}, true, "addBack to fill the list");
testIsEmptyFull(list, false, true);
testSize(list, 10);
testDisplayMatch(list, "size=10; capacity=10; [7, 9, 5, 5, 3, 2, 1, 9, 8, 7]");
testAdd(list, AddRemovePosition.BACK, new Integer[] {4}, false, "addBack to full list");
testSize(list, 10);
testDisplayMatch(list, "size=10; capacity=10; [7, 9, 5, 5, 3, 2, 1, 9, 8, 7]");
System.out.println("\n-----------------------------TESTING CLEAR-----------------------------");
list.clear();
testIsEmptyFull(list, true, false);
testDisplayMatch(list, "size=0; capacity=10; []");
System.out.println("\n-----------------------------TESTING ADD TO FRONT-----------------------------");
list.clear();
// parameter 1: the list
// parameter 2: indicates if we are adding to the front or back
// parameter 3: the values to add; values are added from the array beginning (index 0) to end;
// for example, {1, 2, 3} will first add 1 to the front, then 2 to the front, then 3 to the front
// parameter 4: expected return value (true if the element was added, false otherwise)
// parameter 5: a description of the test
testAdd(list, AddRemovePosition.FRONT, new Integer[] {2}, true, "addFront to empty list");
testDisplayMatch(list, "size=1; capacity=10; [2]");
testAdd(list, AddRemovePosition.FRONT, new Integer[] {4, 3}, true, "addFront to non-empty list");
testIsEmptyFull(list, false, false);
testSize(list, 3);
testDisplayMatch(list, "size=3; capacity=10; [3, 4, 2]");
testAdd(list, AddRemovePosition.FRONT, new Integer[] {7, 9, 5, 4, 3, 8, 1}, true, "addFront to fill the list");
testIsEmptyFull(list, false, true);
testSize(list, 10);
testDisplayMatch(list, "size=10; capacity=10; [1, 8, 3, 4, 5, 9, 7, 3, 4, 2]");
testAdd(list, AddRemovePosition.FRONT, new Integer[] {4}, false, "addFront to full list");
testSize(list, 10);
testDisplayMatch(list, "size=10; capacity=10; [1, 8, 3, 4, 5, 9, 7, 3, 4, 2]");
System.out.println("\n-----------------------------TESTING CONTAINS-----------------------------");
clearAndRefillTheList(list, new Integer[] {1, 8, 3, 4, 5, 9, 7, 3, 4, 2});
// parameter 1: the list
// parameter 2: the element to look for
// parameter 3: the expected result (true if the list contains that element, false otherwise)
// parameter 4: a description of the test
testContains(list, 1, true, "element is in the first position");
testContains(list, 2, true, "element is in the last position");
testContains(list, 5, true, "element is in the middle");
testContains(list, 3, true, "element is in the list more than once");
testContains(list, 0, false, "element is not in the list");
testDisplayMatch(list, "size=10; capacity=10; [1, 8, 3, 4, 5, 9, 7, 3, 4, 2]");
System.out.println("\n-----------------------------TESTING INDEX OF-----------------------------");
clearAndRefillTheList(list, new Integer[] {1, 8, 3, 4, 5, 9, 7, 3, 4, 2});
// parameter 1: the list
// parameter 2: indicates if we are finding the index of the first or last match
// parameter 3: the value to search for
// parameter 4: expected result (the index where the element appears)
// parameter 5: a description of the test
testIndexOf(list, IndexPosition.FIRST, 1, 0, "first element in the list");
testIndexOf(list, IndexPosition.FIRST, 2, 9, "last element in the list");
testIndexOf(list, IndexPosition.FIRST, 5, 4, "element in the middle of the list");
testIndexOf(list, IndexPosition.FIRST, 3, 2, "repeated element in the list");
testIndexOf(list, IndexPosition.FIRST, 0, "element not in the list");
testDisplayMatch(list, "size=10; capacity=10; [1, 8, 3, 4, 5, 9, 7, 3, 4, 2]");
System.out.println("\n-----------------------------TESTING LAST INDEX OF-----------------------------");
clearAndRefillTheList(list, new Integer[] {1, 8, 3, 4, 5, 9, 7, 3, 4, 2});
testIndexOf(list, IndexPosition.LAST, 1, 0, "first element in the list");
testIndexOf(list, IndexPosition.LAST, 2, 9, "last element in the list");
testIndexOf(list, IndexPosition.LAST, 5, 4, "element in the middle of the list");
testIndexOf(list, IndexPosition.LAST, 3, 7, "repeated element in the list");
testIndexOf(list, IndexPosition.LAST, 0, "element not in the list");
testDisplayMatch(list, "size=10; capacity=10; [1, 8, 3, 4, 5, 9, 7, 3, 4, 2]");
System.out.println("\n-----------------------------TESTING REMOVES-----------------------------");
clearAndRefillTheList(list, new Integer[] {1, 8, 3, 4, 5, 9, 7, 3, 4, 2});
// parameter 1: the list
// parameter 2: indicates if we are removing from the front or back
// parameter 3: if it exists, this is the expected value returned from the remove;
// if this parameter is omitted, the expected return value is null
// parameter 4: a description of the test
testRemove(list, AddRemovePosition.FRONT, 1, "remove from non-empty");
testDisplayMatch(list, "size=9; capacity=10; [8, 3, 4, 5, 9, 7, 3, 4, 2]");
testRemove(list, AddRemovePosition.FRONT, 8, "remove from non-empty");
testDisplayMatch(list, "size=8; capacity=10; [3, 4, 5, 9, 7, 3, 4, 2]");
testRemove(list, AddRemovePosition.BACK, 2, "remove from non-empty");
testDisplayMatch(list, "size=7; capacity=10; [3, 4, 5, 9, 7, 3, 4]");
testRemove(list, AddRemovePosition.BACK, 4, "remove from non-empty");
testSize(list, 6);
testDisplayMatch(list, "size=6; capacity=10; [3, 4, 5, 9, 7, 3]");
list.clear();
testRemove(list, AddRemovePosition.FRONT, "remove from empty");
testIsEmptyFull(list, true, false);
testSize(list, 0);
testDisplayMatch(list, "size=0; capacity=10; []");
testRemove(list, AddRemovePosition.BACK, "remove from empty");
testIsEmptyFull(list, true, false);
testSize(list, 0);
testDisplayMatch(list, "size=0; capacity=10; []");
clearAndRefillTheList(list, new Integer[] {1});
testRemove(list, AddRemovePosition.FRONT, 1, "remove from singleton added to front");
testIsEmptyFull(list, true, false);
clearAndRefillTheList(list, new Integer[] {1});
testRemove(list, AddRemovePosition.BACK, 1, "remove from singleton added to back");
testIsEmptyFull(list, true, false);
clearAndRefillTheList(list, new Integer[] {1});
testRemove(list, AddRemovePosition.FRONT, 1, "remove from singleton added to front");
testIsEmptyFull(list, true, false);
clearAndRefillTheList(list, new Integer[] {1});
testRemove(list, AddRemovePosition.BACK, 1, "remove from singleton added to back");
testIsEmptyFull(list, true, false);
System.out.println("\n-----------------------------TESTING MIX OF ADDS AND REMOVES-----------------------------*");
list.clear();
list.addFront(3); list.addBack(2); list.addFront(4);
list.addFront(5); list.addBack(3); list.addBack(8);
list.addBack(9);
testDisplayMatch(list, "size=7; capacity=10; [5, 4, 3, 2, 3, 8, 9]");
list.removeFront(); list.removeBack();
testDisplayMatch(list, "size=5; capacity=10; [4, 3, 2, 3, 8]");
list.removeBack(); list.removeFront(); list.removeFront();
testDisplayMatch(list, "size=2; capacity=10; [2, 3]");
list.addFront(9); list.removeBack();
testDisplayMatch(list, "size=2; capacity=10; [9, 2]");
list.removeFront(); list.addBack(7);
testDisplayMatch(list, "size=2; capacity=10; [2, 7]");
System.out.println("\n-----------------------------TESTING GET ENTRY-----------------------------");
clearAndRefillTheList(list, new Integer[] {4, 3, 2, 3, 8});
// parameter 1: the list
// parameter 2: the index at which you will get the element
// parameter 3: the expected result (the element at the specified index)
// parameter 4: a description of the test
testGetEntry(list, 0, 4, "getting first position");
testGetEntry(list, 4, 8, "getting last position");
testGetEntry(list, 2, 2, "getting a middle position");
// parameter 1: the list
// parameter 2: the index at which you will get the element; these method calls test invalid indices
// parameter 3: a description of the test
testGetEntry(list, -1, "invalid index");
testGetEntry(list, 11, "invalid index");
testGetEntry(list, 5, "invalid index");
testGetEntry(list, 7, "invalid index");
System.out.println("\n-----------------------------TESTING WITH STRINGS-----------------------------");
FrontBackCappedListInterface<String> wordList = new ArrayFrontBackCappedList<String>(20);
testAdd(wordList, AddRemovePosition.FRONT, new String[] {"job!", "Nice", "it!", "did", "You"}, true, "test with Strings");
testAdd(wordList, AddRemovePosition.BACK, new String[] {"You", "rock!"}, true, "test with Strings");
testDisplayMatch(wordList, "size=7; capacity=20; [You, did, it!, Nice, job!, You, rock!]");
testContains(wordList, new String("it!"), true, "test with Strings");
testIndexOf(wordList, IndexPosition.FIRST, new String("You"), 0, "test with Strings");
testIndexOf(wordList, IndexPosition.LAST, new String("You"), 5, "test with Strings");
System.out.println("\n\n-----------------------------TESTING COMPLETE-----------------------------");
if(allTestsPassed) {
System.out.println("----------Summary---------- \nAll automated tests have passed. \nBe sure to manually look at the output to check the contents of the list.\nAlso be sure to manually review your code for style and efficiency.");
} else {
System.out.flush();
System.err.println("**********Summary********** ERROR: There is failure in at least one automated test. Review the output above for details.");
}
}
/*----------------------------------------------------------------------------------------------------------*/
/* TESTER METHODS */
/*----------------------------------------------------------------------------------------------------------*/
/*
* The methods below are designed to help support the tests cases run from main. You don't
* need to use, modify, or understand these methods. You can safely ignore them. :)
*
* Also, you can ignore the use of generics in the tester methods. These methods use
* generics at a level beyond which we use in our class. I only use them here to make this a robust
* and useful testing file. You are NOT required to understand the use of generics in this way.
*/
public static <T> void testDisplayMatch(FrontBackCappedListInterface<T> list, String expectedOutput) {
System.out.println("\nManual check of contents using toString: \nExpected output: " + expectedOutput);
System.out.println(" Actual output: " + list.toString() +"\n");
}
public static <T> void testIsEmptyFull(FrontBackCappedListInterface<T> list, boolean expectedEmptyResult, boolean expectedFullResult) {
System.out.println("\nTesting isEmpty for list: " + list);
System.out.println("Expected: " + expectedEmptyResult + "\n Actual: " + list.isEmpty());
if(expectedEmptyResult != list.isEmpty()) {
allTestsPassed = false;
System.out.println("**********TEST FAILED");
}
System.out.println("\nTesting isFull for list: " + list);
System.out.println("Expected: " + expectedFullResult + "\n Actual: " + list.isFull());
if(expectedFullResult != list.isFull()) {
allTestsPassed = false;
System.out.println("**********TEST FAILED");
}
}
public static <T> void testSize(FrontBackCappedListInterface<T> list, int expectedSize) {
System.out.println("\nTesting size method for list: " + list);
System.out.println("Expected size: " + expectedSize + "\n Actual size: " + list.size() );
if(expectedSize != list.size()) {
allTestsPassed = false;
System.out.println("**********TEST FAILED");
}
}
public static <T> void clearAndRefillTheList(FrontBackCappedListInterface<T> list, T[] valuesToAdd) {
list.clear();
for(T value : valuesToAdd) {
list.addBack(value);
}
System.out.println("\nList cleared and refilled and now contains: " + list);
}
public static <T> void testAdd(FrontBackCappedListInterface<T> list, AddRemovePosition positionToAdd, T[] valuesToAdd, boolean expectedResult, String testDescription) {
System.out.println("\nTesting add method: trying to add " + Arrays.toString(valuesToAdd) + " to " + positionToAdd + " of list");
System.out.println("List before adding: " + list);
int beforeSize = list.size();
for(T value : valuesToAdd) {
boolean actualResult;
if(positionToAdd==AddRemovePosition.FRONT) {
actualResult = list.addFront(value);
} else { // positionToAdd==Position.BACK
actualResult = list.addBack(value);
}
if(actualResult!=expectedResult) {
allTestsPassed = false;
System.out.println("**********TEST FAILED: incorrect return value when adding " + value + ".\ttest:" + testDescription);
System.out.println("Expected return result: " + expectedResult + "\n Actual return result: " + actualResult);
}
}
if(expectedResult==false && beforeSize==list.size()) {
System.out.println("List was full and add was unsuccessful, as expected.");
}
System.out.println("List after adding: " + list);
if(expectedResult==true) {
int afterSize = list.size();
int expectedAfterSize = beforeSize+valuesToAdd.length;
if(expectedAfterSize != afterSize) {
allTestsPassed = false;
System.out.println("**********TEST FAILED: incorrect size after adding " + Arrays.toString(valuesToAdd) + ".\ttest:" + testDescription);
System.out.println("\nExpected after size: " + expectedAfterSize + "\n Actual after size: " + afterSize);
} else {
T expectedLastAddedValue = valuesToAdd[valuesToAdd.length-1];
T actualLastAddedValue;
if(positionToAdd==AddRemovePosition.FRONT) {
actualLastAddedValue = list.getEntry(0);
} else { // positionToAdd==Position.BACK
actualLastAddedValue = list.getEntry(list.size()-1);
}
if(!expectedLastAddedValue.equals(actualLastAddedValue)) {
allTestsPassed = false;
System.out.println("**********TEST FAILED: incorrect list contents. Review the output below.\ttest:" + testDescription);
}
}
}
}
public static <T> void testContains(FrontBackCappedListInterface<T> list, T element, boolean expectedResult, String testDescription) {
boolean actualResult = list.contains(element);
System.out.println("\nTesting contains for target=" + element + " in list: " + list);
System.out.println("Expected result: " + expectedResult);
System.out.println(" Actual result: " + actualResult);
if(expectedResult!=actualResult) {
allTestsPassed = false;
System.out.println("**********TEST FAILED.\ttest:" + testDescription);
}
}
public static <T> void testIndexOf(FrontBackCappedListInterface<T> list, IndexPosition indexPosition, T element, String testDescription) {
testIndexOf(list, indexPosition, element, -1, testDescription);
}
public static <T> void testIndexOf(FrontBackCappedListInterface<T> list, IndexPosition indexPosition, T element, int expectedResult, String testDescription) {
int actualResult;
String methodName = "ndexOf";
if(indexPosition==IndexPosition.FIRST) {
actualResult = list.indexOf(element);
methodName = "i" + methodName;
} else { // position==IndexPosition.LAST
actualResult = list.lastIndexOf(element);
methodName = "lastI" + methodName;
}
System.out.println("\nTesting " + methodName + ": finding target=" + element + " in list: " + list);
System.out.println("Expected " + indexPosition + " index result: " + expectedResult);
System.out.println(" Actual " + indexPosition + " index result: " + actualResult);
if(expectedResult<0 && actualResult>=0) {
allTestsPassed = false;
System.out.println("**********TEST FAILED when finding the index of an element not in the list. \ttest:" + testDescription);
System.out.println(" Result should indicate the element is NOT on the list.");
} else if(expectedResult!=actualResult) {
allTestsPassed = false;
System.out.println("**********TEST FAILED when finding the index. \ttest:" + testDescription);
}
}
public static <T> void testGetEntry(FrontBackCappedListInterface<T> list, int position, String testDescription) {
testGetEntry(list, position, null, testDescription);
}
public static <T> void testGetEntry(FrontBackCappedListInterface<T> list, int position, T expectedResult, String testDescription) {
T actualResult = list.getEntry(position);
System.out.println("\nTesting getEntry for target index = " + position + " in list: " + list);
System.out.println("Expected element at index " + position + ": " + expectedResult);
System.out.println(" Actual element at index " + position + ": " + actualResult);
if(expectedResult==null && actualResult!=null) {
allTestsPassed = false;
System.out.println("**********TEST FAILED when using an invalid position. \ttest:" + testDescription);
} else if(expectedResult!=null && !expectedResult.equals(actualResult)) {
allTestsPassed = false;
System.out.println("**********TEST FAILED to get the expected element. \ttest:" + testDescription);
}
}
public static <T> void testRemove(FrontBackCappedListInterface<T> list, AddRemovePosition positionToRemove, String testDescription) {
testRemove(list, positionToRemove, null, testDescription);
}
public static <T> void testRemove(FrontBackCappedListInterface<T> list, AddRemovePosition positionToRemove, T expectedResult, String testDescription) {
System.out.println("\nTesting remove: trying to remove from the " + positionToRemove + " of list");
System.out.println("List before removing: " + list);
int beforeSize = list.size();
T actualResult;
if(positionToRemove==AddRemovePosition.FRONT) {
actualResult = list.removeFront();
} else { // positionToRemove==Position.BACK
actualResult = list.removeBack();
}
System.out.println("List after removing: " + list);
System.out.println("Expected returned result: " + expectedResult + "\n Actual returned result: " + actualResult);
int expectedAfterSize = 0, actualAfterSize = 0;
if(expectedResult!=null) {
actualAfterSize = list.size();
expectedAfterSize = beforeSize-1;
if(expectedAfterSize != actualAfterSize) {
allTestsPassed = false;
System.out.println("**********TEST FAILED with the wrong list size when removing from " + positionToRemove + ". \ttest: " + testDescription);
System.out.println("Expected after size: " + expectedAfterSize + "\n Actual after size: " + actualAfterSize);
}
}
if(expectedResult==null && actualResult!=null) {
allTestsPassed = false;
System.out.println("**********TEST FAILED: incorrect element returned. \ttest:" + testDescription);
} else if(expectedResult!=null && !expectedResult.equals(actualResult)) {
allTestsPassed = false;
System.out.println("**********TEST FAILED: incorrect element returned. \ttest:" + testDescription);
}
}
public static enum AddRemovePosition {
FRONT, BACK;
public String toString() {
return super.toString().toLowerCase();
}
}
public static enum IndexPosition {
FIRST, LAST;
public String toString() {
return super.toString().toLowerCase();
}
}
}