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