-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedListClassTest.java
More file actions
475 lines (369 loc) · 11 KB
/
LinkedListClassTest.java
File metadata and controls
475 lines (369 loc) · 11 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
import static org.junit.Assert.*;
import java.util.Arrays;
import org.junit.Before;
import org.junit.Test;
//remove obj
//remove index
//removeall
public class LinkedListClassTest {
LinkedListClass<Object> list;
LinkedListNode node;
String expectedOutput;
@Before
public void setup() {
list = new LinkedListClass<Object>();
node = new LinkedListNode();
expectedOutput = "";
}
@Test
public void testToString() {
list = createLinkedList(5);
expectedOutput = "{0,1,2,3,4}";
assertEquals(expectedOutput, list.toString());
}
@Test
public void testToStringForSizeOneList() {
list = createLinkedList(1);
expectedOutput = "{0}";
assertEquals(expectedOutput, list.toString());
}
@Test
public void testToStringForEmptyList() {
expectedOutput = "{}";
assertEquals(expectedOutput, list.toString());
}
@Test
public void testNodeEqualsForTwoEqualNodes() {
Integer equalInt = new Integer(2);
node = new LinkedListNode(equalInt);
LinkedListNode otherNode = new LinkedListNode(equalInt);
assertTrue(node.equals(otherNode));
}
@Test
public void testNodeEqualsForTwoUnequalNodes() {
node = new LinkedListNode(new Integer(2));
LinkedListNode otherNode = new LinkedListNode(new Integer(3));
assertFalse(node.equals(otherNode));
}
@Test
public void testNodeEqualsForTwoDifferentTypes() {
node = new LinkedListNode(new Integer(2));
LinkedListNode otherNode = new LinkedListNode(new String("2"));
assertFalse(node.equals(otherNode));
}
@Test
public void testGetFirstForNullNode() {
assertNull(list.getFirst());
}
@Test
public void testGetFirstForNonNullNode() {
node.setData(new Integer(2));
list = new LinkedListClass(node);
expectedOutput = "" + 2;
assertEquals(expectedOutput,list.getFirst().toString());
}
@Test
public void testNonDefaultConstructor() {
list = new LinkedListClass<Object>(new Object[] {1,2,3,4});
expectedOutput = "{1,2,3,4}";
assertEquals(expectedOutput, list.toString());
}
@Test
public void testNonDefaultConstructorWithEmptyArray() {
list = new LinkedListClass<Object>(new Object[0]);
expectedOutput = "{}";
assertEquals(expectedOutput, list.toString());
}
@Test
public void testGetForValidIndex() {
list = createLinkedList(5);
assertEquals(2, list.get(2));
}
@Test
public void testGetForInvalidIndex() {
list = createLinkedList(5);
assertNull(list.get(6));
}
@Test
public void testAddToEmptyList() {
list.add(new Integer(2));
expectedOutput = "" + 2;
assertEquals(expectedOutput, list.getFirst().toString());
}
@Test
public void testAddToNonemptyList() {
list = createLinkedList(5);
list.add(new Integer(5));
assertEquals(5, list.get(5));
}
@Test
public void testAddIndexToNonemptyList() {
list = new LinkedListClass<Object>(new Object[] {1,2,4,5});
list.add(2,new Integer(3));
expectedOutput = "{1,2,3,4,5}";
assertEquals(expectedOutput, list.toString());
}
@Test
public void testAddIndexForInvalidIndex() {
list = new LinkedListClass<Object>(new Object[] {1,2,4,5});
assertFalse(list.add(6,new Integer(3)));
}
@Test
public void testAddIndexToFirstIndex() {
list = new LinkedListClass<Object>(new Object[] {1,2,3,4});
list.add(0,new Integer(0));
expectedOutput = "{0,1,2,3,4}";
assertEquals(expectedOutput, list.toString());
}
@Test
public void testRemove() {
list = new LinkedListClass<Object>(new Object[] {1,2,4,3,4,5});
expectedOutput = "{1,2,3,4,5}";
assertEquals(new Integer(4), list.remove(2));
assertEquals(expectedOutput, list.toString());
}
@Test
public void testRemoveInvalidIndex() {
list = new LinkedListClass<Object>(new Object[] {1,2,4,3,4,5});
assertNull(list.remove(10));
}
@Test
public void testRemoveZeroIndex() {
list = new LinkedListClass<Object>(new Object[] {2,1,2,3,4,5});
expectedOutput = "{1,2,3,4,5}";
assertEquals(new Integer(2), list.remove(0));
assertEquals(expectedOutput, list.toString());
}
@Test
public void testRemoveLastIndex() {
list = new LinkedListClass<Object>(new Object[] {1,2,3,4,5,6});
expectedOutput = "{1,2,3,4,5}";
assertEquals(new Integer(6), list.remove(5));
assertEquals(expectedOutput, list.toString());
}
@Test
public void testRemoveObject() {
list = new LinkedListClass<Object>(new Object[] {1,2,4,3,4,5});
expectedOutput = "{1,2,3,4,5}";
assertTrue(list.remove(new Integer(4)));
assertEquals(expectedOutput, list.toString());
}
@Test
public void testRemoveObjectNotContained() {
list = new LinkedListClass<Object>(new Object[] {1,2,4,3,4,5});
assertFalse(list.remove(new Integer(6)));
}
@Test
public void testRemoveObjectFirstIndex() {
list = new LinkedListClass<Object>(new Object[] {4,1,2,3,4,5});
expectedOutput = "{1,2,3,4,5}";
assertTrue(list.remove(new Integer(4)));
assertEquals(expectedOutput, list.toString());
}
@Test
public void testRemoveObjectLastIndex() {
list = new LinkedListClass<Object>(new Object[] {1,2,3,4,10});
expectedOutput = "{1,2,3,4}";
assertTrue(list.remove(new Integer(10)));
assertEquals(expectedOutput, list.toString());
}
@Test
public void testRemoveAllObject() {
list = new LinkedListClass<Object>(new Object[] {1,2,3,4,10});
expectedOutput = "{1,2,3,4}";
assertTrue(list.removeAll(new Integer(10)));
assertEquals(expectedOutput, list.toString());
assertEquals(4, list.size());
}
@Test
public void testRemoveAllForMultipleObjects() {
list = new LinkedListClass<Object>(new Object[] {1,2,10,3,4,10});
expectedOutput = "{1,2,3,4}";
assertTrue(list.removeAll(new Integer(10)));
assertEquals(expectedOutput, list.toString());
assertEquals(4, list.size());
}
@Test
public void testRemoveAllForObjectNotContained() {
list = new LinkedListClass<Object>(new Object[] {1,2,10,3,4,10});
assertFalse(list.removeAll(new Integer(11)));
}
@Test
public void testRemoveAllForFirstIndex() {
list = new LinkedListClass<Object>(new Object[] {1,1,1,2,3,4});
expectedOutput = "{2,3,4}";
assertTrue(list.removeAll(new Integer(1)));
assertEquals(expectedOutput, list.toString());
assertEquals(3,list.size());
}
@Test
public void testEqualsForTwoEqualLists() {
list = createLinkedList(5);
LinkedListClass<Object> otherList = createLinkedList(5);
assertTrue(list.equals(otherList));
}
@Test
public void testEqualsForTwoDifferentLengthLists() {
list = createLinkedList(5);
LinkedListClass<Object> otherList = createLinkedList(6);
assertFalse(list.equals(otherList));
}
@Test
public void testEqualsForTwoDifferentContentLists() {
list = createLinkedList(5);
list.add(new Integer(5));
LinkedListClass<Object> otherList = createLinkedList(6);
otherList.add(new Integer(7));
assertFalse(list.equals(otherList));
}
@Test
public void testClearMethod() {
list = createLinkedList(6);
list.clear();
assertEquals(0, list.size());
assertNull(list.getFirst());
}
@Test
public void testClearMethodForEmptyList() {
list.clear();
assertEquals(0, list.size());
assertNull(list.getFirst());
}
@Test
public void testSet() {
list = createLinkedList(6);
assertEquals(4,list.set(4, 10));
expectedOutput = "{0,1,2,3,10,5}";
assertEquals(expectedOutput, list.toString());
}
@Test
public void testSetForInvalidIndex() {
expectedOutput = "Index out of bounds!";
try {
list = createLinkedList(3);
assertEquals(4,list.set(4, 10));
fail("Index out of bounds should be thrown");
}
catch(IndexOutOfBoundsException e) {
assertEquals(expectedOutput, e.getMessage());
}
}
@Test
public void testAddFirstToEmptyList() {
list.addFirst(new Integer(5));
assertEquals(5, list.get(0));
assertEquals(1, list.size());
}
@Test
public void testAddFirstToNonemptyList() {
list = createLinkedList(5);
list.addFirst(new Integer(5));
assertEquals(5, list.get(0));
assertEquals(6, list.size());
}
@Test
public void testToArrayForNonemptyArray() {
list = createLinkedList(5);
Object[] array = new Object[] {0,1,2,3,4};
assertEquals(list.toArray(), array);
}
@Test
public void testToArrayForEmptyArray() {
Object[] array = new Object[0];
assertEquals(list.toArray(), array);
}
@Test
public void testIsEmptyWithEmptyArray() {
assertTrue(list.isEmpty());
}
@Test
public void testIsEmptyWithNonemptyArray() {
list = createLinkedList(5);
assertFalse(list.isEmpty());
}
@Test
public void testGetLastWithNonemptyArray() {
list = new LinkedListClass<Object>(new Object[] {1,2,3,4});
expectedOutput = "4";
assertEquals(expectedOutput, list.getLast().toString());
}
@Test
public void testGetLastWithEmptyArray() {
assertNull(list.getLast());
}
@Test
public void testElementWithNonemptyArray() {
list = new LinkedListClass<Object>(new Object[] {1,2,3,4});
expectedOutput = "1";
assertEquals(expectedOutput, list.element().toString());
}
@Test
public void testElementWithEmptyArray() {
assertNull(list.element());
}
@Test
public void testIndexOf() {
list = new LinkedListClass<Object>(new Object[] {"1","2","3","4"});
assertEquals(1, list.indexOf("2"));
}
@Test
public void testIndexOfOnFirstIndex() {
list = new LinkedListClass<Object>(new Object[] {"1","2","3","4"});
assertEquals(0, list.indexOf("1"));
}
@Test
public void testIndexOfOnNonexistentObject() {
list = new LinkedListClass<Object>(new Object[] {"1","2","3","4"});
assertEquals(-1, list.indexOf("6"));
}
@Test
public void testIndexOfOnEmptyArray() {
assertEquals(-1, list.indexOf("6"));
}
@Test
public void testIndexOfOnLastIndex() {
list = new LinkedListClass<Object>(new Object[] {"1","2","3","4"});
assertEquals(3, list.indexOf("4"));
}
@Test
public void testLastIndexOf() {
list = new LinkedListClass<Object>(new Object[] {"1","2","3","4"});
assertEquals(3, list.lastIndexOf("4"));
}
@Test
public void testLastIndexOfWithMultipleSameObject() {
list = new LinkedListClass<Object>(new Object[] {"4","4","4","4"});
assertEquals(3, list.lastIndexOf("4"));
}
@Test
public void testLastIndexOfWithObjectNotContained() {
list = new LinkedListClass<Object>(new Object[] {"1","1","1","1"});
assertEquals(-1, list.lastIndexOf("4"));
}
@Test
public void testContains() {
list = new LinkedListClass<Object>(new Object[] {"1","1","1","1"});
assertTrue(list.contains("1"));
}
@Test
public void testContainsForArrayNotContainingObject() {
list = new LinkedListClass<Object>(new Object[] {"1","1","1","1"});
assertFalse(list.contains("2"));
}
@Test
public void testContainsForEmptyArray() {
assertFalse(list.contains("1"));
}
//creates a linkedlist with the number of nodes as the parameter specified and populates w/ an integer equal to the index
private LinkedListClass<Object> createLinkedList(int numberOfNodes) {
LinkedListNode[] nodeList = new LinkedListNode[numberOfNodes];
nodeList[0] = new LinkedListNode();
nodeList[0].setData(new Integer(0));
for(int i = 1; i < numberOfNodes; i++) {
nodeList[i] = new LinkedListNode();
nodeList[i].setData(new Integer(i));
nodeList[i - 1].setNext(nodeList[i]);
}
return new LinkedListClass<Object>(nodeList[0]);
}
}