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. |
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/publicdomain/zero/1.0/ |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
10 |
< |
import java.util.*; |
11 |
< |
import java.util.concurrent.*; |
10 |
> |
import java.util.Arrays; |
11 |
> |
import java.util.Iterator; |
12 |
> |
import java.util.LinkedList; |
13 |
> |
import java.util.List; |
14 |
> |
import java.util.ListIterator; |
15 |
> |
import java.util.Vector; |
16 |
> |
import java.util.concurrent.CopyOnWriteArrayList; |
17 |
> |
|
18 |
> |
public class CopyOnWriteArrayListTest extends JSR166TestCase { |
19 |
|
|
12 |
– |
public class CopyOnWriteArrayListTest extends TestCase{ |
13 |
– |
|
20 |
|
public static void main(String[] args) { |
21 |
< |
junit.textui.TestRunner.run (suite()); |
21 |
> |
junit.textui.TestRunner.run(suite()); |
22 |
|
} |
23 |
|
|
24 |
|
public static Test suite() { |
25 |
< |
return new TestSuite(CopyOnWriteArrayListTest.class); |
25 |
> |
return new TestSuite(CopyOnWriteArrayListTest.class); |
26 |
|
} |
27 |
|
|
28 |
< |
static CopyOnWriteArrayList fullArray(int n){ |
29 |
< |
CopyOnWriteArrayList a = new CopyOnWriteArrayList(); |
28 |
> |
static CopyOnWriteArrayList<Integer> populatedArray(int n) { |
29 |
> |
CopyOnWriteArrayList<Integer> a |
30 |
> |
= new CopyOnWriteArrayList<Integer>(); |
31 |
|
assertTrue(a.isEmpty()); |
32 |
< |
for (int i = 0; i < n; ++i) |
33 |
< |
a.add(new Integer(i)); |
32 |
> |
for (int i = 0; i < n; ++i) |
33 |
> |
a.add(i); |
34 |
|
assertFalse(a.isEmpty()); |
35 |
|
assertEquals(n, a.size()); |
36 |
|
return a; |
37 |
|
} |
38 |
|
|
39 |
|
/** |
40 |
< |
* Test to verify addAll correctly adds each element from the given collection |
40 |
> |
* a new list is empty |
41 |
> |
*/ |
42 |
> |
public void testConstructor() { |
43 |
> |
CopyOnWriteArrayList a = new CopyOnWriteArrayList(); |
44 |
> |
assertTrue(a.isEmpty()); |
45 |
> |
} |
46 |
> |
|
47 |
> |
/** |
48 |
> |
* new list contains all elements of initializing array |
49 |
> |
*/ |
50 |
> |
public void testConstructor2() { |
51 |
> |
Integer[] ints = new Integer[SIZE]; |
52 |
> |
for (int i = 0; i < SIZE-1; ++i) |
53 |
> |
ints[i] = new Integer(i); |
54 |
> |
CopyOnWriteArrayList a = new CopyOnWriteArrayList(ints); |
55 |
> |
for (int i = 0; i < SIZE; ++i) |
56 |
> |
assertEquals(ints[i], a.get(i)); |
57 |
> |
} |
58 |
> |
|
59 |
> |
/** |
60 |
> |
* new list contains all elements of initializing collection |
61 |
> |
*/ |
62 |
> |
public void testConstructor3() { |
63 |
> |
Integer[] ints = new Integer[SIZE]; |
64 |
> |
for (int i = 0; i < SIZE-1; ++i) |
65 |
> |
ints[i] = new Integer(i); |
66 |
> |
CopyOnWriteArrayList a = new CopyOnWriteArrayList(Arrays.asList(ints)); |
67 |
> |
for (int i = 0; i < SIZE; ++i) |
68 |
> |
assertEquals(ints[i], a.get(i)); |
69 |
> |
} |
70 |
> |
|
71 |
> |
/** |
72 |
> |
* addAll adds each element from the given collection |
73 |
|
*/ |
74 |
< |
public void testAddAll(){ |
75 |
< |
CopyOnWriteArrayList full = fullArray(3); |
76 |
< |
Vector v = new Vector(); |
77 |
< |
v.add(new Integer(3)); |
78 |
< |
v.add(new Integer(4)); |
79 |
< |
v.add(new Integer(5)); |
80 |
< |
full.addAll(v); |
81 |
< |
assertEquals(6, full.size()); |
74 |
> |
public void testAddAll() { |
75 |
> |
CopyOnWriteArrayList full = populatedArray(3); |
76 |
> |
Vector v = new Vector(); |
77 |
> |
v.add(three); |
78 |
> |
v.add(four); |
79 |
> |
v.add(five); |
80 |
> |
full.addAll(v); |
81 |
> |
assertEquals(6, full.size()); |
82 |
|
} |
83 |
|
|
84 |
|
/** |
85 |
< |
* Test to verify addAllAbsent adds each element from the given collection that did not |
86 |
< |
* already exist in the List |
85 |
> |
* addAllAbsent adds each element from the given collection that did not |
86 |
> |
* already exist in the List |
87 |
|
*/ |
88 |
< |
public void testAddAllAbsent(){ |
89 |
< |
CopyOnWriteArrayList full = fullArray(3); |
90 |
< |
Vector v = new Vector(); |
91 |
< |
v.add(new Integer(3)); |
92 |
< |
v.add(new Integer(4)); |
93 |
< |
v.add(new Integer(1)); // will not add this element |
94 |
< |
full.addAllAbsent(v); |
95 |
< |
assertEquals(5, full.size()); |
88 |
> |
public void testAddAllAbsent() { |
89 |
> |
CopyOnWriteArrayList full = populatedArray(3); |
90 |
> |
Vector v = new Vector(); |
91 |
> |
v.add(three); |
92 |
> |
v.add(four); |
93 |
> |
v.add(one); // will not add this element |
94 |
> |
full.addAllAbsent(v); |
95 |
> |
assertEquals(5, full.size()); |
96 |
|
} |
97 |
|
|
98 |
|
/** |
99 |
< |
* Test to verify addIfAbsent will not add the element if it already exists in the list |
99 |
> |
* addIfAbsent will not add the element if it already exists in the list |
100 |
|
*/ |
101 |
< |
public void testAddIfAbsent(){ |
102 |
< |
CopyOnWriteArrayList full = fullArray(3); |
103 |
< |
full.addIfAbsent(new Integer(1)); |
104 |
< |
assertEquals(3, full.size()); |
101 |
> |
public void testAddIfAbsent() { |
102 |
> |
CopyOnWriteArrayList full = populatedArray(SIZE); |
103 |
> |
full.addIfAbsent(one); |
104 |
> |
assertEquals(SIZE, full.size()); |
105 |
|
} |
106 |
|
|
107 |
|
/** |
108 |
< |
* test to verify addIfAbsent correctly adds the element when it does not exist in the list |
108 |
> |
* addIfAbsent adds the element when it does not exist in the list |
109 |
|
*/ |
110 |
< |
public void testAddIfAbsent2(){ |
111 |
< |
CopyOnWriteArrayList full = fullArray(3); |
112 |
< |
full.addIfAbsent(new Integer(3)); |
113 |
< |
assertTrue(full.contains(new Integer(3))); |
110 |
> |
public void testAddIfAbsent2() { |
111 |
> |
CopyOnWriteArrayList full = populatedArray(SIZE); |
112 |
> |
full.addIfAbsent(three); |
113 |
> |
assertTrue(full.contains(three)); |
114 |
|
} |
115 |
|
|
116 |
|
/** |
117 |
< |
* Test to verify clear correctly removes all elements from the list |
117 |
> |
* clear removes all elements from the list |
118 |
|
*/ |
119 |
< |
public void testClear(){ |
120 |
< |
CopyOnWriteArrayList full = fullArray(3); |
121 |
< |
full.clear(); |
122 |
< |
assertEquals(0, full.size()); |
119 |
> |
public void testClear() { |
120 |
> |
CopyOnWriteArrayList full = populatedArray(SIZE); |
121 |
> |
full.clear(); |
122 |
> |
assertEquals(0, full.size()); |
123 |
|
} |
124 |
|
|
125 |
|
/** |
126 |
< |
* Test to verify contains returns the correct values |
126 |
> |
* Cloned list is equal |
127 |
|
*/ |
128 |
< |
public void testContains(){ |
129 |
< |
CopyOnWriteArrayList full = fullArray(3); |
130 |
< |
assertTrue(full.contains(new Integer(1))); |
131 |
< |
assertFalse(full.contains(new Integer(5))); |
128 |
> |
public void testClone() { |
129 |
> |
CopyOnWriteArrayList l1 = populatedArray(SIZE); |
130 |
> |
CopyOnWriteArrayList l2 = (CopyOnWriteArrayList)(l1.clone()); |
131 |
> |
assertEquals(l1, l2); |
132 |
> |
l1.clear(); |
133 |
> |
assertFalse(l1.equals(l2)); |
134 |
|
} |
135 |
|
|
136 |
+ |
/** |
137 |
+ |
* contains is true for added elements |
138 |
+ |
*/ |
139 |
+ |
public void testContains() { |
140 |
+ |
CopyOnWriteArrayList full = populatedArray(3); |
141 |
+ |
assertTrue(full.contains(one)); |
142 |
+ |
assertFalse(full.contains(five)); |
143 |
+ |
} |
144 |
+ |
|
145 |
+ |
/** |
146 |
+ |
* adding at an index places it in the indicated index |
147 |
+ |
*/ |
148 |
|
public void testAddIndex() { |
149 |
< |
CopyOnWriteArrayList full = fullArray(3); |
150 |
< |
full.add(0, new Integer(-1)); |
149 |
> |
CopyOnWriteArrayList full = populatedArray(3); |
150 |
> |
full.add(0, m1); |
151 |
|
assertEquals(4, full.size()); |
152 |
< |
assertEquals(new Integer(-1), full.get(0)); |
153 |
< |
assertEquals(new Integer(0), full.get(1)); |
152 |
> |
assertEquals(m1, full.get(0)); |
153 |
> |
assertEquals(zero, full.get(1)); |
154 |
|
|
155 |
< |
full.add(2, new Integer(-2)); |
155 |
> |
full.add(2, m2); |
156 |
|
assertEquals(5, full.size()); |
157 |
< |
assertEquals(new Integer(-2), full.get(2)); |
158 |
< |
assertEquals(new Integer(2), full.get(4)); |
157 |
> |
assertEquals(m2, full.get(2)); |
158 |
> |
assertEquals(two, full.get(4)); |
159 |
|
} |
160 |
|
|
161 |
+ |
/** |
162 |
+ |
* lists with same elements are equal and have same hashCode |
163 |
+ |
*/ |
164 |
|
public void testEquals() { |
165 |
< |
CopyOnWriteArrayList a = fullArray(3); |
166 |
< |
CopyOnWriteArrayList b = fullArray(3); |
165 |
> |
CopyOnWriteArrayList a = populatedArray(3); |
166 |
> |
CopyOnWriteArrayList b = populatedArray(3); |
167 |
|
assertTrue(a.equals(b)); |
168 |
|
assertTrue(b.equals(a)); |
169 |
|
assertEquals(a.hashCode(), b.hashCode()); |
170 |
< |
a.add(new Integer(-1)); |
170 |
> |
a.add(m1); |
171 |
|
assertFalse(a.equals(b)); |
172 |
|
assertFalse(b.equals(a)); |
173 |
< |
b.add(new Integer(-1)); |
173 |
> |
b.add(m1); |
174 |
|
assertTrue(a.equals(b)); |
175 |
|
assertTrue(b.equals(a)); |
176 |
|
assertEquals(a.hashCode(), b.hashCode()); |
177 |
|
} |
178 |
|
|
123 |
– |
|
179 |
|
/** |
180 |
< |
* Test to verify containsAll returns the correct values |
180 |
> |
* containsAll returns true for collection with subset of elements |
181 |
|
*/ |
182 |
< |
public void testContainsAll(){ |
183 |
< |
CopyOnWriteArrayList full = fullArray(3); |
184 |
< |
Vector v = new Vector(); |
185 |
< |
v.add(new Integer(1)); |
186 |
< |
v.add(new Integer(2)); |
187 |
< |
assertTrue(full.containsAll(v)); |
188 |
< |
v.add(new Integer(6)); |
189 |
< |
assertFalse(full.containsAll(v)); |
182 |
> |
public void testContainsAll() { |
183 |
> |
CopyOnWriteArrayList full = populatedArray(3); |
184 |
> |
Vector v = new Vector(); |
185 |
> |
v.add(one); |
186 |
> |
v.add(two); |
187 |
> |
assertTrue(full.containsAll(v)); |
188 |
> |
v.add(six); |
189 |
> |
assertFalse(full.containsAll(v)); |
190 |
|
} |
191 |
|
|
192 |
|
/** |
193 |
< |
* Test to verify get returns the correct value for the given index |
193 |
> |
* get returns the value at the given index |
194 |
|
*/ |
195 |
< |
public void testGet(){ |
196 |
< |
CopyOnWriteArrayList full = fullArray(3); |
197 |
< |
assertEquals(0, ((Integer)full.get(0)).intValue()); |
195 |
> |
public void testGet() { |
196 |
> |
CopyOnWriteArrayList full = populatedArray(3); |
197 |
> |
assertEquals(0, full.get(0)); |
198 |
|
} |
199 |
|
|
200 |
|
/** |
201 |
< |
* Test to verify indexOf gives the correct index for the given object |
201 |
> |
* indexOf gives the index for the given object |
202 |
|
*/ |
203 |
< |
public void testIndexOf(){ |
204 |
< |
CopyOnWriteArrayList full = fullArray(3); |
205 |
< |
assertEquals(1, full.indexOf(new Integer(1))); |
206 |
< |
assertEquals(-1, full.indexOf("puppies")); |
203 |
> |
public void testIndexOf() { |
204 |
> |
CopyOnWriteArrayList full = populatedArray(3); |
205 |
> |
assertEquals(1, full.indexOf(one)); |
206 |
> |
assertEquals(-1, full.indexOf("puppies")); |
207 |
|
} |
208 |
|
|
209 |
|
/** |
210 |
< |
* Test to verify indexOf gives the correct index based on the given index |
211 |
< |
* at which to start searching |
210 |
> |
* indexOf gives the index based on the given index |
211 |
> |
* at which to start searching |
212 |
|
*/ |
213 |
< |
public void testIndexOf2(){ |
214 |
< |
CopyOnWriteArrayList full = fullArray(3); |
215 |
< |
assertEquals(1, full.indexOf(new Integer(1), 0)); |
216 |
< |
assertEquals(-1, full.indexOf(new Integer(1), 2)); |
213 |
> |
public void testIndexOf2() { |
214 |
> |
CopyOnWriteArrayList full = populatedArray(3); |
215 |
> |
assertEquals(1, full.indexOf(one, 0)); |
216 |
> |
assertEquals(-1, full.indexOf(one, 2)); |
217 |
|
} |
218 |
|
|
219 |
|
/** |
220 |
< |
* Test to verify isEmpty returns the correct values |
220 |
> |
* isEmpty returns true when empty, else false |
221 |
|
*/ |
222 |
< |
public void testIsEmpty(){ |
223 |
< |
CopyOnWriteArrayList empty = new CopyOnWriteArrayList(); |
224 |
< |
CopyOnWriteArrayList full = fullArray(3); |
225 |
< |
assertTrue(empty.isEmpty()); |
226 |
< |
assertFalse(full.isEmpty()); |
222 |
> |
public void testIsEmpty() { |
223 |
> |
CopyOnWriteArrayList empty = new CopyOnWriteArrayList(); |
224 |
> |
CopyOnWriteArrayList full = populatedArray(SIZE); |
225 |
> |
assertTrue(empty.isEmpty()); |
226 |
> |
assertFalse(full.isEmpty()); |
227 |
|
} |
228 |
|
|
229 |
|
/** |
230 |
< |
* Test to verify iterator() returns an iterator containing the elements of the list |
230 |
> |
* iterator() returns an iterator containing the elements of the list |
231 |
|
*/ |
232 |
< |
public void testIterator(){ |
233 |
< |
CopyOnWriteArrayList full = fullArray(3); |
234 |
< |
Iterator i = full.iterator(); |
235 |
< |
int j; |
236 |
< |
for(j = 0; i.hasNext(); j++) |
237 |
< |
assertEquals(j, ((Integer)i.next()).intValue()); |
238 |
< |
assertEquals(3, j); |
232 |
> |
public void testIterator() { |
233 |
> |
CopyOnWriteArrayList full = populatedArray(SIZE); |
234 |
> |
Iterator i = full.iterator(); |
235 |
> |
int j; |
236 |
> |
for (j = 0; i.hasNext(); j++) |
237 |
> |
assertEquals(j, i.next()); |
238 |
> |
assertEquals(SIZE, j); |
239 |
|
} |
240 |
|
|
241 |
< |
public void testIteratorRemove () { |
242 |
< |
CopyOnWriteArrayList full = fullArray(3); |
241 |
> |
/** |
242 |
> |
* iterator.remove throws UnsupportedOperationException |
243 |
> |
*/ |
244 |
> |
public void testIteratorRemove() { |
245 |
> |
CopyOnWriteArrayList full = populatedArray(SIZE); |
246 |
|
Iterator it = full.iterator(); |
247 |
|
it.next(); |
248 |
|
try { |
249 |
|
it.remove(); |
250 |
< |
fail("should throw"); |
251 |
< |
} |
194 |
< |
catch (UnsupportedOperationException success) {} |
250 |
> |
shouldThrow(); |
251 |
> |
} catch (UnsupportedOperationException success) {} |
252 |
|
} |
253 |
|
|
254 |
< |
public void testToString(){ |
255 |
< |
CopyOnWriteArrayList full = fullArray(3); |
254 |
> |
/** |
255 |
> |
* toString contains toString of elements |
256 |
> |
*/ |
257 |
> |
public void testToString() { |
258 |
> |
CopyOnWriteArrayList full = populatedArray(3); |
259 |
|
String s = full.toString(); |
260 |
|
for (int i = 0; i < 3; ++i) { |
261 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
261 |
> |
assertTrue(s.contains(String.valueOf(i))); |
262 |
|
} |
263 |
< |
} |
263 |
> |
} |
264 |
|
|
265 |
|
/** |
266 |
< |
* Test to verify lastIndexOf returns the correct index for the given object |
266 |
> |
* lastIndexOf returns the index for the given object |
267 |
|
*/ |
268 |
< |
public void testLastIndexOf1(){ |
269 |
< |
CopyOnWriteArrayList full = fullArray(3); |
270 |
< |
full.add(new Integer(1)); |
271 |
< |
full.add(new Integer(3)); |
272 |
< |
assertEquals(3, full.lastIndexOf(new Integer(1))); |
273 |
< |
assertEquals(-1, full.lastIndexOf(new Integer(6))); |
268 |
> |
public void testLastIndexOf1() { |
269 |
> |
CopyOnWriteArrayList full = populatedArray(3); |
270 |
> |
full.add(one); |
271 |
> |
full.add(three); |
272 |
> |
assertEquals(3, full.lastIndexOf(one)); |
273 |
> |
assertEquals(-1, full.lastIndexOf(six)); |
274 |
|
} |
275 |
|
|
276 |
|
/** |
277 |
< |
* Test to verify lastIndexOf returns the correct index from the given starting point |
277 |
> |
* lastIndexOf returns the index from the given starting point |
278 |
|
*/ |
279 |
< |
public void testlastIndexOf2(){ |
280 |
< |
CopyOnWriteArrayList full = fullArray(3); |
281 |
< |
full.add(new Integer(1)); |
282 |
< |
full.add(new Integer(3)); |
283 |
< |
assertEquals(3, full.lastIndexOf(new Integer(1), 4)); |
284 |
< |
assertEquals(-1, full.lastIndexOf(new Integer(3), 3)); |
279 |
> |
public void testLastIndexOf2() { |
280 |
> |
CopyOnWriteArrayList full = populatedArray(3); |
281 |
> |
full.add(one); |
282 |
> |
full.add(three); |
283 |
> |
assertEquals(3, full.lastIndexOf(one, 4)); |
284 |
> |
assertEquals(-1, full.lastIndexOf(three, 3)); |
285 |
|
} |
286 |
|
|
287 |
|
/** |
288 |
< |
* Identical to testIterator, except ListInterator has more functionality |
288 |
> |
* listIterator traverses all elements |
289 |
|
*/ |
290 |
< |
public void testListIterator1(){ |
291 |
< |
CopyOnWriteArrayList full = fullArray(3); |
292 |
< |
ListIterator i = full.listIterator(); |
293 |
< |
int j; |
294 |
< |
for(j = 0; i.hasNext(); j++) |
295 |
< |
assertEquals(j, ((Integer)i.next()).intValue()); |
296 |
< |
assertEquals(3, j); |
290 |
> |
public void testListIterator1() { |
291 |
> |
CopyOnWriteArrayList full = populatedArray(SIZE); |
292 |
> |
ListIterator i = full.listIterator(); |
293 |
> |
int j; |
294 |
> |
for (j = 0; i.hasNext(); j++) |
295 |
> |
assertEquals(j, i.next()); |
296 |
> |
assertEquals(SIZE, j); |
297 |
|
} |
298 |
|
|
299 |
|
/** |
300 |
< |
* Identical to testIterator and testListIterator1, but only returns those elements |
241 |
< |
* after the given index |
300 |
> |
* listIterator only returns those elements after the given index |
301 |
|
*/ |
302 |
< |
public void testListIterator2(){ |
303 |
< |
CopyOnWriteArrayList full = fullArray(3); |
304 |
< |
ListIterator i = full.listIterator(1); |
305 |
< |
int j; |
306 |
< |
for(j = 0; i.hasNext(); j++) |
307 |
< |
assertEquals(j+1, ((Integer)i.next()).intValue()); |
308 |
< |
assertEquals(2, j); |
302 |
> |
public void testListIterator2() { |
303 |
> |
CopyOnWriteArrayList full = populatedArray(3); |
304 |
> |
ListIterator i = full.listIterator(1); |
305 |
> |
int j; |
306 |
> |
for (j = 0; i.hasNext(); j++) |
307 |
> |
assertEquals(j+1, i.next()); |
308 |
> |
assertEquals(2, j); |
309 |
|
} |
310 |
|
|
311 |
|
/** |
312 |
< |
* Test to verify remove correctly removes and returns the object at the given index |
312 |
> |
* remove removes and returns the object at the given index |
313 |
|
*/ |
314 |
< |
public void testRemove(){ |
315 |
< |
CopyOnWriteArrayList full = fullArray(3); |
316 |
< |
assertEquals(new Integer(2), full.remove(2)); |
317 |
< |
assertEquals(2, full.size()); |
314 |
> |
public void testRemove() { |
315 |
> |
CopyOnWriteArrayList full = populatedArray(3); |
316 |
> |
assertEquals(2, full.remove(2)); |
317 |
> |
assertEquals(2, full.size()); |
318 |
|
} |
319 |
|
|
320 |
|
/** |
321 |
< |
* Test to verify removeAll correctly removes all elements from the given collection |
321 |
> |
* removeAll removes all elements from the given collection |
322 |
|
*/ |
323 |
< |
public void testRemoveAll(){ |
324 |
< |
CopyOnWriteArrayList full = fullArray(3); |
325 |
< |
Vector v = new Vector(); |
326 |
< |
v.add(new Integer(1)); |
327 |
< |
v.add(new Integer(2)); |
328 |
< |
full.removeAll(v); |
329 |
< |
assertEquals(1, full.size()); |
323 |
> |
public void testRemoveAll() { |
324 |
> |
CopyOnWriteArrayList full = populatedArray(3); |
325 |
> |
Vector v = new Vector(); |
326 |
> |
v.add(one); |
327 |
> |
v.add(two); |
328 |
> |
full.removeAll(v); |
329 |
> |
assertEquals(1, full.size()); |
330 |
|
} |
331 |
|
|
332 |
|
/** |
333 |
< |
* Test to verify set correctly changes the element at the given index |
333 |
> |
* set changes the element at the given index |
334 |
|
*/ |
335 |
< |
public void testSet(){ |
336 |
< |
CopyOnWriteArrayList full = fullArray(3); |
337 |
< |
assertEquals(new Integer(2), full.set(2, new Integer(4))); |
338 |
< |
assertEquals(4, ((Integer)full.get(2)).intValue()); |
335 |
> |
public void testSet() { |
336 |
> |
CopyOnWriteArrayList full = populatedArray(3); |
337 |
> |
assertEquals(2, full.set(2, four)); |
338 |
> |
assertEquals(4, full.get(2)); |
339 |
|
} |
340 |
|
|
341 |
|
/** |
342 |
< |
* Test to verify size returns the correct values |
342 |
> |
* size returns the number of elements |
343 |
|
*/ |
344 |
< |
public void testSize(){ |
345 |
< |
CopyOnWriteArrayList empty = new CopyOnWriteArrayList(); |
346 |
< |
CopyOnWriteArrayList full = fullArray(3); |
347 |
< |
assertEquals(3, full.size()); |
348 |
< |
assertEquals(0, empty.size()); |
344 |
> |
public void testSize() { |
345 |
> |
CopyOnWriteArrayList empty = new CopyOnWriteArrayList(); |
346 |
> |
CopyOnWriteArrayList full = populatedArray(SIZE); |
347 |
> |
assertEquals(SIZE, full.size()); |
348 |
> |
assertEquals(0, empty.size()); |
349 |
|
} |
350 |
|
|
351 |
|
/** |
352 |
< |
* Test to verify toArray returns an Object array containing all elements from the list |
352 |
> |
* toArray returns an Object array containing all elements from the list |
353 |
|
*/ |
354 |
< |
public void testToArray(){ |
355 |
< |
CopyOnWriteArrayList full = fullArray(3); |
356 |
< |
Object[] o = full.toArray(); |
357 |
< |
assertEquals(3, o.length); |
358 |
< |
assertEquals(0, ((Integer)o[0]).intValue()); |
359 |
< |
assertEquals(1, ((Integer)o[1]).intValue()); |
360 |
< |
assertEquals(2, ((Integer)o[2]).intValue()); |
354 |
> |
public void testToArray() { |
355 |
> |
CopyOnWriteArrayList full = populatedArray(3); |
356 |
> |
Object[] o = full.toArray(); |
357 |
> |
assertEquals(3, o.length); |
358 |
> |
assertEquals(0, o[0]); |
359 |
> |
assertEquals(1, o[1]); |
360 |
> |
assertEquals(2, o[2]); |
361 |
|
} |
362 |
|
|
363 |
|
/** |
364 |
< |
* test to verify toArray returns an Integer array containing all elements from the list |
364 |
> |
* toArray returns an Integer array containing all elements from |
365 |
> |
* the list |
366 |
|
*/ |
367 |
< |
public void testToArray2(){ |
368 |
< |
CopyOnWriteArrayList full = fullArray(3); |
369 |
< |
Integer[] i = new Integer[3]; |
370 |
< |
i = (Integer[])full.toArray(i); |
371 |
< |
assertEquals(3, i.length); |
372 |
< |
assertEquals(0, i[0].intValue()); |
373 |
< |
assertEquals(1, i[1].intValue()); |
374 |
< |
assertEquals(2, i[2].intValue()); |
367 |
> |
public void testToArray2() { |
368 |
> |
final int size = 3; |
369 |
> |
CopyOnWriteArrayList<Integer> full = populatedArray(size); |
370 |
> |
Integer[] ints = new Integer[size]; |
371 |
> |
assertSame(ints, full.toArray(ints)); |
372 |
> |
Iterator<Integer> it = full.iterator(); |
373 |
> |
for (int i = 0; i < size; i++) |
374 |
> |
assertSame(ints[i], it.next()); |
375 |
> |
assertFalse(it.hasNext()); |
376 |
|
} |
377 |
|
|
378 |
< |
|
378 |
> |
/** |
379 |
> |
* sublists contains elements at indexes offset from their base |
380 |
> |
*/ |
381 |
|
public void testSubList() { |
382 |
< |
CopyOnWriteArrayList a = fullArray(10); |
382 |
> |
CopyOnWriteArrayList a = populatedArray(10); |
383 |
|
assertTrue(a.subList(1,1).isEmpty()); |
384 |
< |
for(int j = 0; j < 9; ++j) { |
385 |
< |
for(int i = j ; i < 10; ++i) { |
386 |
< |
List b = a.subList(j,i); |
387 |
< |
for(int k = j; k < i; ++k) { |
388 |
< |
assertEquals(new Integer(k), b.get(k-j)); |
389 |
< |
} |
390 |
< |
} |
391 |
< |
} |
392 |
< |
|
393 |
< |
Integer m1 = new Integer(-1); |
394 |
< |
List s = a.subList(2, 5); |
395 |
< |
assertEquals(s.size(), 3); |
333 |
< |
s.set(2, new Integer(m1)); |
384 |
> |
for (int j = 0; j < 9; ++j) { |
385 |
> |
for (int i = j ; i < 10; ++i) { |
386 |
> |
List b = a.subList(j,i); |
387 |
> |
for (int k = j; k < i; ++k) { |
388 |
> |
assertEquals(new Integer(k), b.get(k-j)); |
389 |
> |
} |
390 |
> |
} |
391 |
> |
} |
392 |
> |
|
393 |
> |
List s = a.subList(2, 5); |
394 |
> |
assertEquals(3, s.size()); |
395 |
> |
s.set(2, m1); |
396 |
|
assertEquals(a.get(4), m1); |
397 |
< |
s.clear(); |
398 |
< |
assertEquals(a.size(), 7); |
397 |
> |
s.clear(); |
398 |
> |
assertEquals(7, a.size()); |
399 |
|
} |
400 |
|
|
401 |
|
// Exception tests |
402 |
|
|
403 |
|
/** |
404 |
< |
* Test to verify toArray throws an ArrayStoreException when the given array |
405 |
< |
* can not store the objects inside the list |
404 |
> |
* toArray throws an ArrayStoreException when the given array |
405 |
> |
* can not store the objects inside the list |
406 |
|
*/ |
407 |
< |
public void testToArray_ArrayStoreException(){ |
408 |
< |
try{ |
407 |
> |
public void testToArray_ArrayStoreException() { |
408 |
> |
try { |
409 |
|
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
410 |
|
c.add("zfasdfsdf"); |
411 |
|
c.add("asdadasd"); |
412 |
|
c.toArray(new Long[5]); |
413 |
< |
fail("Object[] toArray(Object[]) should throw ArrayStoreException"); |
414 |
< |
}catch(ArrayStoreException e){} |
413 |
> |
shouldThrow(); |
414 |
> |
} catch (ArrayStoreException success) {} |
415 |
|
} |
416 |
|
|
417 |
|
/** |
418 |
< |
* Test to verify get throws an IndexOutOfBoundsException on a negative index |
418 |
> |
* get throws an IndexOutOfBoundsException on a negative index |
419 |
|
*/ |
420 |
< |
public void testGet1_IndexOutOfBoundsException(){ |
421 |
< |
try{ |
420 |
> |
public void testGet1_IndexOutOfBoundsException() { |
421 |
> |
try { |
422 |
|
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
423 |
|
c.get(-1); |
424 |
< |
fail("Object get(int) should throw IndexOutOfBounds exception"); |
425 |
< |
}catch(IndexOutOfBoundsException e){} |
424 |
> |
shouldThrow(); |
425 |
> |
} catch (IndexOutOfBoundsException success) {} |
426 |
|
} |
427 |
< |
|
427 |
> |
|
428 |
|
/** |
429 |
< |
* Test to verify get throws an IndexOutOfBoundsException on a too high index |
429 |
> |
* get throws an IndexOutOfBoundsException on a too high index |
430 |
|
*/ |
431 |
< |
public void testGet2_IndexOutOfBoundsException(){ |
432 |
< |
try{ |
431 |
> |
public void testGet2_IndexOutOfBoundsException() { |
432 |
> |
try { |
433 |
|
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
434 |
|
c.add("asdasd"); |
435 |
|
c.add("asdad"); |
436 |
|
c.get(100); |
437 |
< |
fail("Object get(int) should throw IndexOutOfBounds exception"); |
438 |
< |
}catch(IndexOutOfBoundsException e){} |
437 |
> |
shouldThrow(); |
438 |
> |
} catch (IndexOutOfBoundsException success) {} |
439 |
|
} |
440 |
|
|
441 |
|
/** |
442 |
< |
* Test to verify set throws an IndexOutOfBoundsException on a negative index |
442 |
> |
* set throws an IndexOutOfBoundsException on a negative index |
443 |
|
*/ |
444 |
< |
public void testSet1_IndexOutOfBoundsException(){ |
445 |
< |
try{ |
444 |
> |
public void testSet1_IndexOutOfBoundsException() { |
445 |
> |
try { |
446 |
|
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
447 |
|
c.set(-1,"qwerty"); |
448 |
< |
fail("Object get(int, Object) should throw IndexOutOfBounds exception"); |
449 |
< |
}catch(IndexOutOfBoundsException e){} |
448 |
> |
shouldThrow(); |
449 |
> |
} catch (IndexOutOfBoundsException success) {} |
450 |
|
} |
451 |
< |
|
451 |
> |
|
452 |
|
/** |
453 |
< |
* Test to verify set throws an IndexOutOfBoundsException on a too high index |
453 |
> |
* set throws an IndexOutOfBoundsException on a too high index |
454 |
|
*/ |
455 |
< |
public void testSet2(){ |
456 |
< |
try{ |
455 |
> |
public void testSet2() { |
456 |
> |
try { |
457 |
|
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
458 |
|
c.add("asdasd"); |
459 |
|
c.add("asdad"); |
460 |
|
c.set(100, "qwerty"); |
461 |
< |
fail("Object set(int, Object) should throw IndexOutOfBounds exception"); |
462 |
< |
}catch(IndexOutOfBoundsException e){} |
461 |
> |
shouldThrow(); |
462 |
> |
} catch (IndexOutOfBoundsException success) {} |
463 |
|
} |
464 |
|
|
465 |
|
/** |
466 |
< |
* Test to verify add throws an IndexOutOfBoundsException on a negative index |
466 |
> |
* add throws an IndexOutOfBoundsException on a negative index |
467 |
|
*/ |
468 |
< |
public void testAdd1_IndexOutOfBoundsException(){ |
469 |
< |
try{ |
468 |
> |
public void testAdd1_IndexOutOfBoundsException() { |
469 |
> |
try { |
470 |
|
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
471 |
|
c.add(-1,"qwerty"); |
472 |
< |
fail("void add(int, Object) should throw IndexOutOfBounds exception"); |
473 |
< |
}catch(IndexOutOfBoundsException e){} |
472 |
> |
shouldThrow(); |
473 |
> |
} catch (IndexOutOfBoundsException success) {} |
474 |
|
} |
475 |
< |
|
475 |
> |
|
476 |
|
/** |
477 |
< |
* Test to verify add throws an IndexOutOfBoundsException on a too high index |
477 |
> |
* add throws an IndexOutOfBoundsException on a too high index |
478 |
|
*/ |
479 |
< |
public void testAdd2_IndexOutOfBoundsException(){ |
480 |
< |
try{ |
479 |
> |
public void testAdd2_IndexOutOfBoundsException() { |
480 |
> |
try { |
481 |
|
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
482 |
|
c.add("asdasd"); |
483 |
|
c.add("asdasdasd"); |
484 |
|
c.add(100, "qwerty"); |
485 |
< |
fail("void add(int, Object) should throw IndexOutOfBounds exception"); |
486 |
< |
}catch(IndexOutOfBoundsException e){} |
485 |
> |
shouldThrow(); |
486 |
> |
} catch (IndexOutOfBoundsException success) {} |
487 |
|
} |
488 |
|
|
489 |
|
/** |
490 |
< |
* Test to verify remove throws an IndexOutOfBoundsException on a negative index |
490 |
> |
* remove throws an IndexOutOfBoundsException on a negative index |
491 |
|
*/ |
492 |
< |
public void testRemove1_IndexOutOfBounds(){ |
493 |
< |
try{ |
492 |
> |
public void testRemove1_IndexOutOfBounds() { |
493 |
> |
try { |
494 |
|
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
495 |
|
c.remove(-1); |
496 |
< |
fail("Object remove(int) should throw IndexOutOfBounds exception"); |
497 |
< |
}catch(IndexOutOfBoundsException e){} |
496 |
> |
shouldThrow(); |
497 |
> |
} catch (IndexOutOfBoundsException success) {} |
498 |
|
} |
499 |
|
|
500 |
|
/** |
501 |
< |
* Test to verify remove throws an IndexOutOfBoundsException on a too high index |
501 |
> |
* remove throws an IndexOutOfBoundsException on a too high index |
502 |
|
*/ |
503 |
< |
public void testRemove2_IndexOutOfBounds(){ |
504 |
< |
try{ |
503 |
> |
public void testRemove2_IndexOutOfBounds() { |
504 |
> |
try { |
505 |
|
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
506 |
|
c.add("asdasd"); |
507 |
|
c.add("adasdasd"); |
508 |
|
c.remove(100); |
509 |
< |
fail("Object remove(int) should throw IndexOutOfBounds exception"); |
510 |
< |
}catch(IndexOutOfBoundsException e){} |
509 |
> |
shouldThrow(); |
510 |
> |
} catch (IndexOutOfBoundsException success) {} |
511 |
|
} |
512 |
< |
|
512 |
> |
|
513 |
|
/** |
514 |
< |
* Test to verify addAll throws an IndexOutOfBoundsException on a negative index |
514 |
> |
* addAll throws an IndexOutOfBoundsException on a negative index |
515 |
|
*/ |
516 |
< |
public void testAddAll1_IndexOutOfBoundsException(){ |
517 |
< |
try{ |
516 |
> |
public void testAddAll1_IndexOutOfBoundsException() { |
517 |
> |
try { |
518 |
|
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
519 |
|
c.addAll(-1,new LinkedList()); |
520 |
< |
fail("boolean add(int, Collection) should throw IndexOutOfBounds exception"); |
521 |
< |
}catch(IndexOutOfBoundsException e){} |
520 |
> |
shouldThrow(); |
521 |
> |
} catch (IndexOutOfBoundsException success) {} |
522 |
|
} |
523 |
< |
|
523 |
> |
|
524 |
|
/** |
525 |
< |
* Test to verify addAll throws an IndexOutOfBoundsException on a too high index |
525 |
> |
* addAll throws an IndexOutOfBoundsException on a too high index |
526 |
|
*/ |
527 |
< |
public void testAddAll2_IndexOutOfBoundsException(){ |
528 |
< |
try{ |
527 |
> |
public void testAddAll2_IndexOutOfBoundsException() { |
528 |
> |
try { |
529 |
|
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
530 |
|
c.add("asdasd"); |
531 |
|
c.add("asdasdasd"); |
532 |
|
c.addAll(100, new LinkedList()); |
533 |
< |
fail("boolean addAll(int, Collection) should throw IndexOutOfBounds exception"); |
534 |
< |
}catch(IndexOutOfBoundsException e){} |
533 |
> |
shouldThrow(); |
534 |
> |
} catch (IndexOutOfBoundsException success) {} |
535 |
|
} |
536 |
|
|
537 |
|
/** |
538 |
< |
* Test to verify listIterator throws an IndexOutOfBoundsException on a negative index |
538 |
> |
* listIterator throws an IndexOutOfBoundsException on a negative index |
539 |
|
*/ |
540 |
< |
public void testListIterator1_IndexOutOfBoundsException(){ |
541 |
< |
try{ |
540 |
> |
public void testListIterator1_IndexOutOfBoundsException() { |
541 |
> |
try { |
542 |
|
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
543 |
|
c.listIterator(-1); |
544 |
< |
fail("ListIterator listIterator(int) should throw IndexOutOfBounds exceptione"); |
545 |
< |
}catch(IndexOutOfBoundsException e){} |
544 |
> |
shouldThrow(); |
545 |
> |
} catch (IndexOutOfBoundsException success) {} |
546 |
|
} |
547 |
|
|
548 |
|
/** |
549 |
< |
* Test to verify listIterator throws an IndexOutOfBoundsException on a too high index |
549 |
> |
* listIterator throws an IndexOutOfBoundsException on a too high index |
550 |
|
*/ |
551 |
< |
public void testListIterator2_IndexOutOfBoundsException(){ |
552 |
< |
try{ |
551 |
> |
public void testListIterator2_IndexOutOfBoundsException() { |
552 |
> |
try { |
553 |
|
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
554 |
|
c.add("adasd"); |
555 |
|
c.add("asdasdas"); |
556 |
|
c.listIterator(100); |
557 |
< |
fail("ListIterator listIterator(int) should throw IndexOutOfBounds exception"); |
558 |
< |
}catch(IndexOutOfBoundsException e){} |
557 |
> |
shouldThrow(); |
558 |
> |
} catch (IndexOutOfBoundsException success) {} |
559 |
|
} |
560 |
|
|
561 |
|
/** |
562 |
< |
* Test to verify subList throws an IndexOutOfBoundsException on a negative index |
562 |
> |
* subList throws an IndexOutOfBoundsException on a negative index |
563 |
|
*/ |
564 |
< |
public void testSubList1_IndexOutOfBoundsException(){ |
565 |
< |
try{ |
564 |
> |
public void testSubList1_IndexOutOfBoundsException() { |
565 |
> |
try { |
566 |
|
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
567 |
|
c.subList(-1,100); |
568 |
< |
|
569 |
< |
fail("List subList(int, int) should throw IndexOutofBounds exception"); |
508 |
< |
}catch(IndexOutOfBoundsException e){} |
568 |
> |
shouldThrow(); |
569 |
> |
} catch (IndexOutOfBoundsException success) {} |
570 |
|
} |
571 |
|
|
572 |
|
/** |
573 |
< |
* Test to verify subList throws an IndexOutOfBoundsException on a too high index |
573 |
> |
* subList throws an IndexOutOfBoundsException on a too high index |
574 |
|
*/ |
575 |
< |
public void testSubList2_IndexOutOfBoundsException(){ |
576 |
< |
try{ |
575 |
> |
public void testSubList2_IndexOutOfBoundsException() { |
576 |
> |
try { |
577 |
|
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
578 |
|
c.add("asdasd"); |
579 |
|
c.subList(1,100); |
580 |
< |
fail("List subList(int, int) should throw IndexOutofBounds exception"); |
581 |
< |
}catch(IndexOutOfBoundsException e){} |
580 |
> |
shouldThrow(); |
581 |
> |
} catch (IndexOutOfBoundsException success) {} |
582 |
|
} |
583 |
|
|
584 |
|
/** |
585 |
< |
* Test to verify subList throws IndexOutOfBoundsException when the second index |
586 |
< |
* is lower then the first |
585 |
> |
* subList throws IndexOutOfBoundsException when the second index |
586 |
> |
* is lower then the first |
587 |
|
*/ |
588 |
< |
public void testSubList3_IndexOutOfBoundsException(){ |
589 |
< |
try{ |
588 |
> |
public void testSubList3_IndexOutOfBoundsException() { |
589 |
> |
try { |
590 |
|
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
591 |
|
c.subList(3,1); |
592 |
+ |
shouldThrow(); |
593 |
+ |
} catch (IndexOutOfBoundsException success) {} |
594 |
+ |
} |
595 |
|
|
596 |
< |
fail("List subList(int, int) should throw IndexOutofBounds exception"); |
597 |
< |
}catch(IndexOutOfBoundsException e){} |
596 |
> |
/** |
597 |
> |
* a deserialized serialized list is equal |
598 |
> |
*/ |
599 |
> |
public void testSerialization() throws Exception { |
600 |
> |
List x = populatedArray(SIZE); |
601 |
> |
List y = serialClone(x); |
602 |
> |
|
603 |
> |
assertTrue(x != y); |
604 |
> |
assertEquals(x.size(), y.size()); |
605 |
> |
assertEquals(x.toString(), y.toString()); |
606 |
> |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
607 |
> |
assertEquals(x, y); |
608 |
> |
assertEquals(y, x); |
609 |
> |
while (!x.isEmpty()) { |
610 |
> |
assertFalse(y.isEmpty()); |
611 |
> |
assertEquals(x.remove(0), y.remove(0)); |
612 |
> |
} |
613 |
> |
assertTrue(y.isEmpty()); |
614 |
|
} |
615 |
< |
|
615 |
> |
|
616 |
|
} |