forked from zsiyed/cse12-wi22-pa4-Iterator-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyLinkedListPublicTester.java
356 lines (329 loc) · 12.7 KB
/
MyLinkedListPublicTester.java
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
/**
* @author Tim Jiang, Andrew Tang
* Date: 1/26/2022
* This file contains all the public tests (visible on Gradescope)
* Use this as a guide to write tests to verify your MyLinkedList and Iterator
* implementation
* Iterator values were set manually to decouple its dependency on next()
* during testing.
*/
import static org.junit.Assert.*;
import org.junit.*;
import java.util.*;
/**
* This class contains public test cases for MyListIterator. listLen1 is a
* linkedlist of length 1 and listLen2 is a linkedlist of length 2.
*/
public class MyLinkedListPublicTester {
private MyLinkedList listLen1, listLen2;
private MyLinkedList.MyListIterator listLen1Iter, listLen2Iter;
/**
* Standard Test Fixture. A list with one entry and a list with two entries
*/
@Before
public void setUp() {
listLen1 = new MyLinkedList();
listLen1.add("Christine");
listLen1Iter = listLen1.new MyListIterator();
listLen2 = new MyLinkedList();
listLen2.add("Paul");
listLen2.add("Cao");
listLen2Iter = listLen2.new MyListIterator();
}
/**
* Test the MyListIterator constructor
*/
@Test
public void testConstructor() {
assertFalse("The initial value of canRemoveOrSet",
listLen1Iter.canRemoveOrSet);
assertEquals(
"The index of the element the iterator is initially pointing" +
" on", 0, listLen1Iter.idx);
assertEquals("The data of initial iterator's left", listLen1.head,
listLen1Iter.left);
assertEquals("The data of initial iterator's left", null,
listLen1Iter.left.data);
assertEquals("The data of initial iterator's right", "Christine",
listLen1Iter.right.data);
}
/**
* Test hasNext() on listLen2, multiple calls
*/
@Test
public void testHasNext() {
assertTrue("call hasNext when there is a next node",
listLen2Iter.hasNext());
// Manually doing listLen2Iter.next();
listLen2Iter.left = listLen2.head.getNext();
listLen2Iter.right = listLen2.head.getNext().getNext();
listLen2Iter.idx = 1;
listLen2Iter.canRemoveOrSet = true;
listLen2Iter.forward = true;
assertTrue("call hasNext when there is a next node",
listLen2Iter.hasNext());
// Manually doing listLen2Iter.next();
listLen2Iter.left = listLen2.head.getNext().getNext();
listLen2Iter.right = listLen2.head.getNext().getNext().getNext();
listLen2Iter.idx = 2;
listLen2Iter.canRemoveOrSet = true;
listLen2Iter.forward = true;
assertFalse("call hasNext when there isn't a next node",
listLen2Iter.hasNext());
}
/**
* Test next() on listLen1, one call
*/
@Test
public void testNextOneCall() {
assertEquals("The element returned from calling next", "Christine",
listLen1Iter.next());
assertEquals("Index of iterator after 1 next()", 1, listLen1Iter.idx);
assertTrue("Able to remove node", listLen1Iter.canRemoveOrSet);
assertTrue("Direction is forward", listLen1Iter.forward);
}
/**
* Test next() on listLen2, two calls
*/
@Test
public void testNextTwoCalls() {
assertEquals("The element returned from calling next", "Paul",
listLen2Iter.next());
assertEquals("Index of iterator after 1 next()", 1, listLen2Iter.idx);
assertEquals("The element returned from calling next", "Cao",
listLen2Iter.next());
assertEquals("Index of iterator after 2 next()", 2, listLen2Iter.idx);
assertTrue("Able to remove node", listLen2Iter.canRemoveOrSet);
assertTrue("Direction is forward", listLen2Iter.forward);
}
/**
* Test hasPrevious() on listLen2, multiple calls
*/
@Test
public void testHasPrev() {
assertFalse("call when there isn't a previous node",
listLen2Iter.hasPrevious());
// Manually doing listLen2Iter.next();
listLen2Iter.left = listLen2.head.getNext();
listLen2Iter.right = listLen2.head.getNext().getNext();
listLen2Iter.idx = 1;
listLen2Iter.canRemoveOrSet = true;
listLen2Iter.forward = true;
assertTrue("call when there is a previous node",
listLen2Iter.hasPrevious());
// Manually doing listLen2Iter.next();
listLen2Iter.left = listLen2.head.getNext().getNext();
listLen2Iter.right = listLen2.head.getNext().getNext().getNext();
listLen2Iter.idx = 2;
listLen2Iter.canRemoveOrSet = true;
listLen2Iter.forward = true;
assertTrue("call when there is a previous node",
listLen2Iter.hasPrevious());
}
/**
* Test previous() on listLen1, one call
*/
@Test
public void testPreviousOneCall() {
// Manually doing listLen1Iter.next();
listLen1Iter.left = listLen1.head.getNext();
listLen1Iter.right = listLen1.head.getNext().getNext();
listLen1Iter.idx = 1;
listLen1Iter.canRemoveOrSet = true;
listLen1Iter.forward = true;
assertEquals("The element returned from calling previous", "Christine",
listLen1Iter.previous());
assertEquals("Index of iterator after 0 previous()", 0,
listLen1Iter.idx);
assertTrue("Able to remove node", listLen1Iter.canRemoveOrSet);
assertFalse("Direction is not forward", listLen1Iter.forward);
}
/**
* Test previous() on listLen1, no previous
*/
@Test
public void testPreviousNoPrevious() {
// Manually doing listLen1Iter.next();
listLen1Iter.left = listLen1.head.getNext();
listLen1Iter.right = listLen1.head.getNext().getNext();
listLen1Iter.idx = 1;
listLen1Iter.canRemoveOrSet = true;
listLen1Iter.forward = true;
assertEquals("The element returned from calling previous", "Christine",
listLen1Iter.previous());
assertEquals("Index of iterator after 2 previous()", 0,
listLen1Iter.idx);
assertTrue("Able to remove node", listLen1Iter.canRemoveOrSet);
assertFalse("Direction is not forward", listLen1Iter.forward);
try {
listLen1Iter.previous(); // Should throw NoSuchElementException
fail();
} catch (NoSuchElementException e) {
// do nothing
}
}
/**
* Test nextIndex() on listLen2 at the beginning of the list
*/
@Test
public void testNextIndex() {
assertEquals("Index before any moves", 0, listLen2Iter.nextIndex());
// Manually doing listLen2Iter.next();
listLen2Iter.left = listLen2.head.getNext();
listLen2Iter.right = listLen2.head.getNext().getNext();
listLen2Iter.idx = 1;
listLen2Iter.forward = true;
listLen2Iter.canRemoveOrSet = true;
assertEquals("Index after 1 next", 1, listLen2Iter.nextIndex());
}
/**
* Test nextIndex() on listLen2 at the end of the list
*/
@Test
public void testNextIndexLastNode() {
listLen2Iter.left = listLen2.head.getNext().getNext();
listLen2Iter.right = listLen2.head.getNext().getNext().getNext();
listLen2Iter.idx = 2;
listLen2Iter.forward = true;
listLen2Iter.canRemoveOrSet = true;
assertEquals("Index after 2 next", 2, listLen2Iter.nextIndex());
}
/**
* Test previousIndex() on listLen2, two calls
*/
@Test
public void testPrevIndex() {
listLen2Iter.left = listLen2.head.getNext().getNext();
listLen2Iter.right = listLen2.head.getNext().getNext().getNext();
listLen2Iter.idx = 2;
listLen2Iter.forward = true;
listLen2Iter.canRemoveOrSet = true;
assertEquals("Index after 2 next, no previous", 1,
listLen2Iter.previousIndex());
listLen2Iter.left = listLen2.head.getNext();
listLen2Iter.right = listLen2.head.getNext().getNext();
listLen2Iter.idx = 1;
listLen2Iter.forward = true;
listLen2Iter.canRemoveOrSet = true;
assertEquals("Index after 2 next, 1 previous", 0,
listLen2Iter.previousIndex());
}
/**
* Test previousIndex() at the beginning of the list
*/
@Test
public void testPrevIndexFirstNode() {
assertEquals("Previous index at first node", -1,
listLen2Iter.previousIndex());
}
/**
* Test set() when forward is true
*/
@Test
public void testSetForward() {
// equivalent to listLen2Iter.next();
listLen2Iter.left = listLen2.head.getNext();
listLen2Iter.right = listLen2.head.getNext().getNext();
listLen2Iter.idx = 1;
listLen2Iter.forward = true;
listLen2Iter.canRemoveOrSet = true;
listLen2Iter.set("Did it change?");
assertEquals("Valid set when forward", "Did it change?",
listLen2Iter.left.getElement());
assertEquals("Valid set when forward, shouldn't change", "Cao",
listLen2Iter.right.getElement());
}
/**
* Test set() when forward is false
*/
@Test
public void testSetBackward() {
// equivalent to 2 next() and 1 previous()
listLen2Iter.left = listLen2.head.getNext();
listLen2Iter.right = listLen2.head.getNext().getNext();
listLen2Iter.idx = 1;
listLen2Iter.forward = false;
listLen2Iter.canRemoveOrSet = true;
listLen2Iter.set("Did it change?");
assertEquals("Valid set when backward", "Did it change?",
listLen2Iter.right.getElement());
assertEquals("Valid set when backward, shouldn't change", "Paul",
listLen2Iter.left.getElement());
}
/**
* Test remove() when forward is true
*/
@Test
public void testRemoveForward() {
// equivalent to listLen2Iter.next();
listLen2Iter.left = listLen2.head.getNext();
listLen2Iter.right = listLen2.head.getNext().getNext();
listLen2Iter.idx = 1;
listLen2Iter.forward = true;
listLen2Iter.canRemoveOrSet = true;
listLen2Iter.remove();
assertEquals("Valid remove when forward", null,
listLen2Iter.left.getElement());
assertEquals("Valid remove when forward, shouldn't change", "Cao",
listLen2Iter.right.getElement());
assertEquals("Index should decrement after removal", 0,
listLen2Iter.idx);
assertFalse("Prevent another remove or set",
listLen2Iter.canRemoveOrSet);
}
/**
* Test remove() when forward is false
*/
@Test
public void testRemoveBackward() {
// equivalent to listLen2Iter.next();
listLen2Iter.left = listLen2.head.getNext();
listLen2Iter.right = listLen2.head.getNext().getNext();
listLen2Iter.idx = 1;
listLen2Iter.forward = false;
listLen2Iter.canRemoveOrSet = true;
listLen2Iter.remove();
assertEquals("Valid remove when forward", null,
listLen2Iter.right.getElement());
assertEquals("Valid remove when backward, shouldn't change", "Paul",
listLen2Iter.left.getElement());
assertEquals("Index shouldn't decrement after removal", 1,
listLen2Iter.idx);
assertFalse("Prevent another remove or set",
listLen2Iter.canRemoveOrSet);
}
/**
* Test add() in the middle of the list
*/
@Test
public void testAddValidIndex() {
// equivalent to listLen2Iter.next();
listLen2Iter.left = listLen2.head.getNext();
listLen2Iter.right = listLen2.head.getNext().getNext();
listLen2Iter.idx = 1;
listLen2Iter.forward = true;
listLen2Iter.canRemoveOrSet = true;
listLen2Iter.add("CSE 12");
assertEquals("Valid add left", "CSE 12",
listLen2Iter.left.getElement());
assertEquals("Valid add right", "Cao",
listLen2Iter.right.getElement());
assertEquals("Index change after add", 2, listLen2Iter.idx);
assertFalse("Cannot remove immediately after add",
listLen2Iter.canRemoveOrSet);
}
/**
* Test add() in the beginning of the list
*/
@Test
public void testAddFirstIndex() {
listLen2Iter.add("CSE 12");
assertEquals("Valid add left", "CSE 12",
listLen2Iter.left.getElement());
assertEquals("Valid add right", "Paul",
listLen2Iter.right.getElement());
assertEquals("Index change after add", 1, listLen2Iter.idx);
assertFalse("Cannot remove immediately after add",
listLen2Iter.canRemoveOrSet);
}
}