ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CopyOnWriteArraySetTest.java
Revision: 1.17
Committed: Tue Mar 15 19:47:06 2011 UTC (13 years, 2 months ago) by jsr166
Branch: MAIN
CVS Tags: release-1_7_0
Changes since 1.16: +1 -1 lines
Log Message:
Update Creative Commons license URL in legal notices

File Contents

# User Rev Content
1 dl 1.1 /*
2 dl 1.6 * 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 jsr166 1.17 * http://creativecommons.org/publicdomain/zero/1.0/
5 jsr166 1.8 * Other contributors include Andrew Wright, Jeffrey Hayes,
6     * Pat Fisher, Mike Judd.
7 dl 1.1 */
8    
9     import junit.framework.*;
10     import java.util.*;
11     import java.util.concurrent.*;
12 dl 1.2 import java.io.*;
13 dl 1.1
14 dl 1.3 public class CopyOnWriteArraySetTest extends JSR166TestCase {
15 dl 1.1 public static void main(String[] args) {
16 jsr166 1.15 junit.textui.TestRunner.run(suite());
17 dl 1.1 }
18     public static Test suite() {
19 jsr166 1.11 return new TestSuite(CopyOnWriteArraySetTest.class);
20 dl 1.1 }
21    
22 jsr166 1.10 static CopyOnWriteArraySet populatedSet(int n) {
23 jsr166 1.11 CopyOnWriteArraySet a = new CopyOnWriteArraySet();
24 dl 1.1 assertTrue(a.isEmpty());
25 jsr166 1.8 for (int i = 0; i < n; ++i)
26 dl 1.1 a.add(new Integer(i));
27     assertFalse(a.isEmpty());
28     assertEquals(n, a.size());
29     return a;
30     }
31    
32 dl 1.4 /**
33     * Default-constructed set is empty
34     */
35 dl 1.3 public void testConstructor() {
36 jsr166 1.11 CopyOnWriteArraySet a = new CopyOnWriteArraySet();
37 dl 1.3 assertTrue(a.isEmpty());
38     }
39    
40 dl 1.4 /**
41 dl 1.5 * Collection-constructed set holds all of its elements
42 dl 1.4 */
43 dl 1.3 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 jsr166 1.11 CopyOnWriteArraySet a = new CopyOnWriteArraySet(Arrays.asList(ints));
48 jsr166 1.8 for (int i = 0; i < SIZE; ++i)
49 dl 1.3 assertTrue(a.contains(ints[i]));
50     }
51 jsr166 1.8
52 dl 1.3
53 dl 1.1 /**
54 jsr166 1.16 * addAll adds each element from the given collection
55 dl 1.1 */
56 dl 1.4 public void testAddAll() {
57 jsr166 1.11 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 dl 1.1 }
65    
66     /**
67 jsr166 1.16 * addAll adds each element from the given collection that did not
68     * already exist in the set
69 dl 1.1 */
70 dl 1.4 public void testAddAll2() {
71 jsr166 1.11 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 dl 1.1 }
79    
80     /**
81 jsr166 1.16 * add will not add the element if it already exists in the set
82 dl 1.1 */
83 dl 1.4 public void testAdd2() {
84 jsr166 1.11 CopyOnWriteArraySet full = populatedSet(3);
85     full.add(one);
86     assertEquals(3, full.size());
87 dl 1.1 }
88    
89     /**
90 jsr166 1.16 * add adds the element when it does not exist in the set
91 dl 1.1 */
92 dl 1.4 public void testAdd3() {
93 jsr166 1.11 CopyOnWriteArraySet full = populatedSet(3);
94 dl 1.3 full.add(three);
95     assertTrue(full.contains(three));
96 dl 1.1 }
97    
98     /**
99 jsr166 1.16 * clear removes all elements from the set
100 dl 1.1 */
101 dl 1.4 public void testClear() {
102 jsr166 1.11 CopyOnWriteArraySet full = populatedSet(3);
103     full.clear();
104     assertEquals(0, full.size());
105 dl 1.1 }
106    
107     /**
108 jsr166 1.16 * contains returns true for added elements
109 dl 1.1 */
110 dl 1.4 public void testContains() {
111 jsr166 1.11 CopyOnWriteArraySet full = populatedSet(3);
112     assertTrue(full.contains(one));
113     assertFalse(full.contains(five));
114 dl 1.1 }
115    
116 dl 1.4 /**
117     * Sets with equal elements are equal
118     */
119 dl 1.1 public void testEquals() {
120 jsr166 1.11 CopyOnWriteArraySet a = populatedSet(3);
121     CopyOnWriteArraySet b = populatedSet(3);
122 dl 1.1 assertTrue(a.equals(b));
123     assertTrue(b.equals(a));
124     assertEquals(a.hashCode(), b.hashCode());
125 dl 1.3 a.add(m1);
126 dl 1.1 assertFalse(a.equals(b));
127     assertFalse(b.equals(a));
128 dl 1.3 b.add(m1);
129 dl 1.1 assertTrue(a.equals(b));
130     assertTrue(b.equals(a));
131     assertEquals(a.hashCode(), b.hashCode());
132     }
133    
134 jsr166 1.8
135 dl 1.1 /**
136 jsr166 1.16 * containsAll returns true for collections with subset of elements
137 dl 1.1 */
138 dl 1.4 public void testContainsAll() {
139 jsr166 1.11 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 dl 1.1 }
147    
148     /**
149 jsr166 1.16 * isEmpty is true when empty, else false
150 dl 1.1 */
151 dl 1.4 public void testIsEmpty() {
152 jsr166 1.11 CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
153     CopyOnWriteArraySet full = populatedSet(3);
154     assertTrue(empty.isEmpty());
155     assertFalse(full.isEmpty());
156 dl 1.1 }
157    
158     /**
159 jsr166 1.16 * iterator() returns an iterator containing the elements of the set
160 dl 1.1 */
161 dl 1.4 public void testIterator() {
162 jsr166 1.11 CopyOnWriteArraySet full = populatedSet(3);
163     Iterator i = full.iterator();
164     int j;
165     for (j = 0; i.hasNext(); j++)
166 jsr166 1.14 assertEquals(j, i.next());
167 jsr166 1.11 assertEquals(3, j);
168 dl 1.1 }
169    
170 dl 1.4 /**
171     * iterator remove is unsupported
172     */
173 jsr166 1.15 public void testIteratorRemove() {
174 jsr166 1.11 CopyOnWriteArraySet full = populatedSet(3);
175 dl 1.1 Iterator it = full.iterator();
176     it.next();
177     try {
178     it.remove();
179 dl 1.4 shouldThrow();
180 jsr166 1.12 } catch (UnsupportedOperationException success) {}
181 dl 1.1 }
182    
183 dl 1.4 /**
184     * toString holds toString of elements
185     */
186     public void testToString() {
187 jsr166 1.11 CopyOnWriteArraySet full = populatedSet(3);
188 dl 1.1 String s = full.toString();
189     for (int i = 0; i < 3; ++i) {
190     assertTrue(s.indexOf(String.valueOf(i)) >= 0);
191     }
192 jsr166 1.8 }
193 dl 1.1
194    
195     /**
196 jsr166 1.16 * removeAll removes all elements from the given collection
197 dl 1.1 */
198 dl 1.4 public void testRemoveAll() {
199 jsr166 1.11 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 dl 1.1 }
206    
207    
208 dl 1.4 /**
209     * remove removes an element
210     */
211     public void testRemove() {
212 jsr166 1.11 CopyOnWriteArraySet full = populatedSet(3);
213     full.remove(one);
214 dl 1.3 assertFalse(full.contains(one));
215 jsr166 1.11 assertEquals(2, full.size());
216 dl 1.1 }
217    
218     /**
219 jsr166 1.16 * size returns the number of elements
220 dl 1.1 */
221 dl 1.4 public void testSize() {
222 jsr166 1.11 CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
223     CopyOnWriteArraySet full = populatedSet(3);
224     assertEquals(3, full.size());
225     assertEquals(0, empty.size());
226 dl 1.1 }
227    
228     /**
229 jsr166 1.16 * toArray returns an Object array containing all elements from the set
230 dl 1.1 */
231 dl 1.4 public void testToArray() {
232 jsr166 1.11 CopyOnWriteArraySet full = populatedSet(3);
233     Object[] o = full.toArray();
234     assertEquals(3, o.length);
235 jsr166 1.14 assertEquals(0, o[0]);
236     assertEquals(1, o[1]);
237     assertEquals(2, o[2]);
238 dl 1.1 }
239    
240     /**
241 jsr166 1.16 * toArray returns an Integer array containing all elements from
242     * the set
243 dl 1.1 */
244 dl 1.4 public void testToArray2() {
245 jsr166 1.11 CopyOnWriteArraySet full = populatedSet(3);
246     Integer[] i = new Integer[3];
247     i = (Integer[])full.toArray(i);
248     assertEquals(3, i.length);
249 jsr166 1.14 assertEquals(0, (int) i[0]);
250     assertEquals(1, (int) i[1]);
251     assertEquals(2, (int) i[2]);
252 dl 1.1 }
253    
254    
255     /**
256 jsr166 1.16 * toArray throws an ArrayStoreException when the given array can
257     * not store the objects inside the set
258 dl 1.1 */
259 dl 1.4 public void testToArray_ArrayStoreException() {
260     try {
261 dl 1.1 CopyOnWriteArraySet c = new CopyOnWriteArraySet();
262     c.add("zfasdfsdf");
263     c.add("asdadasd");
264     c.toArray(new Long[5]);
265 jsr166 1.11 shouldThrow();
266 jsr166 1.13 } catch (ArrayStoreException success) {}
267 dl 1.2 }
268    
269 dl 1.4 /**
270 dl 1.7 * A deserialized serialized set is equal
271 dl 1.4 */
272 jsr166 1.12 public void testSerialization() throws Exception {
273 dl 1.3 CopyOnWriteArraySet q = populatedSet(SIZE);
274 dl 1.2
275 jsr166 1.12 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 dl 1.1 }
287    
288     }