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 |
|
12 |
public class CopyOnWriteArraySetTest extends TestCase{ |
13 |
|
14 |
public static void main(String[] args) { |
15 |
junit.textui.TestRunner.run (suite()); |
16 |
} |
17 |
|
18 |
public static Test suite() { |
19 |
return new TestSuite(CopyOnWriteArraySetTest.class); |
20 |
} |
21 |
|
22 |
static CopyOnWriteArraySet fullSet(int n){ |
23 |
CopyOnWriteArraySet a = new CopyOnWriteArraySet(); |
24 |
assertTrue(a.isEmpty()); |
25 |
for (int i = 0; i < n; ++i) |
26 |
a.add(new Integer(i)); |
27 |
assertFalse(a.isEmpty()); |
28 |
assertEquals(n, a.size()); |
29 |
return a; |
30 |
} |
31 |
|
32 |
/** |
33 |
* Test to verify addAll correctly adds each element from the given collection |
34 |
*/ |
35 |
public void testAddAll(){ |
36 |
CopyOnWriteArraySet full = fullSet(3); |
37 |
Vector v = new Vector(); |
38 |
v.add(new Integer(3)); |
39 |
v.add(new Integer(4)); |
40 |
v.add(new Integer(5)); |
41 |
full.addAll(v); |
42 |
assertEquals(6, full.size()); |
43 |
} |
44 |
|
45 |
/** |
46 |
* Test to verify addAllAbsent adds each element from the given collection that did not |
47 |
* already exist in the List |
48 |
*/ |
49 |
public void testAddAll2(){ |
50 |
CopyOnWriteArraySet full = fullSet(3); |
51 |
Vector v = new Vector(); |
52 |
v.add(new Integer(3)); |
53 |
v.add(new Integer(4)); |
54 |
v.add(new Integer(1)); // will not add this element |
55 |
full.addAll(v); |
56 |
assertEquals(5, full.size()); |
57 |
} |
58 |
|
59 |
/** |
60 |
* Test to verify addIfAbsent will not add the element if it already exists in the list |
61 |
*/ |
62 |
public void testAdd2(){ |
63 |
CopyOnWriteArraySet full = fullSet(3); |
64 |
full.add(new Integer(1)); |
65 |
assertEquals(3, full.size()); |
66 |
} |
67 |
|
68 |
/** |
69 |
* test to verify addIfAbsent correctly adds the element when it does not exist in the list |
70 |
*/ |
71 |
public void testAdd3(){ |
72 |
CopyOnWriteArraySet full = fullSet(3); |
73 |
full.add(new Integer(3)); |
74 |
assertTrue(full.contains(new Integer(3))); |
75 |
} |
76 |
|
77 |
/** |
78 |
* Test to verify clear correctly removes all elements from the list |
79 |
*/ |
80 |
public void testClear(){ |
81 |
CopyOnWriteArraySet full = fullSet(3); |
82 |
full.clear(); |
83 |
assertEquals(0, full.size()); |
84 |
} |
85 |
|
86 |
/** |
87 |
* Test to verify contains returns the correct values |
88 |
*/ |
89 |
public void testContains(){ |
90 |
CopyOnWriteArraySet full = fullSet(3); |
91 |
assertTrue(full.contains(new Integer(1))); |
92 |
assertFalse(full.contains(new Integer(5))); |
93 |
} |
94 |
|
95 |
public void testEquals() { |
96 |
CopyOnWriteArraySet a = fullSet(3); |
97 |
CopyOnWriteArraySet b = fullSet(3); |
98 |
assertTrue(a.equals(b)); |
99 |
assertTrue(b.equals(a)); |
100 |
assertEquals(a.hashCode(), b.hashCode()); |
101 |
a.add(new Integer(-1)); |
102 |
assertFalse(a.equals(b)); |
103 |
assertFalse(b.equals(a)); |
104 |
b.add(new Integer(-1)); |
105 |
assertTrue(a.equals(b)); |
106 |
assertTrue(b.equals(a)); |
107 |
assertEquals(a.hashCode(), b.hashCode()); |
108 |
} |
109 |
|
110 |
|
111 |
/** |
112 |
* Test to verify containsAll returns the correct values |
113 |
*/ |
114 |
public void testContainsAll(){ |
115 |
CopyOnWriteArraySet full = fullSet(3); |
116 |
Vector v = new Vector(); |
117 |
v.add(new Integer(1)); |
118 |
v.add(new Integer(2)); |
119 |
assertTrue(full.containsAll(v)); |
120 |
v.add(new Integer(6)); |
121 |
assertFalse(full.containsAll(v)); |
122 |
} |
123 |
|
124 |
/** |
125 |
* Test to verify isEmpty returns the correct values |
126 |
*/ |
127 |
public void testIsEmpty(){ |
128 |
CopyOnWriteArraySet empty = new CopyOnWriteArraySet(); |
129 |
CopyOnWriteArraySet full = fullSet(3); |
130 |
assertTrue(empty.isEmpty()); |
131 |
assertFalse(full.isEmpty()); |
132 |
} |
133 |
|
134 |
/** |
135 |
* Test to verify iterator() returns an iterator containing the elements of the list |
136 |
*/ |
137 |
public void testIterator(){ |
138 |
CopyOnWriteArraySet full = fullSet(3); |
139 |
Iterator i = full.iterator(); |
140 |
int j; |
141 |
for(j = 0; i.hasNext(); j++) |
142 |
assertEquals(j, ((Integer)i.next()).intValue()); |
143 |
assertEquals(3, j); |
144 |
} |
145 |
|
146 |
public void testIteratorRemove () { |
147 |
CopyOnWriteArraySet full = fullSet(3); |
148 |
Iterator it = full.iterator(); |
149 |
it.next(); |
150 |
try { |
151 |
it.remove(); |
152 |
fail("should throw"); |
153 |
} |
154 |
catch (UnsupportedOperationException success) {} |
155 |
} |
156 |
|
157 |
public void testToString(){ |
158 |
CopyOnWriteArraySet full = fullSet(3); |
159 |
String s = full.toString(); |
160 |
for (int i = 0; i < 3; ++i) { |
161 |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
162 |
} |
163 |
} |
164 |
|
165 |
|
166 |
/** |
167 |
* Test to verify removeAll correctly removes all elements from the given collection |
168 |
*/ |
169 |
public void testRemoveAll(){ |
170 |
CopyOnWriteArraySet full = fullSet(3); |
171 |
Vector v = new Vector(); |
172 |
v.add(new Integer(1)); |
173 |
v.add(new Integer(2)); |
174 |
full.removeAll(v); |
175 |
assertEquals(1, full.size()); |
176 |
} |
177 |
|
178 |
|
179 |
public void testRemove(){ |
180 |
CopyOnWriteArraySet full = fullSet(3); |
181 |
full.remove(new Integer(1)); |
182 |
assertFalse(full.contains(new Integer(1))); |
183 |
assertEquals(2, full.size()); |
184 |
} |
185 |
|
186 |
/** |
187 |
* Test to verify size returns the correct values |
188 |
*/ |
189 |
public void testSize(){ |
190 |
CopyOnWriteArraySet empty = new CopyOnWriteArraySet(); |
191 |
CopyOnWriteArraySet full = fullSet(3); |
192 |
assertEquals(3, full.size()); |
193 |
assertEquals(0, empty.size()); |
194 |
} |
195 |
|
196 |
/** |
197 |
* Test to verify toArray returns an Object array containing all elements from the list |
198 |
*/ |
199 |
public void testToArray(){ |
200 |
CopyOnWriteArraySet full = fullSet(3); |
201 |
Object[] o = full.toArray(); |
202 |
assertEquals(3, o.length); |
203 |
assertEquals(0, ((Integer)o[0]).intValue()); |
204 |
assertEquals(1, ((Integer)o[1]).intValue()); |
205 |
assertEquals(2, ((Integer)o[2]).intValue()); |
206 |
} |
207 |
|
208 |
/** |
209 |
* test to verify toArray returns an Integer array containing all elements from the list |
210 |
*/ |
211 |
public void testToArray2(){ |
212 |
CopyOnWriteArraySet full = fullSet(3); |
213 |
Integer[] i = new Integer[3]; |
214 |
i = (Integer[])full.toArray(i); |
215 |
assertEquals(3, i.length); |
216 |
assertEquals(0, i[0].intValue()); |
217 |
assertEquals(1, i[1].intValue()); |
218 |
assertEquals(2, i[2].intValue()); |
219 |
} |
220 |
|
221 |
|
222 |
|
223 |
// Exception tests |
224 |
|
225 |
/** |
226 |
* Test to verify toArray throws an ArrayStoreException when the given array |
227 |
* can not store the objects inside the list |
228 |
*/ |
229 |
public void testToArray_ArrayStoreException(){ |
230 |
try{ |
231 |
CopyOnWriteArraySet c = new CopyOnWriteArraySet(); |
232 |
c.add("zfasdfsdf"); |
233 |
c.add("asdadasd"); |
234 |
c.toArray(new Long[5]); |
235 |
fail("Object[] toArray(Object[]) should throw ArrayStoreException"); |
236 |
}catch(ArrayStoreException e){} |
237 |
} |
238 |
|
239 |
} |