ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CopyOnWriteArraySetTest.java
Revision: 1.5
Committed: Thu Sep 25 11:02:41 2003 UTC (20 years, 7 months ago) by dl
Branch: MAIN
CVS Tags: JSR166_NOV3_FREEZE, JSR166_DEC9_PRE_ES_SUBMIT, JSR166_DEC9_POST_ES_SUBMIT
Changes since 1.4: +18 -20 lines
Log Message:
improve tck javadocs; rename and add a few tests

File Contents

# Content
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.
6 */
7
8 import junit.framework.*;
9 import java.util.*;
10 import java.util.concurrent.*;
11 import java.io.*;
12
13 public class CopyOnWriteArraySetTest extends JSR166TestCase {
14 public static void main(String[] args) {
15 junit.textui.TestRunner.run (suite());
16 }
17 public static Test suite() {
18 return new TestSuite(CopyOnWriteArraySetTest.class);
19 }
20
21 static CopyOnWriteArraySet populatedSet(int n){
22 CopyOnWriteArraySet a = new CopyOnWriteArraySet();
23 assertTrue(a.isEmpty());
24 for (int i = 0; i < n; ++i)
25 a.add(new Integer(i));
26 assertFalse(a.isEmpty());
27 assertEquals(n, a.size());
28 return a;
29 }
30
31 /**
32 * Default-constructed set is empty
33 */
34 public void testConstructor() {
35 CopyOnWriteArraySet a = new CopyOnWriteArraySet();
36 assertTrue(a.isEmpty());
37 }
38
39 /**
40 * Collection-constructed set holds all of its elements
41 */
42 public void testConstructor3() {
43 Integer[] ints = new Integer[SIZE];
44 for (int i = 0; i < SIZE-1; ++i)
45 ints[i] = new Integer(i);
46 CopyOnWriteArraySet a = new CopyOnWriteArraySet(Arrays.asList(ints));
47 for (int i = 0; i < SIZE; ++i)
48 assertTrue(a.contains(ints[i]));
49 }
50
51
52 /**
53 * addAll adds each element from the given collection
54 */
55 public void testAddAll() {
56 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 }
64
65 /**
66 * addAll adds each element from the given collection that did not
67 * already exist in the set
68 */
69 public void testAddAll2() {
70 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 }
78
79 /**
80 * add will not add the element if it already exists in the set
81 */
82 public void testAdd2() {
83 CopyOnWriteArraySet full = populatedSet(3);
84 full.add(one);
85 assertEquals(3, full.size());
86 }
87
88 /**
89 * add adds the element when it does not exist
90 * in the set
91 */
92 public void testAdd3() {
93 CopyOnWriteArraySet full = populatedSet(3);
94 full.add(three);
95 assertTrue(full.contains(three));
96 }
97
98 /**
99 * clear removes all elements from the set
100 */
101 public void testClear() {
102 CopyOnWriteArraySet full = populatedSet(3);
103 full.clear();
104 assertEquals(0, full.size());
105 }
106
107 /**
108 * contains returns true for added elements
109 */
110 public void testContains() {
111 CopyOnWriteArraySet full = populatedSet(3);
112 assertTrue(full.contains(one));
113 assertFalse(full.contains(five));
114 }
115
116 /**
117 * Sets with equal elements are equal
118 */
119 public void testEquals() {
120 CopyOnWriteArraySet a = populatedSet(3);
121 CopyOnWriteArraySet b = populatedSet(3);
122 assertTrue(a.equals(b));
123 assertTrue(b.equals(a));
124 assertEquals(a.hashCode(), b.hashCode());
125 a.add(m1);
126 assertFalse(a.equals(b));
127 assertFalse(b.equals(a));
128 b.add(m1);
129 assertTrue(a.equals(b));
130 assertTrue(b.equals(a));
131 assertEquals(a.hashCode(), b.hashCode());
132 }
133
134
135 /**
136 * containsAll returns true for collections with subset of elements
137 */
138 public void testContainsAll() {
139 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 }
147
148 /**
149 * isEmpty is true when empty, else false
150 */
151 public void testIsEmpty() {
152 CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
153 CopyOnWriteArraySet full = populatedSet(3);
154 assertTrue(empty.isEmpty());
155 assertFalse(full.isEmpty());
156 }
157
158 /**
159 * iterator() returns an iterator containing the elements of the set
160 */
161 public void testIterator() {
162 CopyOnWriteArraySet full = populatedSet(3);
163 Iterator i = full.iterator();
164 int j;
165 for(j = 0; i.hasNext(); j++)
166 assertEquals(j, ((Integer)i.next()).intValue());
167 assertEquals(3, j);
168 }
169
170 /**
171 * iterator remove is unsupported
172 */
173 public void testIteratorRemove () {
174 CopyOnWriteArraySet full = populatedSet(3);
175 Iterator it = full.iterator();
176 it.next();
177 try {
178 it.remove();
179 shouldThrow();
180 }
181 catch (UnsupportedOperationException success) {}
182 }
183
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 }
194
195
196 /**
197 * removeAll removes all elements from the given collection
198 */
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 /**
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 * size returns the number of elements
221 */
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 * toArray returns an Object array containing all elements from the set
231 */
232 public void testToArray() {
233 CopyOnWriteArraySet full = populatedSet(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());
239 }
240
241 /**
242 * toArray returns an Integer array containing all elements from
243 * the set
244 */
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, i[0].intValue());
251 assertEquals(1, i[1].intValue());
252 assertEquals(2, i[2].intValue());
253 }
254
255
256 /**
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 {
262 CopyOnWriteArraySet c = new CopyOnWriteArraySet();
263 c.add("zfasdfsdf");
264 c.add("asdadasd");
265 c.toArray(new Long[5]);
266 shouldThrow();
267 } catch(ArrayStoreException e){}
268 }
269
270 /**
271 * A deserialized seriealized set is equal
272 */
273 public void testSerialization() {
274 CopyOnWriteArraySet q = populatedSet(SIZE);
275
276 try {
277 ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
278 ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
279 out.writeObject(q);
280 out.close();
281
282 ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
283 ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
284 CopyOnWriteArraySet r = (CopyOnWriteArraySet)in.readObject();
285 assertEquals(q.size(), r.size());
286 assertTrue(q.equals(r));
287 assertTrue(r.equals(q));
288 } catch(Exception e){
289 unexpectedException();
290 }
291 }
292
293 }