ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CopyOnWriteArrayListTest.java
(Generate patch)

Comparing jsr166/src/test/tck/CopyOnWriteArrayListTest.java (file contents):
Revision 1.2 by dl, Sun Sep 7 20:39:11 2003 UTC vs.
Revision 1.3 by dl, Sun Sep 14 20:42:40 2003 UTC

# Line 10 | Line 10 | import java.util.*;
10   import java.util.concurrent.*;
11   import java.io.*;
12  
13 < public class CopyOnWriteArrayListTest extends TestCase{
13 > public class CopyOnWriteArrayListTest extends JSR166TestCase{
14      
15      public static void main(String[] args) {
16          junit.textui.TestRunner.run (suite());  
# Line 20 | Line 20 | public class CopyOnWriteArrayListTest ex
20          return new TestSuite(CopyOnWriteArrayListTest.class);
21      }
22  
23 <    static CopyOnWriteArrayList fullArray(int n){
23 >    static CopyOnWriteArrayList populatedArray(int n){
24          CopyOnWriteArrayList a = new CopyOnWriteArrayList();
25          assertTrue(a.isEmpty());
26          for (int i = 0; i < n; ++i)
# Line 30 | Line 30 | public class CopyOnWriteArrayListTest ex
30          return a;
31      }
32  
33 +
34 +    public void testConstructor() {
35 +        CopyOnWriteArrayList a = new CopyOnWriteArrayList();
36 +        assertTrue(a.isEmpty());
37 +    }
38 +
39 +    public void testConstructor2() {
40 +        Integer[] ints = new Integer[SIZE];
41 +        for (int i = 0; i < SIZE-1; ++i)
42 +            ints[i] = new Integer(i);
43 +        CopyOnWriteArrayList a = new CopyOnWriteArrayList(ints);
44 +        for (int i = 0; i < SIZE; ++i)
45 +            assertEquals(ints[i], a.get(i));
46 +    }
47 +
48 +    public void testConstructor3() {
49 +        Integer[] ints = new Integer[SIZE];
50 +        for (int i = 0; i < SIZE-1; ++i)
51 +            ints[i] = new Integer(i);
52 +        CopyOnWriteArrayList a = new CopyOnWriteArrayList(Arrays.asList(ints));
53 +        for (int i = 0; i < SIZE; ++i)
54 +            assertEquals(ints[i], a.get(i));
55 +    }
56 +        
57 +
58      /**
59 <     *  Test to verify addAll correctly adds each element from the given collection
59 >     *   addAll correctly adds each element from the given collection
60       */
61      public void testAddAll(){
62 <        CopyOnWriteArrayList full = fullArray(3);
62 >        CopyOnWriteArrayList full = populatedArray(3);
63          Vector v = new Vector();
64 <        v.add(new Integer(3));
65 <        v.add(new Integer(4));
66 <        v.add(new Integer(5));
64 >        v.add(three);
65 >        v.add(four);
66 >        v.add(five);
67          full.addAll(v);
68          assertEquals(6, full.size());
69      }
70  
71      /**
72 <     *  Test to verify addAllAbsent adds each element from the given collection that did not
72 >     *   addAllAbsent adds each element from the given collection that did not
73       *  already exist in the List
74       */
75      public void testAddAllAbsent(){
76 <        CopyOnWriteArrayList full = fullArray(3);
76 >        CopyOnWriteArrayList full = populatedArray(3);
77          Vector v = new Vector();
78 <        v.add(new Integer(3));
79 <        v.add(new Integer(4));
80 <        v.add(new Integer(1)); // will not add this element
78 >        v.add(three);
79 >        v.add(four);
80 >        v.add(one); // will not add this element
81          full.addAllAbsent(v);
82          assertEquals(5, full.size());
83      }
84  
85      /**
86 <     *  Test to verify addIfAbsent will not add the element if it already exists in the list
86 >     *   addIfAbsent will not add the element if it already exists in the list
87       */
88      public void testAddIfAbsent(){
89 <        CopyOnWriteArrayList full = fullArray(3);
90 <        full.addIfAbsent(new Integer(1));
89 >        CopyOnWriteArrayList full = populatedArray(3);
90 >        full.addIfAbsent(one);
91          assertEquals(3, full.size());
92      }
93  
94      /**
95 <     *  test to verify addIfAbsent correctly adds the element when it does not exist in the list
95 >     *   addIfAbsent correctly adds the element when it does not exist in the list
96       */
97      public void testAddIfAbsent2(){
98 <        CopyOnWriteArrayList full = fullArray(3);
99 <        full.addIfAbsent(new Integer(3));
100 <        assertTrue(full.contains(new Integer(3)));
98 >        CopyOnWriteArrayList full = populatedArray(3);
99 >        full.addIfAbsent(three);
100 >        assertTrue(full.contains(three));
101      }
102  
103      /**
104 <     *  Test to verify clear correctly removes all elements from the list
104 >     *   clear correctly removes all elements from the list
105       */
106      public void testClear(){
107 <        CopyOnWriteArrayList full = fullArray(3);
107 >        CopyOnWriteArrayList full = populatedArray(3);
108          full.clear();
109          assertEquals(0, full.size());
110      }
111  
112      /**
113 <     *  Test to verify contains returns the correct values
113 >     *   contains returns the correct values
114       */
115      public void testContains(){
116 <        CopyOnWriteArrayList full = fullArray(3);
117 <        assertTrue(full.contains(new Integer(1)));
118 <        assertFalse(full.contains(new Integer(5)));
116 >        CopyOnWriteArrayList full = populatedArray(3);
117 >        assertTrue(full.contains(one));
118 >        assertFalse(full.contains(five));
119      }
120  
121      public void testAddIndex() {
122 <        CopyOnWriteArrayList full = fullArray(3);
123 <        full.add(0, new Integer(-1));
122 >        CopyOnWriteArrayList full = populatedArray(3);
123 >        full.add(0, m1);
124          assertEquals(4, full.size());
125 <        assertEquals(new Integer(-1), full.get(0));
126 <        assertEquals(new Integer(0), full.get(1));
125 >        assertEquals(m1, full.get(0));
126 >        assertEquals(zero, full.get(1));
127  
128 <        full.add(2, new Integer(-2));
128 >        full.add(2, m2);
129          assertEquals(5, full.size());
130 <        assertEquals(new Integer(-2), full.get(2));
131 <        assertEquals(new Integer(2), full.get(4));
130 >        assertEquals(m2, full.get(2));
131 >        assertEquals(two, full.get(4));
132      }
133  
134      public void testEquals() {
135 <        CopyOnWriteArrayList a = fullArray(3);
136 <        CopyOnWriteArrayList b = fullArray(3);
135 >        CopyOnWriteArrayList a = populatedArray(3);
136 >        CopyOnWriteArrayList b = populatedArray(3);
137          assertTrue(a.equals(b));
138          assertTrue(b.equals(a));
139          assertEquals(a.hashCode(), b.hashCode());
140 <        a.add(new Integer(-1));
140 >        a.add(m1);
141          assertFalse(a.equals(b));
142          assertFalse(b.equals(a));
143 <        b.add(new Integer(-1));
143 >        b.add(m1);
144          assertTrue(a.equals(b));
145          assertTrue(b.equals(a));
146          assertEquals(a.hashCode(), b.hashCode());
# Line 123 | Line 148 | public class CopyOnWriteArrayListTest ex
148  
149      
150      /**
151 <     *  Test to verify containsAll returns the correct values
151 >     *   containsAll returns the correct values
152       */
153      public void testContainsAll(){
154 <        CopyOnWriteArrayList full = fullArray(3);
154 >        CopyOnWriteArrayList full = populatedArray(3);
155          Vector v = new Vector();
156 <        v.add(new Integer(1));
157 <        v.add(new Integer(2));
156 >        v.add(one);
157 >        v.add(two);
158          assertTrue(full.containsAll(v));
159 <        v.add(new Integer(6));
159 >        v.add(six);
160          assertFalse(full.containsAll(v));
161      }
162  
163      /**
164 <     *  Test to verify get returns the correct value for the given index
164 >     *   get returns the correct value for the given index
165       */
166      public void testGet(){
167 <        CopyOnWriteArrayList full = fullArray(3);
167 >        CopyOnWriteArrayList full = populatedArray(3);
168          assertEquals(0, ((Integer)full.get(0)).intValue());
169      }
170  
171      /**
172 <     *  Test to verify indexOf gives the correct index for the given object
172 >     *   indexOf gives the correct index for the given object
173       */
174      public void testIndexOf(){
175 <        CopyOnWriteArrayList full = fullArray(3);
176 <        assertEquals(1, full.indexOf(new Integer(1)));
175 >        CopyOnWriteArrayList full = populatedArray(3);
176 >        assertEquals(1, full.indexOf(one));
177          assertEquals(-1, full.indexOf("puppies"));
178      }
179  
180      /**
181 <     *  Test to verify indexOf gives the correct index based on the given index
181 >     *   indexOf gives the correct index based on the given index
182       *  at which to start searching
183       */
184      public void testIndexOf2(){
185 <        CopyOnWriteArrayList full = fullArray(3);
186 <        assertEquals(1, full.indexOf(new Integer(1), 0));
187 <        assertEquals(-1, full.indexOf(new Integer(1), 2));
185 >        CopyOnWriteArrayList full = populatedArray(3);
186 >        assertEquals(1, full.indexOf(one, 0));
187 >        assertEquals(-1, full.indexOf(one, 2));
188      }
189  
190      /**
191 <     *  Test to verify isEmpty returns the correct values
191 >     *   isEmpty returns the correct values
192       */
193      public void testIsEmpty(){
194          CopyOnWriteArrayList empty = new CopyOnWriteArrayList();
195 <        CopyOnWriteArrayList full = fullArray(3);
195 >        CopyOnWriteArrayList full = populatedArray(3);
196          assertTrue(empty.isEmpty());
197          assertFalse(full.isEmpty());
198      }
199  
200      /**
201 <     *  Test to verify iterator() returns an iterator containing the elements of the list
201 >     *   iterator() returns an iterator containing the elements of the list
202       */
203      public void testIterator(){
204 <        CopyOnWriteArrayList full = fullArray(3);
204 >        CopyOnWriteArrayList full = populatedArray(3);
205          Iterator i = full.iterator();
206          int j;
207          for(j = 0; i.hasNext(); j++)
# Line 185 | Line 210 | public class CopyOnWriteArrayListTest ex
210      }
211  
212      public void testIteratorRemove () {
213 <        CopyOnWriteArrayList full = fullArray(3);
213 >        CopyOnWriteArrayList full = populatedArray(3);
214          Iterator it = full.iterator();
215          it.next();
216          try {
# Line 196 | Line 221 | public class CopyOnWriteArrayListTest ex
221      }
222  
223      public void testToString(){
224 <        CopyOnWriteArrayList full = fullArray(3);
224 >        CopyOnWriteArrayList full = populatedArray(3);
225          String s = full.toString();
226          for (int i = 0; i < 3; ++i) {
227              assertTrue(s.indexOf(String.valueOf(i)) >= 0);
# Line 204 | Line 229 | public class CopyOnWriteArrayListTest ex
229      }        
230  
231      /**
232 <     *  Test to verify lastIndexOf returns the correct index for the given object
232 >     *   lastIndexOf returns the correct index for the given object
233       */
234      public void testLastIndexOf1(){
235 <        CopyOnWriteArrayList full = fullArray(3);
236 <        full.add(new Integer(1));
237 <        full.add(new Integer(3));
238 <        assertEquals(3, full.lastIndexOf(new Integer(1)));
239 <        assertEquals(-1, full.lastIndexOf(new Integer(6)));
235 >        CopyOnWriteArrayList full = populatedArray(3);
236 >        full.add(one);
237 >        full.add(three);
238 >        assertEquals(3, full.lastIndexOf(one));
239 >        assertEquals(-1, full.lastIndexOf(six));
240      }
241  
242      /**
243 <     *  Test to verify lastIndexOf returns the correct index from the given starting point
243 >     *   lastIndexOf returns the correct index from the given starting point
244       */
245      public void testlastIndexOf2(){
246 <        CopyOnWriteArrayList full = fullArray(3);
247 <        full.add(new Integer(1));
248 <        full.add(new Integer(3));
249 <        assertEquals(3, full.lastIndexOf(new Integer(1), 4));
250 <        assertEquals(-1, full.lastIndexOf(new Integer(3), 3));
246 >        CopyOnWriteArrayList full = populatedArray(3);
247 >        full.add(one);
248 >        full.add(three);
249 >        assertEquals(3, full.lastIndexOf(one, 4));
250 >        assertEquals(-1, full.lastIndexOf(three, 3));
251      }
252  
253      /**
254       *  Identical to testIterator, except ListInterator has more functionality
255       */
256      public void testListIterator1(){
257 <        CopyOnWriteArrayList full = fullArray(3);
257 >        CopyOnWriteArrayList full = populatedArray(3);
258          ListIterator i = full.listIterator();
259          int j;
260          for(j = 0; i.hasNext(); j++)
# Line 242 | Line 267 | public class CopyOnWriteArrayListTest ex
267       *  after the given index
268       */
269      public void testListIterator2(){
270 <        CopyOnWriteArrayList full = fullArray(3);
270 >        CopyOnWriteArrayList full = populatedArray(3);
271          ListIterator i = full.listIterator(1);
272          int j;
273          for(j = 0; i.hasNext(); j++)
# Line 251 | Line 276 | public class CopyOnWriteArrayListTest ex
276      }
277  
278      /**
279 <     *  Test to verify remove correctly removes and returns the object at the given index
279 >     *   remove correctly removes and returns the object at the given index
280       */
281      public void testRemove(){
282 <        CopyOnWriteArrayList full = fullArray(3);
283 <        assertEquals(new Integer(2), full.remove(2));
282 >        CopyOnWriteArrayList full = populatedArray(3);
283 >        assertEquals(two, full.remove(2));
284          assertEquals(2, full.size());
285      }
286  
287      /**
288 <     *  Test to verify removeAll correctly removes all elements from the given collection
288 >     *   removeAll correctly removes all elements from the given collection
289       */
290      public void testRemoveAll(){
291 <        CopyOnWriteArrayList full = fullArray(3);
291 >        CopyOnWriteArrayList full = populatedArray(3);
292          Vector v = new Vector();
293 <        v.add(new Integer(1));
294 <        v.add(new Integer(2));
293 >        v.add(one);
294 >        v.add(two);
295          full.removeAll(v);
296          assertEquals(1, full.size());
297      }
298  
299      /**
300 <     *  Test to verify set correctly changes the element at the given index
300 >     *   set correctly changes the element at the given index
301       */
302      public void testSet(){
303 <        CopyOnWriteArrayList full = fullArray(3);
304 <        assertEquals(new Integer(2), full.set(2, new Integer(4)));
303 >        CopyOnWriteArrayList full = populatedArray(3);
304 >        assertEquals(two, full.set(2, four));
305          assertEquals(4, ((Integer)full.get(2)).intValue());
306      }
307  
308      /**
309 <     *  Test to verify size returns the correct values
309 >     *   size returns the correct values
310       */
311      public void testSize(){
312          CopyOnWriteArrayList empty = new CopyOnWriteArrayList();
313 <        CopyOnWriteArrayList full = fullArray(3);
313 >        CopyOnWriteArrayList full = populatedArray(3);
314          assertEquals(3, full.size());
315          assertEquals(0, empty.size());
316      }
317  
318      /**
319 <     *  Test to verify toArray returns an Object array containing all elements from the list
319 >     *   toArray returns an Object array containing all elements from the list
320       */
321      public void testToArray(){
322 <        CopyOnWriteArrayList full = fullArray(3);
322 >        CopyOnWriteArrayList full = populatedArray(3);
323          Object[] o = full.toArray();
324          assertEquals(3, o.length);
325          assertEquals(0, ((Integer)o[0]).intValue());
# Line 306 | Line 331 | public class CopyOnWriteArrayListTest ex
331       *  test to verify toArray returns an Integer array containing all elements from the list
332       */
333      public void testToArray2(){
334 <        CopyOnWriteArrayList full = fullArray(3);
334 >        CopyOnWriteArrayList full = populatedArray(3);
335          Integer[] i = new Integer[3];
336          i = (Integer[])full.toArray(i);
337          assertEquals(3, i.length);
# Line 317 | Line 342 | public class CopyOnWriteArrayListTest ex
342  
343  
344      public void testSubList() {
345 <        CopyOnWriteArrayList a = fullArray(10);
345 >        CopyOnWriteArrayList a = populatedArray(10);
346          assertTrue(a.subList(1,1).isEmpty());
347          for(int j = 0; j < 9; ++j) {
348              for(int i = j ; i < 10; ++i) {
# Line 328 | Line 353 | public class CopyOnWriteArrayListTest ex
353              }
354          }
355  
331        Integer m1 = new Integer(-1);
356          List s = a.subList(2, 5);
357          assertEquals(s.size(), 3);
358 <        s.set(2, new Integer(m1));
358 >        s.set(2, m1);
359          assertEquals(a.get(4), m1);
360          s.clear();
361          assertEquals(a.size(), 7);
# Line 340 | Line 364 | public class CopyOnWriteArrayListTest ex
364      // Exception tests
365  
366      /**
367 <     *  Test to verify toArray throws an ArrayStoreException when the given array
367 >     *   toArray throws an ArrayStoreException when the given array
368       *  can not store the objects inside the list
369       */
370      public void testToArray_ArrayStoreException(){
# Line 354 | Line 378 | public class CopyOnWriteArrayListTest ex
378      }
379  
380      /**
381 <     *  Test to verify get throws an IndexOutOfBoundsException on a negative index
381 >     *   get throws an IndexOutOfBoundsException on a negative index
382       */
383      public void testGet1_IndexOutOfBoundsException(){
384          try{
# Line 365 | Line 389 | public class CopyOnWriteArrayListTest ex
389      }
390      
391      /**
392 <     *  Test to verify get throws an IndexOutOfBoundsException on a too high index
392 >     *   get throws an IndexOutOfBoundsException on a too high index
393       */
394      public void testGet2_IndexOutOfBoundsException(){
395          try{
# Line 378 | Line 402 | public class CopyOnWriteArrayListTest ex
402      }
403  
404      /**
405 <     *  Test to verify set throws an IndexOutOfBoundsException on a negative index
405 >     *   set throws an IndexOutOfBoundsException on a negative index
406       */
407      public void testSet1_IndexOutOfBoundsException(){
408          try{
# Line 389 | Line 413 | public class CopyOnWriteArrayListTest ex
413      }
414      
415      /**
416 <     *  Test to verify set throws an IndexOutOfBoundsException on a too high index
416 >     *   set throws an IndexOutOfBoundsException on a too high index
417       */
418      public void testSet2(){
419          try{
# Line 402 | Line 426 | public class CopyOnWriteArrayListTest ex
426      }
427  
428      /**
429 <     *  Test to verify add throws an IndexOutOfBoundsException on a negative index
429 >     *   add throws an IndexOutOfBoundsException on a negative index
430       */
431      public void testAdd1_IndexOutOfBoundsException(){
432          try{
# Line 413 | Line 437 | public class CopyOnWriteArrayListTest ex
437      }
438      
439      /**
440 <     *  Test to verify add throws an IndexOutOfBoundsException on a too high index
440 >     *   add throws an IndexOutOfBoundsException on a too high index
441       */
442      public void testAdd2_IndexOutOfBoundsException(){
443          try{
# Line 426 | Line 450 | public class CopyOnWriteArrayListTest ex
450      }
451  
452      /**
453 <     *  Test to verify remove throws an IndexOutOfBoundsException on a negative index
453 >     *   remove throws an IndexOutOfBoundsException on a negative index
454       */
455      public void testRemove1_IndexOutOfBounds(){
456          try{
# Line 437 | Line 461 | public class CopyOnWriteArrayListTest ex
461      }
462  
463      /**
464 <     *  Test to verify remove throws an IndexOutOfBoundsException on a too high index
464 >     *   remove throws an IndexOutOfBoundsException on a too high index
465       */
466      public void testRemove2_IndexOutOfBounds(){
467          try{
# Line 450 | Line 474 | public class CopyOnWriteArrayListTest ex
474      }
475      
476      /**
477 <     *  Test to verify addAll throws an IndexOutOfBoundsException on a negative index
477 >     *   addAll throws an IndexOutOfBoundsException on a negative index
478       */
479      public void testAddAll1_IndexOutOfBoundsException(){
480          try{
# Line 461 | Line 485 | public class CopyOnWriteArrayListTest ex
485      }
486      
487      /**
488 <     *  Test to verify addAll throws an IndexOutOfBoundsException on a too high index
488 >     *   addAll throws an IndexOutOfBoundsException on a too high index
489       */
490      public void testAddAll2_IndexOutOfBoundsException(){
491          try{
# Line 474 | Line 498 | public class CopyOnWriteArrayListTest ex
498      }
499  
500      /**
501 <     *  Test to verify listIterator throws an IndexOutOfBoundsException on a negative index
501 >     *   listIterator throws an IndexOutOfBoundsException on a negative index
502       */
503      public void testListIterator1_IndexOutOfBoundsException(){
504          try{
# Line 485 | Line 509 | public class CopyOnWriteArrayListTest ex
509      }
510  
511      /**
512 <     *  Test to verify listIterator throws an IndexOutOfBoundsException on a too high index
512 >     *   listIterator throws an IndexOutOfBoundsException on a too high index
513       */
514      public void testListIterator2_IndexOutOfBoundsException(){
515          try{
# Line 498 | Line 522 | public class CopyOnWriteArrayListTest ex
522      }
523  
524      /**
525 <     *  Test to verify subList throws an IndexOutOfBoundsException on a negative index
525 >     *   subList throws an IndexOutOfBoundsException on a negative index
526       */
527      public void testSubList1_IndexOutOfBoundsException(){
528          try{
# Line 510 | Line 534 | public class CopyOnWriteArrayListTest ex
534      }
535  
536      /**
537 <     *  Test to verify subList throws an IndexOutOfBoundsException on a too high index
537 >     *   subList throws an IndexOutOfBoundsException on a too high index
538       */
539      public void testSubList2_IndexOutOfBoundsException(){
540          try{
# Line 522 | Line 546 | public class CopyOnWriteArrayListTest ex
546      }
547  
548      /**
549 <     *  Test to verify subList throws IndexOutOfBoundsException when the second index
549 >     *   subList throws IndexOutOfBoundsException when the second index
550       *  is lower then the first
551       */
552      public void testSubList3_IndexOutOfBoundsException(){
# Line 535 | Line 559 | public class CopyOnWriteArrayListTest ex
559      }
560  
561      public void testSerialization() {
562 <        CopyOnWriteArrayList q = fullArray(10);
562 >        CopyOnWriteArrayList q = populatedArray(SIZE);
563  
564          try {
565              ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines