1 |
|
/* |
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/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
13 |
|
|
14 |
|
public class AtomicIntegerArrayTest extends JSR166TestCase { |
15 |
|
|
16 |
< |
public static void main (String[] args) { |
17 |
< |
junit.textui.TestRunner.run (suite()); |
16 |
> |
public static void main(String[] args) { |
17 |
> |
junit.textui.TestRunner.run(suite()); |
18 |
|
} |
19 |
|
public static Test suite() { |
20 |
|
return new TestSuite(AtomicIntegerArrayTest.class); |
21 |
|
} |
22 |
|
|
23 |
– |
|
23 |
|
/** |
24 |
|
* constructor creates array of given size with all elements zero |
25 |
|
*/ |
36 |
|
try { |
37 |
|
int[] a = null; |
38 |
|
AtomicIntegerArray ai = new AtomicIntegerArray(a); |
39 |
< |
} catch (NullPointerException success) { |
40 |
< |
} catch (Exception ex) { |
42 |
< |
unexpectedException(); |
43 |
< |
} |
39 |
> |
shouldThrow(); |
40 |
> |
} catch (NullPointerException success) {} |
41 |
|
} |
42 |
|
|
43 |
|
/** |
58 |
|
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
59 |
|
try { |
60 |
|
ai.get(SIZE); |
61 |
+ |
shouldThrow(); |
62 |
|
} catch (IndexOutOfBoundsException success) { |
63 |
|
} |
64 |
|
try { |
65 |
|
ai.get(-1); |
66 |
+ |
shouldThrow(); |
67 |
|
} catch (IndexOutOfBoundsException success) { |
68 |
|
} |
69 |
|
try { |
70 |
|
ai.set(SIZE, 0); |
71 |
+ |
shouldThrow(); |
72 |
|
} catch (IndexOutOfBoundsException success) { |
73 |
|
} |
74 |
|
try { |
75 |
|
ai.set(-1, 0); |
76 |
+ |
shouldThrow(); |
77 |
|
} catch (IndexOutOfBoundsException success) { |
78 |
|
} |
79 |
|
} |
119 |
|
assertTrue(ai.compareAndSet(i, 2,-4)); |
120 |
|
assertEquals(-4,ai.get(i)); |
121 |
|
assertFalse(ai.compareAndSet(i, -5,7)); |
122 |
< |
assertFalse((7 == ai.get(i))); |
122 |
> |
assertEquals(-4,ai.get(i)); |
123 |
|
assertTrue(ai.compareAndSet(i, -4,7)); |
124 |
|
assertEquals(7,ai.get(i)); |
125 |
|
} |
129 |
|
* compareAndSet in one thread enables another waiting for value |
130 |
|
* to succeed |
131 |
|
*/ |
132 |
< |
public void testCompareAndSetInMultipleThreads() { |
132 |
> |
public void testCompareAndSetInMultipleThreads() throws Exception { |
133 |
|
final AtomicIntegerArray a = new AtomicIntegerArray(1); |
134 |
|
a.set(0, 1); |
135 |
< |
Thread t = new Thread(new Runnable() { |
136 |
< |
public void run() { |
137 |
< |
while (!a.compareAndSet(0, 2, 3)) Thread.yield(); |
138 |
< |
}}); |
139 |
< |
try { |
140 |
< |
t.start(); |
141 |
< |
assertTrue(a.compareAndSet(0, 1, 2)); |
142 |
< |
t.join(LONG_DELAY_MS); |
143 |
< |
assertFalse(t.isAlive()); |
144 |
< |
assertEquals(a.get(0), 3); |
145 |
< |
} |
145 |
< |
catch (Exception e) { |
146 |
< |
unexpectedException(); |
147 |
< |
} |
135 |
> |
Thread t = new Thread(new CheckedRunnable() { |
136 |
> |
public void realRun() { |
137 |
> |
while (!a.compareAndSet(0, 2, 3)) |
138 |
> |
Thread.yield(); |
139 |
> |
}}); |
140 |
> |
|
141 |
> |
t.start(); |
142 |
> |
assertTrue(a.compareAndSet(0, 1, 2)); |
143 |
> |
t.join(LONG_DELAY_MS); |
144 |
> |
assertFalse(t.isAlive()); |
145 |
> |
assertEquals(a.get(0), 3); |
146 |
|
} |
147 |
|
|
148 |
|
/** |
162 |
|
} |
163 |
|
|
164 |
|
/** |
165 |
< |
* getAndSet returns previous value and sets to given value at given index |
165 |
> |
* getAndSet returns previous value and sets to given value at given index |
166 |
|
*/ |
167 |
|
public void testGetAndSet() { |
168 |
|
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
175 |
|
} |
176 |
|
|
177 |
|
/** |
178 |
< |
* getAndAdd returns previous value and adds given value |
178 |
> |
* getAndAdd returns previous value and adds given value |
179 |
|
*/ |
180 |
|
public void testGetAndAdd() { |
181 |
|
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
219 |
|
} |
220 |
|
|
221 |
|
/** |
222 |
< |
* addAndGet adds given value to current, and returns current value |
222 |
> |
* addAndGet adds given value to current, and returns current value |
223 |
|
*/ |
224 |
|
public void testAddAndGet() { |
225 |
|
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
247 |
|
} |
248 |
|
|
249 |
|
/** |
250 |
< |
* incrementAndGet increments and returns current value |
250 |
> |
* incrementAndGet increments and returns current value |
251 |
|
*/ |
252 |
|
public void testIncrementAndGet() { |
253 |
|
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
265 |
|
|
266 |
|
static final int COUNTDOWN = 100000; |
267 |
|
|
268 |
< |
class Counter implements Runnable { |
268 |
> |
class Counter extends CheckedRunnable { |
269 |
|
final AtomicIntegerArray ai; |
270 |
|
volatile int counts; |
271 |
|
Counter(AtomicIntegerArray a) { ai = a; } |
272 |
< |
public void run() { |
272 |
> |
public void realRun() { |
273 |
|
for (;;) { |
274 |
|
boolean done = true; |
275 |
|
for (int i = 0; i < ai.length(); ++i) { |
276 |
|
int v = ai.get(i); |
277 |
< |
threadAssertTrue(v >= 0); |
277 |
> |
assertTrue(v >= 0); |
278 |
|
if (v != 0) { |
279 |
|
done = false; |
280 |
|
if (ai.compareAndSet(i, v, v-1)) |
291 |
|
* Multiple threads using same array of counters successfully |
292 |
|
* update a number of times equal to total count |
293 |
|
*/ |
294 |
< |
public void testCountingInMultipleThreads() { |
295 |
< |
try { |
296 |
< |
final AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
297 |
< |
for (int i = 0; i < SIZE; ++i) |
298 |
< |
ai.set(i, COUNTDOWN); |
299 |
< |
Counter c1 = new Counter(ai); |
300 |
< |
Counter c2 = new Counter(ai); |
301 |
< |
Thread t1 = new Thread(c1); |
302 |
< |
Thread t2 = new Thread(c2); |
303 |
< |
t1.start(); |
304 |
< |
t2.start(); |
305 |
< |
t1.join(); |
306 |
< |
t2.join(); |
309 |
< |
assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN); |
310 |
< |
} |
311 |
< |
catch (InterruptedException ie) { |
312 |
< |
unexpectedException(); |
313 |
< |
} |
294 |
> |
public void testCountingInMultipleThreads() throws InterruptedException { |
295 |
> |
final AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
296 |
> |
for (int i = 0; i < SIZE; ++i) |
297 |
> |
ai.set(i, COUNTDOWN); |
298 |
> |
Counter c1 = new Counter(ai); |
299 |
> |
Counter c2 = new Counter(ai); |
300 |
> |
Thread t1 = new Thread(c1); |
301 |
> |
Thread t2 = new Thread(c2); |
302 |
> |
t1.start(); |
303 |
> |
t2.start(); |
304 |
> |
t1.join(); |
305 |
> |
t2.join(); |
306 |
> |
assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN); |
307 |
|
} |
308 |
|
|
316 |
– |
|
309 |
|
/** |
310 |
|
* a deserialized serialized array holds same values |
311 |
|
*/ |
312 |
< |
public void testSerialization() { |
312 |
> |
public void testSerialization() throws Exception { |
313 |
|
AtomicIntegerArray l = new AtomicIntegerArray(SIZE); |
314 |
|
for (int i = 0; i < SIZE; ++i) |
315 |
|
l.set(i, -i); |
316 |
|
|
317 |
< |
try { |
318 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
319 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
320 |
< |
out.writeObject(l); |
321 |
< |
out.close(); |
322 |
< |
|
323 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
324 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
325 |
< |
AtomicIntegerArray r = (AtomicIntegerArray) in.readObject(); |
326 |
< |
for (int i = 0; i < SIZE; ++i) { |
335 |
< |
assertEquals(l.get(i), r.get(i)); |
336 |
< |
} |
337 |
< |
} catch (Exception e) { |
338 |
< |
e.printStackTrace(); |
339 |
< |
unexpectedException(); |
317 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
318 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
319 |
> |
out.writeObject(l); |
320 |
> |
out.close(); |
321 |
> |
|
322 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
323 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
324 |
> |
AtomicIntegerArray r = (AtomicIntegerArray) in.readObject(); |
325 |
> |
for (int i = 0; i < SIZE; ++i) { |
326 |
> |
assertEquals(l.get(i), r.get(i)); |
327 |
|
} |
328 |
|
} |
329 |
|
|
343 |
– |
|
330 |
|
/** |
331 |
|
* toString returns current value. |
332 |
|
*/ |