ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/extra166y/ParallelArrayAsListTest.java
Revision: 1.8
Committed: Tue Oct 25 20:29:12 2011 UTC (12 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.7: +1 -1 lines
Log Message:
whitespace

File Contents

# User Rev Content
1 dl 1.1 /*
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 jsr166 1.7 * http://creativecommons.org/publicdomain/zero/1.0/
5 jsr166 1.2 * Other contributors include Andrew Wright, Jeffrey Hayes,
6     * Pat Fisher, Mike Judd.
7 dl 1.1 */
8    
9     import junit.framework.*;
10     import java.util.*;
11     import java.util.concurrent.*;
12     import jsr166y.*;
13     import extra166y.*;
14     import java.io.*;
15    
16 jsr166 1.8 public class ParallelArrayAsListTest extends JSR166TestCase {
17 jsr166 1.2
18 dl 1.1 public static void main(String[] args) {
19 jsr166 1.5 junit.textui.TestRunner.run(suite());
20 dl 1.1 }
21    
22     public static Test suite() {
23 jsr166 1.4 return new TestSuite(ParallelArrayAsListTest.class);
24 dl 1.1 }
25    
26 jsr166 1.6 static List populatedArray(int n) {
27 jsr166 1.4 List a = ParallelArray.createEmpty(n, Object.class, ParallelArray.defaultExecutor()).asList();
28 dl 1.1 assertTrue(a.isEmpty());
29 jsr166 1.2 for (int i = 0; i < n; ++i)
30 dl 1.1 a.add(new Integer(i));
31     assertFalse(a.isEmpty());
32     assertEquals(n, a.size());
33     return a;
34     }
35    
36    
37 jsr166 1.6 static List emptyArray() {
38 jsr166 1.4 List a = ParallelArray.createEmpty(1, Object.class, ParallelArray.defaultExecutor()).asList();
39 dl 1.1 return a;
40     }
41    
42    
43     /**
44     * a new list is empty
45     */
46     public void testConstructor() {
47 jsr166 1.4 List a = ParallelArray.createEmpty(1, Object.class, ParallelArray.defaultExecutor()).asList();
48 dl 1.1 assertTrue(a.isEmpty());
49     }
50    
51     /**
52     * new list contains all elements of initializing array
53     */
54     public void testConstructor2() {
55     Integer[] ints = new Integer[SIZE];
56     for (int i = 0; i < SIZE-1; ++i)
57     ints[i] = new Integer(i);
58 jsr166 1.4 List a = ParallelArray.createUsingHandoff(ints, ParallelArray.defaultExecutor()).asList();
59 jsr166 1.2 for (int i = 0; i < SIZE; ++i)
60 dl 1.1 assertEquals(ints[i], a.get(i));
61     }
62 jsr166 1.2
63 dl 1.1
64     /**
65 jsr166 1.6 * addAll adds each element from the given collection
66 dl 1.1 */
67     public void testAddAll() {
68 jsr166 1.4 List full = populatedArray(3);
69     Vector v = new Vector();
70     v.add(three);
71     v.add(four);
72     v.add(five);
73     full.addAll(v);
74     assertEquals(6, full.size());
75 dl 1.1 }
76    
77     /**
78 jsr166 1.6 * clear removes all elements from the list
79 dl 1.1 */
80     public void testClear() {
81 jsr166 1.4 List full = populatedArray(SIZE);
82     full.clear();
83     assertEquals(0, full.size());
84 dl 1.1 }
85    
86    
87    
88     /**
89 jsr166 1.6 * contains is true for added elements
90 dl 1.1 */
91     public void testContains() {
92 jsr166 1.4 List full = populatedArray(3);
93     assertTrue(full.contains(one));
94     assertFalse(full.contains(five));
95 dl 1.1 }
96    
97     /**
98     * adding at an index places it in the indicated index
99     */
100     public void testAddIndex() {
101 jsr166 1.4 List full = populatedArray(3);
102 dl 1.1 full.add(0, m1);
103     assertEquals(4, full.size());
104     assertEquals(m1, full.get(0));
105     assertEquals(zero, full.get(1));
106    
107     full.add(2, m2);
108     assertEquals(5, full.size());
109     assertEquals(m2, full.get(2));
110     assertEquals(two, full.get(4));
111     }
112    
113     /**
114     * lists with same elements are equal and have same hashCode
115     */
116     public void testEquals() {
117 jsr166 1.4 List a = populatedArray(3);
118     List b = populatedArray(3);
119 dl 1.1 assertTrue(a.equals(b));
120     assertTrue(b.equals(a));
121     assertEquals(a.hashCode(), b.hashCode());
122     a.add(m1);
123     assertFalse(a.equals(b));
124     assertFalse(b.equals(a));
125     b.add(m1);
126     assertTrue(a.equals(b));
127     assertTrue(b.equals(a));
128     assertEquals(a.hashCode(), b.hashCode());
129     }
130    
131 jsr166 1.2
132 dl 1.1 /**
133 jsr166 1.6 * containsAll returns true for collection with subset of elements
134 dl 1.1 */
135     public void testContainsAll() {
136 jsr166 1.4 List full = populatedArray(3);
137     Vector v = new Vector();
138     v.add(one);
139     v.add(two);
140     assertTrue(full.containsAll(v));
141     v.add(six);
142     assertFalse(full.containsAll(v));
143 dl 1.1 }
144    
145     /**
146 jsr166 1.6 * get returns the value at the given index
147 dl 1.1 */
148     public void testGet() {
149 jsr166 1.4 List full = populatedArray(3);
150     assertEquals(0, ((Integer)full.get(0)).intValue());
151 dl 1.1 }
152    
153     /**
154 jsr166 1.6 * indexOf gives the index for the given object
155 dl 1.1 */
156     public void testIndexOf() {
157 jsr166 1.4 List full = populatedArray(3);
158     assertEquals(1, full.indexOf(one));
159     assertEquals(-1, full.indexOf("puppies"));
160 dl 1.1 }
161    
162     /**
163 jsr166 1.6 * isEmpty returns true when empty, else false
164 dl 1.1 */
165     public void testIsEmpty() {
166 jsr166 1.4 List empty = emptyArray();
167     List full = populatedArray(SIZE);
168     assertTrue(empty.isEmpty());
169     assertFalse(full.isEmpty());
170 dl 1.1 }
171    
172     /**
173 jsr166 1.6 * iterator() returns an iterator containing the elements of the list
174 dl 1.1 */
175     public void testIterator() {
176 jsr166 1.4 List full = populatedArray(SIZE);
177     Iterator i = full.iterator();
178     int j;
179     for (j = 0; i.hasNext(); j++)
180     assertEquals(j, ((Integer)i.next()).intValue());
181     assertEquals(SIZE, j);
182 dl 1.1 }
183    
184     /**
185     * iterator.remove removes element
186     */
187 jsr166 1.5 public void testIteratorRemove() {
188 jsr166 1.4 List full = populatedArray(SIZE);
189 dl 1.1 Iterator it = full.iterator();
190     Object first = full.get(0);
191     it.next();
192     it.remove();
193     assertFalse(full.contains(first));
194     }
195    
196     /**
197     * toString contains toString of elements
198     */
199     public void testToString() {
200 jsr166 1.4 List full = populatedArray(3);
201 dl 1.1 String s = full.toString();
202     for (int i = 0; i < 3; ++i) {
203     assertTrue(s.indexOf(String.valueOf(i)) >= 0);
204     }
205 jsr166 1.2 }
206 dl 1.1
207     /**
208 jsr166 1.6 * lastIndexOf returns the index for the given object
209 dl 1.1 */
210     public void testLastIndexOf1() {
211 jsr166 1.4 List full = populatedArray(3);
212     full.add(one);
213     full.add(three);
214     assertEquals(3, full.lastIndexOf(one));
215     assertEquals(-1, full.lastIndexOf(six));
216 dl 1.1 }
217    
218     /**
219 jsr166 1.6 * listIterator traverses all elements
220 dl 1.1 */
221     public void testListIterator1() {
222 jsr166 1.4 List full = populatedArray(SIZE);
223     ListIterator i = full.listIterator();
224     int j;
225     for (j = 0; i.hasNext(); j++)
226     assertEquals(j, ((Integer)i.next()).intValue());
227     assertEquals(SIZE, j);
228 dl 1.1 }
229    
230     /**
231 jsr166 1.6 * listIterator only returns those elements after the given index
232 dl 1.1 */
233     public void testListIterator2() {
234 jsr166 1.4 List full = populatedArray(3);
235     ListIterator i = full.listIterator(1);
236     int j;
237     for (j = 0; i.hasNext(); j++)
238     assertEquals(j+1, ((Integer)i.next()).intValue());
239     assertEquals(2, j);
240 dl 1.1 }
241    
242     /**
243 jsr166 1.6 * remove removes and returns the object at the given index
244 dl 1.1 */
245     public void testRemove() {
246 jsr166 1.4 List full = populatedArray(3);
247     assertEquals(two, full.remove(2));
248     assertEquals(2, full.size());
249 dl 1.1 }
250    
251     /**
252 jsr166 1.6 * removeAll removes all elements from the given collection
253 dl 1.1 */
254     public void testRemoveAll() {
255 jsr166 1.4 List full = populatedArray(3);
256     Vector v = new Vector();
257     v.add(one);
258     v.add(two);
259     full.removeAll(v);
260     assertEquals(1, full.size());
261 dl 1.1 }
262    
263     /**
264 jsr166 1.6 * set changes the element at the given index
265 dl 1.1 */
266     public void testSet() {
267 jsr166 1.4 List full = populatedArray(3);
268     assertEquals(two, full.set(2, four));
269     assertEquals(4, ((Integer)full.get(2)).intValue());
270 dl 1.1 }
271    
272     /**
273 jsr166 1.6 * size returns the number of elements
274 dl 1.1 */
275     public void testSize() {
276 jsr166 1.4 List empty = emptyArray();
277     List full = populatedArray(SIZE);
278     assertEquals(SIZE, full.size());
279     assertEquals(0, empty.size());
280 dl 1.1 }
281    
282     /**
283 jsr166 1.6 * toArray returns an Object array containing all elements from the list
284 dl 1.1 */
285     public void testToArray() {
286 jsr166 1.4 List full = populatedArray(3);
287     Object[] o = full.toArray();
288     assertEquals(3, o.length);
289     assertEquals(0, ((Integer)o[0]).intValue());
290     assertEquals(1, ((Integer)o[1]).intValue());
291     assertEquals(2, ((Integer)o[2]).intValue());
292 dl 1.1 }
293    
294     /**
295 jsr166 1.6 * toArray returns an Integer array containing all elements from
296     * the list
297 dl 1.1 */
298     public void testToArray2() {
299 jsr166 1.4 List full = populatedArray(3);
300     Integer[] i = new Integer[3];
301     i = (Integer[])full.toArray(i);
302     assertEquals(3, i.length);
303     assertEquals(0, i[0].intValue());
304     assertEquals(1, i[1].intValue());
305     assertEquals(2, i[2].intValue());
306 dl 1.1 }
307    
308    
309     /**
310     * sublists contains elements at indexes offset from their base
311     */
312     public void testSubList() {
313 jsr166 1.4 List a = populatedArray(10);
314 dl 1.1 assertTrue(a.subList(1,1).isEmpty());
315 jsr166 1.4 for (int j = 0; j < 9; ++j) {
316     for (int i = j ; i < 10; ++i) {
317     List b = a.subList(j,i);
318     for (int k = j; k < i; ++k) {
319     assertEquals(new Integer(k), b.get(k-j));
320     }
321     }
322     }
323 dl 1.1
324 jsr166 1.4 List s = a.subList(2, 5);
325 dl 1.1 assertEquals(s.size(), 3);
326     s.set(2, m1);
327     assertEquals(a.get(4), m1);
328 jsr166 1.4 s.clear();
329 dl 1.1 assertEquals(a.size(), 7);
330     }
331    
332     // Exception tests
333    
334     /**
335 jsr166 1.6 * toArray throws an ArrayStoreException when the given array
336     * can not store the objects inside the list
337 dl 1.1 */
338     public void testToArray_ArrayStoreException() {
339     try {
340     List c = emptyArray();
341     c.add("zfasdfsdf");
342     c.add("asdadasd");
343     c.toArray(new Long[5]);
344 jsr166 1.4 shouldThrow();
345 jsr166 1.6 } catch (ArrayStoreException e) {}
346 dl 1.1 }
347    
348     /**
349 jsr166 1.6 * get throws an IndexOutOfBoundsException on a negative index
350 dl 1.1 */
351     public void testGet1_IndexOutOfBoundsException() {
352     try {
353     List c = emptyArray();
354     c.get(-1);
355     shouldThrow();
356 jsr166 1.6 } catch (IndexOutOfBoundsException e) {}
357 dl 1.1 }
358 jsr166 1.2
359 dl 1.1 /**
360 jsr166 1.6 * get throws an IndexOutOfBoundsException on a too high index
361 dl 1.1 */
362     public void testGet2_IndexOutOfBoundsException() {
363     try {
364     List c = emptyArray();
365     c.add("asdasd");
366     c.add("asdad");
367     c.get(100);
368     shouldThrow();
369 jsr166 1.6 } catch (IndexOutOfBoundsException e) {}
370 dl 1.1 }
371    
372     /**
373 jsr166 1.6 * set throws an IndexOutOfBoundsException on a negative index
374 dl 1.1 */
375     public void testSet1_IndexOutOfBoundsException() {
376     try {
377     List c = emptyArray();
378     c.set(-1,"qwerty");
379     shouldThrow();
380 jsr166 1.6 } catch (IndexOutOfBoundsException e) {}
381 dl 1.1 }
382 jsr166 1.2
383 dl 1.1 /**
384 jsr166 1.6 * set throws an IndexOutOfBoundsException on a too high index
385 dl 1.1 */
386     public void testSet2() {
387     try {
388     List c = emptyArray();
389     c.add("asdasd");
390     c.add("asdad");
391     c.set(100, "qwerty");
392     shouldThrow();
393 jsr166 1.6 } catch (IndexOutOfBoundsException e) {}
394 dl 1.1 }
395    
396     /**
397 jsr166 1.6 * add throws an IndexOutOfBoundsException on a negative index
398 dl 1.1 */
399     public void testAdd1_IndexOutOfBoundsException() {
400     try {
401     List c = emptyArray();
402     c.add(-1,"qwerty");
403     shouldThrow();
404 jsr166 1.6 } catch (IndexOutOfBoundsException e) {}
405 dl 1.1 }
406 jsr166 1.2
407 dl 1.1 /**
408 jsr166 1.6 * add throws an IndexOutOfBoundsException on a too high index
409 dl 1.1 */
410     public void testAdd2_IndexOutOfBoundsException() {
411     try {
412     List c = emptyArray();
413     c.add("asdasd");
414     c.add("asdasdasd");
415     c.add(100, "qwerty");
416     shouldThrow();
417 jsr166 1.6 } catch (IndexOutOfBoundsException e) {}
418 dl 1.1 }
419    
420     /**
421 jsr166 1.6 * remove throws an IndexOutOfBoundsException on a negative index
422 dl 1.1 */
423     public void testRemove1_IndexOutOfBounds() {
424     try {
425     List c = emptyArray();
426     c.remove(-1);
427     shouldThrow();
428 jsr166 1.6 } catch (IndexOutOfBoundsException e) {}
429 dl 1.1 }
430    
431     /**
432 jsr166 1.6 * remove throws an IndexOutOfBoundsException on a too high index
433 dl 1.1 */
434     public void testRemove2_IndexOutOfBounds() {
435     try {
436     List c = emptyArray();
437     c.add("asdasd");
438     c.add("adasdasd");
439     c.remove(100);
440     shouldThrow();
441 jsr166 1.6 } catch (IndexOutOfBoundsException e) {}
442 dl 1.1 }
443 jsr166 1.2
444 dl 1.1 /**
445 jsr166 1.6 * addAll throws an IndexOutOfBoundsException on a negative index
446 dl 1.1 */
447     public void testAddAll1_IndexOutOfBoundsException() {
448     try {
449     List c = emptyArray();
450     c.addAll(-1,new LinkedList());
451     shouldThrow();
452 jsr166 1.6 } catch (IndexOutOfBoundsException e) {}
453 dl 1.1 }
454 jsr166 1.2
455 dl 1.1 /**
456 jsr166 1.6 * addAll throws an IndexOutOfBoundsException on a too high index
457 dl 1.1 */
458     public void testAddAll2_IndexOutOfBoundsException() {
459     try {
460     List c = emptyArray();
461     c.add("asdasd");
462     c.add("asdasdasd");
463     c.addAll(100, new LinkedList());
464     shouldThrow();
465 jsr166 1.6 } catch (IndexOutOfBoundsException e) {}
466 dl 1.1 }
467    
468     /**
469 jsr166 1.6 * listIterator throws an IndexOutOfBoundsException on a negative index
470 dl 1.1 */
471     public void testListIterator1_IndexOutOfBoundsException() {
472     try {
473     List c = emptyArray();
474     c.listIterator(-1);
475     shouldThrow();
476 jsr166 1.6 } catch (IndexOutOfBoundsException e) {}
477 dl 1.1 }
478    
479     /**
480 jsr166 1.6 * listIterator throws an IndexOutOfBoundsException on a too high index
481 dl 1.1 */
482     public void testListIterator2_IndexOutOfBoundsException() {
483     try {
484     List c = emptyArray();
485     c.add("adasd");
486     c.add("asdasdas");
487     c.listIterator(100);
488     shouldThrow();
489 jsr166 1.6 } catch (IndexOutOfBoundsException e) {}
490 dl 1.1 }
491    
492     /**
493 jsr166 1.6 * subList throws an IndexOutOfBoundsException on a negative index
494 dl 1.1 */
495     public void testSubList1_IndexOutOfBoundsException() {
496     try {
497     List c = emptyArray();
498     c.subList(-1,100);
499    
500     shouldThrow();
501 jsr166 1.6 } catch (IndexOutOfBoundsException e) {}
502 dl 1.1 }
503    
504     /**
505 jsr166 1.6 * subList throws an IndexOutOfBoundsException on a too high index
506 dl 1.1 */
507     public void testSubList2_IndexOutOfBoundsException() {
508     try {
509     List c = emptyArray();
510     c.add("asdasd");
511     c.subList(1,100);
512     shouldThrow();
513 jsr166 1.6 } catch (IndexOutOfBoundsException e) {}
514 dl 1.1 }
515    
516     /**
517 jsr166 1.6 * subList throws IndexOutOfBoundsException when the second index
518     * is lower then the first
519 dl 1.1 */
520     public void testSubList3_IndexOutOfBoundsException() {
521     try {
522     List c = emptyArray();
523     c.subList(3,1);
524    
525     shouldThrow();
526 jsr166 1.6 } catch (IndexOutOfBoundsException e) {}
527 dl 1.1 }
528    
529 jsr166 1.2
530 dl 1.1 }