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.1 by dl, Sun Aug 31 19:24:54 2003 UTC vs.
Revision 1.22 by jsr166, Tue Nov 29 05:23:56 2011 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/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.*;
10 > import java.util.Arrays;
11 > import java.util.Collection;
12 > import java.util.Collections;
13 > import java.util.Iterator;
14 > import java.util.Set;
15 > import java.util.Vector;
16 > import java.util.concurrent.CopyOnWriteArraySet;
17  
18 < public class CopyOnWriteArraySetTest extends TestCase{
13 <    
18 > public class CopyOnWriteArraySetTest extends JSR166TestCase {
19      public static void main(String[] args) {
20 <        junit.textui.TestRunner.run (suite());  
20 >        junit.textui.TestRunner.run(suite());
21      }
17
22      public static Test suite() {
23 <        return new TestSuite(CopyOnWriteArraySetTest.class);
23 >        return new TestSuite(CopyOnWriteArraySetTest.class);
24      }
25  
26 <    static CopyOnWriteArraySet fullSet(int n){
27 <        CopyOnWriteArraySet a = new CopyOnWriteArraySet();
26 >    static CopyOnWriteArraySet<Integer> populatedSet(int n) {
27 >        CopyOnWriteArraySet<Integer> a = new CopyOnWriteArraySet<Integer>();
28          assertTrue(a.isEmpty());
29 <        for (int i = 0; i < n; ++i)
30 <            a.add(new Integer(i));
29 >        for (int i = 0; i < n; i++)
30 >            a.add(i);
31          assertFalse(a.isEmpty());
32          assertEquals(n, a.size());
33          return a;
34      }
35  
36 +    static CopyOnWriteArraySet populatedSet(Integer[] elements) {
37 +        CopyOnWriteArraySet<Integer> a = new CopyOnWriteArraySet<Integer>();
38 +        assertTrue(a.isEmpty());
39 +        for (int i = 0; i < elements.length; i++)
40 +            a.add(elements[i]);
41 +        assertFalse(a.isEmpty());
42 +        assertEquals(elements.length, a.size());
43 +        return a;
44 +    }
45 +
46 +    /**
47 +     * Default-constructed set is empty
48 +     */
49 +    public void testConstructor() {
50 +        CopyOnWriteArraySet a = new CopyOnWriteArraySet();
51 +        assertTrue(a.isEmpty());
52 +    }
53 +
54 +    /**
55 +     * Collection-constructed set holds all of its elements
56 +     */
57 +    public void testConstructor3() {
58 +        Integer[] ints = new Integer[SIZE];
59 +        for (int i = 0; i < SIZE-1; ++i)
60 +            ints[i] = new Integer(i);
61 +        CopyOnWriteArraySet a = new CopyOnWriteArraySet(Arrays.asList(ints));
62 +        for (int i = 0; i < SIZE; ++i)
63 +            assertTrue(a.contains(ints[i]));
64 +    }
65 +
66      /**
67 <     *  Test to verify addAll correctly adds each element from the given collection
67 >     * addAll adds each element from the given collection
68       */
69 <    public void testAddAll(){
70 <        CopyOnWriteArraySet full = fullSet(3);
71 <        Vector v = new Vector();
72 <        v.add(new Integer(3));
73 <        v.add(new Integer(4));
74 <        v.add(new Integer(5));
75 <        full.addAll(v);
76 <        assertEquals(6, full.size());
69 >    public void testAddAll() {
70 >        CopyOnWriteArraySet full = populatedSet(3);
71 >        Vector v = new Vector();
72 >        v.add(three);
73 >        v.add(four);
74 >        v.add(five);
75 >        full.addAll(v);
76 >        assertEquals(6, full.size());
77      }
78  
79      /**
80 <     *  Test to verify addAllAbsent adds each element from the given collection that did not
81 <     *  already exist in the List
80 >     * addAll adds each element from the given collection that did not
81 >     * already exist in the set
82       */
83 <    public void testAddAll2(){
84 <        CopyOnWriteArraySet full = fullSet(3);
85 <        Vector v = new Vector();
86 <        v.add(new Integer(3));
87 <        v.add(new Integer(4));
88 <        v.add(new Integer(1)); // will not add this element
89 <        full.addAll(v);
90 <        assertEquals(5, full.size());
83 >    public void testAddAll2() {
84 >        CopyOnWriteArraySet full = populatedSet(3);
85 >        Vector v = new Vector();
86 >        v.add(three);
87 >        v.add(four);
88 >        v.add(one); // will not add this element
89 >        full.addAll(v);
90 >        assertEquals(5, full.size());
91      }
92  
93      /**
94 <     *  Test to verify addIfAbsent will not add the element if it already exists in the list
94 >     * add will not add the element if it already exists in the set
95       */
96 <    public void testAdd2(){
97 <        CopyOnWriteArraySet full = fullSet(3);
98 <        full.add(new Integer(1));
99 <        assertEquals(3, full.size());
96 >    public void testAdd2() {
97 >        CopyOnWriteArraySet full = populatedSet(3);
98 >        full.add(one);
99 >        assertEquals(3, full.size());
100      }
101  
102      /**
103 <     *  test to verify addIfAbsent correctly adds the element when it does not exist in the list
103 >     * add adds the element when it does not exist in the set
104       */
105 <    public void testAdd3(){
106 <        CopyOnWriteArraySet full = fullSet(3);
107 <        full.add(new Integer(3));
108 <        assertTrue(full.contains(new Integer(3)));
105 >    public void testAdd3() {
106 >        CopyOnWriteArraySet full = populatedSet(3);
107 >        full.add(three);
108 >        assertTrue(full.contains(three));
109      }
110  
111      /**
112 <     *  Test to verify clear correctly removes all elements from the list
112 >     * clear removes all elements from the set
113       */
114 <    public void testClear(){
115 <        CopyOnWriteArraySet full = fullSet(3);
116 <        full.clear();
117 <        assertEquals(0, full.size());
114 >    public void testClear() {
115 >        CopyOnWriteArraySet full = populatedSet(3);
116 >        full.clear();
117 >        assertEquals(0, full.size());
118      }
119  
120      /**
121 <     *  Test to verify contains returns the correct values
121 >     * contains returns true for added elements
122       */
123 <    public void testContains(){
124 <        CopyOnWriteArraySet full = fullSet(3);
125 <        assertTrue(full.contains(new Integer(1)));
126 <        assertFalse(full.contains(new Integer(5)));
123 >    public void testContains() {
124 >        CopyOnWriteArraySet full = populatedSet(3);
125 >        assertTrue(full.contains(one));
126 >        assertFalse(full.contains(five));
127      }
128  
129 +    /**
130 +     * Sets with equal elements are equal
131 +     */
132      public void testEquals() {
133 <        CopyOnWriteArraySet a = fullSet(3);
134 <        CopyOnWriteArraySet b = fullSet(3);
133 >        CopyOnWriteArraySet a = populatedSet(3);
134 >        CopyOnWriteArraySet b = populatedSet(3);
135          assertTrue(a.equals(b));
136          assertTrue(b.equals(a));
137          assertEquals(a.hashCode(), b.hashCode());
138 <        a.add(new Integer(-1));
138 >        a.add(m1);
139          assertFalse(a.equals(b));
140          assertFalse(b.equals(a));
141 <        b.add(new Integer(-1));
141 >        b.add(m1);
142          assertTrue(a.equals(b));
143          assertTrue(b.equals(a));
144          assertEquals(a.hashCode(), b.hashCode());
145      }
146  
110    
147      /**
148 <     *  Test to verify containsAll returns the correct values
148 >     * containsAll returns true for collections with subset of elements
149       */
150 <    public void testContainsAll(){
151 <        CopyOnWriteArraySet full = fullSet(3);
152 <        Vector v = new Vector();
153 <        v.add(new Integer(1));
154 <        v.add(new Integer(2));
155 <        assertTrue(full.containsAll(v));
156 <        v.add(new Integer(6));
157 <        assertFalse(full.containsAll(v));
150 >    public void testContainsAll() {
151 >        CopyOnWriteArraySet full = populatedSet(3);
152 >        Vector v = new Vector();
153 >        v.add(one);
154 >        v.add(two);
155 >        assertTrue(full.containsAll(v));
156 >        v.add(six);
157 >        assertFalse(full.containsAll(v));
158      }
159  
160      /**
161 <     *  Test to verify isEmpty returns the correct values
161 >     * isEmpty is true when empty, else false
162       */
163 <    public void testIsEmpty(){
164 <        CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
165 <        CopyOnWriteArraySet full = fullSet(3);
166 <        assertTrue(empty.isEmpty());
167 <        assertFalse(full.isEmpty());
163 >    public void testIsEmpty() {
164 >        CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
165 >        CopyOnWriteArraySet full = populatedSet(3);
166 >        assertTrue(empty.isEmpty());
167 >        assertFalse(full.isEmpty());
168      }
169  
170      /**
171 <     *  Test to verify iterator() returns an iterator containing the elements of the list
171 >     * iterator() returns an iterator containing the elements of the set
172       */
173 <    public void testIterator(){
174 <        CopyOnWriteArraySet full = fullSet(3);
175 <        Iterator i = full.iterator();
176 <        int j;
177 <        for(j = 0; i.hasNext(); j++)
178 <            assertEquals(j, ((Integer)i.next()).intValue());
179 <        assertEquals(3, j);
173 >    public void testIterator() {
174 >        CopyOnWriteArraySet full = populatedSet(3);
175 >        Iterator i = full.iterator();
176 >        int j;
177 >        for (j = 0; i.hasNext(); j++)
178 >            assertEquals(j, i.next());
179 >        assertEquals(3, j);
180      }
181  
182 <    public void testIteratorRemove () {
183 <        CopyOnWriteArraySet full = fullSet(3);
182 >    /**
183 >     * iterator remove is unsupported
184 >     */
185 >    public void testIteratorRemove() {
186 >        CopyOnWriteArraySet full = populatedSet(3);
187          Iterator it = full.iterator();
188          it.next();
189          try {
190              it.remove();
191 <            fail("should throw");
192 <        }
154 <        catch (UnsupportedOperationException success) {}
191 >            shouldThrow();
192 >        } catch (UnsupportedOperationException success) {}
193      }
194  
195 <    public void testToString(){
196 <        CopyOnWriteArraySet full = fullSet(3);
195 >    /**
196 >     * toString holds toString of elements
197 >     */
198 >    public void testToString() {
199 >        CopyOnWriteArraySet full = populatedSet(3);
200          String s = full.toString();
201          for (int i = 0; i < 3; ++i) {
202 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
202 >            assertTrue(s.contains(String.valueOf(i)));
203          }
204 <    }        
164 <
204 >    }
205  
206      /**
207 <     *  Test to verify removeAll correctly removes all elements from the given collection
207 >     * removeAll removes all elements from the given collection
208       */
209 <    public void testRemoveAll(){
210 <        CopyOnWriteArraySet full = fullSet(3);
211 <        Vector v = new Vector();
212 <        v.add(new Integer(1));
213 <        v.add(new Integer(2));
214 <        full.removeAll(v);
215 <        assertEquals(1, full.size());
209 >    public void testRemoveAll() {
210 >        CopyOnWriteArraySet full = populatedSet(3);
211 >        Vector v = new Vector();
212 >        v.add(one);
213 >        v.add(two);
214 >        full.removeAll(v);
215 >        assertEquals(1, full.size());
216      }
217  
218 <
219 <    public void testRemove(){
220 <        CopyOnWriteArraySet full = fullSet(3);
221 <        full.remove(new Integer(1));
222 <        assertFalse(full.contains(new Integer(1)));
223 <        assertEquals(2, full.size());
218 >    /**
219 >     * remove removes an element
220 >     */
221 >    public void testRemove() {
222 >        CopyOnWriteArraySet full = populatedSet(3);
223 >        full.remove(one);
224 >        assertFalse(full.contains(one));
225 >        assertEquals(2, full.size());
226      }
227  
228      /**
229 <     *  Test to verify size returns the correct values
229 >     * size returns the number of elements
230       */
231 <    public void testSize(){
232 <        CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
233 <        CopyOnWriteArraySet full = fullSet(3);
234 <        assertEquals(3, full.size());
235 <        assertEquals(0, empty.size());
231 >    public void testSize() {
232 >        CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
233 >        CopyOnWriteArraySet full = populatedSet(3);
234 >        assertEquals(3, full.size());
235 >        assertEquals(0, empty.size());
236      }
237  
238      /**
239 <     *  Test to verify toArray returns an Object array containing all elements from the list
239 >     * toArray() returns an Object array containing all elements from
240 >     * the set in insertion order
241       */
242 <    public void testToArray(){
243 <        CopyOnWriteArraySet full = fullSet(3);
244 <        Object[] o = full.toArray();
245 <        assertEquals(3, o.length);
246 <        assertEquals(0, ((Integer)o[0]).intValue());
247 <        assertEquals(1, ((Integer)o[1]).intValue());
248 <        assertEquals(2, ((Integer)o[2]).intValue());
242 >    public void testToArray() {
243 >        Object[] a = new CopyOnWriteArraySet().toArray();
244 >        assertTrue(Arrays.equals(new Object[0], a));
245 >        assertSame(Object[].class, a.getClass());
246 >
247 >        Integer[] elements = new Integer[SIZE];
248 >        for (int i = 0; i < SIZE; i++)
249 >            elements[i] = i;
250 >        Collections.shuffle(Arrays.asList(elements));
251 >        Collection<Integer> full = populatedSet(elements);
252 >
253 >        assertTrue(Arrays.equals(elements, full.toArray()));
254 >        assertSame(Object[].class, full.toArray().getClass());
255      }
256  
257      /**
258 <     *  test to verify toArray returns an Integer array containing all elements from the list
258 >     * toArray(Integer array) returns an Integer array containing all
259 >     * elements from the set in insertion order
260       */
261 <    public void testToArray2(){
262 <        CopyOnWriteArraySet full = fullSet(3);
263 <        Integer[] i = new Integer[3];
214 <        i = (Integer[])full.toArray(i);
215 <        assertEquals(3, i.length);
216 <        assertEquals(0, i[0].intValue());
217 <        assertEquals(1, i[1].intValue());
218 <        assertEquals(2, i[2].intValue());
219 <    }
261 >    public void testToArray2() {
262 >        Collection empty = new CopyOnWriteArraySet();
263 >        Integer[] a;
264  
265 +        a = new Integer[0];
266 +        assertSame(a, empty.toArray(a));
267  
268 +        a = new Integer[SIZE/2];
269 +        Arrays.fill(a, 42);
270 +        assertSame(a, empty.toArray(a));
271 +        assertNull(a[0]);
272 +        for (int i = 1; i < a.length; i++)
273 +            assertEquals(42, (int) a[i]);
274  
275 <    // Exception tests
275 >        Integer[] elements = new Integer[SIZE];
276 >        for (int i = 0; i < SIZE; i++)
277 >            elements[i] = i;
278 >        Collections.shuffle(Arrays.asList(elements));
279 >        Collection<Integer> full = populatedSet(elements);
280 >
281 >        Arrays.fill(a, 42);
282 >        assertTrue(Arrays.equals(elements, full.toArray(a)));
283 >        for (int i = 0; i < a.length; i++)
284 >            assertEquals(42, (int) a[i]);
285 >        assertSame(Integer[].class, full.toArray(a).getClass());
286 >
287 >        a = new Integer[SIZE];
288 >        Arrays.fill(a, 42);
289 >        assertSame(a, full.toArray(a));
290 >        assertTrue(Arrays.equals(elements, a));
291 >
292 >        a = new Integer[2*SIZE];
293 >        Arrays.fill(a, 42);
294 >        assertSame(a, full.toArray(a));
295 >        assertTrue(Arrays.equals(elements, Arrays.copyOf(a, SIZE)));
296 >        assertNull(a[SIZE]);
297 >        for (int i = SIZE + 1; i < a.length; i++)
298 >            assertEquals(42, (int) a[i]);
299 >    }
300  
301      /**
302 <     *  Test to verify toArray throws an ArrayStoreException when the given array
303 <     *  can not store the objects inside the list
302 >     * toArray throws an ArrayStoreException when the given array can
303 >     * not store the objects inside the set
304       */
305 <    public void testToArray_ArrayStoreException(){
306 <        try{
305 >    public void testToArray_ArrayStoreException() {
306 >        try {
307              CopyOnWriteArraySet c = new CopyOnWriteArraySet();
308              c.add("zfasdfsdf");
309              c.add("asdadasd");
310              c.toArray(new Long[5]);
311 <            fail("Object[] toArray(Object[]) should throw ArrayStoreException");
312 <        }catch(ArrayStoreException e){}
311 >            shouldThrow();
312 >        } catch (ArrayStoreException success) {}
313 >    }
314 >
315 >    /**
316 >     * A deserialized serialized set is equal
317 >     */
318 >    public void testSerialization() throws Exception {
319 >        Set x = populatedSet(SIZE);
320 >        Set y = serialClone(x);
321 >
322 >        assertTrue(x != y);
323 >        assertEquals(x.size(), y.size());
324 >        assertEquals(x, y);
325 >        assertEquals(y, x);
326      }
327  
328   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines