-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathList12Tester.java
More file actions
525 lines (502 loc) · 12.8 KB
/
List12Tester.java
File metadata and controls
525 lines (502 loc) · 12.8 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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
/**
* @author Cindy Yu (cs12shz)
* @see java.util.list<E>
*
*/
import java.util.*;
/**
* Class List12Tester
* This class is basically a test plan that implements the Java Collections
* interface List<E>, and implement the test plan using the JUnit Framework. It
* contains many tests that are used to verify that the methods being used in
* the list is performing correctly and have the correct parameters.
*/
public class List12Tester extends junit.framework.TestCase
{
/**
* The main method will run the List12Tester class as a program once there is
* a List12<E> class to test it with. The main method will provide a GUI
* program that will show if the List12 provided passed or failed any tests.
*/
public static void main(String args[])
{
junit.swingui.TestRunner.main(new String[] {"List12Tester"});
}
/** Checks to see if the program adds the specified elements to the list
* and does not add anything that was not specified. Uses the contains
* method to check the add method to check that it is added within.
**/
public void testAddContains()
{
// Setup
List<String> theL = new List12<String>();
// Apply Mutations
theL.add("A");
theL.add("B");
// Verifying Mutations
assertTrue(theL.contains("A"));
assertTrue(theL.contains("B"));
assertFalse(theL.contains("C"));
}
/**
* Checks to see if the program is adding the elements at the end of the list
* and then compares it to the index to verify. Uses the get method to check
* that the add method added the elements correctly.
**/
public void testAddGet()
{
// Setup
List<Integer> theL = new List12<Integer>();
// Applying Mutations
for(int i = 1; i<=1000; i++)
{
theL.add(i);
}
// Verifying all the Mutations
for(int i = 0; i<1000; i++)
{
assertEquals(theL.get(i), new Integer(i+1));
}
}
/**
* Checks to see if the program adds the elements and returns true
* or false depending on if it did or not. Uses the return values to check
* the add method.
**/
public void testAddReturn()
{
// Setup
List<Integer> theL = new List12<Integer>();
// Verifying and applying the mutations
assertTrue(theL.add(1));
assertTrue(theL.add(2));
assertTrue(theL.add(1));
}
/**
* Checks to see if the program throws an exception when an index out of bound
* is used. Uses a try-catch to catch the IndexOutOfBounds exception.
**/
public void testAddIndex()
{
// Setup
List<Integer> theL = new List12<Integer>();
try
{
// Apply the Mutator
theL.add(1,15);
theL.add(-1,40);
fail(); // Fails the list after it has gone to the catch block
}
// Catches the exception
catch(IndexOutOfBoundsException ex)
{
//Passes the test
}
// Catches any unexpected exceptions
catch(Throwable ex)
{
fail();
}
}
/**
* Checks to see if the program contains the specified elements after it has
* been added to the list. Uses set to test the contain method.
*/
public void testContainsSet()
{
// Setup
List<String> theL = new List12<String>();
// Apply the Mutators
theL.add("A");
theL.add("B");
theL.set(0,"C");
theL.set(1,"D");
// Verifying the mutators
assertFalse(theL.contains("A"));
assertFalse(theL.contains("B"));
//assertTrue(theL.contains("C"));
assertTrue(theL.contains("D"));
}
/**
* Checks to see if the program contains the specified elements after it has
* been removed from the list. Uses remove method to test the contain method.
*/
public void testContainsRemove()
{
// Setup
List<String> theL = new List12<String>();
// Apply the Mutators
theL.add("A");
theL.add("B");
theL.add("C");
theL.add("D");
// Removing several elements for testing
theL.remove(1);
theL.remove("C");
// Verifying the mutators
assertTrue(theL.contains("A"));
assertFalse(theL.contains("B"));
assertFalse(theL.contains("C"));
assertTrue(theL.contains("D"));
}
/**
* Checks to see if the program can get the element at the position in the
* list. Uses the remove method to test the get method.
*/
public void testGetRemove()
{
// Setup
List<String> theL = new List12<String>();
// Apply the Mutators
theL.add("A");
theL.add("B");
theL.add("C");
theL.add("D");
theL.add(1,"E");
theL.remove(2);
theL.remove("D");
// Verifying the mutators
assertTrue(theL.get(0) == "A");
assertFalse(theL.get(1) == "B");
assertTrue(theL.get(1) == "E");
assertTrue(theL.get(2) == "C");
}
/**
* Checks to see if the program can get the element at the position in the
* list. Uses the set method to test the get method.
*/
public void testGetSet()
{
// Setup
List<String> theL = new List12<String>();
// Apply the Mutators
theL.add("A");
theL.add("B");
theL.add("C");
theL.add("D");
theL.set(1, "E");
theL.set(3, "F");
// Verifying the mutators
assertTrue(theL.get(0) == "A");
assertFalse(theL.get(1) == "B");
assertTrue(theL.get(1) == "E");
assertTrue(theL.get(2) == "C");
assertFalse(theL.get(3) == "E");
assertTrue(theL.get(3) == "F");
}
/**
* Checks to see if the program will fail when it tries to get an element
* from an index that does not exist in the list.
*/
public void testGetIndex()
{
//Setup
List<Integer> theL = new List12<Integer>();
try
{
//Attempts to get an index that does not exist
theL.get(1);
fail();
}
// Catches the exception
catch(IndexOutOfBoundsException ex)
{
//Passes the test
}
// Catches any unexpected exceptions
catch(Throwable ex)
{
fail();
}
}
/**
* Checks to see if the program will remove the specified element from list.
* Checks that the boolean return from the remove method is true.
*/
public void testRemoveIndex()
{
//Setup
List<String> theL = new List12<String>();
// Applying the mutators
theL.add("A");
theL.add("B");
theL.add("C");
theL.add("D");
// Verifying the mutators
assertTrue(theL.remove("A"));
assertTrue(theL.remove("C"));
assertTrue(theL.remove("D"));
}
/**
* Checks to see if the program will remove an index that does not exist.
* It should throw an IndexOutOfBounds exception.
*/
public void testRemoveExcept()
{
// Setup
List<Integer> theL = new List12<Integer>();
try
{
// Attempts to remove an index that does not exist
theL.remove(0);
fail();
}
// Catches the exception
catch(IndexOutOfBoundsException ex)
{
//Passes the test
}
// Catches any unexpected exceptions
catch(Throwable ex)
{
fail();
}
}
/**
* Checks to see if the program will replace the element at an index that
* does not exist. It should throw an IndexOutOfBounds exception.
*/
public void testSetIndex()
{
// Setup
List<Integer> theL = new List12<Integer>();
try
{
// Attempts to set something to index 0
theL.set(0, 1);
fail();
}
// Catches the exception
catch(IndexOutOfBoundsException ex)
{
// Passes the test
}
// Catches any unexpected exceptions
catch(Throwable ex)
{
fail();
}
}
/**
* Checks to see if the program has the correct number of elements in the
* list. Uses the add method to test the size method.
*/
public void testSizeAdd()
{
// Setup
List<String> theL = new List12<String>();
// Apply the Mutators
theL.add("A");
theL.add("B");
// Verify the mutators
assertEquals(theL.size(), 2);
// Apply more mutators
theL.add("C");
theL.add(1,"D");
// Verify that the add(index, element) is successful
assertEquals(theL.size(), 4);
}
/**
* Checks to see if the program has the correct number of elements in the
* list. Uses the remove method to test the size method.
*/
public void testSizeRemove()
{
// Setup
List<String> theL = new List12<String>();
// Apply the Mutators
theL.add("A");
theL.add("B");
theL.add("C");
theL.add("D");
theL.remove(1);
theL.remove("D");
// Verify the mutators
assertEquals(theL.size(), 2);
}
/**
* Checks to see if the program has the correct number of elements in the
* list. Uses the set method to test the size method.
*/
public void testSizeSet()
{
// Setup
List<String> theL = new List12<String>();
// Apply the mutators
theL.add("A");
theL.add("B");
theL.add("C");
theL.add("D");
assertEquals(theL.set(1,"E"), new String("B"));
assertEquals(theL.set(3,"F"), new String("D"));
// Verify the mutators
assertEquals(theL.size(), 4);
}
/**
* Checks to see if the iterator's remove method will throw an exception
* when the next method has not been called yet.
*/
public void testIteratorRemove()
{
try
{
// Setup
List<Integer> myList = new List12<Integer>();
// Apply Mutators
myList.add(1);
// Setup the iterator list
Iterator<Integer> iterL = myList.iterator();
// Attempts to call remove
iterL.remove();
fail();
}
// Catches the IllegalState exception
catch(IllegalStateException ex)
{
//Passes the test
}
// Catches any unexpected exceptions
catch(Throwable ex)
{
fail();
}
}
/**
* Checks to see if the iterator's remove method will throw an exception when
* the remove method is called more than once after the last next method.
*/
public void testIterRemove()
{
try
{
// Setup
List<Integer> myList = new List12<Integer>();
// Apply mutators
myList.add(1);
myList.add(2);
myList.add(3);
myList.add(4);
// Setup the iterator list
Iterator<Integer> iterL = myList.iterator();
// Calls next once but remove twice
iterL.next();
iterL.remove();
iterL.remove();
fail();
}
// Catches the IllegalState exception
catch(IllegalStateException ex)
{
//Passes the test
}
// Catches any unexpected exceptions
catch(Throwable ex)
{
fail();
}
}
/**
* Checks to see if the iterator's remove method will throw an exception when
* it turns a list into an empty list.
*/
public void testIterateRemove()
{
try
{
// Setup
List<Integer> myList= new List12<Integer>();
// Apply Mutators
myList.add(1);
myList.add(2);
myList.add(3);
myList.add(4);
// Setup the iterator list
Iterator<Integer> iterL = myList.iterator();
// Loops through the list and removes all the elements
while(iterL.hasNext())
{
iterL.next();
iterL.remove();
}
}
// Catches any unexpected exceptions
catch(IllegalStateException ex)
{
fail(); //Should not reach here
}
catch(Throwable ex)
{
fail(); //Should not reach here
}
}
/**
* Checks to see if the iterator's hasNext method will return true if the
* iteration has more elements.
*/
public void testIterHasNext()
{
// Setup
List<String> myList = new List12<String>();
Iterator<String> iterL = myList.iterator();
// Verifying that this is an empty list
assertFalse(iterL.hasNext());
// Apply mutators
myList.add("A");
myList.add("B");
// Update iterator list
iterL = myList.iterator();
// Verify list
assertTrue(iterL.hasNext());
iterL.next();
assertTrue(iterL.hasNext());
iterL.next();
assertFalse(iterL.hasNext());
}
/**
* Checks to see if the iterator's next method will return an exception
* when it is called on an empty list.
*/
public void testIterNextException()
{
try
{
// Setup
List<String> myList = new List12<String>();
myList.add("A");
Iterator<String> iterL = myList.iterator();
// Calls next on an empty list
iterL.next();
iterL.next();
fail();
}
// Catches the NoSuchElement exception
catch(NoSuchElementException ex)
{
//Passes the test
}
// Catches any unexpected exceptions
catch(Throwable ex)
{
fail();
}
}
/**
* Checks to see if the iterator's next method will return the next element
* in the iteration.
*/
public void testIterNext()
{
// Setup
List<String> myList = new List12<String>();
// Apply mutators
myList.add("A");
myList.add("B");
myList.add("C");
// Setup the iterator list
Iterator<String> iterL = myList.iterator();
// Verify that the next method returns elements that equal the inputted ones
assertEquals(iterL.next(), "A");
assertEquals(iterL.next(), "B");
assertEquals(iterL.next(), "C");
}
}