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.15 by jsr166, Wed Aug 25 00:07:03 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.*;
10   import java.util.*;
11   import java.util.concurrent.*;
12 + import java.io.*;
13  
14 < public class CopyOnWriteArraySetTest extends TestCase{
13 <    
14 > public class CopyOnWriteArraySetTest extends JSR166TestCase {
15      public static void main(String[] args) {
16 <        junit.textui.TestRunner.run (suite());  
16 >        junit.textui.TestRunner.run(suite());
17      }
17
18      public static Test suite() {
19 <        return new TestSuite(CopyOnWriteArraySetTest.class);
19 >        return new TestSuite(CopyOnWriteArraySetTest.class);
20      }
21  
22 <    static CopyOnWriteArraySet fullSet(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());
# Line 30 | Line 30 | public class CopyOnWriteArraySetTest ext
30      }
31  
32      /**
33 <     *  Test to verify addAll correctly adds each element from the given collection
33 >     * Default-constructed set is empty
34 >     */
35 >    public void testConstructor() {
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)
49 >            assertTrue(a.contains(ints[i]));
50 >    }
51 >
52 >
53 >    /**
54 >     *  addAll adds each element from the given collection
55       */
56 <    public void testAddAll(){
57 <        CopyOnWriteArraySet full = fullSet(3);
58 <        Vector v = new Vector();
59 <        v.add(new Integer(3));
60 <        v.add(new Integer(4));
61 <        v.add(new Integer(5));
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 <     *  Test to verify 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 = fullSet(3);
72 <        Vector v = new Vector();
73 <        v.add(new Integer(3));
74 <        v.add(new Integer(4));
75 <        v.add(new Integer(1)); // 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 <     *  Test to verify 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 = fullSet(3);
85 <        full.add(new Integer(1));
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 <     *  test to verify addIfAbsent correctly adds the element when it does not exist in the list
90 >     *  add  adds the element when it does not exist
91 >     *  in the set
92       */
93 <    public void testAdd3(){
94 <        CopyOnWriteArraySet full = fullSet(3);
95 <        full.add(new Integer(3));
96 <        assertTrue(full.contains(new Integer(3)));
93 >    public void testAdd3() {
94 >        CopyOnWriteArraySet full = populatedSet(3);
95 >        full.add(three);
96 >        assertTrue(full.contains(three));
97      }
98  
99      /**
100 <     *  Test to verify clear correctly removes all elements from the list
100 >     *  clear removes all elements from the set
101       */
102 <    public void testClear(){
103 <        CopyOnWriteArraySet full = fullSet(3);
104 <        full.clear();
105 <        assertEquals(0, full.size());
102 >    public void testClear() {
103 >        CopyOnWriteArraySet full = populatedSet(3);
104 >        full.clear();
105 >        assertEquals(0, full.size());
106      }
107  
108      /**
109 <     *  Test to verify contains returns the correct values
109 >     *   contains returns true for added elements
110       */
111 <    public void testContains(){
112 <        CopyOnWriteArraySet full = fullSet(3);
113 <        assertTrue(full.contains(new Integer(1)));
114 <        assertFalse(full.contains(new Integer(5)));
111 >    public void testContains() {
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 = fullSet(3);
122 <        CopyOnWriteArraySet b = fullSet(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());
126 <        a.add(new Integer(-1));
126 >        a.add(m1);
127          assertFalse(a.equals(b));
128          assertFalse(b.equals(a));
129 <        b.add(new Integer(-1));
129 >        b.add(m1);
130          assertTrue(a.equals(b));
131          assertTrue(b.equals(a));
132          assertEquals(a.hashCode(), b.hashCode());
133      }
134  
135 <    
135 >
136      /**
137 <     *  Test to verify containsAll returns the correct values
137 >     *  containsAll returns true for collections with subset of elements
138       */
139 <    public void testContainsAll(){
140 <        CopyOnWriteArraySet full = fullSet(3);
141 <        Vector v = new Vector();
142 <        v.add(new Integer(1));
143 <        v.add(new Integer(2));
144 <        assertTrue(full.containsAll(v));
145 <        v.add(new Integer(6));
146 <        assertFalse(full.containsAll(v));
139 >    public void testContainsAll() {
140 >        CopyOnWriteArraySet full = populatedSet(3);
141 >        Vector v = new Vector();
142 >        v.add(one);
143 >        v.add(two);
144 >        assertTrue(full.containsAll(v));
145 >        v.add(six);
146 >        assertFalse(full.containsAll(v));
147      }
148  
149      /**
150 <     *  Test to verify isEmpty returns the correct values
150 >     *  isEmpty is true when empty, else false
151       */
152 <    public void testIsEmpty(){
153 <        CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
154 <        CopyOnWriteArraySet full = fullSet(3);
155 <        assertTrue(empty.isEmpty());
156 <        assertFalse(full.isEmpty());
152 >    public void testIsEmpty() {
153 >        CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
154 >        CopyOnWriteArraySet full = populatedSet(3);
155 >        assertTrue(empty.isEmpty());
156 >        assertFalse(full.isEmpty());
157      }
158  
159      /**
160 <     *  Test to verify iterator() returns an iterator containing the elements of the list
160 >     *  iterator() returns an iterator containing the elements of the set
161       */
162 <    public void testIterator(){
163 <        CopyOnWriteArraySet full = fullSet(3);
164 <        Iterator i = full.iterator();
165 <        int j;
166 <        for(j = 0; i.hasNext(); j++)
167 <            assertEquals(j, ((Integer)i.next()).intValue());
168 <        assertEquals(3, j);
162 >    public void testIterator() {
163 >        CopyOnWriteArraySet full = populatedSet(3);
164 >        Iterator i = full.iterator();
165 >        int j;
166 >        for (j = 0; i.hasNext(); j++)
167 >            assertEquals(j, i.next());
168 >        assertEquals(3, j);
169      }
170  
171 <    public void testIteratorRemove () {
172 <        CopyOnWriteArraySet full = fullSet(3);
171 >    /**
172 >     * iterator remove is unsupported
173 >     */
174 >    public void testIteratorRemove() {
175 >        CopyOnWriteArraySet full = populatedSet(3);
176          Iterator it = full.iterator();
177          it.next();
178          try {
179              it.remove();
180 <            fail("should throw");
181 <        }
154 <        catch (UnsupportedOperationException success) {}
180 >            shouldThrow();
181 >        } catch (UnsupportedOperationException success) {}
182      }
183  
184 <    public void testToString(){
185 <        CopyOnWriteArraySet full = fullSet(3);
184 >    /**
185 >     * toString holds toString of elements
186 >     */
187 >    public void testToString() {
188 >        CopyOnWriteArraySet full = populatedSet(3);
189          String s = full.toString();
190          for (int i = 0; i < 3; ++i) {
191              assertTrue(s.indexOf(String.valueOf(i)) >= 0);
192          }
193 <    }        
193 >    }
194  
195  
196      /**
197 <     *  Test to verify removeAll correctly removes all elements from the given collection
197 >     *  removeAll  removes all elements from the given collection
198       */
199 <    public void testRemoveAll(){
200 <        CopyOnWriteArraySet full = fullSet(3);
201 <        Vector v = new Vector();
202 <        v.add(new Integer(1));
203 <        v.add(new Integer(2));
204 <        full.removeAll(v);
205 <        assertEquals(1, full.size());
199 >    public void testRemoveAll() {
200 >        CopyOnWriteArraySet full = populatedSet(3);
201 >        Vector v = new Vector();
202 >        v.add(one);
203 >        v.add(two);
204 >        full.removeAll(v);
205 >        assertEquals(1, full.size());
206      }
207  
208  
209 <    public void testRemove(){
210 <        CopyOnWriteArraySet full = fullSet(3);
211 <        full.remove(new Integer(1));
212 <        assertFalse(full.contains(new Integer(1)));
213 <        assertEquals(2, full.size());
209 >    /**
210 >     * remove removes an element
211 >     */
212 >    public void testRemove() {
213 >        CopyOnWriteArraySet full = populatedSet(3);
214 >        full.remove(one);
215 >        assertFalse(full.contains(one));
216 >        assertEquals(2, full.size());
217      }
218  
219      /**
220 <     *  Test to verify size returns the correct values
220 >     *  size returns the number of elements
221       */
222 <    public void testSize(){
223 <        CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
224 <        CopyOnWriteArraySet full = fullSet(3);
225 <        assertEquals(3, full.size());
226 <        assertEquals(0, empty.size());
222 >    public void testSize() {
223 >        CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
224 >        CopyOnWriteArraySet full = populatedSet(3);
225 >        assertEquals(3, full.size());
226 >        assertEquals(0, empty.size());
227      }
228  
229      /**
230 <     *  Test to verify toArray returns an Object array containing all elements from the list
230 >     *  toArray returns an Object array containing all elements from the set
231       */
232 <    public void testToArray(){
233 <        CopyOnWriteArraySet full = fullSet(3);
234 <        Object[] o = full.toArray();
235 <        assertEquals(3, o.length);
236 <        assertEquals(0, ((Integer)o[0]).intValue());
237 <        assertEquals(1, ((Integer)o[1]).intValue());
238 <        assertEquals(2, ((Integer)o[2]).intValue());
232 >    public void testToArray() {
233 >        CopyOnWriteArraySet full = populatedSet(3);
234 >        Object[] o = full.toArray();
235 >        assertEquals(3, o.length);
236 >        assertEquals(0, o[0]);
237 >        assertEquals(1, o[1]);
238 >        assertEquals(2, o[2]);
239      }
240  
241      /**
242 <     *  test to verify toArray returns an Integer array containing all elements from the list
242 >     *  toArray returns an Integer array containing all elements from
243 >     *  the set
244       */
245 <    public void testToArray2(){
246 <        CopyOnWriteArraySet full = fullSet(3);
247 <        Integer[] i = new Integer[3];
248 <        i = (Integer[])full.toArray(i);
249 <        assertEquals(3, i.length);
250 <        assertEquals(0, i[0].intValue());
251 <        assertEquals(1, i[1].intValue());
252 <        assertEquals(2, i[2].intValue());
245 >    public void testToArray2() {
246 >        CopyOnWriteArraySet full = populatedSet(3);
247 >        Integer[] i = new Integer[3];
248 >        i = (Integer[])full.toArray(i);
249 >        assertEquals(3, i.length);
250 >        assertEquals(0, (int) i[0]);
251 >        assertEquals(1, (int) i[1]);
252 >        assertEquals(2, (int) i[2]);
253      }
254  
255  
222
223    // Exception tests
224
256      /**
257 <     *  Test to verify toArray throws an ArrayStoreException when the given array
258 <     *  can not store the objects inside the list
257 >     *  toArray throws an ArrayStoreException when the given array can
258 >     *  not store the objects inside the set
259       */
260 <    public void testToArray_ArrayStoreException(){
261 <        try{
260 >    public void testToArray_ArrayStoreException() {
261 >        try {
262              CopyOnWriteArraySet c = new CopyOnWriteArraySet();
263              c.add("zfasdfsdf");
264              c.add("asdadasd");
265              c.toArray(new Long[5]);
266 <            fail("Object[] toArray(Object[]) should throw ArrayStoreException");
267 <        }catch(ArrayStoreException e){}
266 >            shouldThrow();
267 >        } catch (ArrayStoreException success) {}
268 >    }
269 >
270 >    /**
271 >     * A deserialized serialized set is equal
272 >     */
273 >    public void testSerialization() throws Exception {
274 >        CopyOnWriteArraySet q = populatedSet(SIZE);
275 >
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));
286 >        assertTrue(r.equals(q));
287      }
288  
289   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines