6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
– |
import junit.framework.*; |
9 |
|
import java.util.Arrays; |
10 |
|
import java.util.concurrent.atomic.AtomicReferenceArray; |
11 |
|
|
12 |
+ |
import junit.framework.Test; |
13 |
+ |
import junit.framework.TestSuite; |
14 |
+ |
|
15 |
|
public class AtomicReferenceArrayTest extends JSR166TestCase { |
16 |
|
public static void main(String[] args) { |
17 |
< |
junit.textui.TestRunner.run(suite()); |
17 |
> |
main(suite(), args); |
18 |
|
} |
19 |
|
public static Test suite() { |
20 |
|
return new TestSuite(AtomicReferenceArrayTest.class); |
24 |
|
* constructor creates array of given size with all elements null |
25 |
|
*/ |
26 |
|
public void testConstructor() { |
27 |
< |
AtomicReferenceArray<Integer> aa = new AtomicReferenceArray<Integer>(SIZE); |
27 |
> |
AtomicReferenceArray<Integer> aa = new AtomicReferenceArray<>(SIZE); |
28 |
|
for (int i = 0; i < SIZE; i++) { |
29 |
|
assertNull(aa.get(i)); |
30 |
|
} |
36 |
|
public void testConstructor2NPE() { |
37 |
|
try { |
38 |
|
Integer[] a = null; |
39 |
< |
AtomicReferenceArray<Integer> aa = new AtomicReferenceArray<Integer>(a); |
39 |
> |
new AtomicReferenceArray<Integer>(a); |
40 |
|
shouldThrow(); |
41 |
|
} catch (NullPointerException success) {} |
42 |
|
} |
46 |
|
*/ |
47 |
|
public void testConstructor2() { |
48 |
|
Integer[] a = { two, one, three, four, seven }; |
49 |
< |
AtomicReferenceArray<Integer> aa = new AtomicReferenceArray<Integer>(a); |
49 |
> |
AtomicReferenceArray<Integer> aa = new AtomicReferenceArray<>(a); |
50 |
|
assertEquals(a.length, aa.length()); |
51 |
|
for (int i = 0; i < a.length; i++) |
52 |
|
assertEquals(a[i], aa.get(i)); |
71 |
|
* get and set for out of bound indices throw IndexOutOfBoundsException |
72 |
|
*/ |
73 |
|
public void testIndexing() { |
74 |
< |
AtomicReferenceArray<Integer> aa = new AtomicReferenceArray<Integer>(SIZE); |
74 |
> |
AtomicReferenceArray<Integer> aa = new AtomicReferenceArray<>(SIZE); |
75 |
|
for (int index : new int[] { -1, SIZE }) { |
76 |
|
try { |
77 |
|
aa.get(index); |
171 |
|
AtomicReferenceArray aa = new AtomicReferenceArray(SIZE); |
172 |
|
for (int i = 0; i < SIZE; i++) { |
173 |
|
aa.set(i, one); |
174 |
< |
while (!aa.weakCompareAndSet(i, one, two)); |
175 |
< |
while (!aa.weakCompareAndSet(i, two, m4)); |
174 |
> |
do {} while (!aa.weakCompareAndSet(i, one, two)); |
175 |
> |
do {} while (!aa.weakCompareAndSet(i, two, m4)); |
176 |
|
assertSame(m4, aa.get(i)); |
177 |
< |
while (!aa.weakCompareAndSet(i, m4, seven)); |
177 |
> |
do {} while (!aa.weakCompareAndSet(i, m4, seven)); |
178 |
|
assertSame(seven, aa.get(i)); |
179 |
|
} |
180 |
|
} |
193 |
|
} |
194 |
|
|
195 |
|
/** |
196 |
< |
* a deserialized serialized array holds same values |
196 |
> |
* a deserialized/reserialized array holds same values in same order |
197 |
|
*/ |
198 |
|
public void testSerialization() throws Exception { |
199 |
|
AtomicReferenceArray x = new AtomicReferenceArray(SIZE); |
201 |
|
x.set(i, new Integer(-i)); |
202 |
|
} |
203 |
|
AtomicReferenceArray y = serialClone(x); |
204 |
< |
assertTrue(x != y); |
204 |
> |
assertNotSame(x, y); |
205 |
|
assertEquals(x.length(), y.length()); |
206 |
|
for (int i = 0; i < SIZE; i++) { |
207 |
|
assertEquals(x.get(i), y.get(i)); |
213 |
|
*/ |
214 |
|
public void testToString() { |
215 |
|
Integer[] a = { two, one, three, four, seven }; |
216 |
< |
AtomicReferenceArray<Integer> aa = new AtomicReferenceArray<Integer>(a); |
216 |
> |
AtomicReferenceArray<Integer> aa = new AtomicReferenceArray<>(a); |
217 |
|
assertEquals(Arrays.toString(a), aa.toString()); |
218 |
|
} |
219 |
+ |
|
220 |
|
} |