15 |
|
public class AtomicIntegerArrayTest extends JSR166TestCase { |
16 |
|
|
17 |
|
public static void main(String[] args) { |
18 |
< |
junit.textui.TestRunner.run(suite()); |
18 |
> |
main(suite(), args); |
19 |
|
} |
20 |
|
public static Test suite() { |
21 |
|
return new TestSuite(AtomicIntegerArrayTest.class); |
36 |
|
public void testConstructor2NPE() { |
37 |
|
try { |
38 |
|
int[] a = null; |
39 |
< |
AtomicIntegerArray aa = new AtomicIntegerArray(a); |
39 |
> |
new AtomicIntegerArray(a); |
40 |
|
shouldThrow(); |
41 |
|
} catch (NullPointerException success) {} |
42 |
|
} |
55 |
|
/** |
56 |
|
* get and set for out of bound indices throw IndexOutOfBoundsException |
57 |
|
*/ |
58 |
+ |
@SuppressWarnings("deprecation") |
59 |
|
public void testIndexing() { |
60 |
|
AtomicIntegerArray aa = new AtomicIntegerArray(SIZE); |
61 |
|
for (int index : new int[] { -1, SIZE }) { |
161 |
|
* repeated weakCompareAndSet succeeds in changing value when equal |
162 |
|
* to expected |
163 |
|
*/ |
164 |
+ |
@SuppressWarnings("deprecation") |
165 |
|
public void testWeakCompareAndSet() { |
166 |
|
AtomicIntegerArray aa = new AtomicIntegerArray(SIZE); |
167 |
|
for (int i = 0; i < SIZE; i++) { |
168 |
|
aa.set(i, 1); |
169 |
< |
while (!aa.weakCompareAndSet(i, 1, 2)); |
170 |
< |
while (!aa.weakCompareAndSet(i, 2, -4)); |
169 |
> |
do {} while (!aa.weakCompareAndSet(i, 1, 2)); |
170 |
> |
do {} while (!aa.weakCompareAndSet(i, 2, -4)); |
171 |
|
assertEquals(-4, aa.get(i)); |
172 |
< |
while (!aa.weakCompareAndSet(i, -4, 7)); |
172 |
> |
do {} while (!aa.weakCompareAndSet(i, -4, 7)); |
173 |
|
assertEquals(7, aa.get(i)); |
174 |
|
} |
175 |
|
} |
278 |
|
|
279 |
|
class Counter extends CheckedRunnable { |
280 |
|
final AtomicIntegerArray aa; |
281 |
< |
volatile int counts; |
281 |
> |
int decs; |
282 |
|
Counter(AtomicIntegerArray a) { aa = a; } |
283 |
|
public void realRun() { |
284 |
|
for (;;) { |
288 |
|
assertTrue(v >= 0); |
289 |
|
if (v != 0) { |
290 |
|
done = false; |
291 |
< |
if (aa.compareAndSet(i, v, v-1)) |
292 |
< |
++counts; |
291 |
> |
if (aa.compareAndSet(i, v, v - 1)) |
292 |
> |
decs++; |
293 |
|
} |
294 |
|
} |
295 |
|
if (done) |
309 |
|
aa.set(i, countdown); |
310 |
|
Counter c1 = new Counter(aa); |
311 |
|
Counter c2 = new Counter(aa); |
312 |
< |
Thread t1 = new Thread(c1); |
313 |
< |
Thread t2 = new Thread(c2); |
312 |
< |
t1.start(); |
313 |
< |
t2.start(); |
312 |
> |
Thread t1 = newStartedThread(c1); |
313 |
> |
Thread t2 = newStartedThread(c2); |
314 |
|
t1.join(); |
315 |
|
t2.join(); |
316 |
< |
assertEquals(c1.counts+c2.counts, SIZE * countdown); |
316 |
> |
assertEquals(c1.decs + c2.decs, SIZE * countdown); |
317 |
|
} |
318 |
|
|
319 |
|
/** |
320 |
< |
* a deserialized serialized array holds same values |
320 |
> |
* a deserialized/reserialized array holds same values in same order |
321 |
|
*/ |
322 |
|
public void testSerialization() throws Exception { |
323 |
|
AtomicIntegerArray x = new AtomicIntegerArray(SIZE); |