1 |
|
/* |
2 |
< |
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
< |
* public domain. Use, modify, and redistribute this code in any way |
4 |
< |
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
< |
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
2 |
> |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
> |
* Expert Group and released to the public domain, as explained at |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
< |
import junit.framework.*; |
10 |
< |
import java.util.*; |
11 |
< |
import java.util.concurrent.*; |
12 |
< |
|
13 |
< |
public class LinkedListTest extends TestCase { |
14 |
< |
|
15 |
< |
private static final int N = 10; |
15 |
< |
private static final long SHORT_DELAY_MS = 100; |
16 |
< |
private static final long MEDIUM_DELAY_MS = 1000; |
17 |
< |
private static final long LONG_DELAY_MS = 10000; |
9 |
> |
import java.util.Arrays; |
10 |
> |
import java.util.Collection; |
11 |
> |
import java.util.Iterator; |
12 |
> |
import java.util.LinkedList; |
13 |
> |
import java.util.List; |
14 |
> |
import java.util.NoSuchElementException; |
15 |
> |
import java.util.concurrent.ThreadLocalRandom; |
16 |
|
|
17 |
+ |
import junit.framework.Test; |
18 |
+ |
|
19 |
+ |
public class LinkedListTest extends JSR166TestCase { |
20 |
|
public static void main(String[] args) { |
21 |
< |
junit.textui.TestRunner.run (suite()); |
21 |
> |
main(suite(), args); |
22 |
|
} |
23 |
|
|
24 |
|
public static Test suite() { |
25 |
< |
return new TestSuite(LinkedListTest.class); |
25 |
> |
class Implementation implements CollectionImplementation { |
26 |
> |
public Class<?> klazz() { return LinkedList.class; } |
27 |
> |
public List emptyCollection() { return new LinkedList(); } |
28 |
> |
public Object makeElement(int i) { return JSR166TestCase.itemFor(i); } |
29 |
> |
public boolean isConcurrent() { return false; } |
30 |
> |
public boolean permitsNulls() { return true; } |
31 |
> |
} |
32 |
> |
class SubListImplementation extends Implementation { |
33 |
> |
@SuppressWarnings("unchecked") |
34 |
> |
public List emptyCollection() { |
35 |
> |
List list = super.emptyCollection(); |
36 |
> |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
37 |
> |
if (rnd.nextBoolean()) |
38 |
> |
list.add(makeElement(rnd.nextInt())); |
39 |
> |
int i = rnd.nextInt(list.size() + 1); |
40 |
> |
return list.subList(i, i); |
41 |
> |
} |
42 |
> |
} |
43 |
> |
return newTestSuite( |
44 |
> |
LinkedListTest.class, |
45 |
> |
CollectionTest.testSuite(new Implementation()), |
46 |
> |
CollectionTest.testSuite(new SubListImplementation())); |
47 |
|
} |
48 |
|
|
49 |
|
/** |
50 |
< |
* Create a queue of given size containing consecutive |
51 |
< |
* Integers 0 ... n. |
50 |
> |
* Returns a new queue of given size containing consecutive |
51 |
> |
* Items 0 ... n - 1. |
52 |
|
*/ |
53 |
< |
private LinkedList fullQueue(int n) { |
54 |
< |
LinkedList q = new LinkedList(); |
53 |
> |
private static LinkedList<Item> populatedQueue(int n) { |
54 |
> |
LinkedList<Item> q = new LinkedList<>(); |
55 |
|
assertTrue(q.isEmpty()); |
56 |
< |
for(int i = 0; i < n; ++i) |
57 |
< |
assertTrue(q.offer(new Integer(i))); |
56 |
> |
for (int i = 0; i < n; ++i) |
57 |
> |
mustOffer(q, i); |
58 |
|
assertFalse(q.isEmpty()); |
59 |
< |
assertEquals(n, q.size()); |
59 |
> |
mustEqual(n, q.size()); |
60 |
> |
mustEqual(0, q.peekFirst()); |
61 |
> |
mustEqual((n - 1), q.peekLast()); |
62 |
|
return q; |
63 |
|
} |
64 |
< |
|
65 |
< |
public void testConstructor1(){ |
66 |
< |
assertEquals(0, new LinkedList().size()); |
64 |
> |
|
65 |
> |
/** |
66 |
> |
* new queue is empty |
67 |
> |
*/ |
68 |
> |
public void testConstructor1() { |
69 |
> |
mustEqual(0, new LinkedList<Item>().size()); |
70 |
|
} |
71 |
|
|
72 |
+ |
/** |
73 |
+ |
* Initializing from null Collection throws NPE |
74 |
+ |
*/ |
75 |
|
public void testConstructor3() { |
76 |
|
try { |
77 |
< |
LinkedList q = new LinkedList((Collection)null); |
78 |
< |
fail("Cannot make from null collection"); |
79 |
< |
} |
50 |
< |
catch (NullPointerException success) {} |
77 |
> |
new LinkedList<Item>((Collection<Item>)null); |
78 |
> |
shouldThrow(); |
79 |
> |
} catch (NullPointerException success) {} |
80 |
|
} |
81 |
|
|
82 |
< |
public void testConstructor6(){ |
83 |
< |
try { |
84 |
< |
Integer[] ints = new Integer[N]; |
85 |
< |
for (int i = 0; i < N; ++i) |
86 |
< |
ints[i] = new Integer(i); |
87 |
< |
LinkedList q = new LinkedList(Arrays.asList(ints)); |
88 |
< |
for (int i = 0; i < N; ++i) |
89 |
< |
assertEquals(ints[i], q.poll()); |
61 |
< |
} |
62 |
< |
finally {} |
82 |
> |
/** |
83 |
> |
* Queue contains all elements of collection used to initialize |
84 |
> |
*/ |
85 |
> |
public void testConstructor6() { |
86 |
> |
Item[] items = defaultItems; |
87 |
> |
LinkedList<Item> q = new LinkedList<>(Arrays.asList(items)); |
88 |
> |
for (int i = 0; i < SIZE; ++i) |
89 |
> |
mustEqual(items[i], q.poll()); |
90 |
|
} |
91 |
|
|
92 |
+ |
/** |
93 |
+ |
* isEmpty is true before add, false after |
94 |
+ |
*/ |
95 |
|
public void testEmpty() { |
96 |
< |
LinkedList q = new LinkedList(); |
96 |
> |
LinkedList<Item> q = new LinkedList<>(); |
97 |
|
assertTrue(q.isEmpty()); |
98 |
< |
q.add(new Integer(1)); |
98 |
> |
q.add(one); |
99 |
|
assertFalse(q.isEmpty()); |
100 |
< |
q.add(new Integer(2)); |
100 |
> |
q.add(two); |
101 |
|
q.remove(); |
102 |
|
q.remove(); |
103 |
|
assertTrue(q.isEmpty()); |
104 |
|
} |
105 |
|
|
106 |
+ |
/** |
107 |
+ |
* size changes when elements added and removed |
108 |
+ |
*/ |
109 |
|
public void testSize() { |
110 |
< |
LinkedList q = fullQueue(N); |
111 |
< |
for (int i = 0; i < N; ++i) { |
112 |
< |
assertEquals(N-i, q.size()); |
110 |
> |
LinkedList<Item> q = populatedQueue(SIZE); |
111 |
> |
for (int i = 0; i < SIZE; ++i) { |
112 |
> |
mustEqual(SIZE - i, q.size()); |
113 |
|
q.remove(); |
114 |
|
} |
115 |
< |
for (int i = 0; i < N; ++i) { |
116 |
< |
assertEquals(i, q.size()); |
117 |
< |
q.add(new Integer(i)); |
115 |
> |
for (int i = 0; i < SIZE; ++i) { |
116 |
> |
mustEqual(i, q.size()); |
117 |
> |
mustAdd(q, i); |
118 |
|
} |
119 |
|
} |
120 |
|
|
121 |
< |
public void testOfferNull(){ |
122 |
< |
try { |
123 |
< |
LinkedList q = new LinkedList(); |
124 |
< |
q.offer(null); |
125 |
< |
} catch (NullPointerException ie) { |
126 |
< |
fail("should not throw NPE"); |
127 |
< |
} |
121 |
> |
/** |
122 |
> |
* offer(null) succeeds |
123 |
> |
*/ |
124 |
> |
public void testOfferNull() { |
125 |
> |
LinkedList<Item> q = new LinkedList<>(); |
126 |
> |
q.offer(null); |
127 |
> |
assertNull(q.get(0)); |
128 |
> |
assertTrue(q.contains(null)); |
129 |
|
} |
130 |
|
|
131 |
+ |
/** |
132 |
+ |
* Offer succeeds |
133 |
+ |
*/ |
134 |
|
public void testOffer() { |
135 |
< |
LinkedList q = new LinkedList(); |
136 |
< |
assertTrue(q.offer(new Integer(0))); |
137 |
< |
assertTrue(q.offer(new Integer(1))); |
135 |
> |
LinkedList<Item> q = new LinkedList<>(); |
136 |
> |
mustOffer(q, zero); |
137 |
> |
mustOffer(q, one); |
138 |
|
} |
139 |
|
|
140 |
< |
public void testAdd(){ |
141 |
< |
LinkedList q = new LinkedList(); |
142 |
< |
for (int i = 0; i < N; ++i) { |
143 |
< |
assertEquals(i, q.size()); |
144 |
< |
assertTrue(q.add(new Integer(i))); |
140 |
> |
/** |
141 |
> |
* add succeeds |
142 |
> |
*/ |
143 |
> |
public void testAdd() { |
144 |
> |
LinkedList<Item> q = new LinkedList<>(); |
145 |
> |
for (int i = 0; i < SIZE; ++i) { |
146 |
> |
mustEqual(i, q.size()); |
147 |
> |
mustAdd(q, i); |
148 |
|
} |
149 |
|
} |
150 |
|
|
151 |
< |
public void testAddAll1(){ |
151 |
> |
/** |
152 |
> |
* addAll(null) throws NPE |
153 |
> |
*/ |
154 |
> |
public void testAddAll1() { |
155 |
> |
LinkedList<Item> q = new LinkedList<>(); |
156 |
|
try { |
113 |
– |
LinkedList q = new LinkedList(); |
157 |
|
q.addAll(null); |
158 |
< |
fail("Cannot add null collection"); |
159 |
< |
} |
160 |
< |
catch (NullPointerException success) {} |
158 |
> |
shouldThrow(); |
159 |
> |
} catch (NullPointerException success) {} |
160 |
> |
} |
161 |
> |
|
162 |
> |
/** |
163 |
> |
* Queue contains all elements, in traversal order, of successful addAll |
164 |
> |
*/ |
165 |
> |
public void testAddAll5() { |
166 |
> |
Item[] empty = new Item[0]; |
167 |
> |
Item[] items = defaultItems; |
168 |
> |
LinkedList<Item> q = new LinkedList<>(); |
169 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
170 |
> |
assertTrue(q.addAll(Arrays.asList(items))); |
171 |
> |
for (int i = 0; i < SIZE; ++i) |
172 |
> |
mustEqual(items[i], q.poll()); |
173 |
> |
} |
174 |
> |
|
175 |
> |
/** |
176 |
> |
* addAll with too large an index throws IOOBE |
177 |
> |
*/ |
178 |
> |
public void testAddAll2_IndexOutOfBoundsException() { |
179 |
> |
LinkedList<Item> l = new LinkedList<>(); |
180 |
> |
l.add(zero); |
181 |
> |
LinkedList<Item> m = new LinkedList<>(); |
182 |
> |
m.add(one); |
183 |
> |
try { |
184 |
> |
l.addAll(4,m); |
185 |
> |
shouldThrow(); |
186 |
> |
} catch (IndexOutOfBoundsException success) {} |
187 |
|
} |
188 |
|
|
189 |
< |
public void testAddAll5(){ |
189 |
> |
/** |
190 |
> |
* addAll with negative index throws IOOBE |
191 |
> |
*/ |
192 |
> |
public void testAddAll4_BadIndex() { |
193 |
> |
LinkedList<Item> l = new LinkedList<>(); |
194 |
> |
l.add(zero); |
195 |
> |
LinkedList<Item> m = new LinkedList<>(); |
196 |
> |
m.add(one); |
197 |
|
try { |
198 |
< |
Integer[] empty = new Integer[0]; |
199 |
< |
Integer[] ints = new Integer[N]; |
200 |
< |
for (int i = 0; i < N; ++i) |
125 |
< |
ints[i] = new Integer(i); |
126 |
< |
LinkedList q = new LinkedList(); |
127 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
128 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
129 |
< |
for (int i = 0; i < N; ++i) |
130 |
< |
assertEquals(ints[i], q.poll()); |
131 |
< |
} |
132 |
< |
finally {} |
198 |
> |
l.addAll(-1,m); |
199 |
> |
shouldThrow(); |
200 |
> |
} catch (IndexOutOfBoundsException success) {} |
201 |
|
} |
202 |
|
|
203 |
< |
public void testPoll(){ |
204 |
< |
LinkedList q = fullQueue(N); |
205 |
< |
for (int i = 0; i < N; ++i) { |
206 |
< |
assertEquals(i, ((Integer)q.poll()).intValue()); |
203 |
> |
/** |
204 |
> |
* poll succeeds unless empty |
205 |
> |
*/ |
206 |
> |
public void testPoll() { |
207 |
> |
LinkedList<Item> q = populatedQueue(SIZE); |
208 |
> |
for (int i = 0; i < SIZE; ++i) { |
209 |
> |
mustEqual(i, q.poll()); |
210 |
|
} |
211 |
< |
assertNull(q.poll()); |
211 |
> |
assertNull(q.poll()); |
212 |
|
} |
213 |
|
|
214 |
< |
public void testPeek(){ |
215 |
< |
LinkedList q = fullQueue(N); |
216 |
< |
for (int i = 0; i < N; ++i) { |
217 |
< |
assertEquals(i, ((Integer)q.peek()).intValue()); |
218 |
< |
q.poll(); |
214 |
> |
/** |
215 |
> |
* peek returns next element, or null if empty |
216 |
> |
*/ |
217 |
> |
public void testPeek() { |
218 |
> |
LinkedList<Item> q = populatedQueue(SIZE); |
219 |
> |
for (int i = 0; i < SIZE; ++i) { |
220 |
> |
mustEqual(i, q.peek()); |
221 |
> |
mustEqual(i, q.poll()); |
222 |
|
assertTrue(q.peek() == null || |
223 |
< |
i != ((Integer)q.peek()).intValue()); |
223 |
> |
!q.peek().equals(i)); |
224 |
|
} |
225 |
< |
assertNull(q.peek()); |
225 |
> |
assertNull(q.peek()); |
226 |
|
} |
227 |
|
|
228 |
< |
public void testElement(){ |
229 |
< |
LinkedList q = fullQueue(N); |
230 |
< |
for (int i = 0; i < N; ++i) { |
231 |
< |
assertEquals(i, ((Integer)q.element()).intValue()); |
232 |
< |
q.poll(); |
228 |
> |
/** |
229 |
> |
* element returns next element, or throws NSEE if empty |
230 |
> |
*/ |
231 |
> |
public void testElement() { |
232 |
> |
LinkedList<Item> q = populatedQueue(SIZE); |
233 |
> |
for (int i = 0; i < SIZE; ++i) { |
234 |
> |
mustEqual(i, q.element()); |
235 |
> |
mustEqual(i, q.poll()); |
236 |
|
} |
237 |
|
try { |
238 |
|
q.element(); |
239 |
< |
fail("no such element"); |
240 |
< |
} |
164 |
< |
catch (NoSuchElementException success) {} |
239 |
> |
shouldThrow(); |
240 |
> |
} catch (NoSuchElementException success) {} |
241 |
|
} |
242 |
|
|
243 |
< |
public void testRemove(){ |
244 |
< |
LinkedList q = fullQueue(N); |
245 |
< |
for (int i = 0; i < N; ++i) { |
246 |
< |
assertEquals(i, ((Integer)q.remove()).intValue()); |
243 |
> |
/** |
244 |
> |
* remove removes next element, or throws NSEE if empty |
245 |
> |
*/ |
246 |
> |
public void testRemove() { |
247 |
> |
LinkedList<Item> q = populatedQueue(SIZE); |
248 |
> |
for (int i = 0; i < SIZE; ++i) { |
249 |
> |
mustEqual(i, q.remove()); |
250 |
|
} |
251 |
|
try { |
252 |
|
q.remove(); |
253 |
< |
fail("remove should throw"); |
254 |
< |
} catch (NoSuchElementException success){ |
176 |
< |
} |
253 |
> |
shouldThrow(); |
254 |
> |
} catch (NoSuchElementException success) {} |
255 |
|
} |
256 |
|
|
257 |
< |
public void testRemoveElement(){ |
258 |
< |
LinkedList q = fullQueue(N); |
259 |
< |
for (int i = 1; i < N; i+=2) { |
260 |
< |
assertTrue(q.remove(new Integer(i))); |
261 |
< |
} |
262 |
< |
for (int i = 0; i < N; i+=2) { |
263 |
< |
assertTrue(q.remove(new Integer(i))); |
264 |
< |
assertFalse(q.remove(new Integer(i+1))); |
257 |
> |
/** |
258 |
> |
* remove(x) removes x and returns true if present |
259 |
> |
*/ |
260 |
> |
public void testRemoveElement() { |
261 |
> |
LinkedList<Item> q = populatedQueue(SIZE); |
262 |
> |
for (int i = 1; i < SIZE; i += 2) { |
263 |
> |
mustContain(q, i); |
264 |
> |
mustRemove(q, i); |
265 |
> |
mustNotContain(q, i); |
266 |
> |
mustContain(q, i - 1); |
267 |
> |
} |
268 |
> |
for (int i = 0; i < SIZE; i += 2) { |
269 |
> |
mustContain(q, i); |
270 |
> |
mustRemove(q, i); |
271 |
> |
mustNotContain(q, i); |
272 |
> |
mustNotRemove(q, i + 1); |
273 |
> |
mustNotContain(q, i + 1); |
274 |
|
} |
275 |
|
assertTrue(q.isEmpty()); |
276 |
|
} |
277 |
< |
|
278 |
< |
public void testContains(){ |
279 |
< |
LinkedList q = fullQueue(N); |
280 |
< |
for (int i = 0; i < N; ++i) { |
281 |
< |
assertTrue(q.contains(new Integer(i))); |
277 |
> |
|
278 |
> |
/** |
279 |
> |
* contains(x) reports true when elements added but not yet removed |
280 |
> |
*/ |
281 |
> |
public void testContains() { |
282 |
> |
LinkedList<Item> q = populatedQueue(SIZE); |
283 |
> |
for (int i = 0; i < SIZE; ++i) { |
284 |
> |
mustContain(q, i); |
285 |
|
q.poll(); |
286 |
< |
assertFalse(q.contains(new Integer(i))); |
286 |
> |
mustNotContain(q, i); |
287 |
|
} |
288 |
|
} |
289 |
|
|
290 |
< |
public void testClear(){ |
291 |
< |
LinkedList q = fullQueue(N); |
290 |
> |
/** |
291 |
> |
* clear removes all elements |
292 |
> |
*/ |
293 |
> |
public void testClear() { |
294 |
> |
LinkedList<Item> q = populatedQueue(SIZE); |
295 |
|
q.clear(); |
296 |
|
assertTrue(q.isEmpty()); |
297 |
< |
assertEquals(0, q.size()); |
298 |
< |
q.add(new Integer(1)); |
297 |
> |
mustEqual(0, q.size()); |
298 |
> |
mustAdd(q, one); |
299 |
|
assertFalse(q.isEmpty()); |
300 |
|
q.clear(); |
301 |
|
assertTrue(q.isEmpty()); |
302 |
|
} |
303 |
|
|
304 |
< |
public void testContainsAll(){ |
305 |
< |
LinkedList q = fullQueue(N); |
306 |
< |
LinkedList p = new LinkedList(); |
307 |
< |
for (int i = 0; i < N; ++i) { |
304 |
> |
/** |
305 |
> |
* containsAll(c) is true when c contains a subset of elements |
306 |
> |
*/ |
307 |
> |
public void testContainsAll() { |
308 |
> |
LinkedList<Item> q = populatedQueue(SIZE); |
309 |
> |
LinkedList<Item> p = new LinkedList<>(); |
310 |
> |
for (int i = 0; i < SIZE; ++i) { |
311 |
|
assertTrue(q.containsAll(p)); |
312 |
|
assertFalse(p.containsAll(q)); |
313 |
< |
p.add(new Integer(i)); |
313 |
> |
mustAdd(p, i); |
314 |
|
} |
315 |
|
assertTrue(p.containsAll(q)); |
316 |
|
} |
317 |
|
|
318 |
< |
public void testRetainAll(){ |
319 |
< |
LinkedList q = fullQueue(N); |
320 |
< |
LinkedList p = fullQueue(N); |
321 |
< |
for (int i = 0; i < N; ++i) { |
318 |
> |
/** |
319 |
> |
* retainAll(c) retains only those elements of c and reports true if changed |
320 |
> |
*/ |
321 |
> |
public void testRetainAll() { |
322 |
> |
LinkedList<Item> q = populatedQueue(SIZE); |
323 |
> |
LinkedList<Item> p = populatedQueue(SIZE); |
324 |
> |
for (int i = 0; i < SIZE; ++i) { |
325 |
|
boolean changed = q.retainAll(p); |
326 |
|
if (i == 0) |
327 |
|
assertFalse(changed); |
329 |
|
assertTrue(changed); |
330 |
|
|
331 |
|
assertTrue(q.containsAll(p)); |
332 |
< |
assertEquals(N-i, q.size()); |
332 |
> |
mustEqual(SIZE - i, q.size()); |
333 |
|
p.remove(); |
334 |
|
} |
335 |
|
} |
336 |
|
|
337 |
< |
public void testRemoveAll(){ |
338 |
< |
for (int i = 1; i < N; ++i) { |
339 |
< |
LinkedList q = fullQueue(N); |
340 |
< |
LinkedList p = fullQueue(i); |
337 |
> |
/** |
338 |
> |
* removeAll(c) removes only those elements of c and reports true if changed |
339 |
> |
*/ |
340 |
> |
public void testRemoveAll() { |
341 |
> |
for (int i = 1; i < SIZE; ++i) { |
342 |
> |
LinkedList<Item> q = populatedQueue(SIZE); |
343 |
> |
LinkedList<Item> p = populatedQueue(i); |
344 |
|
assertTrue(q.removeAll(p)); |
345 |
< |
assertEquals(N-i, q.size()); |
345 |
> |
mustEqual(SIZE - i, q.size()); |
346 |
|
for (int j = 0; j < i; ++j) { |
347 |
< |
Integer I = (Integer)(p.remove()); |
246 |
< |
assertFalse(q.contains(I)); |
347 |
> |
mustNotContain(q, p.remove()); |
348 |
|
} |
349 |
|
} |
350 |
|
} |
351 |
|
|
352 |
< |
public void testToArray(){ |
353 |
< |
LinkedList q = fullQueue(N); |
354 |
< |
Object[] o = q.toArray(); |
355 |
< |
Arrays.sort(o); |
356 |
< |
for(int i = 0; i < o.length; i++) |
357 |
< |
assertEquals(o[i], q.poll()); |
358 |
< |
} |
359 |
< |
|
360 |
< |
public void testToArray2(){ |
361 |
< |
LinkedList q = fullQueue(N); |
261 |
< |
Integer[] ints = new Integer[N]; |
262 |
< |
ints = (Integer[])q.toArray(ints); |
263 |
< |
Arrays.sort(ints); |
264 |
< |
for(int i = 0; i < ints.length; i++) |
265 |
< |
assertEquals(ints[i], q.poll()); |
266 |
< |
} |
267 |
< |
|
268 |
< |
public void testIterator(){ |
269 |
< |
LinkedList q = fullQueue(N); |
270 |
< |
int i = 0; |
271 |
< |
Iterator it = q.iterator(); |
272 |
< |
while(it.hasNext()) { |
273 |
< |
assertTrue(q.contains(it.next())); |
274 |
< |
++i; |
275 |
< |
} |
276 |
< |
assertEquals(i, N); |
352 |
> |
/** |
353 |
> |
* toArray contains all elements in FIFO order |
354 |
> |
*/ |
355 |
> |
public void testToArray() { |
356 |
> |
LinkedList<Item> q = populatedQueue(SIZE); |
357 |
> |
Object[] a = q.toArray(); |
358 |
> |
assertSame(Object[].class, a.getClass()); |
359 |
> |
for (Object o : a) |
360 |
> |
assertSame(o, q.poll()); |
361 |
> |
assertTrue(q.isEmpty()); |
362 |
|
} |
363 |
|
|
364 |
< |
public void testIteratorOrdering() { |
364 |
> |
/** |
365 |
> |
* toArray(a) contains all elements in FIFO order |
366 |
> |
*/ |
367 |
> |
public void testToArray2() { |
368 |
> |
LinkedList<Item> q = populatedQueue(SIZE); |
369 |
> |
Item[] items = new Item[SIZE]; |
370 |
> |
Item[] array = q.toArray(items); |
371 |
> |
assertSame(items, array); |
372 |
> |
for (Item o : items) |
373 |
> |
assertSame(o, q.poll()); |
374 |
> |
assertTrue(q.isEmpty()); |
375 |
> |
} |
376 |
> |
|
377 |
> |
/** |
378 |
> |
* toArray(null) throws NullPointerException |
379 |
> |
*/ |
380 |
> |
public void testToArray_NullArg() { |
381 |
> |
LinkedList<Item> l = new LinkedList<>(); |
382 |
> |
l.add(zero); |
383 |
> |
try { |
384 |
> |
l.toArray((Item[])null); |
385 |
> |
shouldThrow(); |
386 |
> |
} catch (NullPointerException success) {} |
387 |
> |
} |
388 |
|
|
389 |
< |
final LinkedList q = new LinkedList(); |
389 |
> |
/** |
390 |
> |
* toArray(incompatible array type) throws ArrayStoreException |
391 |
> |
*/ |
392 |
> |
public void testToArray1_BadArg() { |
393 |
> |
LinkedList<Item> l = new LinkedList<>(); |
394 |
> |
l.add(five); |
395 |
> |
try { |
396 |
> |
l.toArray(new String[10]); |
397 |
> |
shouldThrow(); |
398 |
> |
} catch (ArrayStoreException success) {} |
399 |
> |
} |
400 |
> |
|
401 |
> |
/** |
402 |
> |
* iterator iterates through all elements |
403 |
> |
*/ |
404 |
> |
public void testIterator() { |
405 |
> |
LinkedList<Item> q = populatedQueue(SIZE); |
406 |
> |
Iterator<? extends Item> it = q.iterator(); |
407 |
> |
int i; |
408 |
> |
for (i = 0; it.hasNext(); i++) |
409 |
> |
mustContain(q, it.next()); |
410 |
> |
mustEqual(i, SIZE); |
411 |
> |
assertIteratorExhausted(it); |
412 |
> |
} |
413 |
|
|
414 |
< |
q.add(new Integer(1)); |
415 |
< |
q.add(new Integer(2)); |
416 |
< |
q.add(new Integer(3)); |
414 |
> |
/** |
415 |
> |
* iterator of empty collection has no elements |
416 |
> |
*/ |
417 |
> |
public void testEmptyIterator() { |
418 |
> |
assertIteratorExhausted(new LinkedList<>().iterator()); |
419 |
> |
} |
420 |
|
|
421 |
+ |
/** |
422 |
+ |
* iterator ordering is FIFO |
423 |
+ |
*/ |
424 |
+ |
public void testIteratorOrdering() { |
425 |
+ |
final LinkedList<Item> q = new LinkedList<>(); |
426 |
+ |
q.add(one); |
427 |
+ |
q.add(two); |
428 |
+ |
q.add(three); |
429 |
|
int k = 0; |
430 |
< |
for (Iterator it = q.iterator(); it.hasNext();) { |
431 |
< |
int i = ((Integer)(it.next())).intValue(); |
290 |
< |
assertEquals("items should come out in order", ++k, i); |
430 |
> |
for (Iterator<? extends Item> it = q.iterator(); it.hasNext();) { |
431 |
> |
mustEqual(++k, it.next()); |
432 |
|
} |
433 |
|
|
434 |
< |
assertEquals("should go through 3 elements", 3, k); |
434 |
> |
mustEqual(3, k); |
435 |
> |
} |
436 |
> |
|
437 |
> |
/** |
438 |
> |
* iterator.remove removes current element |
439 |
> |
*/ |
440 |
> |
public void testIteratorRemove() { |
441 |
> |
final LinkedList<Item> q = new LinkedList<>(); |
442 |
> |
q.add(one); |
443 |
> |
q.add(two); |
444 |
> |
q.add(three); |
445 |
> |
Iterator<? extends Item> it = q.iterator(); |
446 |
> |
mustEqual(1, it.next()); |
447 |
> |
it.remove(); |
448 |
> |
it = q.iterator(); |
449 |
> |
mustEqual(2, it.next()); |
450 |
> |
mustEqual(3, it.next()); |
451 |
> |
assertFalse(it.hasNext()); |
452 |
> |
} |
453 |
> |
|
454 |
> |
/** |
455 |
> |
* Descending iterator iterates through all elements |
456 |
> |
*/ |
457 |
> |
public void testDescendingIterator() { |
458 |
> |
LinkedList<Item> q = populatedQueue(SIZE); |
459 |
> |
int i = 0; |
460 |
> |
Iterator<? extends Item> it = q.descendingIterator(); |
461 |
> |
while (it.hasNext()) { |
462 |
> |
mustContain(q, it.next()); |
463 |
> |
++i; |
464 |
> |
} |
465 |
> |
mustEqual(i, SIZE); |
466 |
> |
assertFalse(it.hasNext()); |
467 |
> |
try { |
468 |
> |
it.next(); |
469 |
> |
shouldThrow(); |
470 |
> |
} catch (NoSuchElementException success) {} |
471 |
|
} |
472 |
|
|
473 |
< |
public void testIteratorRemove () { |
474 |
< |
final LinkedList q = new LinkedList(); |
473 |
> |
/** |
474 |
> |
* Descending iterator ordering is reverse FIFO |
475 |
> |
*/ |
476 |
> |
public void testDescendingIteratorOrdering() { |
477 |
> |
final LinkedList<Item> q = new LinkedList<>(); |
478 |
> |
q.add(three); |
479 |
> |
q.add(two); |
480 |
> |
q.add(one); |
481 |
> |
int k = 0; |
482 |
> |
for (Iterator<? extends Item> it = q.descendingIterator(); it.hasNext();) { |
483 |
> |
mustEqual(++k, it.next()); |
484 |
> |
} |
485 |
|
|
486 |
< |
q.add(new Integer(1)); |
487 |
< |
q.add(new Integer(2)); |
301 |
< |
q.add(new Integer(3)); |
486 |
> |
mustEqual(3, k); |
487 |
> |
} |
488 |
|
|
489 |
< |
Iterator it = q.iterator(); |
489 |
> |
/** |
490 |
> |
* descendingIterator.remove removes current element |
491 |
> |
*/ |
492 |
> |
public void testDescendingIteratorRemove() { |
493 |
> |
final LinkedList<Item> q = new LinkedList<>(); |
494 |
> |
q.add(three); |
495 |
> |
q.add(two); |
496 |
> |
q.add(one); |
497 |
> |
Iterator<? extends Item> it = q.descendingIterator(); |
498 |
|
it.next(); |
499 |
|
it.remove(); |
500 |
< |
|
501 |
< |
it = q.iterator(); |
502 |
< |
assertEquals(it.next(), new Integer(2)); |
309 |
< |
assertEquals(it.next(), new Integer(3)); |
500 |
> |
it = q.descendingIterator(); |
501 |
> |
assertSame(it.next(), two); |
502 |
> |
assertSame(it.next(), three); |
503 |
|
assertFalse(it.hasNext()); |
504 |
|
} |
505 |
|
|
506 |
< |
|
507 |
< |
public void testToString(){ |
508 |
< |
LinkedList q = fullQueue(N); |
506 |
> |
/** |
507 |
> |
* toString contains toStrings of elements |
508 |
> |
*/ |
509 |
> |
public void testToString() { |
510 |
> |
LinkedList<Item> q = populatedQueue(SIZE); |
511 |
|
String s = q.toString(); |
512 |
< |
for (int i = 0; i < N; ++i) { |
513 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
512 |
> |
for (int i = 0; i < SIZE; ++i) { |
513 |
> |
assertTrue(s.contains(String.valueOf(i))); |
514 |
|
} |
515 |
< |
} |
515 |
> |
} |
516 |
|
|
517 |
< |
public void testAddFirst(){ |
518 |
< |
LinkedList q = fullQueue(3); |
519 |
< |
q.addFirst(new Integer(4)); |
520 |
< |
assertEquals(new Integer(4),q.get(0)); |
521 |
< |
} |
522 |
< |
|
523 |
< |
public void testAddLast(){ |
329 |
< |
LinkedList q = fullQueue(3); |
330 |
< |
q.addLast(new Integer(3)); |
331 |
< |
assertEquals(new Integer(3),q.get(3)); |
332 |
< |
} |
333 |
< |
|
334 |
< |
public void testGetFirst() { |
335 |
< |
LinkedList q = fullQueue(3); |
336 |
< |
assertEquals(new Integer(0),q.getFirst()); |
337 |
< |
} |
338 |
< |
|
339 |
< |
public void testGetLast() { |
340 |
< |
LinkedList q = fullQueue(3); |
341 |
< |
assertEquals(new Integer(2),q.getLast()); |
342 |
< |
} |
343 |
< |
|
344 |
< |
public void testIndexOf(){ |
345 |
< |
LinkedList q = fullQueue(3); |
346 |
< |
assertEquals(0,q.indexOf(new Integer(0))); |
347 |
< |
assertEquals(1,q.indexOf(new Integer(1))); |
348 |
< |
assertEquals(2,q.indexOf(new Integer(2))); |
349 |
< |
assertEquals(-1, q.indexOf("not there")); |
350 |
< |
} |
351 |
< |
|
352 |
< |
public void testLastIndexOf(){ |
353 |
< |
LinkedList q = fullQueue(3); |
354 |
< |
q.add(new Integer(2)); |
355 |
< |
assertEquals(3,q.lastIndexOf(new Integer(2))); |
356 |
< |
assertEquals(-1, q.lastIndexOf("not there")); |
357 |
< |
} |
358 |
< |
|
359 |
< |
public void testSet(){ |
360 |
< |
LinkedList q = fullQueue(3); |
361 |
< |
q.set(0,(new Integer(1))); |
362 |
< |
assertFalse(q.contains(new Integer(0))); |
363 |
< |
assertEquals(new Integer(1), q.get(0)); |
364 |
< |
} |
365 |
< |
|
366 |
< |
|
367 |
< |
public void testGetFirst_NoSuchElementException(){ |
368 |
< |
try { |
369 |
< |
LinkedList l = new LinkedList(); |
370 |
< |
l.getFirst(); |
371 |
< |
fail("First Element"); |
372 |
< |
} |
373 |
< |
catch(NoSuchElementException success) {} |
374 |
< |
} |
375 |
< |
|
376 |
< |
public void testRemoveFirst() { |
377 |
< |
try { |
378 |
< |
LinkedList l = new LinkedList(); |
379 |
< |
l.removeFirst(); |
380 |
< |
fail("R: First Element"); |
381 |
< |
} |
382 |
< |
catch(NoSuchElementException success) {} |
383 |
< |
} |
384 |
< |
|
385 |
< |
public void testRemoveLast() { |
386 |
< |
try { |
387 |
< |
LinkedList l = new LinkedList(); |
388 |
< |
l.removeLast(); |
389 |
< |
fail("R: Last Element"); |
390 |
< |
} |
391 |
< |
catch(NoSuchElementException success) {} |
392 |
< |
} |
393 |
< |
|
394 |
< |
public void testGetLast_NoSuchElementException(){ |
395 |
< |
try { |
396 |
< |
LinkedList l = new LinkedList(); |
397 |
< |
l.getLast(); |
398 |
< |
fail("Last Element"); |
399 |
< |
} |
400 |
< |
catch(NoSuchElementException success) {} |
401 |
< |
} |
402 |
< |
|
403 |
< |
|
404 |
< |
public void testAddAll_NullPointerException(){ |
405 |
< |
try { |
406 |
< |
LinkedList l = new LinkedList(); |
407 |
< |
l.addAll((Collection)null); |
408 |
< |
fail("Add All Failed"); |
409 |
< |
} |
410 |
< |
catch(NullPointerException success){} |
411 |
< |
} |
412 |
< |
|
413 |
< |
|
414 |
< |
public void testAddAll1_OutOfBounds() { |
415 |
< |
try { |
416 |
< |
LinkedList l = new LinkedList(); |
417 |
< |
l.addAll(4,new LinkedList()); |
418 |
< |
fail("boolean addAll(int, Collection) should throw IndexOutOfBoundsException"); |
419 |
< |
} |
420 |
< |
catch(IndexOutOfBoundsException success) {} |
517 |
> |
/** |
518 |
> |
* peek returns element inserted with addFirst |
519 |
> |
*/ |
520 |
> |
public void testAddFirst() { |
521 |
> |
LinkedList<Item> q = populatedQueue(3); |
522 |
> |
q.addFirst(four); |
523 |
> |
assertSame(four, q.peek()); |
524 |
|
} |
525 |
< |
|
526 |
< |
|
527 |
< |
public void testAddAll2_IndexOutOfBoundsException() { |
528 |
< |
try { |
529 |
< |
LinkedList l = new LinkedList(); |
530 |
< |
l.add(new Object()); |
531 |
< |
LinkedList m = new LinkedList(); |
532 |
< |
m.add(new Object()); |
430 |
< |
l.addAll(4,m); |
431 |
< |
fail("Add All Failed " + l.size()); |
432 |
< |
}catch(IndexOutOfBoundsException success) {} |
525 |
> |
|
526 |
> |
/** |
527 |
> |
* peekFirst returns element inserted with push |
528 |
> |
*/ |
529 |
> |
public void testPush() { |
530 |
> |
LinkedList<Item> q = populatedQueue(3); |
531 |
> |
q.push(four); |
532 |
> |
assertSame(four, q.peekFirst()); |
533 |
|
} |
534 |
|
|
535 |
< |
public void testAddAll4_BadIndex() { |
536 |
< |
try { |
537 |
< |
LinkedList l = new LinkedList(); |
538 |
< |
l.add(new Object()); |
539 |
< |
LinkedList m = new LinkedList(); |
540 |
< |
m.add(new Object()); |
541 |
< |
l.addAll(-1,m); |
542 |
< |
fail("Add All Failed " + l.size()); |
543 |
< |
}catch(IndexOutOfBoundsException success){} |
544 |
< |
} |
545 |
< |
|
546 |
< |
public void testget1() { |
447 |
< |
try { |
448 |
< |
LinkedList l = new LinkedList(); |
449 |
< |
l.add(new Object()); |
450 |
< |
l.get(-1); |
451 |
< |
fail("get Failed - l.get(-1)"); |
452 |
< |
}catch(IndexOutOfBoundsException success) {} |
453 |
< |
} |
454 |
< |
|
455 |
< |
public void testget2() { |
456 |
< |
try { |
457 |
< |
LinkedList l = new LinkedList(); |
458 |
< |
l.add(new Object()); |
459 |
< |
l.get(5); |
460 |
< |
fail("get Failed - l.get(5) l.size(): " + l.size()); |
461 |
< |
}catch(IndexOutOfBoundsException success){} |
462 |
< |
} |
463 |
< |
|
464 |
< |
public void testset1() { |
465 |
< |
try { |
466 |
< |
LinkedList l = new LinkedList(); |
467 |
< |
l.add(new Object()); |
468 |
< |
l.set(-1,new Object()); |
469 |
< |
fail("set failed - l.set(-1,...)" + l.size()); |
470 |
< |
}catch(IndexOutOfBoundsException success){} |
471 |
< |
} |
472 |
< |
|
473 |
< |
public void testset2() { |
474 |
< |
try { |
475 |
< |
LinkedList l = new LinkedList(); |
476 |
< |
l.add(new Object()); |
477 |
< |
l.set(5,new Object()); |
478 |
< |
fail("set failed = l.set(5,..) l.size():" + l.size()); |
479 |
< |
}catch(IndexOutOfBoundsException success){} |
480 |
< |
} |
481 |
< |
|
482 |
< |
public void testadd1() { |
483 |
< |
try { |
484 |
< |
LinkedList l = new LinkedList(); |
485 |
< |
l.add(new Object()); |
486 |
< |
l.add(-1,new Object()); |
487 |
< |
fail("Add Failed - l.add(-1) l.size(): " + l.size()); |
488 |
< |
}catch(IndexOutOfBoundsException success){} |
489 |
< |
} |
490 |
< |
|
491 |
< |
public void add2(){ |
492 |
< |
try { |
493 |
< |
LinkedList l = new LinkedList(); |
494 |
< |
l.add(new Object()); |
495 |
< |
l.add(5,new Object()); |
496 |
< |
fail("Add Failed l.add(f,...)"); |
497 |
< |
}catch(IndexOutOfBoundsException success) {} |
498 |
< |
} |
499 |
< |
|
500 |
< |
public void testremove(){ |
501 |
< |
try { |
502 |
< |
LinkedList l = new LinkedList(); |
503 |
< |
l.add(new Object()); |
504 |
< |
l.remove(-1); |
505 |
< |
fail("Remove Failed l.remove(-1); l.size():" + l.size()); |
506 |
< |
}catch(IndexOutOfBoundsException success){} |
507 |
< |
} |
508 |
< |
|
509 |
< |
public void testremove1(){ |
510 |
< |
try { |
511 |
< |
LinkedList l = new LinkedList(); |
512 |
< |
l.add(new Object()); |
513 |
< |
l.remove(5); |
514 |
< |
fail("Remove Failed l.remove(5); l.size():" + l.size()); |
515 |
< |
}catch(IndexOutOfBoundsException success){} |
516 |
< |
} |
517 |
< |
|
518 |
< |
|
519 |
< |
public void testremove2(){ |
520 |
< |
try{ |
521 |
< |
LinkedList l = new LinkedList(); |
522 |
< |
l.remove(); |
523 |
< |
fail("LinkedList - Object remove() should throw a NoSuchElementException"); |
524 |
< |
}catch(NoSuchElementException e){} |
525 |
< |
} |
526 |
< |
|
527 |
< |
public void testlistIt1() { |
528 |
< |
try { |
529 |
< |
LinkedList l = new LinkedList(); |
530 |
< |
l.add(new Object()); |
531 |
< |
l.listIterator(5); |
532 |
< |
fail("l.listIterator(5) l.size():" + l.size()); |
533 |
< |
}catch(IndexOutOfBoundsException success){} |
534 |
< |
} |
535 |
< |
|
536 |
< |
public void testlistIt2() { |
537 |
< |
try { |
538 |
< |
LinkedList l = new LinkedList(); |
539 |
< |
l.add(new Object()); |
540 |
< |
l.listIterator(-1); |
541 |
< |
fail("l.listIterator(-1) l.size():" + l.size()); |
542 |
< |
}catch(IndexOutOfBoundsException success){} |
543 |
< |
} |
544 |
< |
|
545 |
< |
public void testlistIt3() { |
546 |
< |
try { |
547 |
< |
LinkedList l = new LinkedList(); |
548 |
< |
l.add(new Object()); |
549 |
< |
ListIterator a = l.listIterator(0); |
550 |
< |
l.removeFirst(); |
551 |
< |
a.next(); |
552 |
< |
fail("l.listIterator(-1) l.size():" + l.size()); |
553 |
< |
}catch(ConcurrentModificationException success){} |
554 |
< |
} |
555 |
< |
|
556 |
< |
public void testToArray_BadArg() { |
557 |
< |
try { |
558 |
< |
LinkedList l = new LinkedList(); |
559 |
< |
l.add(new Object()); |
560 |
< |
Object o[] = l.toArray(null); |
561 |
< |
fail("l.toArray(null) did not throw an exception"); |
562 |
< |
}catch(NullPointerException success){} |
535 |
> |
/** |
536 |
> |
* pop removes next element, or throws NSEE if empty |
537 |
> |
*/ |
538 |
> |
public void testPop() { |
539 |
> |
LinkedList<Item> q = populatedQueue(SIZE); |
540 |
> |
for (int i = 0; i < SIZE; ++i) { |
541 |
> |
mustEqual(i, q.pop()); |
542 |
> |
} |
543 |
> |
try { |
544 |
> |
q.pop(); |
545 |
> |
shouldThrow(); |
546 |
> |
} catch (NoSuchElementException success) {} |
547 |
|
} |
548 |
|
|
549 |
< |
public void testToArray1_BadArg() { |
550 |
< |
try { |
551 |
< |
LinkedList l = new LinkedList(); |
552 |
< |
l.add(new Integer(5)); |
553 |
< |
Object o[] = l.toArray(new String[10] ); |
554 |
< |
fail("l.toArray(String[] f) did not throw an exception, an Integer was added"); |
555 |
< |
}catch(ArrayStoreException success){} |
549 |
> |
/** |
550 |
> |
* OfferFirst succeeds |
551 |
> |
*/ |
552 |
> |
public void testOfferFirst() { |
553 |
> |
LinkedList<Item> q = new LinkedList<>(); |
554 |
> |
assertTrue(q.offerFirst(zero)); |
555 |
> |
assertTrue(q.offerFirst(one)); |
556 |
> |
} |
557 |
> |
|
558 |
> |
/** |
559 |
> |
* OfferLast succeeds |
560 |
> |
*/ |
561 |
> |
public void testOfferLast() { |
562 |
> |
LinkedList<Item> q = new LinkedList<>(); |
563 |
> |
assertTrue(q.offerLast(zero)); |
564 |
> |
assertTrue(q.offerLast(one)); |
565 |
> |
} |
566 |
> |
|
567 |
> |
/** |
568 |
> |
* pollLast succeeds unless empty |
569 |
> |
*/ |
570 |
> |
public void testPollLast() { |
571 |
> |
LinkedList<Item> q = populatedQueue(SIZE); |
572 |
> |
for (int i = SIZE - 1; i >= 0; --i) { |
573 |
> |
mustEqual(i, q.pollLast()); |
574 |
> |
} |
575 |
> |
assertNull(q.pollLast()); |
576 |
> |
} |
577 |
> |
|
578 |
> |
/** |
579 |
> |
* peekFirst returns next element, or null if empty |
580 |
> |
*/ |
581 |
> |
public void testPeekFirst() { |
582 |
> |
LinkedList<Item> q = populatedQueue(SIZE); |
583 |
> |
for (int i = 0; i < SIZE; ++i) { |
584 |
> |
mustEqual(i, q.peekFirst()); |
585 |
> |
mustEqual(i, q.pollFirst()); |
586 |
> |
assertTrue(q.peekFirst() == null || |
587 |
> |
!q.peekFirst().equals(i)); |
588 |
> |
} |
589 |
> |
assertNull(q.peekFirst()); |
590 |
> |
} |
591 |
> |
|
592 |
> |
/** |
593 |
> |
* peekLast returns next element, or null if empty |
594 |
> |
*/ |
595 |
> |
public void testPeekLast() { |
596 |
> |
LinkedList<Item> q = populatedQueue(SIZE); |
597 |
> |
for (int i = SIZE - 1; i >= 0; --i) { |
598 |
> |
mustEqual(i, q.peekLast()); |
599 |
> |
mustEqual(i, q.pollLast()); |
600 |
> |
assertTrue(q.peekLast() == null || |
601 |
> |
!q.peekLast().equals(i)); |
602 |
> |
} |
603 |
> |
assertNull(q.peekLast()); |
604 |
> |
} |
605 |
> |
|
606 |
> |
public void testFirstElement() { |
607 |
> |
LinkedList<Item> q = populatedQueue(SIZE); |
608 |
> |
for (int i = 0; i < SIZE; ++i) { |
609 |
> |
mustEqual(i, q.getFirst()); |
610 |
> |
mustEqual(i, q.pollFirst()); |
611 |
> |
} |
612 |
> |
try { |
613 |
> |
q.getFirst(); |
614 |
> |
shouldThrow(); |
615 |
> |
} catch (NoSuchElementException success) {} |
616 |
> |
} |
617 |
> |
|
618 |
> |
/** |
619 |
> |
* getLast returns next element, or throws NSEE if empty |
620 |
> |
*/ |
621 |
> |
public void testLastElement() { |
622 |
> |
LinkedList<Item> q = populatedQueue(SIZE); |
623 |
> |
for (int i = SIZE - 1; i >= 0; --i) { |
624 |
> |
mustEqual(i, q.getLast()); |
625 |
> |
mustEqual(i, q.pollLast()); |
626 |
> |
} |
627 |
> |
try { |
628 |
> |
q.getLast(); |
629 |
> |
shouldThrow(); |
630 |
> |
} catch (NoSuchElementException success) {} |
631 |
> |
assertNull(q.peekLast()); |
632 |
> |
} |
633 |
> |
|
634 |
> |
/** |
635 |
> |
* removeFirstOccurrence(x) removes x and returns true if present |
636 |
> |
*/ |
637 |
> |
public void testRemoveFirstOccurrence() { |
638 |
> |
LinkedList<Item> q = populatedQueue(SIZE); |
639 |
> |
for (int i = 1; i < SIZE; i += 2) { |
640 |
> |
assertTrue(q.removeFirstOccurrence(itemFor(i))); |
641 |
> |
} |
642 |
> |
for (int i = 0; i < SIZE; i += 2) { |
643 |
> |
assertTrue(q.removeFirstOccurrence(itemFor(i))); |
644 |
> |
assertFalse(q.removeFirstOccurrence(itemFor(i + 1))); |
645 |
> |
} |
646 |
> |
assertTrue(q.isEmpty()); |
647 |
> |
} |
648 |
> |
|
649 |
> |
/** |
650 |
> |
* removeLastOccurrence(x) removes x and returns true if present |
651 |
> |
*/ |
652 |
> |
public void testRemoveLastOccurrence() { |
653 |
> |
LinkedList<Item> q = populatedQueue(SIZE); |
654 |
> |
for (int i = 1; i < SIZE; i += 2) { |
655 |
> |
assertTrue(q.removeLastOccurrence(itemFor(i))); |
656 |
> |
} |
657 |
> |
for (int i = 0; i < SIZE; i += 2) { |
658 |
> |
assertTrue(q.removeLastOccurrence(itemFor(i))); |
659 |
> |
assertFalse(q.removeLastOccurrence(itemFor(i + 1))); |
660 |
> |
} |
661 |
> |
assertTrue(q.isEmpty()); |
662 |
|
} |
663 |
|
|
664 |
|
} |