ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CopyOnWriteArraySetTest.java
Revision: 1.18
Committed: Fri May 27 19:26:42 2011 UTC (12 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.17: +1 -6 lines
Log Message:
indexOf => contains

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.1 /**
53 jsr166 1.16 * addAll adds each element from the given collection
54 dl 1.1 */
55 dl 1.4 public void testAddAll() {
56 jsr166 1.11 CopyOnWriteArraySet full = populatedSet(3);
57     Vector v = new Vector();
58     v.add(three);
59     v.add(four);
60     v.add(five);
61     full.addAll(v);
62     assertEquals(6, full.size());
63 dl 1.1 }
64    
65     /**
66 jsr166 1.16 * addAll adds each element from the given collection that did not
67     * already exist in the set
68 dl 1.1 */
69 dl 1.4 public void testAddAll2() {
70 jsr166 1.11 CopyOnWriteArraySet full = populatedSet(3);
71     Vector v = new Vector();
72     v.add(three);
73     v.add(four);
74     v.add(one); // will not add this element
75     full.addAll(v);
76     assertEquals(5, full.size());
77 dl 1.1 }
78    
79     /**
80 jsr166 1.16 * add will not add the element if it already exists in the set
81 dl 1.1 */
82 dl 1.4 public void testAdd2() {
83 jsr166 1.11 CopyOnWriteArraySet full = populatedSet(3);
84     full.add(one);
85     assertEquals(3, full.size());
86 dl 1.1 }
87    
88     /**
89 jsr166 1.16 * add adds the element when it does not exist in the set
90 dl 1.1 */
91 dl 1.4 public void testAdd3() {
92 jsr166 1.11 CopyOnWriteArraySet full = populatedSet(3);
93 dl 1.3 full.add(three);
94     assertTrue(full.contains(three));
95 dl 1.1 }
96    
97     /**
98 jsr166 1.16 * clear removes all elements from the set
99 dl 1.1 */
100 dl 1.4 public void testClear() {
101 jsr166 1.11 CopyOnWriteArraySet full = populatedSet(3);
102     full.clear();
103     assertEquals(0, full.size());
104 dl 1.1 }
105    
106     /**
107 jsr166 1.16 * contains returns true for added elements
108 dl 1.1 */
109 dl 1.4 public void testContains() {
110 jsr166 1.11 CopyOnWriteArraySet full = populatedSet(3);
111     assertTrue(full.contains(one));
112     assertFalse(full.contains(five));
113 dl 1.1 }
114    
115 dl 1.4 /**
116     * Sets with equal elements are equal
117     */
118 dl 1.1 public void testEquals() {
119 jsr166 1.11 CopyOnWriteArraySet a = populatedSet(3);
120     CopyOnWriteArraySet b = populatedSet(3);
121 dl 1.1 assertTrue(a.equals(b));
122     assertTrue(b.equals(a));
123     assertEquals(a.hashCode(), b.hashCode());
124 dl 1.3 a.add(m1);
125 dl 1.1 assertFalse(a.equals(b));
126     assertFalse(b.equals(a));
127 dl 1.3 b.add(m1);
128 dl 1.1 assertTrue(a.equals(b));
129     assertTrue(b.equals(a));
130     assertEquals(a.hashCode(), b.hashCode());
131     }
132    
133     /**
134 jsr166 1.16 * containsAll returns true for collections with subset of elements
135 dl 1.1 */
136 dl 1.4 public void testContainsAll() {
137 jsr166 1.11 CopyOnWriteArraySet full = populatedSet(3);
138     Vector v = new Vector();
139     v.add(one);
140     v.add(two);
141     assertTrue(full.containsAll(v));
142     v.add(six);
143     assertFalse(full.containsAll(v));
144 dl 1.1 }
145    
146     /**
147 jsr166 1.16 * isEmpty is true when empty, else false
148 dl 1.1 */
149 dl 1.4 public void testIsEmpty() {
150 jsr166 1.11 CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
151     CopyOnWriteArraySet full = populatedSet(3);
152     assertTrue(empty.isEmpty());
153     assertFalse(full.isEmpty());
154 dl 1.1 }
155    
156     /**
157 jsr166 1.16 * iterator() returns an iterator containing the elements of the set
158 dl 1.1 */
159 dl 1.4 public void testIterator() {
160 jsr166 1.11 CopyOnWriteArraySet full = populatedSet(3);
161     Iterator i = full.iterator();
162     int j;
163     for (j = 0; i.hasNext(); j++)
164 jsr166 1.14 assertEquals(j, i.next());
165 jsr166 1.11 assertEquals(3, j);
166 dl 1.1 }
167    
168 dl 1.4 /**
169     * iterator remove is unsupported
170     */
171 jsr166 1.15 public void testIteratorRemove() {
172 jsr166 1.11 CopyOnWriteArraySet full = populatedSet(3);
173 dl 1.1 Iterator it = full.iterator();
174     it.next();
175     try {
176     it.remove();
177 dl 1.4 shouldThrow();
178 jsr166 1.12 } catch (UnsupportedOperationException success) {}
179 dl 1.1 }
180    
181 dl 1.4 /**
182     * toString holds toString of elements
183     */
184     public void testToString() {
185 jsr166 1.11 CopyOnWriteArraySet full = populatedSet(3);
186 dl 1.1 String s = full.toString();
187     for (int i = 0; i < 3; ++i) {
188 jsr166 1.18 assertTrue(s.contains(String.valueOf(i)));
189 dl 1.1 }
190 jsr166 1.8 }
191 dl 1.1
192     /**
193 jsr166 1.16 * removeAll removes all elements from the given collection
194 dl 1.1 */
195 dl 1.4 public void testRemoveAll() {
196 jsr166 1.11 CopyOnWriteArraySet full = populatedSet(3);
197     Vector v = new Vector();
198     v.add(one);
199     v.add(two);
200     full.removeAll(v);
201     assertEquals(1, full.size());
202 dl 1.1 }
203    
204 dl 1.4 /**
205     * remove removes an element
206     */
207     public void testRemove() {
208 jsr166 1.11 CopyOnWriteArraySet full = populatedSet(3);
209     full.remove(one);
210 dl 1.3 assertFalse(full.contains(one));
211 jsr166 1.11 assertEquals(2, full.size());
212 dl 1.1 }
213    
214     /**
215 jsr166 1.16 * size returns the number of elements
216 dl 1.1 */
217 dl 1.4 public void testSize() {
218 jsr166 1.11 CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
219     CopyOnWriteArraySet full = populatedSet(3);
220     assertEquals(3, full.size());
221     assertEquals(0, empty.size());
222 dl 1.1 }
223    
224     /**
225 jsr166 1.16 * toArray returns an Object array containing all elements from the set
226 dl 1.1 */
227 dl 1.4 public void testToArray() {
228 jsr166 1.11 CopyOnWriteArraySet full = populatedSet(3);
229     Object[] o = full.toArray();
230     assertEquals(3, o.length);
231 jsr166 1.14 assertEquals(0, o[0]);
232     assertEquals(1, o[1]);
233     assertEquals(2, o[2]);
234 dl 1.1 }
235    
236     /**
237 jsr166 1.16 * toArray returns an Integer array containing all elements from
238     * the set
239 dl 1.1 */
240 dl 1.4 public void testToArray2() {
241 jsr166 1.11 CopyOnWriteArraySet full = populatedSet(3);
242     Integer[] i = new Integer[3];
243     i = (Integer[])full.toArray(i);
244     assertEquals(3, i.length);
245 jsr166 1.14 assertEquals(0, (int) i[0]);
246     assertEquals(1, (int) i[1]);
247     assertEquals(2, (int) i[2]);
248 dl 1.1 }
249    
250     /**
251 jsr166 1.16 * toArray throws an ArrayStoreException when the given array can
252     * not store the objects inside the set
253 dl 1.1 */
254 dl 1.4 public void testToArray_ArrayStoreException() {
255     try {
256 dl 1.1 CopyOnWriteArraySet c = new CopyOnWriteArraySet();
257     c.add("zfasdfsdf");
258     c.add("asdadasd");
259     c.toArray(new Long[5]);
260 jsr166 1.11 shouldThrow();
261 jsr166 1.13 } catch (ArrayStoreException success) {}
262 dl 1.2 }
263    
264 dl 1.4 /**
265 dl 1.7 * A deserialized serialized set is equal
266 dl 1.4 */
267 jsr166 1.12 public void testSerialization() throws Exception {
268 dl 1.3 CopyOnWriteArraySet q = populatedSet(SIZE);
269 dl 1.2
270 jsr166 1.12 ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
271     ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
272     out.writeObject(q);
273     out.close();
274    
275     ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
276     ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
277     CopyOnWriteArraySet r = (CopyOnWriteArraySet)in.readObject();
278     assertEquals(q.size(), r.size());
279     assertTrue(q.equals(r));
280     assertTrue(r.equals(q));
281 dl 1.1 }
282    
283     }