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 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
10 |
< |
import java.util.concurrent.atomic.*; |
11 |
< |
import java.io.*; |
12 |
< |
import java.util.*; |
10 |
> |
import java.util.Arrays; |
11 |
> |
import java.util.concurrent.atomic.AtomicIntegerArray; |
12 |
|
|
13 |
|
public class AtomicIntegerArrayTest extends JSR166TestCase { |
14 |
|
|
15 |
< |
public static void main (String[] args) { |
16 |
< |
junit.textui.TestRunner.run (suite()); |
15 |
> |
public static void main(String[] args) { |
16 |
> |
junit.textui.TestRunner.run(suite()); |
17 |
|
} |
18 |
|
public static Test suite() { |
19 |
|
return new TestSuite(AtomicIntegerArrayTest.class); |
20 |
|
} |
21 |
|
|
23 |
– |
|
22 |
|
/** |
23 |
|
* constructor creates array of given size with all elements zero |
24 |
|
*/ |
43 |
|
* constructor with array is of same size and has all elements |
44 |
|
*/ |
45 |
|
public void testConstructor2() { |
46 |
< |
int[] a = { 17, 3, -42, 99, -7}; |
46 |
> |
int[] a = { 17, 3, -42, 99, -7 }; |
47 |
|
AtomicIntegerArray ai = new AtomicIntegerArray(a); |
48 |
|
assertEquals(a.length, ai.length()); |
49 |
|
for (int i = 0; i < a.length; ++i) |
118 |
|
assertTrue(ai.compareAndSet(i, 2,-4)); |
119 |
|
assertEquals(-4,ai.get(i)); |
120 |
|
assertFalse(ai.compareAndSet(i, -5,7)); |
121 |
< |
assertFalse((7 == ai.get(i))); |
121 |
> |
assertEquals(-4,ai.get(i)); |
122 |
|
assertTrue(ai.compareAndSet(i, -4,7)); |
123 |
|
assertEquals(7,ai.get(i)); |
124 |
|
} |
131 |
|
public void testCompareAndSetInMultipleThreads() throws Exception { |
132 |
|
final AtomicIntegerArray a = new AtomicIntegerArray(1); |
133 |
|
a.set(0, 1); |
134 |
< |
Thread t = new Thread(new Runnable() { |
135 |
< |
public void run() { |
136 |
< |
while (!a.compareAndSet(0, 2, 3)) Thread.yield(); |
137 |
< |
}}); |
134 |
> |
Thread t = new Thread(new CheckedRunnable() { |
135 |
> |
public void realRun() { |
136 |
> |
while (!a.compareAndSet(0, 2, 3)) |
137 |
> |
Thread.yield(); |
138 |
> |
}}); |
139 |
|
|
140 |
|
t.start(); |
141 |
|
assertTrue(a.compareAndSet(0, 1, 2)); |
161 |
|
} |
162 |
|
|
163 |
|
/** |
164 |
< |
* getAndSet returns previous value and sets to given value at given index |
164 |
> |
* getAndSet returns previous value and sets to given value at given index |
165 |
|
*/ |
166 |
|
public void testGetAndSet() { |
167 |
|
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
174 |
|
} |
175 |
|
|
176 |
|
/** |
177 |
< |
* getAndAdd returns previous value and adds given value |
177 |
> |
* getAndAdd returns previous value and adds given value |
178 |
|
*/ |
179 |
|
public void testGetAndAdd() { |
180 |
|
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
218 |
|
} |
219 |
|
|
220 |
|
/** |
221 |
< |
* addAndGet adds given value to current, and returns current value |
221 |
> |
* addAndGet adds given value to current, and returns current value |
222 |
|
*/ |
223 |
|
public void testAddAndGet() { |
224 |
|
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
246 |
|
} |
247 |
|
|
248 |
|
/** |
249 |
< |
* incrementAndGet increments and returns current value |
249 |
> |
* incrementAndGet increments and returns current value |
250 |
|
*/ |
251 |
|
public void testIncrementAndGet() { |
252 |
|
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
264 |
|
|
265 |
|
static final int COUNTDOWN = 100000; |
266 |
|
|
267 |
< |
class Counter implements Runnable { |
267 |
> |
class Counter extends CheckedRunnable { |
268 |
|
final AtomicIntegerArray ai; |
269 |
|
volatile int counts; |
270 |
|
Counter(AtomicIntegerArray a) { ai = a; } |
271 |
< |
public void run() { |
271 |
> |
public void realRun() { |
272 |
|
for (;;) { |
273 |
|
boolean done = true; |
274 |
|
for (int i = 0; i < ai.length(); ++i) { |
275 |
|
int v = ai.get(i); |
276 |
< |
threadAssertTrue(v >= 0); |
276 |
> |
assertTrue(v >= 0); |
277 |
|
if (v != 0) { |
278 |
|
done = false; |
279 |
|
if (ai.compareAndSet(i, v, v-1)) |
305 |
|
assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN); |
306 |
|
} |
307 |
|
|
309 |
– |
|
308 |
|
/** |
309 |
|
* a deserialized serialized array holds same values |
310 |
|
*/ |
311 |
|
public void testSerialization() throws Exception { |
312 |
< |
AtomicIntegerArray l = new AtomicIntegerArray(SIZE); |
313 |
< |
for (int i = 0; i < SIZE; ++i) |
314 |
< |
l.set(i, -i); |
315 |
< |
|
316 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
317 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
318 |
< |
out.writeObject(l); |
319 |
< |
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) { |
327 |
< |
assertEquals(l.get(i), r.get(i)); |
312 |
> |
AtomicIntegerArray x = new AtomicIntegerArray(SIZE); |
313 |
> |
for (int i = 0; i < SIZE; i++) |
314 |
> |
x.set(i, -i); |
315 |
> |
AtomicIntegerArray y = serialClone(x); |
316 |
> |
assertTrue(x != y); |
317 |
> |
assertEquals(x.length(), y.length()); |
318 |
> |
for (int i = 0; i < SIZE; i++) { |
319 |
> |
assertEquals(x.get(i), y.get(i)); |
320 |
|
} |
321 |
|
} |
322 |
|
|
331 |
– |
|
323 |
|
/** |
324 |
|
* toString returns current value. |
325 |
|
*/ |
326 |
|
public void testToString() { |
327 |
< |
int[] a = { 17, 3, -42, 99, -7}; |
327 |
> |
int[] a = { 17, 3, -42, 99, -7 }; |
328 |
|
AtomicIntegerArray ai = new AtomicIntegerArray(a); |
329 |
|
assertEquals(Arrays.toString(a), ai.toString()); |
330 |
|
} |