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.3 by dl, Sun Sep 14 20:42:40 2003 UTC vs.
Revision 1.16 by jsr166, Sat Oct 9 19:30:35 2010 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines