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

Comparing jsr166/src/test/tck/CopyOnWriteArraySetTest.java (file contents):
Revision 1.6 by dl, Sat Dec 27 19:26:43 2003 UTC vs.
Revision 1.20 by jsr166, Sat Nov 26 07:39:02 2011 UTC

# Line 1 | Line 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 < * http://creativecommons.org/licenses/publicdomain
5 < * Other contributors include Andrew Wright, Jeffrey Hayes,
6 < * Pat Fisher, Mike Judd.
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 < import java.io.*;
10 > import java.util.Arrays;
11 > import java.util.Iterator;
12 > import java.util.Set;
13 > import java.util.Vector;
14 > import java.util.concurrent.CopyOnWriteArraySet;
15  
16   public class CopyOnWriteArraySetTest extends JSR166TestCase {
17      public static void main(String[] args) {
18 <        junit.textui.TestRunner.run (suite());  
18 >        junit.textui.TestRunner.run(suite());
19      }
20      public static Test suite() {
21 <        return new TestSuite(CopyOnWriteArraySetTest.class);
21 >        return new TestSuite(CopyOnWriteArraySetTest.class);
22      }
23  
24 <    static CopyOnWriteArraySet populatedSet(int n){
25 <        CopyOnWriteArraySet a = new CopyOnWriteArraySet();
24 >    static CopyOnWriteArraySet populatedSet(int n) {
25 >        CopyOnWriteArraySet a = new CopyOnWriteArraySet();
26          assertTrue(a.isEmpty());
27 <        for (int i = 0; i < n; ++i)
27 >        for (int i = 0; i < n; ++i)
28              a.add(new Integer(i));
29          assertFalse(a.isEmpty());
30          assertEquals(n, a.size());
# Line 33 | Line 35 | public class CopyOnWriteArraySetTest ext
35       * Default-constructed set is empty
36       */
37      public void testConstructor() {
38 <        CopyOnWriteArraySet a = new CopyOnWriteArraySet();
38 >        CopyOnWriteArraySet a = new CopyOnWriteArraySet();
39          assertTrue(a.isEmpty());
40      }
41  
# Line 44 | Line 46 | public class CopyOnWriteArraySetTest ext
46          Integer[] ints = new Integer[SIZE];
47          for (int i = 0; i < SIZE-1; ++i)
48              ints[i] = new Integer(i);
49 <        CopyOnWriteArraySet a = new CopyOnWriteArraySet(Arrays.asList(ints));
50 <        for (int i = 0; i < SIZE; ++i)
49 >        CopyOnWriteArraySet a = new CopyOnWriteArraySet(Arrays.asList(ints));
50 >        for (int i = 0; i < SIZE; ++i)
51              assertTrue(a.contains(ints[i]));
52      }
51        
53  
54      /**
55 <     *   addAll  adds each element from the given collection
55 >     * addAll adds each element from the given collection
56       */
57      public void testAddAll() {
58 <        CopyOnWriteArraySet full = populatedSet(3);
59 <        Vector v = new Vector();
60 <        v.add(three);
61 <        v.add(four);
62 <        v.add(five);
63 <        full.addAll(v);
64 <        assertEquals(6, full.size());
58 >        CopyOnWriteArraySet full = populatedSet(3);
59 >        Vector v = new Vector();
60 >        v.add(three);
61 >        v.add(four);
62 >        v.add(five);
63 >        full.addAll(v);
64 >        assertEquals(6, full.size());
65      }
66  
67      /**
68 <     *   addAll adds each element from the given collection that did not
69 <     *  already exist in the set
68 >     * addAll adds each element from the given collection that did not
69 >     * already exist in the set
70       */
71      public void testAddAll2() {
72 <        CopyOnWriteArraySet full = populatedSet(3);
73 <        Vector v = new Vector();
74 <        v.add(three);
75 <        v.add(four);
76 <        v.add(one); // will not add this element
77 <        full.addAll(v);
78 <        assertEquals(5, full.size());
72 >        CopyOnWriteArraySet full = populatedSet(3);
73 >        Vector v = new Vector();
74 >        v.add(three);
75 >        v.add(four);
76 >        v.add(one); // will not add this element
77 >        full.addAll(v);
78 >        assertEquals(5, full.size());
79      }
80  
81      /**
82 <     *   add will not add the element if it already exists in the set
82 >     * add will not add the element if it already exists in the set
83       */
84      public void testAdd2() {
85 <        CopyOnWriteArraySet full = populatedSet(3);
86 <        full.add(one);
87 <        assertEquals(3, full.size());
85 >        CopyOnWriteArraySet full = populatedSet(3);
86 >        full.add(one);
87 >        assertEquals(3, full.size());
88      }
89  
90      /**
91 <     *   add  adds the element when it does not exist
91 <     *   in the set
91 >     * add adds the element when it does not exist in the set
92       */
93      public void testAdd3() {
94 <        CopyOnWriteArraySet full = populatedSet(3);
94 >        CopyOnWriteArraySet full = populatedSet(3);
95          full.add(three);
96          assertTrue(full.contains(three));
97      }
98  
99      /**
100 <     *   clear  removes all elements from the set
100 >     * clear removes all elements from the set
101       */
102      public void testClear() {
103 <        CopyOnWriteArraySet full = populatedSet(3);
104 <        full.clear();
105 <        assertEquals(0, full.size());
103 >        CopyOnWriteArraySet full = populatedSet(3);
104 >        full.clear();
105 >        assertEquals(0, full.size());
106      }
107  
108      /**
109 <     *   contains returns true for added elements
109 >     * contains returns true for added elements
110       */
111      public void testContains() {
112 <        CopyOnWriteArraySet full = populatedSet(3);
113 <        assertTrue(full.contains(one));
114 <        assertFalse(full.contains(five));
112 >        CopyOnWriteArraySet full = populatedSet(3);
113 >        assertTrue(full.contains(one));
114 >        assertFalse(full.contains(five));
115      }
116  
117      /**
118       * Sets with equal elements are equal
119       */
120      public void testEquals() {
121 <        CopyOnWriteArraySet a = populatedSet(3);
122 <        CopyOnWriteArraySet b = populatedSet(3);
121 >        CopyOnWriteArraySet a = populatedSet(3);
122 >        CopyOnWriteArraySet b = populatedSet(3);
123          assertTrue(a.equals(b));
124          assertTrue(b.equals(a));
125          assertEquals(a.hashCode(), b.hashCode());
# Line 132 | Line 132 | public class CopyOnWriteArraySetTest ext
132          assertEquals(a.hashCode(), b.hashCode());
133      }
134  
135    
135      /**
136 <     *   containsAll returns true for collections with subset of elements
136 >     * containsAll returns true for collections with subset of elements
137       */
138      public void testContainsAll() {
139 <        CopyOnWriteArraySet full = populatedSet(3);
140 <        Vector v = new Vector();
141 <        v.add(one);
142 <        v.add(two);
143 <        assertTrue(full.containsAll(v));
144 <        v.add(six);
145 <        assertFalse(full.containsAll(v));
139 >        CopyOnWriteArraySet full = populatedSet(3);
140 >        Vector v = new Vector();
141 >        v.add(one);
142 >        v.add(two);
143 >        assertTrue(full.containsAll(v));
144 >        v.add(six);
145 >        assertFalse(full.containsAll(v));
146      }
147  
148      /**
149 <     *   isEmpty is true when empty, else false
149 >     * isEmpty is true when empty, else false
150       */
151      public void testIsEmpty() {
152 <        CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
153 <        CopyOnWriteArraySet full = populatedSet(3);
154 <        assertTrue(empty.isEmpty());
155 <        assertFalse(full.isEmpty());
152 >        CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
153 >        CopyOnWriteArraySet full = populatedSet(3);
154 >        assertTrue(empty.isEmpty());
155 >        assertFalse(full.isEmpty());
156      }
157  
158      /**
159 <     *   iterator() returns an iterator containing the elements of the set
159 >     * iterator() returns an iterator containing the elements of the set
160       */
161      public void testIterator() {
162 <        CopyOnWriteArraySet full = populatedSet(3);
163 <        Iterator i = full.iterator();
164 <        int j;
165 <        for(j = 0; i.hasNext(); j++)
166 <            assertEquals(j, ((Integer)i.next()).intValue());
167 <        assertEquals(3, j);
162 >        CopyOnWriteArraySet full = populatedSet(3);
163 >        Iterator i = full.iterator();
164 >        int j;
165 >        for (j = 0; i.hasNext(); j++)
166 >            assertEquals(j, i.next());
167 >        assertEquals(3, j);
168      }
169  
170      /**
171       * iterator remove is unsupported
172       */
173 <    public void testIteratorRemove () {
174 <        CopyOnWriteArraySet full = populatedSet(3);
173 >    public void testIteratorRemove() {
174 >        CopyOnWriteArraySet full = populatedSet(3);
175          Iterator it = full.iterator();
176          it.next();
177          try {
178              it.remove();
179              shouldThrow();
180 <        }
182 <        catch (UnsupportedOperationException success) {}
180 >        } catch (UnsupportedOperationException success) {}
181      }
182  
183      /**
184       * toString holds toString of elements
185       */
186      public void testToString() {
187 <        CopyOnWriteArraySet full = populatedSet(3);
187 >        CopyOnWriteArraySet full = populatedSet(3);
188          String s = full.toString();
189          for (int i = 0; i < 3; ++i) {
190 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
190 >            assertTrue(s.contains(String.valueOf(i)));
191          }
192 <    }        
195 <
192 >    }
193  
194      /**
195 <     *   removeAll  removes all elements from the given collection
195 >     * removeAll removes all elements from the given collection
196       */
197      public void testRemoveAll() {
198 <        CopyOnWriteArraySet full = populatedSet(3);
199 <        Vector v = new Vector();
200 <        v.add(one);
201 <        v.add(two);
202 <        full.removeAll(v);
203 <        assertEquals(1, full.size());
198 >        CopyOnWriteArraySet full = populatedSet(3);
199 >        Vector v = new Vector();
200 >        v.add(one);
201 >        v.add(two);
202 >        full.removeAll(v);
203 >        assertEquals(1, full.size());
204      }
205  
209
206      /**
207       * remove removes an element
208       */
209      public void testRemove() {
210 <        CopyOnWriteArraySet full = populatedSet(3);
211 <        full.remove(one);
210 >        CopyOnWriteArraySet full = populatedSet(3);
211 >        full.remove(one);
212          assertFalse(full.contains(one));
213 <        assertEquals(2, full.size());
213 >        assertEquals(2, full.size());
214      }
215  
216      /**
217 <     *   size returns the number of elements
217 >     * size returns the number of elements
218       */
219      public void testSize() {
220 <        CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
221 <        CopyOnWriteArraySet full = populatedSet(3);
222 <        assertEquals(3, full.size());
223 <        assertEquals(0, empty.size());
220 >        CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
221 >        CopyOnWriteArraySet full = populatedSet(3);
222 >        assertEquals(3, full.size());
223 >        assertEquals(0, empty.size());
224      }
225  
226      /**
227 <     *   toArray returns an Object array containing all elements from the set
227 >     * toArray returns an Object array containing all elements from the set
228       */
229      public void testToArray() {
230 <        CopyOnWriteArraySet full = populatedSet(3);
231 <        Object[] o = full.toArray();
232 <        assertEquals(3, o.length);
233 <        assertEquals(0, ((Integer)o[0]).intValue());
234 <        assertEquals(1, ((Integer)o[1]).intValue());
235 <        assertEquals(2, ((Integer)o[2]).intValue());
230 >        CopyOnWriteArraySet<Integer> full = populatedSet(SIZE);
231 >        Object[] array = full.toArray();
232 >        Iterator<Integer> it = full.iterator();
233 >        for (int i = 0; i < SIZE; i++)
234 >            assertSame(array[i], it.next());
235 >        assertFalse(it.hasNext());
236      }
237  
238      /**
239 <     *   toArray returns an Integer array containing all elements from
240 <     *   the set
239 >     * toArray returns an Integer array containing all elements from
240 >     * the set
241       */
242      public void testToArray2() {
243 <        CopyOnWriteArraySet full = populatedSet(3);
244 <        Integer[] i = new Integer[3];
245 <        i = (Integer[])full.toArray(i);
246 <        assertEquals(3, i.length);
247 <        assertEquals(0, i[0].intValue());
248 <        assertEquals(1, i[1].intValue());
249 <        assertEquals(2, i[2].intValue());
243 >        CopyOnWriteArraySet<Integer> full = populatedSet(SIZE);
244 >        Integer[] ints = new Integer[SIZE];
245 >        assertSame(ints, full.toArray(ints));
246 >        Iterator<Integer> it = full.iterator();
247 >        for (int i = 0; i < SIZE; i++)
248 >            assertSame(ints[i], it.next());
249 >        assertFalse(it.hasNext());
250      }
251  
256
252      /**
253 <     *  toArray throws an ArrayStoreException when the given array can
254 <     *  not store the objects inside the set
253 >     * toArray throws an ArrayStoreException when the given array can
254 >     * not store the objects inside the set
255       */
256      public void testToArray_ArrayStoreException() {
257          try {
# Line 264 | Line 259 | public class CopyOnWriteArraySetTest ext
259              c.add("zfasdfsdf");
260              c.add("asdadasd");
261              c.toArray(new Long[5]);
262 <            shouldThrow();
263 <        } catch(ArrayStoreException e){}
262 >            shouldThrow();
263 >        } catch (ArrayStoreException success) {}
264      }
265  
266      /**
267 <     * A deserialized seriealized set is equal
267 >     * A deserialized serialized set is equal
268       */
269 <    public void testSerialization() {
270 <        CopyOnWriteArraySet q = populatedSet(SIZE);
269 >    public void testSerialization() throws Exception {
270 >        Set x = populatedSet(SIZE);
271 >        Set y = serialClone(x);
272  
273 <        try {
274 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
275 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
276 <            out.writeObject(q);
281 <            out.close();
282 <
283 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
284 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
285 <            CopyOnWriteArraySet r = (CopyOnWriteArraySet)in.readObject();
286 <            assertEquals(q.size(), r.size());
287 <            assertTrue(q.equals(r));
288 <            assertTrue(r.equals(q));
289 <        } catch(Exception e){
290 <            unexpectedException();
291 <        }
273 >        assertTrue(x != y);
274 >        assertEquals(x.size(), y.size());
275 >        assertEquals(x, y);
276 >        assertEquals(y, x);
277      }
278  
279   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines