9 |
|
import java.util.*; |
10 |
|
import java.util.concurrent.*; |
11 |
|
|
12 |
< |
public class LinkedListTest extends TestCase { |
13 |
< |
|
14 |
< |
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; |
18 |
< |
|
12 |
> |
public class LinkedListTest extends JSR166TestCase { |
13 |
|
public static void main(String[] args) { |
14 |
|
junit.textui.TestRunner.run (suite()); |
15 |
|
} |
22 |
|
* Create a queue of given size containing consecutive |
23 |
|
* Integers 0 ... n. |
24 |
|
*/ |
25 |
< |
private LinkedList fullQueue(int n) { |
25 |
> |
private LinkedList populatedQueue(int n) { |
26 |
|
LinkedList q = new LinkedList(); |
27 |
|
assertTrue(q.isEmpty()); |
28 |
|
for(int i = 0; i < n; ++i) |
46 |
|
|
47 |
|
public void testConstructor6(){ |
48 |
|
try { |
49 |
< |
Integer[] ints = new Integer[N]; |
50 |
< |
for (int i = 0; i < N; ++i) |
49 |
> |
Integer[] ints = new Integer[SIZE]; |
50 |
> |
for (int i = 0; i < SIZE; ++i) |
51 |
|
ints[i] = new Integer(i); |
52 |
|
LinkedList q = new LinkedList(Arrays.asList(ints)); |
53 |
< |
for (int i = 0; i < N; ++i) |
53 |
> |
for (int i = 0; i < SIZE; ++i) |
54 |
|
assertEquals(ints[i], q.poll()); |
55 |
|
} |
56 |
|
finally {} |
68 |
|
} |
69 |
|
|
70 |
|
public void testSize() { |
71 |
< |
LinkedList q = fullQueue(N); |
72 |
< |
for (int i = 0; i < N; ++i) { |
73 |
< |
assertEquals(N-i, q.size()); |
71 |
> |
LinkedList q = populatedQueue(SIZE); |
72 |
> |
for (int i = 0; i < SIZE; ++i) { |
73 |
> |
assertEquals(SIZE-i, q.size()); |
74 |
|
q.remove(); |
75 |
|
} |
76 |
< |
for (int i = 0; i < N; ++i) { |
76 |
> |
for (int i = 0; i < SIZE; ++i) { |
77 |
|
assertEquals(i, q.size()); |
78 |
|
q.add(new Integer(i)); |
79 |
|
} |
96 |
|
|
97 |
|
public void testAdd(){ |
98 |
|
LinkedList q = new LinkedList(); |
99 |
< |
for (int i = 0; i < N; ++i) { |
99 |
> |
for (int i = 0; i < SIZE; ++i) { |
100 |
|
assertEquals(i, q.size()); |
101 |
|
assertTrue(q.add(new Integer(i))); |
102 |
|
} |
114 |
|
public void testAddAll5(){ |
115 |
|
try { |
116 |
|
Integer[] empty = new Integer[0]; |
117 |
< |
Integer[] ints = new Integer[N]; |
118 |
< |
for (int i = 0; i < N; ++i) |
117 |
> |
Integer[] ints = new Integer[SIZE]; |
118 |
> |
for (int i = 0; i < SIZE; ++i) |
119 |
|
ints[i] = new Integer(i); |
120 |
|
LinkedList q = new LinkedList(); |
121 |
|
assertFalse(q.addAll(Arrays.asList(empty))); |
122 |
|
assertTrue(q.addAll(Arrays.asList(ints))); |
123 |
< |
for (int i = 0; i < N; ++i) |
123 |
> |
for (int i = 0; i < SIZE; ++i) |
124 |
|
assertEquals(ints[i], q.poll()); |
125 |
|
} |
126 |
|
finally {} |
127 |
|
} |
128 |
|
|
129 |
|
public void testPoll(){ |
130 |
< |
LinkedList q = fullQueue(N); |
131 |
< |
for (int i = 0; i < N; ++i) { |
130 |
> |
LinkedList q = populatedQueue(SIZE); |
131 |
> |
for (int i = 0; i < SIZE; ++i) { |
132 |
|
assertEquals(i, ((Integer)q.poll()).intValue()); |
133 |
|
} |
134 |
|
assertNull(q.poll()); |
135 |
|
} |
136 |
|
|
137 |
|
public void testPeek(){ |
138 |
< |
LinkedList q = fullQueue(N); |
139 |
< |
for (int i = 0; i < N; ++i) { |
138 |
> |
LinkedList q = populatedQueue(SIZE); |
139 |
> |
for (int i = 0; i < SIZE; ++i) { |
140 |
|
assertEquals(i, ((Integer)q.peek()).intValue()); |
141 |
|
q.poll(); |
142 |
|
assertTrue(q.peek() == null || |
146 |
|
} |
147 |
|
|
148 |
|
public void testElement(){ |
149 |
< |
LinkedList q = fullQueue(N); |
150 |
< |
for (int i = 0; i < N; ++i) { |
149 |
> |
LinkedList q = populatedQueue(SIZE); |
150 |
> |
for (int i = 0; i < SIZE; ++i) { |
151 |
|
assertEquals(i, ((Integer)q.element()).intValue()); |
152 |
|
q.poll(); |
153 |
|
} |
159 |
|
} |
160 |
|
|
161 |
|
public void testRemove(){ |
162 |
< |
LinkedList q = fullQueue(N); |
163 |
< |
for (int i = 0; i < N; ++i) { |
162 |
> |
LinkedList q = populatedQueue(SIZE); |
163 |
> |
for (int i = 0; i < SIZE; ++i) { |
164 |
|
assertEquals(i, ((Integer)q.remove()).intValue()); |
165 |
|
} |
166 |
|
try { |
171 |
|
} |
172 |
|
|
173 |
|
public void testRemoveElement(){ |
174 |
< |
LinkedList q = fullQueue(N); |
175 |
< |
for (int i = 1; i < N; i+=2) { |
174 |
> |
LinkedList q = populatedQueue(SIZE); |
175 |
> |
for (int i = 1; i < SIZE; i+=2) { |
176 |
|
assertTrue(q.remove(new Integer(i))); |
177 |
|
} |
178 |
< |
for (int i = 0; i < N; i+=2) { |
178 |
> |
for (int i = 0; i < SIZE; i+=2) { |
179 |
|
assertTrue(q.remove(new Integer(i))); |
180 |
|
assertFalse(q.remove(new Integer(i+1))); |
181 |
|
} |
183 |
|
} |
184 |
|
|
185 |
|
public void testContains(){ |
186 |
< |
LinkedList q = fullQueue(N); |
187 |
< |
for (int i = 0; i < N; ++i) { |
186 |
> |
LinkedList q = populatedQueue(SIZE); |
187 |
> |
for (int i = 0; i < SIZE; ++i) { |
188 |
|
assertTrue(q.contains(new Integer(i))); |
189 |
|
q.poll(); |
190 |
|
assertFalse(q.contains(new Integer(i))); |
192 |
|
} |
193 |
|
|
194 |
|
public void testClear(){ |
195 |
< |
LinkedList q = fullQueue(N); |
195 |
> |
LinkedList q = populatedQueue(SIZE); |
196 |
|
q.clear(); |
197 |
|
assertTrue(q.isEmpty()); |
198 |
|
assertEquals(0, q.size()); |
203 |
|
} |
204 |
|
|
205 |
|
public void testContainsAll(){ |
206 |
< |
LinkedList q = fullQueue(N); |
206 |
> |
LinkedList q = populatedQueue(SIZE); |
207 |
|
LinkedList p = new LinkedList(); |
208 |
< |
for (int i = 0; i < N; ++i) { |
208 |
> |
for (int i = 0; i < SIZE; ++i) { |
209 |
|
assertTrue(q.containsAll(p)); |
210 |
|
assertFalse(p.containsAll(q)); |
211 |
|
p.add(new Integer(i)); |
214 |
|
} |
215 |
|
|
216 |
|
public void testRetainAll(){ |
217 |
< |
LinkedList q = fullQueue(N); |
218 |
< |
LinkedList p = fullQueue(N); |
219 |
< |
for (int i = 0; i < N; ++i) { |
217 |
> |
LinkedList q = populatedQueue(SIZE); |
218 |
> |
LinkedList p = populatedQueue(SIZE); |
219 |
> |
for (int i = 0; i < SIZE; ++i) { |
220 |
|
boolean changed = q.retainAll(p); |
221 |
|
if (i == 0) |
222 |
|
assertFalse(changed); |
224 |
|
assertTrue(changed); |
225 |
|
|
226 |
|
assertTrue(q.containsAll(p)); |
227 |
< |
assertEquals(N-i, q.size()); |
227 |
> |
assertEquals(SIZE-i, q.size()); |
228 |
|
p.remove(); |
229 |
|
} |
230 |
|
} |
231 |
|
|
232 |
|
public void testRemoveAll(){ |
233 |
< |
for (int i = 1; i < N; ++i) { |
234 |
< |
LinkedList q = fullQueue(N); |
235 |
< |
LinkedList p = fullQueue(i); |
233 |
> |
for (int i = 1; i < SIZE; ++i) { |
234 |
> |
LinkedList q = populatedQueue(SIZE); |
235 |
> |
LinkedList p = populatedQueue(i); |
236 |
|
assertTrue(q.removeAll(p)); |
237 |
< |
assertEquals(N-i, q.size()); |
237 |
> |
assertEquals(SIZE-i, q.size()); |
238 |
|
for (int j = 0; j < i; ++j) { |
239 |
|
Integer I = (Integer)(p.remove()); |
240 |
|
assertFalse(q.contains(I)); |
243 |
|
} |
244 |
|
|
245 |
|
public void testToArray(){ |
246 |
< |
LinkedList q = fullQueue(N); |
246 |
> |
LinkedList q = populatedQueue(SIZE); |
247 |
|
Object[] o = q.toArray(); |
248 |
|
Arrays.sort(o); |
249 |
|
for(int i = 0; i < o.length; i++) |
251 |
|
} |
252 |
|
|
253 |
|
public void testToArray2(){ |
254 |
< |
LinkedList q = fullQueue(N); |
255 |
< |
Integer[] ints = new Integer[N]; |
254 |
> |
LinkedList q = populatedQueue(SIZE); |
255 |
> |
Integer[] ints = new Integer[SIZE]; |
256 |
|
ints = (Integer[])q.toArray(ints); |
257 |
|
Arrays.sort(ints); |
258 |
|
for(int i = 0; i < ints.length; i++) |
260 |
|
} |
261 |
|
|
262 |
|
public void testIterator(){ |
263 |
< |
LinkedList q = fullQueue(N); |
263 |
> |
LinkedList q = populatedQueue(SIZE); |
264 |
|
int i = 0; |
265 |
|
Iterator it = q.iterator(); |
266 |
|
while(it.hasNext()) { |
267 |
|
assertTrue(q.contains(it.next())); |
268 |
|
++i; |
269 |
|
} |
270 |
< |
assertEquals(i, N); |
270 |
> |
assertEquals(i, SIZE); |
271 |
|
} |
272 |
|
|
273 |
|
public void testIteratorOrdering() { |
306 |
|
|
307 |
|
|
308 |
|
public void testToString(){ |
309 |
< |
LinkedList q = fullQueue(N); |
309 |
> |
LinkedList q = populatedQueue(SIZE); |
310 |
|
String s = q.toString(); |
311 |
< |
for (int i = 0; i < N; ++i) { |
311 |
> |
for (int i = 0; i < SIZE; ++i) { |
312 |
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
313 |
|
} |
314 |
|
} |
315 |
|
|
316 |
|
public void testAddFirst(){ |
317 |
< |
LinkedList q = fullQueue(3); |
317 |
> |
LinkedList q = populatedQueue(3); |
318 |
|
q.addFirst(new Integer(4)); |
319 |
|
assertEquals(new Integer(4),q.get(0)); |
320 |
|
} |
321 |
|
|
322 |
|
public void testAddLast(){ |
323 |
< |
LinkedList q = fullQueue(3); |
323 |
> |
LinkedList q = populatedQueue(3); |
324 |
|
q.addLast(new Integer(3)); |
325 |
|
assertEquals(new Integer(3),q.get(3)); |
326 |
|
} |
327 |
|
|
328 |
|
public void testGetFirst() { |
329 |
< |
LinkedList q = fullQueue(3); |
329 |
> |
LinkedList q = populatedQueue(3); |
330 |
|
assertEquals(new Integer(0),q.getFirst()); |
331 |
|
} |
332 |
|
|
333 |
|
public void testGetLast() { |
334 |
< |
LinkedList q = fullQueue(3); |
334 |
> |
LinkedList q = populatedQueue(3); |
335 |
|
assertEquals(new Integer(2),q.getLast()); |
336 |
|
} |
337 |
|
|
338 |
|
public void testIndexOf(){ |
339 |
< |
LinkedList q = fullQueue(3); |
339 |
> |
LinkedList q = populatedQueue(3); |
340 |
|
assertEquals(0,q.indexOf(new Integer(0))); |
341 |
|
assertEquals(1,q.indexOf(new Integer(1))); |
342 |
|
assertEquals(2,q.indexOf(new Integer(2))); |
344 |
|
} |
345 |
|
|
346 |
|
public void testLastIndexOf(){ |
347 |
< |
LinkedList q = fullQueue(3); |
347 |
> |
LinkedList q = populatedQueue(3); |
348 |
|
q.add(new Integer(2)); |
349 |
|
assertEquals(3,q.lastIndexOf(new Integer(2))); |
350 |
|
assertEquals(-1, q.lastIndexOf("not there")); |
351 |
|
} |
352 |
|
|
353 |
|
public void testSet(){ |
354 |
< |
LinkedList q = fullQueue(3); |
354 |
> |
LinkedList q = populatedQueue(3); |
355 |
|
q.set(0,(new Integer(1))); |
356 |
|
assertFalse(q.contains(new Integer(0))); |
357 |
|
assertEquals(new Integer(1), q.get(0)); |